1
/**********************************************************************
2
Copyright (C) 2005 by Chris Morley
4
This file is part of the Open Babel project.
5
For more information, see <http://openbabel.sourceforge.net/>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation version 2 of the License.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
***********************************************************************/
16
#include <openbabel/babelconfig.h>
18
#pragma warning (disable : 4786)
19
#pragma warning (disable : 4251) //
23
#include <openbabel/obmolecformat.h>
24
#include <openbabel/kinetics.h>
29
class ThermoFormat : public OBMoleculeFormat
34
OBConversion::RegisterFormat("therm",this);
35
OBConversion::RegisterFormat("tdd",this);
38
virtual const char* Description()
42
"Reads and writes old-style NASA polynomials in original fixed format\n"
43
"Read Options e.g. -ae\n"
44
" -e Terminate on \"END\"\n\n";
47
////////////////////////////////////////////////////
48
/// The "API" interface functions
49
virtual bool ReadMolecule(OBBase* pReact, OBConversion* pConv);
50
virtual bool WriteMolecule(OBBase* pReact, OBConversion* pConv);
53
////////////////////////////////////////////
54
//Make an instance of the format class
55
ThermoFormat theThermoFormat;
56
////////////////////////////////////////////
58
bool ThermoFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
60
OBMol* pmol = pOb->CastAndClear<OBMol>();
63
bool stopOnEnd = pConv->IsOption("e",OBConversion::INOPTIONS)!=NULL;
64
pmol->SetDimension(0);
65
OBNasaThermoData* pND = new OBNasaThermoData; //to store rate constant data
66
pND->SetOrigin(fileformatInput);
69
istream &ifs = *pConv->GetInStream();
71
double DefaultMidT = 1500;
75
//find line with 1 in col 80
78
if(!ifs.getline(ln,BUFF_SIZE) || stopOnEnd && !strncasecmp(ln,"END",3))
82
char phase, nam[25], dum[7], elname[3];
87
sscanf(ln,"%18s%6s",nam,dum);
92
//Reaction Design extension
95
if(!getline(ifs,line))return false;
97
tokenize(toks,line," \t\n\r");
98
for(i=0;i<toks.size();i+=2)
101
atom.SetAtomicNum(etab.GetAtomicNum(toks[i].c_str()));
102
elnum = atoi(toks[i+1].c_str());
104
for(;elnum>0;--elnum)
110
for(i=0;i<4;i++,p+=5)
112
char snum[4]={0,0,0,0};//Was problem with F 10 0 reading as ten
113
sscanf(p,"%c%c%c%c%c",elname,elname+1,snum,snum+1,snum+2);
115
if(elname[0]!=' ' && elname[0]!='0')
120
atom.SetAtomicNum(etab.GetAtomicNum(elname));
122
for(;elnum>0;--elnum)
127
double LoT, HiT, MidT=0;
128
/* int nc = */sscanf(p,"%c%10lf%10lf10%lf",&phase, &LoT, &HiT, &MidT);
129
pND->SetPhase(phase);
132
if(MidT>HiT || MidT<LoT)
135
if (!ifs.getline(ln, BUFF_SIZE)) return false;
137
for(i=0;i<5;i++,p+=15)
138
sscanf(p,"%15lf",&Coeff[i]);
139
if (!ifs.getline(ln, BUFF_SIZE)) return false;
141
for(i=5;i<10;i++,p+=15)
142
sscanf(p,"%15lf",&Coeff[i]);
143
if (!ifs.getline(ln, BUFF_SIZE)) return false;
145
for(i=10;i<14;i++,p+=15)
146
sscanf(p,"%15lf",&Coeff[i]);
149
pND->SetCoeff(i, Coeff[i]);
151
pmol->AssignSpinMultiplicity();
155
////////////////////////////////////////
156
bool ThermoFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
158
OBMol* pmol = dynamic_cast<OBMol*>(pOb);
159
string title(pmol->GetTitle());
160
OBNasaThermoData* pND = static_cast<OBNasaThermoData*>(pmol->GetData(ThermoData));
163
obErrorLog.ThrowError(__FUNCTION__,"No thermo data in " + title, obWarning);
166
ostream &ofs = *pConv->GetOutStream();
169
unsigned oldf = _set_output_format(_TWO_DIGIT_EXPONENT);
171
string formula = pmol->GetSpacedFormula();
173
tokenize(toks,formula);
175
ofs << left << setw(24) << title.substr(0,24);
176
//Check that atom numbers are less than 999
177
bool toobig=toks.size()>8;
178
for(i=0;i<toks.size() && !toobig ;i+=2)
179
if(atoi(toks[i+1].c_str())>999)
182
//Reaction Design extension
183
ofs << string(20,' ');
188
ofs << left << setw(2) << toks[i] << right << setw(3) << toks[i+1];
190
ofs << right << pND->GetPhase() << fixed << setprecision(3) << setw(10) << pND->GetLoT();
191
ofs << setw(10) << pND->GetHiT() << setw(9) << pND->GetMidT() << " 01";
194
ofs << "&\n" << formula << '\n';
198
ofs << scientific << setprecision(7);
200
ofs << setw(15) << pND->GetCoeff(i);
203
ofs << setw(15) << pND->GetCoeff(i);
206
ofs << setw(15) << pND->GetCoeff(i);
210
_set_output_format(oldf);
216
}//OpenBabel namespace