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

GnuPlotterTrajectoryObserver.cpp

Go to the documentation of this file.
00001 // GnuPlotterTrajectoryObserver.cpp: implementation of the GnuPlotterTrajectoryObserver class.
00002 //
00004 
00005 #include "GnuPlotterTrajectoryObserver.h"
00006 
00008 // Construction/Destruction
00010 
00011 GnuPlotterTrajectoryObserver::GnuPlotterTrajectoryObserver(int chemX, int chemY, int chemZ)
00012 {
00013         this->chemX = chemX;
00014         this->chemY = chemY;
00015         this->chemZ = chemZ;
00016 }
00017 
00018 GnuPlotterTrajectoryObserver::~GnuPlotterTrajectoryObserver()
00019 {
00020 
00021 }
00022 
00023 void GnuPlotterTrajectoryObserver::AddChemicalToList(int chemNumber)
00024 {
00025         otherChem.push_back(chemNumber);
00026 }
00027 
00028 void GnuPlotterTrajectoryObserver::Update(Subject *theChangedSubject)
00029 {
00030         int i;
00031         // static cast may be dangerous
00032         SingleNetworkMinimizable *nM = (SingleNetworkMinimizable *)theChangedSubject;
00033 
00034         // reopen the temp file
00035         freopen(buffername,"w",buffer);
00036 
00037         int maxTime = nM->GetCellObserver()->GetNumberOfTimeSteps();
00038         
00039         char chemXName[100],chemYName[100],chemZName[100];
00040         char titlestring[100],commandstring[1000];
00041         char xLabelString[100],yLabelString[100],zLabelString[100];
00042 
00043         // 2D plot
00044         if(chemZ == -1)
00045         {
00046                 // try to normalize
00047                 double basalX = nM->GetCellObserver()->GetAverageConcentration()[chemX][0];
00048                 double basalY = nM->GetCellObserver()->GetAverageConcentration()[chemY][0];
00049                 sprintf(chemXName,nM->GetExperiment()->GetReactionNetwork()->GetChemical(chemX)->GetName().c_str());
00050                 sprintf(chemYName,nM->GetExperiment()->GetReactionNetwork()->GetChemical(chemY)->GetName().c_str());
00051                 double rescale;
00052                 if(basalX > 0.0)
00053                 {
00054                         rescale = basalX;
00055                         sprintf(xLabelString,"set xlabel \"[%s]/[%s]_0\"\n",chemXName,chemXName);
00056                         sprintf(yLabelString,"set ylabel \"[%s]/[%s]_0\"\n",chemYName,chemXName);
00057                 }
00058                 else if(basalY > 0.0)
00059                 {
00060                         rescale = basalY;
00061                         sprintf(xLabelString,"set xlabel \"[%s]/[%s]_0\"\n",chemXName,chemYName);
00062                         sprintf(yLabelString,"set ylabel \"[%s]/[%s]_0\"\n",chemYName,chemYName);
00063                 }
00064                 else
00065                 {       
00066                         rescale = 1.0;
00067                         sprintf(xLabelString,"set xlabel \"[%s]\"\n",chemXName);
00068                         sprintf(yLabelString,"set ylabel \"[%s]\"\n",chemYName);
00069 
00070                 }
00071         
00072                 // loop over the time points and draw the trajectory
00073                 // vectors are set to be 1/4 the length between adjacent 
00074                 // points
00075         
00076                 double firstX,firstY,secondX,secondY,deltaX,deltaY;
00077 
00078                 // uses vectors
00079                 for(i = 0; i < maxTime - 2; i++)
00080                 {
00081                         // get the appropriate points
00082                         firstX = (nM->GetCellObserver()->GetAverageConcentration()[chemX][i])/rescale;
00083                         firstY = (nM->GetCellObserver()->GetAverageConcentration()[chemY][i])/rescale;
00084 
00085                         secondX = (nM->GetCellObserver()->GetAverageConcentration()[chemX][i+1])/rescale;
00086                         secondY = (nM->GetCellObserver()->GetAverageConcentration()[chemY][i+1])/rescale;
00087 
00088                         deltaX = .25*(secondX - firstX);
00089                         deltaY = .25*(secondY - firstY);
00090 
00091                         // write to the buffer
00092                         fprintf(buffer,"%e\t%e\t%e\t%e\n",firstX,firstY,deltaX,deltaY);
00093                 }
00094                 // print the last line, using the previous vector length for
00095                 // the last vector length
00096                 fprintf(buffer,"%e\t%e\t%e\t%e\n",secondX,secondY,deltaX,deltaY);
00097                 // pad with double newlines
00098                 fprintf(buffer,"\n\n");
00099 
00100         }
00101         else // 3D plot
00102         {
00103                 // try to normalize
00104                 double basalX = nM->GetCellObserver()->GetAverageConcentration()[chemX][0];
00105                 double basalY = nM->GetCellObserver()->GetAverageConcentration()[chemY][0];
00106                 double basalZ = nM->GetCellObserver()->GetAverageConcentration()[chemZ][0];
00107                 sprintf(chemXName,nM->GetExperiment()->GetReactionNetwork()->GetChemical(chemX)->GetName().c_str());
00108                 sprintf(chemYName,nM->GetExperiment()->GetReactionNetwork()->GetChemical(chemY)->GetName().c_str());
00109                 sprintf(chemZName,nM->GetExperiment()->GetReactionNetwork()->GetChemical(chemZ)->GetName().c_str());
00110                 double rescale;
00111                 if(basalX > 0.0)
00112                 {
00113                         rescale = basalX;
00114                         sprintf(xLabelString,"set xlabel \"[%s]/[%s]_0\"\n",chemXName,chemXName);
00115                         sprintf(yLabelString,"set ylabel \"[%s]/[%s]_0\"\n",chemYName,chemXName);
00116                         sprintf(zLabelString,"set zlabel \"[%s]/[%s]_0\"\n",chemZName,chemXName);
00117 
00118                 }
00119                 else if(basalY > 0.0)
00120                 {
00121                         rescale = basalY;
00122                         sprintf(xLabelString,"set xlabel \"[%s]/[%s]_0\"\n",chemXName,chemYName);
00123                         sprintf(yLabelString,"set ylabel \"[%s]/[%s]_0\"\n",chemYName,chemYName);
00124                         sprintf(zLabelString,"set zlabel \"[%s]/[%s]_0\"\n",chemZName,chemYName);
00125                 }
00126                 else if(basalZ > 0.0)
00127                 {       
00128                         rescale = basalZ;
00129                         sprintf(xLabelString,"set xlabel \"[%s]/[%s]_0\"\n",chemXName,chemZName);
00130                         sprintf(yLabelString,"set ylabel \"[%s]/[%s]_0\"\n",chemYName,chemZName);
00131                         sprintf(zLabelString,"set zlabel \"[%s]/[%s]_0\"\n",chemZName,chemZName);
00132 
00133                 }
00134                 else
00135                 {
00136                         rescale = 1.0;
00137                         sprintf(xLabelString,"set xlabel \"[%s]\"\n",chemXName);
00138                         sprintf(yLabelString,"set ylabel \"[%s]\"\n",chemYName);
00139                         sprintf(zLabelString,"set zlabel \"[%s]\"\n",chemZName);
00140                 }
00141         
00142                 // no vector support for splot in current GNUplot version
00143                 for(i = 0; i < maxTime - 2; i++)
00144                 {
00145                         // get the appropriate points
00146                         double x = (nM->GetCellObserver()->GetAverageConcentration()[chemX][i])/rescale;
00147                         double y = (nM->GetCellObserver()->GetAverageConcentration()[chemY][i])/rescale;
00148                         double z = (nM->GetCellObserver()->GetAverageConcentration()[chemZ][i])/rescale; 
00149                         // write to the buffer
00150                         fprintf(buffer,"%e\t%e\t%e\n",x,y,z);
00151                 }
00152                 // pad with double newlines
00153                 fprintf(buffer,"\n\n");
00154         }
00155         // now dump the time series data for the chemicals to the temp file
00156         for(i = 0; i < otherChem.size(); i++)
00157         {
00158                 for(int t = 0; t < (maxTime-1); t++)
00159                 {
00160                         double time = double (t);
00161                         double sim = nM->GetCellObserver()->GetAverageConcentration()[otherChem[i]][t];
00162                         fprintf(buffer,"%e\t\t%e\n",time,sim);
00163                 }
00164                 // pad each series with two newlines
00165                 fprintf(buffer,"\n\n");
00166         }
00167         
00168         // set up for multiplot
00169         // first index is for the trajectory plot (one data series) and successive
00170         // indices are for the additional time series
00171         // FIRST PLOT
00172         SendPlotCommand("set multiplot\n");
00173         SendPlotCommand("set origin 0.0,0.0\n");
00174         SendPlotCommand("set size 0.5,1.0\n");
00175         sprintf(titlestring,"set title \"Chemical Trajectory\"\n");
00176         SendPlotCommand(titlestring);
00177         SendPlotCommand("set grid\n");
00178         SendPlotCommand("set nokey\n");
00179         
00180         if(chemZ == -1)
00181         {
00182                 // now plot all the style stuff
00183                 sprintf(commandstring,"plot \"%s\" index 0 w vector\n",buffername);
00184                 fflush(buffer);
00185                 SendPlotCommand(xLabelString);
00186                 SendPlotCommand(yLabelString);
00187         }
00188         else
00189         {
00190                 // now all the style stuff
00191                 sprintf(commandstring,"splot \"%s\" index 0 w linespoints\n",buffername);
00192                 fflush(buffer);
00193                 SendPlotCommand("set ticslevel 0\n");
00194                 SendPlotCommand("set border 4095\n");
00195                 SendPlotCommand(xLabelString);
00196                 SendPlotCommand(yLabelString);
00197                 SendPlotCommand(zLabelString);
00198         }
00199         
00200         // change single backslashes to forward slashes to keep windows happy
00201 #ifdef _WIN32
00202         int iterator = 0;
00203         while(commandstring[iterator] != '\0')
00204         {
00205                 if(commandstring[iterator] == '\\') {commandstring[iterator] = '/';}
00206                 iterator++;
00207         }
00208 #endif
00209         
00210         SendPlotCommand(commandstring);
00211         
00212         // SECOND PLOT
00213         SendPlotCommand("set origin 0.5,0.0\n");
00214         SendPlotCommand("set size 0.5,1.0\n");
00215         sprintf(titlestring,"set title \"Time Series Monitor\"\n");
00216         SendPlotCommand(titlestring);
00217         SendPlotCommand("set xlabel \"time (min)\"\n");
00218         SendPlotCommand("set ylabel \"[Chemical] (molecules)\"\n");
00219         SendPlotCommand("set key\n");
00220         // plot the first chemical manually
00221         char namestring[200];
00222         sprintf(namestring,nM->GetExperiment()->GetReactionNetwork()->GetChemical(otherChem[0])->GetName().c_str());
00223         sprintf(commandstring,"plot \"%s\" index 1 t \"%s\" w lines",buffername,namestring);
00224         // the rest - experiment first
00225         for(i = 1; i < otherChem.size(); i++)
00226         {
00227                 char tempbuffer[500];
00228                 sprintf(namestring,nM->GetExperiment()->GetReactionNetwork()->GetChemical(otherChem[i])->GetName().c_str());
00229                 sprintf(tempbuffer,",\"%s\" index %i t \"%s\" w lines",buffername,i+1,namestring);
00230                 strcat(commandstring,tempbuffer);
00231         }
00232         strcat(commandstring,"\n");
00233         fflush(buffer);
00234         
00235         // change single backslashes to forward slashes to keep windows happy
00236 #ifdef _WIN32
00237         iterator = 0;
00238         while(commandstring[iterator] != '\0')
00239         {
00240                 if(commandstring[iterator] == '\\') {commandstring[iterator] = '/';}
00241                 iterator++;
00242         }
00243 #endif
00244         
00245         SendPlotCommand(commandstring);
00246 }

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