00001
00002
00004
00005 #include "EnsembleCombinationDirector.h"
00006
00008
00010
00011 CEnsembleCombinationDirector::CEnsembleCombinationDirector(int ensembleSize)
00012 {
00013 m_iEnsembleSize = ensembleSize;
00014 }
00015
00016 CEnsembleCombinationDirector::~CEnsembleCombinationDirector()
00017 {
00018 int i;
00019
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
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
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
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
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
00121
00122
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
00151 for(int eCount = 0; eCount < m_iEnsembleSize; eCount++)
00152 {
00153
00154 for(i = 0; i < m_vpRunnables.size(); i++)
00155 {
00156
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
00165 m_vpRunnables[i]->Run();
00166
00167
00168
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
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
00184 for(i = 0; i < m_vpMinimizables.size(); i++)
00185 {
00186
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
00196 double cost = m_vpMinimizables[i]->ObjectiveFunction(parameters);
00197
00198
00199
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
00215 cout << "Ensemble member " << eCount << " run." << endl;
00216 }
00217
00218
00219 ConvertToStatistics();
00220
00221 Notify();
00222
00223
00224
00225
00226
00227
00228 char filename[200];
00229 for(i = 0; i < m_vpRunnables.size(); i++)
00230 {
00231
00232 sprintf(filename,"runnable%i.dat",i);
00233
00234 FILE *pfData = fopen(filename,"w");
00235
00236 fprintf(pfData,"##################################################################\n");
00237 fprintf(pfData,"# ENSEMBLE CHEMICAL DATA FOR RUNNABLE %i\n",i);
00238 fprintf(pfData,"##################################################################\n");
00239
00240 int nChem = m_vpRunnables[i]->GetReactionNetwork()->GetNumberOfChemicals();
00241 int nSteps = m_vpRunnables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00242
00243 for(int cCount = 0; cCount < nChem; cCount++)
00244 {
00245
00246 fprintf(pfData,"# %i %s\n",cCount,m_vpRunnables[i]->GetReactionNetwork()->GetChemical(cCount)->GetName().c_str());
00247
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
00260 for(i = 0; i < m_vpMinimizables.size(); i++)
00261 {
00262
00263 sprintf(filename,"minimizable%i.dat",i);
00264
00265
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
00271 int nChem = m_vpMinimizables[i]->GetExperiment()->GetChemicalTimeSeriesData()->GetNChemicals();
00272 int nSteps = m_vpMinimizables[i]->GetCellObserver()->GetNumberOfTimeSteps();
00273
00274 for(int cCount = 0; cCount < nChem; cCount++)
00275 {
00276
00277 fprintf(pfData,"# %i %s\n",cCount,m_vpMinimizables[i]->GetExperiment()->GetReactionNetwork()->GetChemical(cCount)->GetName().c_str());
00278
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
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
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
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
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
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
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 }