Subversion Repositories wimsdev

Rev

Rev 13121 | Rev 17747 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12963 georgesk 1
// -*- coding: utf-8 -*-
2
#ifndef CHEMEQ_H
3
#define CHEMEQ_H
4
 
5
#include <cstring>
6
#include <sstream>
7
#include <iostream>
8
#include <vector>
9
#include <string>
10
#include <map>
11
 
13121 georgesk 12
#define VERSION "2.14"
12963 georgesk 13
 
13121 georgesk 14
/* Constante d'Avogadro, recommandée par CODATA, 2006 */
12963 georgesk 15
#define Avogadro 6.022141e+23
16
 
13121 georgesk 17
/* Charge élémentaire, voir wikipedia, 2007 */
12963 georgesk 18
#define Electron 1.602176e-19
19
 
20
/* Constante de Boltzmann, voir Wikipedia, 2007 */
21
#define Kb 1.3806e-23
22
 
13121 georgesk 23
/* D'où la constante de Faraday */
12963 georgesk 24
#define Faraday (Avogadro * Electron)
25
 
13121 georgesk 26
/* D'où la constante des Gaz parfaits 8.314 J.K^-1.mol^-1 */
12963 georgesk 27
#define R (Kb * Avogadro)
28
 
13121 georgesk 29
/* Température de référence pour les réactions chimiques, 25°C */
12963 georgesk 30
#define T0 (273.15+25)
31
 
32
/* MINVAL est une valeur impossible tant pour un potentiel standard */
13121 georgesk 33
/* que pour une constante d'équilibre                               */
12963 georgesk 34
#define MINVAL -999
35
 
36
typedef struct {
37
  int Zed;
38
  char symb[4];
39
} atome;
40
 
41
extern atome lesatomes[];
42
 
43
typedef std::pair<std::string,int> AtomeCompte;
44
 
45
class Compteur : public std::map<std::string,float>{
46
public:
47
  std::ostream & operator << (std::ostream & o)const;
48
};
49
 
50
std::ostream & operator << (std::ostream & o, const Compteur & c);
51
 
52
class Chemeq;
53
 
54
class fraction{
55
public:
56
  int i;
57
  int d;
58
  fraction(int numerateur, int denominateur=1):i(numerateur),d(denominateur){};
59
  void inverse(){int n=i; i=d; d=n;};
60
  void simplifie();
61
};
62
 
63
const fraction & minFraction(const fraction&, const fraction &);
64
 
65
std::ostream & operator << (std::ostream & o, fraction f);
66
 
67
fraction operator * (fraction f, int m);
68
fraction operator * (int m, fraction f);
69
fraction operator * (fraction f, fraction m);
70
fraction operator + (fraction f, fraction g);
71
fraction operator - (fraction f, fraction g);
72
 
73
bool operator > (fraction f, int i);
74
bool operator > (fraction f1, fraction f2);
75
bool operator != (fraction f, int i);
76
 
77
class AtomeListe{
78
  AtomeListe * suiv, *group;
79
  char symb[4];
80
  int Zed, nb, no, sqbr;
81
 
82
 public:
83
  AtomeListe(const char* nom, int num, AtomeListe * s=0, AtomeListe * g=0){
84
    strncpy(symb,nom,3); Zed=num; nb=1; sqbr=0;
85
    /* sqbr == 1 quand il y a un square bracket */
86
    suiv = s; group=g;
87
  };
88
  AtomeListe(const AtomeListe & a):
89
    suiv(a.suiv), group(a.group), Zed(a.Zed), nb(a.nb), sqbr(a.sqbr){
90
    strncpy(symb,a.symb,3);
91
  };
92
  const char * symbole() const{return symb;};
93
  int Z()const{return Zed;};
94
  int sq()const{return sqbr;};
95
  void sq(int val){sqbr=val;};
96
  void numerote(int n=0);
97
  void setmolecularite(int n){nb=n;};
98
  int getmolecularite()const{return nb;};
99
  AtomeListe * groupe(){return group;};
100
  void groupe(AtomeListe * al){group= al;};
101
  const AtomeListe * suivant()const{return suiv;};
102
  const AtomeListe * groupe()const{return group;};
103
  void setsuivant(AtomeListe * s){suiv=s;};
104
  void compte (Compteur &c, fraction mult=fraction(1,1))const;
105
  double weight(fraction mult=fraction(1,1))const;
106
  static AtomeListe * triage(AtomeListe * al);
107
  void printcount(std::ostream & o, const fraction&, int multiple) const;
108
  void printnorm(std::ostream & o) const;
109
  bool isEqual(const AtomeListe & a2) const;
110
  void debug(int decal = 0)const{
111
    for (int i=0; i< decal; i++) std::cout << " ";
112
    std::cout << "AtomeListe : ( & = " << this << " symb=\"" << symb << "\" Zed = " << Zed 
113
	 << " nb = " << nb << " no = " << no 
114
	 << " suiv = " << suiv << " group = " << group
115
	 << ")\n";
116
    if(group) group->debug(2+decal);
117
    if(suiv) suiv->debug(decal);
118
  };
119
};
120
 
121
typedef enum { aqueous, aqueous_explicit, gas, liquid, sol } moltype;
122
 
13121 georgesk 123
extern const char* moltypeStr[]; /* les chaînes aq, g,s */
12963 georgesk 124
 
125
class Membre;
126
 
127
class Molec{
128
  AtomeListe * al;
129
  int ch;
130
  fraction nb;
131
  int no;
132
  moltype t;
133
 public:
134
  Molec(AtomeListe * a, int c = 0, int n=1, int d=1): 
135
    al(a), ch(c), nb(n,d), t(aqueous){};
136
  Molec(const Molec & m):
137
    al(m.al), ch(m.ch), nb(m.nb.i,m.nb.d), t(m.t) {}
138
  AtomeListe & liste()const{return *al;};
139
  int charge()const{return ch;};
140
  bool eqMol(const Molec * m) const {
141
    return (al->isEqual(*(m->al))) && (ch== m->ch);
142
  }
143
  void nombre(int n, int d=1){nb=fraction(n,d);};
144
  fraction nombre()const{return nb;};
145
  void add(fraction f);
146
  void sub(fraction f);
147
  moltype typage()const{return t;};
148
  void typage(moltype at){t=at;};
149
  void numero(int n){no=n;};
150
  int numero()const{return no;};
151
  void triage(){al = AtomeListe::triage(al);};
152
  void compte(Compteur & c)const{if (al) al->compte(c,nb);};
153
  double weight(void)const{if (al) return al->weight(nb); else return 0.0;};
154
  const std::string signature()const;
155
  void printNombre(std::ostream & o)const;
156
  bool printcount(std::ostream & o, bool first) const;
157
  bool printelec(std::ostream & o, bool first) const;
158
  bool printspecies(std::ostream & o, bool first) const;
159
  void printnorm(std::ostream & o)const;
160
  void printweigh(std::ostream & o)const;
161
  void coeff( fraction f);
162
  bool printNernst(std::ostream & o, const char * prefix ="");
163
  bool printNernstWIMS(std::ostream & o, bool wantedlatex);
164
  bool iswater()const;
165
  bool iselectron()const;
166
  fraction  nbelectron()const;
167
  void debug(int decal = 0)const{
168
    for (int i=0; i < decal; i++) std::cout << " ";
169
    std::cout << "Molec : ( " << this << " charge = " << ch 
170
	 << " nombre = " << nb << " no = " << no;
171
    al->debug(decal+2);
172
    std::cout << ")\n";
173
  };
174
  // two Molecs are equal if the AtomLists and the charges are equal.
175
  friend Membre operator & (Membre & m1, Membre & m2);
176
  friend Membre operator - (Membre & m1, Membre & m2);
177
};
178
 
179
std::ostream & operator << (std::ostream & o, const AtomeListe & l);
180
std::ostream & operator << (std::ostream & o, const Molec & m);
181
 
182
class Membre : public std::vector<Molec *>{
183
public:
184
  int findMol(const Molec *);
185
  void addMol(const Molec *);
186
  void addMembre(const Membre *);
187
  void eraseNull();
188
  void compte(Compteur & c)const;
189
  void numerote();
190
  void triage();
191
  void printnorm(std::ostream & o) const;
192
  void printcount(std::ostream & o) const;
193
  void printelec(std::ostream & o) const;
194
  void printspecies(std::ostream & o) const;
195
  void printweight(std::ostream & o) const;
196
  void coeff( fraction f);
197
  void printNernst(std::ostream & o);
198
  void printNernstWIMS(std::ostream & o, bool wantedlatex);
199
  int printableNernst();
200
  bool redox()const;
201
  fraction  nbelectron()const;
202
  void debug(int decal = 0)const{
203
    for (int i=0; i < decal; i++) std::cout << " ";
204
    std::cout << "Membre : ( " << this;
13325 georgesk 205
    for (unsigned int j=0; j < size(); j++){
12963 georgesk 206
      std::cout << j << " :\n";
207
      operator[](j)->debug(decal+2);
208
    }
209
    std::cout << "\n";
210
  }
211
};
212
 
213
// intersection between two Membres
214
Membre operator & (Membre & m1, Membre & m2);
215
// members of first set which are not in the second
216
Membre operator - (Membre & m1, Membre & m2);
217
 
218
std::ostream & operator << (std::ostream & o, const Membre & m);
219
 
220
class Chemeq{
221
  Membre * gauche, * droit;
222
  std::string cste;
223
  long double val;
13121 georgesk 224
  bool equ; /* false by default, true when an equilibrium must be rendered with \doubleharpoons */
12963 georgesk 225
public:
13121 georgesk 226
 Chemeq(Membre * g, Membre * d, bool equilibrium = false) :
227
  gauche (g), droit(d), val(MINVAL), equ(equilibrium) {
228
  };
12963 georgesk 229
  const Membre * membredroit()const{return droit;};
230
  const Membre * membregauche()const{return gauche;};
13121 georgesk 231
  const bool is_equilibrium() const {return equ;};
12963 georgesk 232
  void addChemeq(const Chemeq *);
233
  void subChemeq(const Chemeq *);
234
  void simplifie(bool tri);
235
  void numerote(){gauche->numerote(); droit->numerote();};
236
  void triage(){gauche->triage(); droit->triage();};
13121 georgesk 237
  /* ajuste le coefficient pour qu'il y ait 1 mol du premier réactif */
12963 georgesk 238
  void coeff1();
239
  /* mutiplie par la fraction num/den */
240
  void multiply(int num, int den);
241
  fraction nbelectron()const{return gauche->nbelectron()-droit->nbelectron();};
242
  /* renvoie val ou nbelectron()*Faraday*val */
243
  long double enthalpy() const;
244
  void normalise(){numerote(); triage(); coeff1();};
245
  void printnorm(std::ostream & o);
246
  void printcount(std::ostream & o) const;
247
  void printelec(std::ostream & o) const;
248
  void printspecies(std::ostream & o) const;
249
  void printweight(std::ostream & o) const;
250
  void printNernst(std::ostream & o, std::ostream & w, bool wantedlatex=false);
251
  std::string equilibre();
252
  bool redox()const;
253
  const std::string constante()const{ return cste;};
254
  void constante (const std::string s) { cste = s;};
255
  bool valdefined()const;
256
  double valeur()const{return val;};
257
  std::string valeur_latex()const;
258
  void valeur(double r){val=r;};
259
};
260
 
261
std::ostream & operator << (std::ostream & o, const Chemeq & c);
262
//struct taken from Kyle Burton's gperiodic
263
 
264
/* structure to hold element data, as initialized from the static
265
 * mendeleiev.c */
266
enum info_types {
267
  NAME, SYMBOL, NUMBER, WEIGHT, MELTING, BOILING, PAULING,
268
  MAX_INFO_NR      /* Has to be the last element */
269
};
270
 
271
struct table_entry {
272
  const char *info[MAX_INFO_NR];
273
};
274
 
275
extern struct table_entry table[];
276
 
277
double mendelweight(int i);
278
int findmendel(const char * symb);
279
double mendelweight(const char * symb);
280
 
281
#endif