Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

ReactionNetwork.cpp

Go to the documentation of this file.
00001 // ReactionNetwork.cpp: implementation of the ReactionNetwork class.
00002 //
00004 
00005 #ifdef _WINDOWS
00006 #include "stdafx.h"
00007 #endif
00008 
00009 #include "ReactionNetwork.h"
00010 #include "Reaction.h"
00011 
00013 // Construction/Destruction
00015 
00016 ReactionNetwork::ReactionNetwork()
00017 {
00018         // sort the list of algebraic indices - this way we don't 
00019         // have to jump around in the chemical vector
00020         std::sort(algebraicIndices.begin(),algebraicIndices.end());
00021 }
00022 
00023 ReactionNetwork::~ReactionNetwork()
00024 {
00025         int i;
00026         // first delete chemicals and rate constants
00027         for (i = 0; i < chemicals.size(); i++) delete chemicals[i];
00028         for (i = 0; i < rateConstants.size(); i++) delete rateConstants[i];
00029         // now delete reactions
00030         for (i = 0; i < reactions.size(); i++) delete reactions[i];
00031         // clean up vector of algebraic indices
00032         algebraicIndices.erase(algebraicIndices.begin(),algebraicIndices.end());
00033 }
00034 
00035 int ReactionNetwork::GetNumberOfChemicals() const
00036 {
00037         return chemicals.size();
00038 }       
00039 
00040 int ReactionNetwork::GetNumberOfAlgebraicChemicals() const
00041 {
00042         return algebraicIndices.size();
00043 }
00044 
00045 int ReactionNetwork::GetNumberOfReactions() const
00046 {
00047         return reactions.size();
00048 }
00049 
00050 int ReactionNetwork::GetNumberOfRateConstants() const
00051 {
00052         return rateConstants.size();
00053 }       
00054 
00055 void ReactionNetwork::ChemicalReset()
00056 {
00057         for(int i = 0; i < GetNumberOfChemicals(); i++)
00058         {
00059                 GetChemical(i)->Reset();
00060         }
00061 }
00062 
00063 void ReactionNetwork::RateConstantReset()
00064 {
00065         for(int i = 0; i < GetNumberOfRateConstants(); i++)
00066         {
00067                 GetRateConstant(i)->Reset();
00068         }
00069 }
00070 
00071 Chemical *ReactionNetwork::GetChemical(int chemicalNumber)
00072 {
00073         return chemicals[chemicalNumber];
00074 }       
00075 
00076 RateConstant *ReactionNetwork::GetRateConstant(int rateConstantNumber)
00077 {
00078         return rateConstants[rateConstantNumber];
00079 }       
00080 
00081 Reaction *ReactionNetwork::GetReaction(int reactionNumber)
00082 {
00083         return reactions[reactionNumber];
00084 }
00085 
00086 std::vector<int> *ReactionNetwork::GetAlgebraicIndices()
00087 {
00088         return &algebraicIndices;
00089 }
00090 
00091 std::vector<Reaction*> *ReactionNetwork::GetReactions()
00092 {
00093         return &reactions;
00094 }
00095 
00096 std::vector<Chemical*> *ReactionNetwork::GetChemicals()
00097 {
00098         return &chemicals;
00099 }
00100 
00101 std::vector<RateConstant*> *ReactionNetwork::GetRateConstants()
00102 {
00103         return &rateConstants;
00104 }
00105 
00106 std::vector<double> *ReactionNetwork::GetReactionRates()
00107 {
00108         // ensure synchrony with the reactions
00109         if(reactionRates.size() != this->GetNumberOfReactions())
00110         {
00111                 // clear out the old vector
00112                 reactionRates.erase(reactionRates.begin(),reactionRates.end());
00113                 // fill the vector with the current rates
00114                 for(int i = 0; i < this->GetNumberOfReactions(); i++)
00115                 {
00116                         reactionRates.push_back(reactions[i]->GetRate());
00117                 }
00118         }
00119         else
00120         {
00121                 for(int i = 0; i < this->GetNumberOfReactions(); i++)
00122                 {
00123                         reactionRates[i] = reactions[i]->GetRate();
00124                 }
00125         }
00126         
00127         // return the vector
00128         return &reactionRates;
00129 }
00130 
00131 void ReactionNetwork::SetAmount(int chemicalNumber, double amount)
00132 {
00133         this->GetChemical(chemicalNumber)->SetAmount(amount);
00134 }
00135 
00136 void ReactionNetwork::AddAlgebraicIndex(Chemical *chemical)
00137 {
00138         int index = chemical->GetChemicalNumber();
00139         algebraicIndices.push_back(index);
00140 }
00141 
00142 void ReactionNetwork::RemoveReaction(int rxnNumber)
00143 {
00144         // first delete the reaction so memory is properly freed
00145         delete reactions[rxnNumber];
00146         // now remove from the vector
00147         reactions.erase(reactions.begin() + rxnNumber);
00148 }
00149 
00150 void ReactionNetwork::DumpTeXForm(std::string fileName)
00151 {
00152     int i;
00153         int nChem = chemicals.size();
00154         int nReac = reactions.size();
00155         int cNum = 0;
00156         std::string sign;
00157         
00158         std::string junk;
00159         junk = "-";
00160 
00161         int nTerms = this->GetNTermsPerLine();
00162         
00163         // keeps track of how many terms have been written so linebreaks can be put in
00164         std::vector<int> viTerms;
00165 #ifdef _WIN32
00166 //      std::vector<strstream *> vpStreams;
00167         std::vector<std::ostringstream *> vpStreams;
00168 #else
00169         std::vector<std::ostringstream *> vpStreams;
00170 #endif  // _WIN32
00171 
00172         for(i = 0; i < nChem; i++)
00173         {
00174                 viTerms.push_back(0);
00175 #ifdef _WIN32
00176 //              vpStreams.push_back(new strstream);
00177                 vpStreams.push_back(new std::ostringstream);
00178 #else
00179                 vpStreams.push_back(new std::ostringstream);
00180 #endif
00181         }
00182 
00183         fstream *pTeXFile = new fstream(fileName.c_str(),ios::out);
00184 
00185         *pTeXFile << "\\documentclass[12pt]{article}" << endl;
00186         *pTeXFile << "\\topmargin 0.0cm" << endl;
00187         *pTeXFile << "\\oddsidemargin 0.2cm" << endl;
00188         *pTeXFile << "\\textwidth 16cm" << endl;
00189         *pTeXFile << "\\textheight 21cm" << endl;
00190         *pTeXFile << "\\footskip 1.0cm" << endl;
00191         *pTeXFile << "\\begin{document}" << endl << endl;
00192         *pTeXFile << "\\begin{eqnarray*}" << endl;
00193         
00194         
00195         // build up the text RHSs the same way that the RHSs are numerically computed
00196         for (int rNum = 0; rNum < nReac; rNum++)
00197         {
00198                 std::vector<Chemical *> *localChemicals = reactions[rNum]->GetChemicals();
00199                 int nLC = localChemicals->size();
00200                 
00201                 for (int lcNum = 0; lcNum < nLC; lcNum++)
00202                 {
00203                         cNum = localChemicals->at(lcNum)->GetChemicalNumber();
00204                         int nChanged = reactions[rNum]->GetNumberChangedByReaction(lcNum);
00205                         // next lines are for formatting
00206                         if(nChanged == 0)
00207                         {
00208                                 // don't print anything
00209                         }
00210                         else 
00211                         {
00212                                 if(nChanged > 0){sign = "+";}
00213                                 else {sign = "-";}
00214                                 
00215                                 if(abs(nChanged) == 1)
00216                                 {
00217                                         if((viTerms[cNum] % nTerms == 0) && viTerms[cNum] > 0)
00218                                         {
00219                                                 *vpStreams[cNum] << "\\\\ \n & &";
00220                                         }
00221                                         // don't print the 1, just the sign 
00222                                         *vpStreams[cNum] << sign.c_str() << " " << reactions[rNum]->GetTeXForm().c_str() << " ";
00223                                 }
00224                                 else
00225                                 {
00226                                         if((viTerms[cNum] % nTerms == 0) && viTerms[cNum] > 0)
00227                                         {
00228                                                 *vpStreams[cNum] << "\\\\ \n & &";
00229                                         }
00230                                         // print the sign and then abs(nChanged)
00231                                         *vpStreams[cNum] << sign.c_str() << " " << abs(nChanged) << reactions[rNum]->GetTeXForm().c_str() << " ";
00232                                 }
00233                                 viTerms[cNum]++;
00234                         }
00235                 }
00236         }
00237         
00238 
00239         
00240         // now loop over chemicals and write everything
00241         int counter = 0;
00242         for(int cLoop = 0;  cLoop < nChem; cLoop++)
00243         {       
00244                 // if this is an algebraic, ignore it
00245                 if((counter < algebraicIndices.size()) && (cLoop == algebraicIndices[counter]))
00246                 {
00247                         counter++;
00248                         continue;
00249                 }
00250                 
00251                 *pTeXFile << "{d \\left[ " << chemicals[cLoop]->GetTeXName().c_str() << "\\right]";
00252                 *pTeXFile << " \\over {dt}} & = & ";
00253                 
00254                 if(viTerms[cLoop] == 0)
00255                 {
00256                         *pTeXFile << "0 \\\\" << endl;
00257                 }
00258 //              *vpStreams[cLoop] << std::ends;
00259                 
00260                 *pTeXFile << vpStreams[cLoop]->str(); 
00261                 *pTeXFile << "\\\\" << endl;
00262         }
00263         
00264         
00265         *pTeXFile << "\\end{eqnarray*}" << endl << endl;
00266         *pTeXFile << "\\end{document}" << endl;
00267         
00268         for(i = 0; i < vpStreams.size(); i++)
00269         {
00270 //              vpStreams[i]->rdbuf()->freeze(0);
00271                 delete vpStreams[i];
00272         }
00273         delete pTeXFile;
00274 }
00275 
00276 void ReactionNetwork::DumpPetriNetDotFile(std::string fileName)
00277 {
00278         int i;
00279         int nChem = chemicals.size();
00280         int nReac = reactions.size();
00281         int cNum = 0;
00282         int lcNum = 0;
00283         
00284         // open file for writing
00285         fstream *pDotFile = new fstream(fileName.c_str(),ios::out);
00286 
00287         *pDotFile << "digraph G {" << endl;
00288         if(this->GetLRGraph())
00289         {
00290                 *pDotFile << "\t rankdir = LR;" << endl;
00291         }
00292         *pDotFile << "\t center = true;" << endl;
00293         
00294         // first list all the chemicals (circles) and reactions (boxes)
00295         *pDotFile << "\t node [shape = circle];" << endl;
00296         for(i = 0; i < nChem; i++)
00297         {
00298                 *pDotFile << "\t\t " << "\"" << chemicals[i]->GetName().c_str() << "\";" << endl;
00299         }
00300         *pDotFile << "\t node [shape = box, height = 0.75, width = 0.25];" << endl;
00301         for(i = 0; i < nReac; i++)
00302         {
00303                 *pDotFile << "\t\t " << "\"" << reactions[i]->GetName().c_str() << "\";" <<  endl;
00304         }
00305         
00306         // now connect up the circles and boxes - stoichiometry gives the direction of
00307         // the arrows
00308         for (int rNum = 0; rNum < nReac; rNum++)
00309         {
00310                 std::vector<Chemical *> *localChemicals = reactions[rNum]->GetChemicals();
00311                 int nLC = localChemicals->size();
00312 
00313                 // loop over the chemicals involved in this reaction
00314                 for(lcNum = 0; lcNum < nLC; lcNum++)
00315                 {
00316                         cNum = localChemicals->at(lcNum)->GetChemicalNumber();
00317                         int nChanged = reactions[rNum]->GetNumberChangedByReaction(lcNum);
00318                         
00319                         if(nChanged == 0) // catalytic, arrow goes to and from reaction
00320                         {
00321                                 *pDotFile << "\t\t " << "\"" << chemicals[cNum]->GetName().c_str() << "\"" << " -> ";
00322                                 *pDotFile << "\"" << reactions[rNum]->GetName().c_str() << "\"" << ";" << endl;
00323                                 *pDotFile << "\t\t " << "\"" << reactions[rNum]->GetName().c_str() << "\"" << " -> ";
00324                                 *pDotFile << "\"" << chemicals[cNum]->GetName().c_str() << "\"" << ";" << endl;
00325                         }
00326                         else if(nChanged > 0) // from transition to place
00327                         {
00328                                 *pDotFile << "\t\t " << "\"" << reactions[rNum]->GetName().c_str() << "\"" << " -> ";
00329                                 *pDotFile << "\"" << chemicals[cNum]->GetName().c_str() << "\"" << ";" << endl;
00330                         }
00331                         else // from place to transition
00332                         {
00333                                 *pDotFile << "\t\t " << "\"" << chemicals[cNum]->GetName().c_str() << "\"" << " -> ";
00334                                 *pDotFile << "\"" << reactions[rNum]->GetName().c_str() << "\"" << ";" << endl;
00335                         }
00336                 }
00337                 
00338         }
00339 
00340         // write closing bracket
00341         *pDotFile << "}" << endl;
00342 
00343         delete pDotFile;
00344 }

Generated on Mon Nov 3 09:38:11 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002