Subversion Repositories wimsdev

Rev

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