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

EnsembleCombinationDirector.cpp

Go to the documentation of this file.
00001 // EnsembleCombinationDirector.cpp: implementation of the CEnsembleCombinationDirector class.
00002 //
00004 
00005 #include "EnsembleCombinationDirector.h"
00006 
00008 // Construction/Destruction
00010 
00011 CEnsembleCombinationDirector::CEnsembleCombinationDirector(int ensembleSize)
00012 {
00013         m_iEnsembleSize = ensembleSize;
00014 }
00015 
00016 CEnsembleCombinationDirector::~CEnsembleCombinationDirector()
00017 {
00018         int i;
00019         // lots to delete
00020         for(i = 0; i < m_vpdEnsembleParameters.size(); i++)
00021         {
00022                 delete [] m_vpdEnsembleParameters[i];
00023         }
00024         for(i = 0; i < m_vpdNoDataSumC.size(); i++)
00025         {
00026                 delete [] m_vpdNoDataSumC[i][0];
00027                 delete [] m_vpdNoDataSumC[i];
00028                 delete [] m_vpdNoDataSumCSqr[i][0];
00029                 delete [] m_vpdNoDataSumCSqr[i];
00030         }
00031         for(i = 0; i < m_vpdDataSumC.size(); i++)
00032         {
00033                 delete [] m_vpdDataSumC[i][0];
00034                 delete [] m_vpdDataSumC[i];
00035                 delete [] m_vpdDataSumCSqr[i][0];
00036                 delete [] m_vpdDataSumCSqr[i];
00037         }
00038         for(i = 0; i < m_vpEnsembleDataPlotters.size(); i++)
00039         {
00040                 delete m_vpEnsembleDataPlotters[i];
00041         }
00042         for(i = 0; i < m_vpEnsembleRunPlotters.size(); i++)
00043         {
00044                 delete m_vpEnsembleRunPlotters[i];
00045         }
00046         for(i = 0; i < m_vpMinimizables.size(); i++)
00047         {
00048                 delete m_vpMinimizables[i];
00049         }
00050         for(i = 0; i < m_vpNetworks.size(); i++)
00051         {
00052                 delete m_vpNetworks[i];
00053         }
00054         for(i = 0; i < m_vpExperiments.size(); i++)
00055         {
00056                 delete m_vpExperiments[i];
00057         }
00058         for(i = 0; i < m_vpMovers.size(); i++)
00059         {
00060                 delete m_vpMovers[i];
00061         }
00062 
00063 }
00064 
00065 void CEnsembleCombinationDirector::SumAllocate()
00066 {
00067         int i;
00068         // allocate for the runnables
00069         for(i = 0; i < m_vpRunnables.size(); i++)
00070         {
00071                 int nSteps = m_vpRunnables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00072                 int nChem = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfChemicals();
00073                 m_vpdNoDataSumC.push_back(new double *[nChem]);
00074                 m_vpdNoDataSumCSqr.push_back(new double *[nChem]);
00075                 m_vpdNoDataSumC[i][0] = new double[nChem*nSteps];
00076                 m_vpdNoDataSumCSqr[i][0] = new double[nChem*nSteps];
00077                 for(int chem = 1; chem < nChem; chem++)
00078                 {
00079                         m_vpdNoDataSumC[i][chem] = &(m_vpdNoDataSumC[i][0][chem*nSteps]);
00080                         m_vpdNoDataSumCSqr[i][chem] = &(m_vpdNoDataSumCSqr[i][0][chem*nSteps]);
00081                 }
00082                 // zero the array just created
00083                 for(int l = 0; l < nChem; l++)
00084                 {
00085                         for(int m = 0; m < nSteps; m++)
00086                         {
00087                                 m_vpdNoDataSumC[i][l][m] = 0;
00088                                 m_vpdNoDataSumCSqr[i][l][m] = 0;
00089                         }
00090                 }
00091         }
00092         // do the same thing for the minimizables
00093         for(i = 0; i < m_vpMinimizables.size(); i++)
00094         {
00095                 int nSteps = m_vpMinimizables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00096                 int nChem = m_vpMinimizables[i]->GetExperiment()->GetChemicalTimeSeriesData()->GetNChemicals();
00097                 m_vpdDataSumC.push_back(new double *[nChem]);
00098                 m_vpdDataSumCSqr.push_back(new double *[nChem]);
00099                 m_vpdDataSumC[i][0] = new double[nChem*nSteps];
00100                 m_vpdDataSumCSqr[i][0] = new double[nChem*nSteps];
00101                 for(int chem = 1; chem < nChem; chem++)
00102                 {
00103                         m_vpdDataSumC[i][chem] = &(m_vpdDataSumC[i][0][chem*nSteps]);
00104                         m_vpdDataSumCSqr[i][chem] = &(m_vpdDataSumCSqr[i][0][chem*nSteps]);
00105                 }
00106                 // zero the array just created
00107                 for(int l = 0; l < nChem; l++)
00108                 {
00109                         for(int m = 0; m < nSteps; m++)
00110                         {
00111                                 m_vpdDataSumC[i][l][m] = 0;
00112                                 m_vpdDataSumCSqr[i][l][m] = 0;
00113                         }
00114                 }
00115         }
00116 }
00117 
00118 void CEnsembleCombinationDirector::LoadEnsembleInfo(std::string parameterFile)
00119 {
00120         // read in the vectors of parameters - this assumes absolutely everybody has
00121         // exactly the same number of parameters; all runs are just different facets
00122         // of a single network
00123         int nRC = 0;
00124         if(m_vpRunnables.size() > 0)
00125         {
00126                 nRC = m_vpRunnables[0]->GetReactionNetwork()->GetNumberOfRateConstants();
00127         }
00128         else
00129         {
00130                 nRC = m_vpMinimizables[0]->GetNParameters();
00131         }
00132         ParameterReader *pEnsReader = new ParameterReader(parameterFile.c_str());
00133         for(int i = 0; i < m_iEnsembleSize; i++)
00134         {
00135                 m_vpdEnsembleParameters.push_back(new double[nRC]);
00136                 for(int j = 0; j < nRC; j++)
00137                 {
00138                         double parameter = pEnsReader->ReadParameter();
00139                         m_vpdEnsembleParameters[i][j] = parameter;
00140                 }
00141         }
00142         delete pEnsReader;
00143 
00144         return;
00145 }
00146 
00147 void CEnsembleCombinationDirector::Execute()
00148 {
00149         int i;
00150         // run over all runnables and all minimizables for each ensemble member
00151         for(int eCount = 0; eCount < m_iEnsembleSize; eCount++)
00152         {
00153                 // this is for all the runnables
00154                 for(i = 0; i < m_vpRunnables.size(); i++)
00155                 {
00156                         // set the parameters to the correct ensemble parameters
00157                         int nRC = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfRateConstants();
00158                         for(int j = 0; j < nRC; j++)
00159                         {
00160                                 double rate = m_vpdEnsembleParameters[eCount][j];
00161                                 m_vpRunnables[i]->GetReactionNetwork()->GetRateConstant(j)->SetRateConstant(rate);
00162                         }
00163                 
00164                         // run the simulation
00165                         m_vpRunnables[i]->Run();
00166                         
00167                         // now increment the statistical arrays; each curve is normalized so 
00168                         // so that C(tmax) = 1.0
00169                         int nChem = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfChemicals();
00170                         int nSteps = m_vpRunnables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00171                         for(int cCount = 0; cCount < nChem; cCount++)
00172                         {
00173                                 //double norm = m_vpRunnables[i]->GetCellObserver()->GetAverageConcentration()[cCount][nSteps-1];
00174                                 double norm = 1.0;
00175                                 for(int sCount = 0; sCount < nSteps; sCount++)
00176                                 {
00177                                         double conc = m_vpRunnables[i]->GetCellObserver()->GetAverageConcentration()[cCount][sCount];
00178                                         m_vpdNoDataSumC[i][cCount][sCount] += conc/norm;
00179                                         m_vpdNoDataSumCSqr[i][cCount][sCount] += (conc/norm)*(conc/norm);
00180                                 }
00181                         }
00182                 }
00183                 // this is for all the minimizables
00184                 for(i = 0; i < m_vpMinimizables.size(); i++)
00185                 {
00186                         // set the parameters to the correct ensemble parameters
00187                         int nRC = m_vpMinimizables[i]->GetNParameters();
00188                         double *parameters = new double[nRC];
00189                         for(int j = 0; j < nRC; j++)
00190                         {
00191                                 double rate = m_vpdEnsembleParameters[eCount][j];
00192                                 parameters[j] = rate;
00193                         }
00194                 
00195                         // run the simulation
00196                         double cost = m_vpMinimizables[i]->ObjectiveFunction(parameters);
00197                         
00198                         // now increment the statistical arrays; multiply by the conversion 
00199                         // factor before incrementing the array
00200                         int nChem = m_vpMinimizables[i]->GetExperiment()->GetChemicalTimeSeriesData()->GetNChemicals();
00201                         int nSteps = m_vpMinimizables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00202                         for(int cCount = 0; cCount < nChem; cCount++)
00203                         {
00204                                 for(int sCount = 0; sCount < nSteps; sCount++)
00205                                 {
00206                                         double conc = m_vpMinimizables[i]->GetCellObserver()->GetAverageConcentration()[cCount][sCount];
00207                                         double scale = m_vpMinimizables[i]->GetConversionFactor(cCount)->GetFactorValue();
00208                                         m_vpdDataSumC[i][cCount][sCount] += conc*scale;
00209                                         m_vpdDataSumCSqr[i][cCount][sCount] += (conc*scale)*(conc*scale);
00210                                 }
00211                         }
00212                         delete [] parameters;
00213                 }
00214                 // XXX debug
00215                 cout << "Ensemble member " << eCount << " run." << endl;
00216         }
00217 
00218         // convert the sums/squared sums to average/std. dev.
00219         ConvertToStatistics();
00220         // Notify observers (like plotters)
00221         Notify();
00222         // Now dump all the data to a file, one file for each minimizable/runnable
00223         // Format should be:  t   avg   avg+std   avg-std
00224         // Separator is \n\n#, with the name and chemical number of the 
00225         //    measurement on the second line so they can later be identified
00226         
00227         // runnables
00228         char filename[200];
00229         for(i = 0; i < m_vpRunnables.size(); i++)
00230         {
00231                 // set the name of the file
00232                 sprintf(filename,"runnable%i.dat",i);
00233                 // open the file
00234                 FILE *pfData = fopen(filename,"w");
00235                 // write a short header
00236                 fprintf(pfData,"##################################################################\n");
00237                 fprintf(pfData,"# ENSEMBLE CHEMICAL DATA FOR RUNNABLE %i\n",i);
00238                 fprintf(pfData,"##################################################################\n");
00239                 // number of chemicals/number of data points
00240                 int nChem = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfChemicals();
00241                 int nSteps = m_vpRunnables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00242                 // loop over chemicals and print the necessary info
00243                 for(int cCount = 0; cCount < nChem; cCount++)
00244                 {
00245                         // write chemical number/name on comment line
00246                         fprintf(pfData,"# %i %s\n",cCount,m_vpRunnables[i]->GetReactionNetwork()->GetChemical(cCount)->GetName().c_str());
00247                         // loop over number of measurements and write
00248                         for(int sCount = 0; sCount < nSteps; sCount++)
00249                         {
00250                                 double t = (double) sCount;
00251                                 double avg = m_vpdNoDataSumC[i][cCount][sCount];
00252                                 double sig = m_vpdNoDataSumCSqr[i][cCount][sCount];
00253                                 fprintf(pfData,"%16.6e %16.6e %16.6e %16.6e\n",t,avg,avg+sig,avg-sig);
00254                         }
00255                         fprintf(pfData,"\n\n");
00256                 }
00257                 fclose(pfData);
00258         }
00259         // minimizables
00260         for(i = 0; i < m_vpMinimizables.size(); i++)
00261         {
00262                 // set the name of the file
00263                 sprintf(filename,"minimizable%i.dat",i);
00264                 // open the file for reading - use std c functions because C++
00265                 // has awful numerical formatting
00266                 FILE *pfData = fopen(filename,"w");
00267                 fprintf(pfData,"##################################################################\n");
00268                 fprintf(pfData,"# ENSEMBLE CHEMICAL DATA FOR MINIMIZABLE %i\n",i);
00269                 fprintf(pfData,"##################################################################\n");
00270                 // number of chemicals/number of data points
00271                 int nChem = m_vpMinimizables[i]->GetExperiment()->GetChemicalTimeSeriesData()->GetNChemicals();
00272                 int nSteps = m_vpMinimizables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00273                 // loop over chemicals and print the necessary info
00274                 for(int cCount = 0; cCount < nChem; cCount++)
00275                 {
00276                         // write chemical number/name on a comment line
00277                         fprintf(pfData,"# %i %s\n",cCount,m_vpMinimizables[i]->GetExperiment()->GetReactionNetwork()->GetChemical(cCount)->GetName().c_str());
00278                         // loop over number of measurements and write
00279                         for(int sCount = 0; sCount < nSteps; sCount++)
00280                         {
00281                                 double t = (double) sCount;
00282                                 double avg = m_vpdDataSumC[i][cCount][sCount];
00283                                 double sig = m_vpdDataSumCSqr[i][cCount][sCount];
00284                                 fprintf(pfData,"%16.6e %16.6e %16.6e %16.6e\n",t,avg,avg+sig,avg-sig);
00285                         }
00286                         // double newlines to next data set
00287                         fprintf(pfData,"\n\n");
00288                 }               
00289                 fclose(pfData);
00290         }
00291         
00292         cout << "Data dumped to files." << endl;
00293 
00294         return;
00295 }
00296 
00297 
00298 void CEnsembleCombinationDirector::ConvertToStatistics()
00299 {
00300         int i;
00301         // now convert all the arrays
00302         for(i = 0; i < m_vpRunnables.size(); i++)
00303         {
00304                 int nChem = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfChemicals();
00305                 int nSteps = m_vpRunnables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00306                 for(int chem = 0; chem < nChem; chem++)
00307                 {
00308                         for(int step = 0; step < nSteps; step++)
00309                         {
00310                                 // find averages
00311                                 m_vpdNoDataSumC[i][chem][step] = m_vpdNoDataSumC[i][chem][step]/m_iEnsembleSize;
00312                                 m_vpdNoDataSumCSqr[i][chem][step] = m_vpdNoDataSumCSqr[i][chem][step]/m_iEnsembleSize;
00313                                 // convert sum(C^2) to sigma (fabs ensures numerical solvency for constants)
00314                                 m_vpdNoDataSumCSqr[i][chem][step] -= m_vpdNoDataSumC[i][chem][step]*m_vpdNoDataSumC[i][chem][step];
00315                                 m_vpdNoDataSumCSqr[i][chem][step] = sqrt(fabs(m_vpdNoDataSumCSqr[i][chem][step]));
00316                         }
00317                 }
00318         }
00319         for(i = 0; i < m_vpMinimizables.size(); i++)
00320         {
00321                 int nChem = m_vpMinimizables[i]->GetExperiment()->GetChemicalTimeSeriesData()->GetNChemicals();
00322                 int nSteps = m_vpMinimizables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00323                 for(int chem = 0; chem < nChem; chem++)
00324                 {
00325                         for(int step = 0; step < nSteps; step++)
00326                         {
00327                                 // find averages
00328                                 m_vpdDataSumC[i][chem][step] = m_vpdDataSumC[i][chem][step]/m_iEnsembleSize;
00329                                 m_vpdDataSumCSqr[i][chem][step] = m_vpdDataSumCSqr[i][chem][step]/m_iEnsembleSize;
00330                                 // convert sum(C^2) to sigma (fabs ensures numerical solvency for constants)
00331                                 m_vpdDataSumCSqr[i][chem][step] -= m_vpdDataSumC[i][chem][step]*m_vpdDataSumC[i][chem][step];
00332                                 m_vpdDataSumCSqr[i][chem][step] = sqrt(fabs(m_vpdDataSumCSqr[i][chem][step]));
00333                         }
00334                 }
00335         }
00336         
00337         cout << "Sum arrays converted to <C> and sigma(C)" << endl;
00338 }

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