~ubuntu-branches/debian/stretch/openbabel/stretch

« back to all changes in this revision

Viewing changes to src/formats/thermoformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2008-07-22 23:54:58 UTC
  • mfrom: (3.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080722235458-3o606czluviz4akx
Tags: 2.2.0-2
* Upload to unstable.
* debian/control: Updated descriptions.
* debian/patches/gauss_cube_format.patch: New patch, makes the 
  gaussian cube format available again.
* debian/rules (DEB_DH_MAKESHLIBS_ARGS_libopenbabel3): Removed.
* debian/rules (DEB_CONFIGURE_EXTRA_FLAGS): Likewise.
* debian/libopenbabel3.install: Adjust formats directory to single 
  version hierarchy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
Copyright (C) 2005 by Chris Morley
 
3
 
 
4
This file is part of the Open Babel project.
 
5
For more information, see <http://openbabel.sourceforge.net/>
 
6
 
 
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.
 
10
 
 
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>
 
17
#ifdef WIN32
 
18
#pragma warning (disable : 4786)
 
19
#pragma warning (disable : 4251) //
 
20
#endif
 
21
#include <string>
 
22
#include <iomanip>
 
23
#include <openbabel/obmolecformat.h>
 
24
#include <openbabel/kinetics.h>
 
25
 
 
26
using namespace std;
 
27
namespace OpenBabel
 
28
{
 
29
class ThermoFormat : public OBMoleculeFormat
 
30
{
 
31
public:
 
32
  ThermoFormat()
 
33
  {
 
34
      OBConversion::RegisterFormat("therm",this);
 
35
      OBConversion::RegisterFormat("tdd",this);
 
36
  }
 
37
 
 
38
  virtual const char* Description()
 
39
  {
 
40
    return
 
41
      "Thermo format\n"
 
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";
 
45
  }
 
46
 
 
47
  ////////////////////////////////////////////////////
 
48
  /// The "API" interface functions
 
49
  virtual bool ReadMolecule(OBBase* pReact, OBConversion* pConv);
 
50
  virtual bool WriteMolecule(OBBase* pReact, OBConversion* pConv);
 
51
};
 
52
 
 
53
////////////////////////////////////////////
 
54
//Make an instance of the format class
 
55
ThermoFormat theThermoFormat;
 
56
////////////////////////////////////////////
 
57
 
 
58
bool ThermoFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
 
59
{
 
60
  OBMol* pmol = pOb->CastAndClear<OBMol>();
 
61
  if(!pmol)
 
62
    return false;
 
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);
 
67
  pmol->SetData(pND);
 
68
 
 
69
  istream &ifs = *pConv->GetInStream();
 
70
 
 
71
  double DefaultMidT = 1500;
 
72
  char ln[BUFF_SIZE];
 
73
  unsigned int i;
 
74
 
 
75
  //find line with 1 in col 80
 
76
  do
 
77
  {
 
78
    if(!ifs.getline(ln,BUFF_SIZE) || stopOnEnd && !strncasecmp(ln,"END",3))
 
79
      return false; 
 
80
  }while(ln[79]!='1');
 
81
 
 
82
  char phase, nam[25], dum[7], elname[3];
 
83
  elname[2]=0;
 
84
  int elnum;
 
85
  double Coeff[14];
 
86
 
 
87
  sscanf(ln,"%18s%6s",nam,dum);
 
88
  pmol->SetTitle(nam);
 
89
  char* p=ln+24;
 
90
  if(ln[80]=='&')
 
91
  {
 
92
    //Reaction Design extension
 
93
    p+=20;
 
94
    string line;
 
95
    if(!getline(ifs,line))return false; 
 
96
    vector<string> toks;
 
97
    tokenize(toks,line," \t\n\r");
 
98
    for(i=0;i<toks.size();i+=2)
 
99
    {
 
100
      OBAtom atom;
 
101
      atom.SetAtomicNum(etab.GetAtomicNum(toks[i].c_str()));
 
102
      elnum = atoi(toks[i+1].c_str());
 
103
      atom.ForceNoH();
 
104
      for(;elnum>0;--elnum)
 
105
        pmol->AddAtom(atom);
 
106
    }   
 
107
  }
 
108
  else
 
109
  {
 
110
    for(i=0;i<4;i++,p+=5)
 
111
    {
 
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);
 
114
      elnum=atoi(snum);
 
115
      if(elname[0]!=' ' && elname[0]!='0')
 
116
      {
 
117
        if(elname[1]==' ')
 
118
          elname[1]=0;
 
119
        OBAtom atom;
 
120
        atom.SetAtomicNum(etab.GetAtomicNum(elname));
 
121
        atom.ForceNoH();
 
122
        for(;elnum>0;--elnum)
 
123
          pmol->AddAtom(atom);
 
124
      }
 
125
    }
 
126
  }
 
127
  double LoT, HiT, MidT=0;
 
128
  /* int nc = */sscanf(p,"%c%10lf%10lf10%lf",&phase, &LoT, &HiT, &MidT);
 
129
  pND->SetPhase(phase);
 
130
  pND->SetLoT(LoT);
 
131
  pND->SetHiT(HiT);
 
132
  if(MidT>HiT || MidT<LoT)
 
133
    MidT=DefaultMidT;
 
134
  pND->SetMidT(MidT);
 
135
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
 
136
  p=ln;
 
137
  for(i=0;i<5;i++,p+=15)
 
138
    sscanf(p,"%15lf",&Coeff[i]);
 
139
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
 
140
  p=ln;
 
141
  for(i=5;i<10;i++,p+=15)
 
142
    sscanf(p,"%15lf",&Coeff[i]);
 
143
  if (!ifs.getline(ln, BUFF_SIZE)) return false;
 
144
  p=ln;
 
145
  for(i=10;i<14;i++,p+=15)
 
146
    sscanf(p,"%15lf",&Coeff[i]);
 
147
  
 
148
  for(i=0;i<14;++i)
 
149
    pND->SetCoeff(i, Coeff[i]);
 
150
 
 
151
  pmol->AssignSpinMultiplicity();
 
152
  return true;
 
153
}
 
154
 
 
155
////////////////////////////////////////
 
156
bool ThermoFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
157
{
 
158
  OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
159
  string title(pmol->GetTitle());
 
160
  OBNasaThermoData* pND = static_cast<OBNasaThermoData*>(pmol->GetData(ThermoData));
 
161
  if(!pND)
 
162
  {
 
163
    obErrorLog.ThrowError(__FUNCTION__,"No thermo data in " + title, obWarning);
 
164
    return false;
 
165
  }
 
166
  ostream &ofs = *pConv->GetOutStream();
 
167
  unsigned int i;
 
168
#ifdef _MSC_VER
 
169
  unsigned oldf = _set_output_format(_TWO_DIGIT_EXPONENT);
 
170
#endif
 
171
  string formula = pmol->GetSpacedFormula();
 
172
  vector<string> toks;
 
173
  tokenize(toks,formula);
 
174
 
 
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)
 
180
      toobig =true;
 
181
  if(toobig)
 
182
    //Reaction Design extension
 
183
    ofs << string(20,' ');
 
184
  else
 
185
  {
 
186
    toks.resize(8);
 
187
    for(i=0;i<8;i+=2)
 
188
    ofs << left << setw(2) << toks[i] << right << setw(3) << toks[i+1];
 
189
  }
 
190
  ofs << right << pND->GetPhase() << fixed << setprecision(3) << setw(10) << pND->GetLoT();
 
191
  ofs << setw(10) << pND->GetHiT() << setw(9) << pND->GetMidT() << "    01";
 
192
  
 
193
  if(toobig)
 
194
    ofs << "&\n" << formula << '\n'; 
 
195
  else
 
196
    ofs << '\n';
 
197
 
 
198
  ofs << scientific << setprecision(7);
 
199
  for(i=0;i<5;++i)
 
200
    ofs << setw(15) << pND->GetCoeff(i);
 
201
  ofs << "    2\n";
 
202
  for(i=5;i<10;++i)
 
203
    ofs << setw(15) << pND->GetCoeff(i);
 
204
  ofs << "    3\n";
 
205
  for(i=10;i<14;++i)
 
206
    ofs << setw(15) << pND->GetCoeff(i);
 
207
  ofs << "                   4\n";
 
208
 
 
209
#ifdef _MSC_VER
 
210
  _set_output_format(oldf);
 
211
#endif
 
212
 
 
213
  return true;
 
214
}
 
215
 
 
216
}//OpenBabel namespace