00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "CellDiffusionDataOutput.h"
00024
00025 #include <CompuCell3D/Potts3D/CellInventory.h>
00026 #include <CompuCell3D/Automaton/Automaton.h>
00027
00028 #include <CompuCell3D/Simulator.h>
00029 #include <CompuCell3D/Potts3D/Cell.h>
00030 #include <CompuCell3D/Potts3D/Potts3D.h>
00031 #include <CompuCell3D/Field3D/Point3D.h>
00032 #include <CompuCell3D/Field3D/Dim3D.h>
00033 using namespace CompuCell3D;
00034
00035 #include <XMLCereal/XMLPullParser.h>
00036 #include <XMLCereal/XMLSerializer.h>
00037
00038 #include <BasicUtils/BasicString.h>
00039 #include <BasicUtils/BasicException.h>
00040
00041
00042 #include <string>
00043
00044 #include <plugins/CenterOfMass/CenterOfMassPlugin.h>
00045 #include <fstream>
00046 #include <sstream>
00047
00048 using namespace std;
00049
00050
00051 CellDiffusionDataOutput::CellDiffusionDataOutput() :
00052 potts(0)
00053 {
00054
00055 cellIDFlag=false;
00056 deltaPositionFlag=false;
00057
00058 }
00059
00060 CellDiffusionDataOutput::~CellDiffusionDataOutput(){
00061
00062 for(int i = 0 ; i < filePtrVec.size() ; ++i){
00063 if(filePtrVec[i]){
00064
00065 filePtrVec[i]->close();
00066 delete filePtrVec[i];
00067 filePtrVec[i]=0;
00068
00069 }
00070 }
00071
00072 }
00073
00074 void CellDiffusionDataOutput::init(Simulator *simulator) {
00075 potts = simulator->getPotts();
00076 cellInventoryPtr=& potts->getCellInventory();
00077 }
00078
00079 void CellDiffusionDataOutput::extraInit(Simulator *simulator) {
00080
00081
00082
00083
00084 }
00085
00086
00087
00088 void CellDiffusionDataOutput::start() {
00089
00090
00091
00092 CenterOfMassPlugin * centerOfMassPluginPtr=(CenterOfMassPlugin*)(Simulator::pluginManager.get("CenterOfMass"));
00093 ASSERT_OR_THROW("CenterOfMass plugin not initialized!", centerOfMassPluginPtr);
00094
00095 CellInventory::cellInventoryIterator cInvItr;
00096 CellG * cell;
00097
00098 for (int i = 0 ; i < cellIds.size() ; ++i ){
00099 cInvItr=cellInventoryPtr->find(cellIds[i]);
00100 if(cInvItr != cellInventoryPtr->cellInventoryEnd()){
00101 cell=*cInvItr;
00102 cellIdsPtrs.push_back(cell);
00103 }else{
00104 cerr<<"Could not find in the inventory cell with id="<<cellIds[i]<<" . Ignoring request"<<endl;
00105 }
00106 }
00107
00108
00109 cellPositions.assign(cellIdsPtrs.size(),Coordinates3D<float>());
00110
00111 filePtrVec.assign(cellIdsPtrs.size(),0);
00112 for(int i = 0 ; i < filePtrVec.size() ; ++i){
00113 ostringstream str;
00114 str<<fileName<<"_"<<cellIdsPtrs[i]->id<<".txt";
00115 filePtrVec[i]=new ofstream(str.str().c_str());
00116 cerr<<"Creating file name="<<str.str()<<endl;
00117 }
00118
00119
00120
00121 float xCM,yCM,zCM;
00122
00123
00124 if(deltaPositionFlag){
00125 for(int i = 0 ; i < cellPositions.size() ; ++i ){
00126
00127 if( cellInventoryPtr->find(cellIdsPtrs[i]) == cellInventoryPtr->cellInventoryEnd() ){
00128 continue;
00129 }
00130
00131 cell= cellIdsPtrs[i];
00132 xCM=cell->xCM/(float)cell->volume;
00133 yCM=cell->yCM/(float)cell->volume;
00134 zCM=cell->zCM/(float)cell->volume;
00135 cellPositions[i]=Coordinates3D<float>(xCM,yCM,zCM);
00136
00137 }
00138 }
00139
00140 }
00141
00142
00143
00144 void CellDiffusionDataOutput::step(const unsigned int currentStep) {
00145
00146 float xCM,yCM,zCM;
00147 CellG * cell;
00148 float deltaPos;
00149 Point3D prevCM;
00150
00151 for(int i = 0 ; i < cellIdsPtrs.size() ; ++i){
00152 ofstream &out=(*filePtrVec[i]);
00153 if( cellInventoryPtr->find(cellIdsPtrs[i]) == cellInventoryPtr->cellInventoryEnd() ){
00154 filePtrVec[i]->close();
00155 continue;
00156 }
00157 cell= cellIdsPtrs[i];
00158
00159 xCM=cell->xCM/(float)cell->volume;
00160 yCM=cell->yCM/(float)cell->volume;
00161 zCM=cell->zCM/(float)cell->volume;
00162
00163 out<<cell->id<<"\t";
00164 out<<currentStep<<"\t";
00165 out<<xCM<<"\t"<<yCM<<"\t"<<zCM<<"\t";
00166
00167 if(deltaPositionFlag){
00168
00169 prevCM.x=cellPositions[i].X();
00170 prevCM.y=cellPositions[i].Y();
00171 prevCM.z=cellPositions[i].Z();
00172 deltaPos=sqrt((xCM-prevCM.x)*(xCM-prevCM.x)+(yCM-prevCM.y)*(yCM-prevCM.y)+(zCM-prevCM.z)*(zCM-prevCM.z));
00173
00174 out<<deltaPos<<"\t";
00175 }
00176
00177 out<<endl;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 }
00187
00188 void CellDiffusionDataOutput::readXML(XMLPullParser &in) {
00189
00190 in.skip(TEXT);
00191
00192 string type;
00193
00194 while (in.check(START_ELEMENT)) {
00195
00196 if(in.getName() == "OutputFormat"){
00197
00198 if(in.findAttribute("DeltaPosition")>=0){
00199
00200 deltaPositionFlag=true;
00201 }
00202
00203 if(in.findAttribute("FileName")>=0){
00204 fileName=in.getAttribute("FileName").value;;
00205 }else{
00206 fileName="Output";
00207 }
00208
00209 in.matchSimple();
00210 }
00211
00212 else if (in.getName() == "OutputID") {
00213
00214
00215 if(in.findAttribute("CellID")>=0){
00216 unsigned int id=BasicString::parseUInteger(in.getAttribute("CellID").value);
00217 cellIds.push_back(id);
00218 }
00219
00220
00221
00222 in.matchSimple();
00223
00224 }else {
00225 throw BasicException(string("Unexpected element '") + in.getName() +
00226 "'!", in.getLocation());
00227 }
00228
00229 in.skip(TEXT);
00230 }
00231 }
00232
00233 void CellDiffusionDataOutput::writeXML(XMLSerializer &out) {
00234 }
00235
00236
00237
00238
00239
00240
00241