This repository has been archived by the owner on Oct 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chem.h
818 lines (773 loc) · 25 KB
/
chem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*This is a program to simulate chemical tests. A list of reactions will be provided (inbuilt[+user]).
The user will enter products and reactants will be computed.
Physical characteristics of the compound will also be computed using files
Files used by the program are in a folder called progdata
When I started making this project, I didn't know that usually STL uses
(!(a>b) && !(b<a)) instead of a==b
Because comp_t::operator==(const comp_t&)const is not a transitive function,
I had to write my own algorithms at some places.
Also, I cannot always use the fact that std::set<comp_t> is sorted
as MgCO3(s)==CO32-(aq) while sorting is on the basis of chemical formula.
Hence, while checking if one set includes the other,
I can't use the algorithm optimized for sorted ranges
Because of the above reason, the program may seem a bit chaotic.
*/
#pragma warning(disable:4996)
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<string>
#include<set>
#include<algorithm>
#include<boost\foreach.hpp>
#include<boost\tokenizer.hpp>
#include<boost\lexical_cast.hpp>
//#include<windows.h>
#define COMP_FORM_SIZE 20
#define FILE_PATH_SIZE 256
#define FILE_NAME_SIZE 30
//File Names
#define CATION_LIST "progdata\\cations.txt"
#define ANION_LIST "progdata\\anions.txt"
#define SALT_SPLIT "progdata\\salt split.txt"
#define PPTS "progdata\\precipitates.txt"
//#define PPTS_FOLDER "progdata\\ppt\\";
#define NEVER_PPT_ION "progdata\\never ppt ion.txt"
#define DISSOC_MEDIA "progdata\\dissoc media.txt"
#define RXNS_FOLDER "progdata\\rxns\\"
#define RXNS_FILE "progdata\\rxns.txt"
#define ION_COLOR "progdata\\coloured ions.txt"
#define GAS_SMELL_COLOR "progdata\\gases.txt"
#define ANAL_SALTS "progdata\\salt analysis\\salts.txt"
#define ANAL_CATIONS "progdata\\salt analysis\\cations.txt"
#define ANAL_ANIONS "progdata\\salt analysis\\anions.txt"
#define HELP_FILE "progdata\\help.txt"
#define ALIAS_FILE "progdata\\alias.txt"
//global=functions================================================================
int crude_split_salt(const char* salt,char* cation,char* anion)
/*splits salt into cation and anion
the cation can't be polyatomic
the anion isn't correctly identified.
Eg. AlCl3 as salt gives Cl3 as anion instead of Cl
Because of the above constraints, the function is named 'crude'
The cations and anions are actually found by comp_t::split()
comp_t::split() calls this function as a subprocedure*/
{
int i,j,k,n=std::strlen(salt),ret;
char num[10];
bool cbrac_present=false;
cation[0]='\0';anion[0]='\0';
if(salt[0]<'A' || salt[0]>'Z')return 0;
for(i=1;i<n && salt[i]>='a' && salt[i]<='z';i++);
//keep looping till an non-lowercase character is encountered
for(j=0;j<i;j++)cation[j]=salt[j];cation[j]='\0';
//keep the first i characters in cation
for(j=0;i<n && salt[i]>='0' && salt[i]<='9';i++,j++)num[j]=salt[i];
num[j]='\0';
if(strlen(num)==0)ret=1;
else ret=std::atoi(num);
//count number of cations
if(salt[i]=='(')
{j=i+1;for(i=n-1;i>=0 && salt[i]!=')';--i);}
else
{j=i;/*for(;i>=0 && salt[i]>='0' && salt[i]<='9';--i);i++;*/i=n;}
for(k=0;j<i;j++,k++)anion[k]=salt[j];
anion[k]='\0';
return ret;
}
bool ion_match(const char* ion,const char* stdion)
//checks if anion obtained after crude_salt_split is correct
{
int i,h=strlen(ion),hstd=strlen(stdion);
if(h<hstd)return false;
for(i=0;i<hstd;i++)if(ion[i]!=stdion[i])return false;
// for(i=0;i<h;i++)if((ion[i]<'\0' || ion[i]>'9') && ion[i]!='-' && ion[i]!='+')return false;
return true;
}
std::istream& my_getline(std::istream& is,std::string& str,char delim='\n')
{
if(is.peek()=='\n')is.ignore();
std::getline(is,str,delim);
return is;
}
std::istream& my_getline(std::istream& is,char* str,int size,char delim='\n')
{
if(is.peek()=='\n')is.ignore();
is.getline(str,size,delim);
return is;
}
//Classes=========================================================================
namespace myenum
{
enum state_t{gas,solid,liq,aq,dil,conc,vdil,nil,any_st};
//enum containing possible states of compound
//enum charge_t{positive,negative,neutral};
//enum containing possible charges on a species
enum comp_type_t{salt,cation,anion,notsalt,none};
//Compound type: electrovalent, covalent, unspecified
}
class comp_t;
class tube_t;
class rxn_t;
namespace boost
{
template<>
myenum::state_t lexical_cast<myenum::state_t,std::string>(const std::string& str)
{
if(str=="g")return myenum::gas;
else if(str=="l")return myenum::liq;
else if(str=="s")return myenum::solid;
else if(str=="aq")return myenum::aq;
else if(str=="dil")return myenum::dil;
else if(str=="conc")return myenum::conc;
else if(str=="vdil")return myenum::vdil;
else if(str=="any")return myenum::any_st;
else return myenum::nil;
}
template<>
std::string lexical_cast<std::string,myenum::state_t>(const myenum::state_t& state)
{
if(state==myenum::solid)return "s";
else if(state==myenum::aq)return "aq";
else if(state==myenum::liq)return "l";
else if(state==myenum::gas)return "g";
else if(state==myenum::dil)return "dil";
else if(state==myenum::conc)return "conc";
else if(state==myenum::vdil)return "vdil";
else if(state==myenum::any_st)return "any";
else return "nil";
}
template<>
myenum::comp_type_t lexical_cast<myenum::comp_type_t,std::string>(const std::string& str)
{
if(str=="salt")return myenum::salt;
else if(str=="notsalt" || str=="not salt")return myenum::notsalt;
else if(str=="cation")return myenum::cation;
else if(str=="anion")return myenum::anion;
else return myenum::none;
}
template<>
std::string lexical_cast<std::string,myenum::comp_type_t>(const myenum::comp_type_t& comp_type)
{
if(comp_type==myenum::salt)return "salt";
else if(comp_type==myenum::notsalt)return "notsalt";
else if(comp_type==myenum::cation)return "cation";
else if(comp_type==myenum::anion)return "anion";
else return "none";
}
template<>
comp_t lexical_cast<comp_t,std::string>(const std::string& form_with_state);
template<>
std::string lexical_cast<std::string,comp_t>(const comp_t& comp);
template<>
std::string lexical_cast<std::string,tube_t>(const tube_t& comp);
template<>
rxn_t lexical_cast<rxn_t,std::string>(const std::string& str);
}
//--------------------------------------------------------------------------------
class comp_t
//compound class
//contains formula and state of compound
{public:
char form[COMP_FORM_SIZE]; //chemical formula of compound object
char cation[COMP_FORM_SIZE]; //cation of compound (if exists)
char anion[COMP_FORM_SIZE]; //anion of compound (if exists)
myenum::comp_type_t comp_type;
myenum::state_t st; //physical state of compound
comp_t():st(myenum::nil),comp_type(myenum::none)
{form[0]='\0';cation[0]='\0';anion[0]='\0';}
comp_t(const char* formula,myenum::state_t state,myenum::comp_type_t type=myenum::none):st(state),comp_type(type)
{std::strcpy(form,formula);cation[0]='\0';anion[0]='\0';}
comp_t(const comp_t& comp):st(comp.st),comp_type(comp.comp_type)
{
std::strcpy(form,comp.form);
std::strcpy(cation,comp.cation);
std::strcpy(anion,comp.anion);
}
bool operator<(const comp_t& comp)const;
//used for compatibility with std::set<comp_t>
bool operator==(const comp_t& comp)const
{
return (std::strcmp(form,comp.form)==0
|| (anion[0]!='\0' && std::strcmp(anion,comp.form)==0)
|| (cation[0]!='\0' && std::strcmp(cation,comp.form)==0))
&& (st==comp.st || st==myenum::any_st || comp.st==myenum::any_st);
}
bool exact_equal(const comp_t& comp)const
{
return std::strcmp(form,comp.form)==0
&& (st==comp.st || st==myenum::any_st || comp.st==myenum::any_st);
}
bool ion_equal(const comp_t& comp)const
{
return ((anion[0]!='\0' && std::strcmp(anion,comp.form)==0)
|| (cation[0]!='\0' && std::strcmp(cation,comp.form)==0))
&& (st==comp.st || st==myenum::any_st || comp.st==myenum::any_st);
}
bool anion_equal(const comp_t& comp)const
{
return anion[0]!='\0' && std::strcmp(anion,comp.form)==0
&& (st==comp.st || st==myenum::any_st || comp.st==myenum::any_st);
}
bool cation_equal(const comp_t& comp)const
{
return cation[0]!='\0' && std::strcmp(cation,comp.form)==0
&& (st==comp.st || st==myenum::any_st || comp.st==myenum::any_st);
}
void operator=(const std::string& form_with_state);
//eg. input "(NH4)2CO3(s)" will set form as "(NH4)2CO3" and state as solid.
void split();
//gets the cation and anion of salt
//leaves them "" if compound is not a salt
//sets comp_type as myenum::salt if salt and myenum::notsalt if not a salt
};
bool comp_t::operator<(const comp_t& comp)const
{
int streq=strcmp(form,comp.form);
if(streq<0)return true;
else if(streq>0)return false;
else return st<comp.st;
}
//--------------------------------------------------------------------------------
class tube_t
//test tube class
//contains list of compounds in it and temperature
{public:
std::set<comp_t> comp_list; //list of compounds in test tube
bool hight; //is temperature high?
bool show_mech; //
tube_t(bool high_temp=false,bool show_mechanism=false):
hight(high_temp),show_mech(show_mechanism){}
void add(const comp_t& comp)
{comp_list.insert(comp);}
void add(const char* formula,myenum::state_t state,myenum::comp_type_t type=myenum::none)
{comp_list.insert(comp_t(formula,state,type));}
bool carry_rxn(const rxn_t& rxn,std::string& obs);
//carries out given reaction in tube (if possible)
//rxn should be pre-filled using rxn_t::operator=(const std::string& str);
//return true if reaction happened
void react_prep();
//dissociate double salts and other reaction initialization
void assign(std::string str,bool heat=false);
void add(std::string str,bool heat=false);
void precipitate();
//checks for ions present and precipitates precipitable salts.
void react(const char* fname,std::set<std::string>& obs_set);
//carries out reactions from a reaction file
void react_list(const char* fname,std::set<std::string>& obs_set);
//carries out reactions from a reaction list file
//The reaction list file contains file names from the RXNS_FOLDER
//These file names have reactions in them
//displays computer mechanism of reaction using std::cout if show_mech is true
void phys_obs(std::set<std::string>& obs_set)const;
//physical observation of test tube is added to the set
void remove_with_state(myenum::state_t state)
{
std::set<comp_t>::iterator it=comp_list.begin();
for(;it!=comp_list.end();)
{
if(it->st==state)comp_list.erase(it++);
else ++it;
}
}
};
//--------------------------------------------------------------------------------
class rxn_t
//reaction class
//contains list of compounds in it and temperature
{public:
std::set<comp_t> reac_list; //list of reactants used
std::set<comp_t> prod_list; //list of products formed
std::set<comp_t> cat_list; //list of catalysts
std::string comment;
bool hight; //is temperature high
bool lowt; //is temperature low
rxn_t(bool high_temp=false,bool low_temp=false):hight(high_temp),lowt(low_temp){}
void operator=(const std::string& str);
//BUG: interprets '+' sign in anions as '+' of adding reagents
};
//Implementation==================================================================
namespace boost
{
template<>
comp_t lexical_cast<comp_t,std::string>(const std::string& form_with_state)
{
int pos;std::string state;
comp_t comp;
//find last occurence of '('
comp.comp_type=myenum::none;
pos=form_with_state.find_last_of("(");
if(pos==-1)
{comp.st=myenum::any_st;std::strcpy(comp.form,form_with_state.c_str());}
else
{
state.assign(form_with_state,pos+1,form_with_state.size()-pos-2);
comp.st=boost::lexical_cast<myenum::state_t>(state);
if(comp.st==myenum::nil)
{comp.st=myenum::any_st;std::strcpy(comp.form,form_with_state.c_str());}
else std::strcpy(comp.form,form_with_state.substr(0,pos).c_str());
}
//assign charge
if(comp.form[pos-1]=='+')comp.comp_type=myenum::cation;
else if(comp.form[pos-1]=='-')comp.comp_type=myenum::anion;
else comp.comp_type=myenum::none;
//remove coefficients
for(pos=0;comp.form[pos]>='0' && comp.form[pos]<='9';++pos);
std::strcpy(comp.form,comp.form+pos);
return comp;
}
template<>
std::string lexical_cast<std::string,comp_t>(const comp_t& comp)
{
std::string str(comp.form);
str.append(1,'(');
str.append(boost::lexical_cast<std::string>(comp.st));
str.append(1,')');
return str;
}
template<>
std::string lexical_cast<std::string,tube_t>(const tube_t& tube)
{
std::string str;
if(tube.comp_list.empty())return "";
std::set<comp_t>::const_iterator it=tube.comp_list.begin();
for(;it!=tube.comp_list.end();++it)
{
str.append(lexical_cast<std::string>(*it));
str.append(1,'+');
}
str.erase(str.size()-1,1);
return str;
}
template<>
rxn_t lexical_cast<rxn_t,std::string>(const std::string& str2)
{
int pos1,pos2;
std::string reac,prod,cat,temp,str=str2;
comp_t comp;
rxn_t rxn;
int i;
boost::char_separator<char> sep("&");
boost::tokenizer<boost::char_separator<char> >tokens(reac);
//take out comment
pos1=str.find("//");
if(pos1==-1)pos1=str.find("\\\\");
else if(pos1!=-1)
{
rxn.comment.assign(str,pos1+2,std::string::npos);
str.erase(pos1,std::string::npos);
for(i=0;i<int(rxn.comment.size());i++)
{if(rxn.comment[i]=='_')rxn.comment[i]=' ';}
}
//splits reaction into reactants,products,catalysts
pos1=str.find("--");
reac.assign(str,0,pos1);
pos2=str.find("-->",pos1);
if(pos2-pos1>2)cat.assign(str,pos1+2,pos2-pos1-2);
prod.assign(str,pos2+3,std::string::npos);
//split reactants
if(reac.size()>0)
{
for(i=0;i<int(reac.size())-1;i++)if(reac[i]=='+' && ((i>0 && reac[i-1]==')') || (reac[i+1]!='+' && reac[i+1]!='(')))reac[i]='&';
tokens.assign(reac,sep);
BOOST_FOREACH(temp,tokens)
{rxn.reac_list.insert(boost::lexical_cast<comp_t>(temp));}
}
//split products
if(prod.size()>0)
{
for(i=0;i<int(prod.size())-1;i++)if(prod[i]=='+' && ((i>0 && prod[i-1]==')') || (prod[i+1]!='+' && prod[i+1]!='(')))prod[i]='&';
tokens.assign(prod,sep);
BOOST_FOREACH(temp,tokens)
{rxn.prod_list.insert(boost::lexical_cast<comp_t>(temp));}
}
//split catalysts (if exist)
if(pos2-pos1>2)
{
for(i=0;i<int(cat.size())-1;i++)if(cat[i]=='+' && ((i>0 && cat[i-1]==')') || (cat[i+1]!='+' && cat[i+1]!='(')))cat[i]='&';
tokens.assign(cat,sep);
BOOST_FOREACH(temp,tokens)
{
if(temp=="heat"){rxn.hight=true;rxn.lowt=false;}
else if(temp=="cold"){rxn.lowt=true;rxn.hight=false;}
else rxn.cat_list.insert(boost::lexical_cast<comp_t>(temp));
}
}
return rxn;
}
}
void get_random_line(const char* fname,char* line)
//gets a random line from file 'fname' into 'line'
{
std::ifstream ifile(fname);
int i,size,pos;
comp_t comp;
if(!ifile.is_open())disp_error(std::string("Can't open ")+fname);
for(size=0;ifile.ignore(10000,'\n');++size);
ifile.clear();
ifile.seekg(0);
srand(unsigned(std::time(NULL)));pos=rand()%size;
for(i=0;i<=pos;i++)ifile>>line;
ifile.close();
}
comp_t get_random_anion()
{
comp_t comp;
get_random_line(ANAL_ANIONS,comp.form);
comp.st=myenum::solid;
comp.comp_type=myenum::anion;
return comp;
}
comp_t get_random_cation()
{
comp_t comp;
get_random_line(ANAL_CATIONS,comp.form);
comp.st=myenum::solid;
comp.comp_type=myenum::cation;
return comp;
}
comp_t get_random_salt()
{
comp_t comp;
get_random_line(ANAL_SALTS,comp.form);
comp.st=myenum::solid;
comp.comp_type=myenum::salt;
comp.split();
return comp;
}
bool my_includes(const std::set<comp_t>& bigset,const std::set<comp_t>& smallset)
/*my own version of std::includes written specially for std::set<comp_t>
This had to be done because comp_t::operator==(const comp_t&)const was not transitive
So I couldn't rewrite comp_t::operator<(const comp_t&)const so that STL can
use (!(a<b) && !(b<a)) to get the same result as a==b*/
{
std::set<comp_t>::const_iterator its,itb;
bool found_elem_in_big=false;
if(smallset.size()>bigset.size())return false;
for(its=smallset.begin();its!=smallset.end();++its)
{
for(itb=bigset.begin();itb!=bigset.end();++itb)
{
found_elem_in_big=itb->operator==(*its);
if(found_elem_in_big)break;
}
if(!found_elem_in_big)return false;
}
return true;
}
void comp_t::operator=(const std::string& form_with_state)
{
int pos;std::string state;
//find last occurence of '('
comp_type=myenum::none;
pos=form_with_state.find_last_of("(");
if(pos==-1)
{st=myenum::any_st;std::strcpy(form,form_with_state.c_str());}
else
{
state.assign(form_with_state,pos+1,form_with_state.size()-pos-2);
st=boost::lexical_cast<myenum::state_t>(state);
if(st==myenum::nil)
{st=myenum::any_st;std::strcpy(form,form_with_state.c_str());}
else std::strcpy(form,form_with_state.substr(0,pos).c_str());
}
//assign charge
if(form[pos-1]=='+')comp_type=myenum::cation;
else if(form[pos-1]=='-')comp_type=myenum::anion;
else comp_type=myenum::none;
//remove coefficients
for(pos=0;form[pos]>='0' && form[pos]<='9';++pos);
std::strcpy(form,form+pos);
}
void comp_t::split()
{
char stdform[COMP_FORM_SIZE],stdcat[COMP_FORM_SIZE],stdan[COMP_FORM_SIZE],charge[4];
bool found;
//first check "salt split.txt" for standard splitting
std::ifstream ifile(SALT_SPLIT);
if(!ifile.is_open())disp_error(std::string("Can't open ")+SALT_SPLIT);
while(ifile>>stdform>>stdcat>>stdan)if(std::strcmp(stdform,form)==0)
{
ifile.close();
std::strcpy(cation,stdcat);
std::strcpy(anion,stdan);
comp_type=myenum::salt;
return;
}
ifile.close();
//if not found, use call crude_split_salt(form,cation,anion) to get crude ions
crude_split_salt(form,cation,anion);
//then check if cation exists using "cations.txt"
//if it exists, get its charge and append the charge at the end of the data member 'cation'
ifile.clear();
ifile.open(CATION_LIST);
if(!ifile.is_open())disp_error(std::string("Can't open ")+CATION_LIST);
found=false;
while(ifile>>stdcat>>charge)if(std::strcmp(stdcat,cation)==0)
{
ifile.close();
found=true;
std::strcat(cation,charge);
break;
}
ifile.close();
if(!found){cation[0]='\0';anion[0]='\0';comp_type=myenum::notsalt;return;}
//check if anion exists using "anions.txt" and function ion_match()
//if it exists, get its charge and append the charge at the end of the data member 'anion'
ifile.clear();
ifile.open(ANION_LIST);
if(!ifile.is_open())disp_error(std::string("Can't open ")+ANION_LIST);
found=false;
while(ifile>>stdan>>charge)if(ion_match(anion,stdan))
{
ifile.close();
found=true;
std::strcpy(anion,stdan);
std::strcat(anion,charge);
break;
}
ifile.close();
if(!found)
{
cation[0]='\0';
anion[0]='\0';
comp_type=myenum::notsalt;
return;
}
comp_type=myenum::salt;
}
bool tube_t::carry_rxn(const rxn_t& rxn,std::string& obs)
{
std::set<comp_t>::const_iterator itr;
std::set<comp_t>::iterator it;
comp_t comp;
if(!my_includes(comp_list,rxn.reac_list))return false;
if(!my_includes(comp_list,rxn.cat_list))return false;
if((rxn.hight && !hight)||(rxn.lowt && hight))return false;
for(itr=rxn.reac_list.begin();itr!=rxn.reac_list.end();++itr)
{
for(it=comp_list.begin();it!=comp_list.end();++it)
{
if(it->exact_equal(*itr))
{comp_list.erase(it);break;}
else if(it->anion_equal(*itr))
{
comp=comp_t(it->cation,myenum::aq,myenum::cation);
comp_list.erase(it);
comp_list.insert(comp);
break;
}
else if(it->cation_equal(*itr))
{
comp=comp_t(it->anion,myenum::aq,myenum::anion);
comp_list.erase(it);
comp_list.insert(comp);
break;
}
}
}
comp_list.insert(rxn.prod_list.begin(),rxn.prod_list.end());
obs=rxn.comment;
if(show_mech)std::cout<<boost::lexical_cast<std::string>(*this)<<std::endl;
return true;
}
void tube_t::assign(std::string str,bool heat/*=false*/)
{
std::string comp_str;
comp_list.clear();
hight=heat;
int i;
for(i=0;i<int(str.size())-1;i++)if(str[i]=='+' && ((i>0 && str[i-1]==')') || (str[i+1]!='+' && str[i+1]!='(')))str[i]='&';
boost::char_separator<char> sep("&");
boost::tokenizer<boost::char_separator<char> >tokens(str,sep);
BOOST_FOREACH(comp_str,tokens)
{comp_list.insert(boost::lexical_cast<comp_t>(comp_str));}
}
void tube_t::add(std::string str,bool heat/*=false*/)
{
std::string comp_str;
hight=heat;
int i;
for(i=0;i<int(str.size())-1;i++)if(str[i]=='+' && ((i>0 && str[i-1]==')') || (str[i+1]!='+' && str[i+1]!='(')))str[i]='&';
boost::char_separator<char> sep("&");
boost::tokenizer<boost::char_separator<char> >tokens(str,sep);
BOOST_FOREACH(comp_str,tokens)
{comp_list.insert(boost::lexical_cast<comp_t>(comp_str));}
}
void tube_t::react_prep()
{
char salt[COMP_FORM_SIZE],dummy[COMP_FORM_SIZE];
bool is_ppt=false,found=false,never_ppt=false;
comp_t comp;
std::set<comp_t> new_comp_list;
std::ifstream ifile,ifile2;
std::set<comp_t>::iterator it;
char ch;
//find type of and cations and anions in each compound
for(it=comp_list.begin();it!=comp_list.end();++it)
{
if(it->comp_type==myenum::none)
{it->split();}
}
//check for dissoc media
ifile.open(DISSOC_MEDIA);
if(!ifile.is_open())disp_error(std::string("Can't open ")+DISSOC_MEDIA);
while(ifile>>dummy)
{
comp=dummy;
found=std::binary_search(comp_list.begin(),comp_list.end(),comp);
if(found)break;
}
ifile.close();
if(!found)return;
//convert solid ions to aqueous ions
for(it=comp_list.begin();it!=comp_list.end();++it)
{if(it->comp_type==myenum::anion || it->comp_type==myenum::cation)it->st=myenum::aq;}
//dissociate if not in ppts file and found in NEVER_PPT_ION
ifile.clear();
ifile.open(PPTS);
ifile2.open(NEVER_PPT_ION);
if(!ifile.is_open())disp_error(std::string("Can't open ")+PPTS);
if(!ifile2.is_open())disp_error(std::string("Can't open ")+NEVER_PPT_ION);
new_comp_list=comp_list;
BOOST_FOREACH(comp,comp_list)if(comp.comp_type==myenum::salt)
{
is_ppt=false;never_ppt=false;
while(ifile2>>dummy)
{
ch=dummy[strlen(dummy)-1];
if((ch=='+' && strcmp(comp.cation,dummy)==0) || (ch=='-' && strcmp(comp.anion,dummy)==0))
{never_ppt=true;break;}
}
if(!never_ppt)while(ifile>>salt>>dummy)
{
is_ppt=(std::strcmp(salt,comp.form)==0);
if(is_ppt)break;
}
if(!is_ppt)
{
new_comp_list.insert(comp_t(comp.cation,myenum::aq,myenum::cation));
new_comp_list.insert(comp_t(comp.anion,myenum::aq,myenum::anion));
new_comp_list.erase(comp);
}
}
comp_list=new_comp_list;
ifile.close();
}
void tube_t::precipitate()
{
comp_t comp,compcat,compan;
std::string ppt,dummy;
std::ifstream ifile(PPTS);
std::set<comp_t>::iterator itcat,itan;
bool fcat,fan;
while(ifile>>ppt>>dummy)
{
comp=ppt;
comp.st=myenum::solid;
comp.split();
compcat=comp_t(comp.cation,myenum::aq,myenum::cation);
compan=comp_t(comp.anion,myenum::aq,myenum::anion);
itcat=comp_list.find(compcat);
fcat=itcat!=comp_list.end();
itan=comp_list.find(compan);
fan=itan!=comp_list.end();
if(fcat&&fan)
{
comp_list.erase(compcat);
comp_list.erase(compan);
comp_list.insert(comp);
}
}
}
/*void tube_t::ppt_by_cation()
{
std::ifstream ifile;
char path[FILE_PATH_SIZE],ion[FILE_NAME_SIZE];
std::set<comp_t>::iterator it,it2;
std::set<comp_t>::to_remove,to_add;
for(it=comp_list.begin();it!=comp_list.end();++it)
{
if(!it->comp_type==myenum::cation)continue;
std::strcpy(ion,it->form);
ifile.clear();
std::strcpy(path,PPTS_FOLDER);
std::strcat(path,ion)
ifile.open(path);
if(!ifile.open()){ifile.close();continue;}
to_remove.insert(*it);
while(ifile>>ion)
{
for(it2=comp_list.begin();it2!=comp_list.end();++it2)
{if(it2->comp_type==myenum::anion && std::strcmp(it2->form,ion)==0)to_remove.insert(*it2);}
//generate ppt from *it and *it2 and insert it in 'to_add'
}
ifile.close();
}
//remove 'to_remove' from 'comp_list'
//add 'to_add' to 'comp_list'
}*/
void tube_t::react(const char* fname,std::set<std::string>& obs_set)
{
std::ifstream ifile(fname);
if(!ifile.is_open())disp_error(std::string("Can't open ")+fname);
std::string str,obs;
while(ifile>>str)
{
carry_rxn(boost::lexical_cast<rxn_t>(str),obs);
if(!obs.empty())obs_set.insert(obs);
}
ifile.close();
}
void tube_t::react_list(const char* fname/*=RXNS_FILE*/,std::set<std::string>& obs_set)
{
char path[FILE_PATH_SIZE],str[FILE_NAME_SIZE];
std::ifstream ifile(fname);
if(!ifile.is_open())disp_error(std::string("Can't open ")+fname);
react_prep();
while(my_getline(ifile,str,FILE_NAME_SIZE))
{
strcpy(path,RXNS_FOLDER);
strcat(path,str);
react(path,obs_set);
}
precipitate();
}
void tube_t::phys_obs(std::set<std::string>& obs_set)const
{
std::ifstream pptfile(PPTS);
std::ifstream ionfile(ION_COLOR);
std::ifstream gasfile(GAS_SMELL_COLOR);
std::set<comp_t>::const_iterator it=comp_list.begin();
std::string comp,color,smell;
for(;it!=comp_list.end();++it)
{
if(it->st==myenum::solid)
{
pptfile.clear();
pptfile.seekg(0);
while(pptfile>>comp>>color)if(comp==it->form)obs_set.insert(color+" ppt");
}
else if(it->st==myenum::gas)
{
gasfile.clear();
gasfile.seekg(0);
while(gasfile>>comp>>smell>>color)if(comp==it->form)
{
if(color!="none")obs_set.insert(color+" gas");
if(smell!="none")obs_set.insert(smell+" smell");
}
}
else if(it->comp_type==myenum::anion || it->comp_type==myenum::cation || it->comp_type==myenum::notsalt)
{
ionfile.clear();
ionfile.seekg(0);
while(ionfile>>comp>>color)if(comp==it->form)obs_set.insert(color+" solution");
}
}
pptfile.close();
ionfile.close();
gasfile.close();
}
//================================================================================