Subversion Repositories wimsdev

Rev

Rev 110 | Rev 820 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 110 Rev 472
Line 1... Line 1...
1
// -*- coding: utf-8 -*-
1
// -*- coding: utf-8 -*-
2
#include "chemeq.h"
2
#include "chemeq.h"
3
#include <math.h>
3
#include <cmath>
4
#include <sstream>
-
 
5
#include <stdlib.h>
4
#include <cstdlib>
6
 
5
 
7
atome lesatomes[] ={
6
atome lesatomes[] ={
8
{-1, "e"},
7
{-1, "e"},
9
{1, "H"},
8
{1, "H"},
10
{2, "He"},
9
{2, "He"},
Line 333... Line 332...
333
      printNombre(o);
332
      printNombre(o);
334
      o << "}";
333
      o << "}";
335
    }
334
    }
336
    return 1;
335
    return 1;
337
  }
336
  }
-
 
337
}
-
 
338
 
-
 
339
bool Molec::printNernstWIMS(std::ostream & o, bool wantedlatex){
-
 
340
  if (iswater() || iselectron()) {
-
 
341
    return false;
-
 
342
  }
-
 
343
  switch(t){
-
 
344
  case sol : {
-
 
345
    return false;
-
 
346
  }
-
 
347
  case aqueous :
-
 
348
    if(wantedlatex){
-
 
349
      o <<  "[" << *al;
-
 
350
    }else{
-
 
351
      o << "["; al->printnorm(o);
-
 
352
    }
-
 
353
    if (ch){
-
 
354
      if(wantedlatex){
-
 
355
        o << "^{";
-
 
356
      }else{
-
 
357
        o << "^";
-
 
358
      }
-
 
359
      if(fabs(1.0*ch)!=1) o << fabs(1.0*ch);
-
 
360
      if(wantedlatex){
-
 
361
        if(ch>0) o << "+}"; else o << "-}";
-
 
362
      }else{
-
 
363
        if(ch>0) o << "+"; else o << "-";
-
 
364
      }
-
 
365
    }
-
 
366
    o  <<"]";
-
 
367
    if (nb!=1) {
-
 
368
      if(wantedlatex){
-
 
369
        o << "^{";
-
 
370
        if (nb.d==1){
-
 
371
          o << nb.i ;
-
 
372
        }
-
 
373
        else {
-
 
374
          o << "\\frac{" << nb.i << "}{" << nb.d << "}";
-
 
375
        }
-
 
376
        o << "}";
-
 
377
      }else{
-
 
378
        o << "^" << nb;
-
 
379
      }
-
 
380
    }
-
 
381
    return true;
-
 
382
  case gas :
-
 
383
    if(wantedlatex){
-
 
384
      o << "P_{" << *al << "}";
-
 
385
    }else{
-
 
386
      o << "P_"; al->printnorm(o);
-
 
387
    }
-
 
388
    if (nb!=1) {
-
 
389
      if(wantedlatex){
-
 
390
        o << "^{";
-
 
391
        if (nb.d==1){
-
 
392
          o << nb.i ;
-
 
393
        }
-
 
394
        else {
-
 
395
          o << "\\frac{" << nb.i << "}{" << nb.d << "}";
-
 
396
        }
-
 
397
        o << "}";
-
 
398
      }else{
-
 
399
        o << "^" << nb;
-
 
400
      }
-
 
401
    }
-
 
402
    return true;
-
 
403
  }
-
 
404
  return false;
338
}
405
}
339
 
406
 
340
bool Molec::iswater()const{
407
bool Molec::iswater()const{
341
  if (t != aqueous) return 0;
408
  if (t != aqueous) return 0;
342
  if (signature()==std::string("H_{2}O") ||
409
  if (signature()==std::string("H_{2}O") ||
Line 512... Line 579...
512
  const char * prefix="";
579
  const char * prefix="";
513
  for (int i=0; i<size(); i++) {
580
  for (int i=0; i<size(); i++) {
514
    if (i>0) prefix="\\,";
581
    if (i>0) prefix="\\,";
515
    if (operator[](i)->printNernst(o, prefix)){
582
    if (operator[](i)->printNernst(o, prefix)){
516
      printed = 1;
583
      printed = 1;
-
 
584
    }
-
 
585
  }
-
 
586
  if (!printed) o << "1";
-
 
587
}
-
 
588
 
-
 
589
void Membre::printNernstWIMS(std::ostream & o, bool wantedlatex){
-
 
590
  bool printed = false;
-
 
591
  bool addcomma = false;
-
 
592
  for (int i=0; i<size(); i++) {
-
 
593
    std::ostringstream w;
-
 
594
    if (operator[](i)->printNernstWIMS(w,wantedlatex)){
-
 
595
      if (addcomma) o << ", ";
-
 
596
      o << w.str();
-
 
597
      printed = true;
-
 
598
      addcomma = true;
-
 
599
    } else {
-
 
600
      addcomma = false;
517
    }
601
    }
518
  }
602
  }
519
  if (!printed) o << "1";
603
  if (!printed) o << "1";
520
}
604
}
521
 
605
 
Line 572... Line 656...
572
  if (valdefined() && c->valdefined()){
656
  if (valdefined() && c->valdefined()){
573
    long double e1=enthalpy(), e2=c->enthalpy();
657
    long double e1=enthalpy(), e2=c->enthalpy();
574
    fraction n1=nbelectron(), n2=c->nbelectron();
658
    fraction n1=nbelectron(), n2=c->nbelectron();
575
    long double e = e1+e2;
659
    long double e = e1+e2;
576
    fraction n=n1+n2;
660
    fraction n=n1+n2;
577
    if (n.i==0) val=expl(-e/R/T0);
661
    if (n.i==0) val=exp(-e/R/T0);
578
    else val=-e*n.d/n.i/Faraday;
662
    else val=-e*n.d/n.i/Faraday;
579
  } else {
663
  } else {
580
    val=MINVAL;
664
    val=MINVAL;
581
  }
665
  }
582
  gauche->addMembre(c->gauche);
666
  gauche->addMembre(c->gauche);
Line 589... Line 673...
589
    long double e1=enthalpy(), e2=c->enthalpy();
673
    long double e1=enthalpy(), e2=c->enthalpy();
590
    fraction n1=nbelectron(), n2=c->nbelectron();
674
    fraction n1=nbelectron(), n2=c->nbelectron();
591
    long double e = e1-e2;
675
    long double e = e1-e2;
592
    fraction n=n1-n2;
676
    fraction n=n1-n2;
593
    if (n.i==0) {
677
    if (n.i==0) {
594
      val=expl(-e/R/T0);
678
    val=exp(-e/R/T0);
595
    } else{
679
    } else{
596
      val=-e*n.d/n.i/Faraday;
680
      val=-e*n.d/n.i/Faraday;
597
    }
681
    }
598
  } else {
682
  } else {
599
    val=MINVAL;
683
    val=MINVAL;
Line 606... Line 690...
606
long double Chemeq::enthalpy() const{
690
long double Chemeq::enthalpy() const{
607
  fraction n=nbelectron();
691
  fraction n=nbelectron();
608
  if (redox()){
692
  if (redox()){
609
    return -val*n.i/n.d*Faraday;
693
    return -val*n.i/n.d*Faraday;
610
  } else {
694
  } else {
611
    return -R*T0*logl(val);
695
    return -R*T0*log(val);
612
  }
696
  }
613
}
697
}
614
 
698
 
615
void Chemeq::simplifie(bool tri=false){
699
void Chemeq::simplifie(bool tri=false){
616
  Membre communs(*gauche & *droit);
700
  Membre communs(*gauche & *droit);
Line 639... Line 723...
639
    o << " (";
723
    o << " (";
640
    if (cste!=std::string("")) o << cste << " = ";
724
    if (cste!=std::string("")) o << cste << " = ";
641
    o << val;
725
    o << val;
642
    if (redox()) o << " V";
726
    if (redox()) o << " V";
643
    o << ")";
727
    o << ")";
644
  }
728
  }
645
}
729
}
646
 
730
 
647
void Chemeq::printcount(std::ostream & o) const {
731
void Chemeq::printcount(std::ostream & o) const {
648
  gauche->printcount(o);
732
  gauche->printcount(o);
649
  o << "; ";
733
  o << "; ";
650
  droit->printcount(o);
734
  droit->printcount(o);
Line 701... Line 785...
701
 
785
 
702
bool Chemeq::redox()const{
786
bool Chemeq::redox()const{
703
  return gauche->redox() || droit->redox();
787
  return gauche->redox() || droit->redox();
704
}
788
}
705
 
789
 
706
void Chemeq::printNernst(std::ostream & o){
790
void Chemeq::printNernst(std::ostream & o,
-
 
791
                         std::ostream & w,
-
 
792
                         bool wantedlatex){
707
  Membre * ga, * dr;
793
  Membre * ga, * dr;
708
  if (!redox()){
794
  if (!redox()){
709
    if (gauche->printableNernst()){
795
    if (gauche->printableNernst()){
710
      o << "\\frac{";
796
      o << "\\frac{";
711
      droit->printNernst(o);
797
      droit->printNernst(o);
Line 714... Line 800...
714
      o << "}";
800
      o << "}";
715
    }
801
    }
716
    else {
802
    else {
717
      droit->printNernst(o);
803
      droit->printNernst(o);
718
    }
804
    }
-
 
805
    droit->printNernstWIMS(w,wantedlatex);
-
 
806
    w << "; ";
-
 
807
    gauche->printNernstWIMS(w,wantedlatex);
719
    if (val > MINVAL) {
808
    if (val > MINVAL) {
720
      o << "\\,=\\,";
809
      o << "\\,=\\,";
721
      if (cste!=std::string("")) o << cste << "\\,=\\,";
810
      if (cste!=std::string("")) o << cste << "\\,=\\,";
722
      o << valeur_latex();
811
      o << valeur_latex();
-
 
812
      if(wantedlatex){
-
 
813
        w << "; " << valeur_latex();
-
 
814
      }else{
-
 
815
        w << "; " << val;
-
 
816
      }
723
    }
817
    }
724
    else{
818
    else{
725
      o << "\\,=\\,K";
819
      o << "\\,=\\,K";
-
 
820
      w << "; K";
726
    }
821
    }
727
  }
822
  }
728
  else{ /* c'est une réaction redox */
823
  else{ /* c'est une réaction redox  */
729
    o << "E\\,=\\,";
824
    o << "E\\,=\\,";
-
 
825
    if(wantedlatex){
-
 
826
      w << "E\\,=\\,";
-
 
827
    }else{
-
 
828
      w << "E=";
-
 
829
    }
730
    if (val > MINVAL) {
830
    if (val > MINVAL) {
731
      o << val;
831
      o << val;
-
 
832
      w << val << ";";
732
    }
833
    }
733
    else{
834
    else{
734
      o << "E_{0}";
835
      o << "E_{0}";
-
 
836
      if(wantedlatex){
-
 
837
        w << "E_{0};";
-
 
838
      }else{
-
 
839
        w << "E0;";
-
 
840
      }
735
    }
841
    }
736
    o << "\\,+\\,\\frac{R\\,T}{";
842
    o << "\\,+\\,\\frac{R\\,T}{";
737
    o << gauche->nbelectron()+droit->nbelectron() << "\\,F}";
843
    o << gauche->nbelectron()+droit->nbelectron() << "\\,F}";
738
    o << "\\log";
844
    o << "\\log";
-
 
845
    w << gauche->nbelectron()+droit->nbelectron() << ";";
739
    if (gauche->redox()){ /* c'est une réduction */
846
    if (gauche->redox()){ /* c'est une réduction */
740
      ga=gauche; dr=droit;
847
      ga=gauche; dr=droit;
741
    }
848
    }
742
    else{ /* c'est une oxydation */
849
    else{ /* c'est une oxydation */
743
      ga=droit; dr=gauche;
850
      ga=droit; dr=gauche;
744
    }
851
    }
745
    if (dr->printableNernst()){
852
    if (dr->printableNernst()){
746
        o << "\\frac{";
853
      o << "\\frac{";
747
        ga->printNernst(o);
854
      ga->printNernst(o);
748
        o << "}{";
855
      o << "}{";
749
        dr->printNernst(o);
856
      dr->printNernst(o);
750
        o << "}";
857
      o << "}";
751
      }
858
    }
752
      else {
859
    else {
753
        o << "(";
860
      o << "(";
754
        ga->printNernst(o);
861
      ga->printNernst(o);
755
        o << ")";
862
      o << ")";
756
      }
863
    }
-
 
864
    // implanter la sortie pour Wims ici.
-
 
865
    dr->printNernstWIMS(w,wantedlatex);
-
 
866
    w << "; ";
-
 
867
    ga->printNernstWIMS(w,wantedlatex);
757
  }
868
  }
758
}
869
}
759
 
870
 
760
std::string Chemeq::valeur_latex()const{
871
std::string Chemeq::valeur_latex()const{
761
  std::ostringstream so;
872
  std::ostringstream so;