-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutput.cpp
More file actions
468 lines (377 loc) · 13.4 KB
/
Output.cpp
File metadata and controls
468 lines (377 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/***************************************************************************//**
* Project: Colony
*
* \file Output.cpp
* \author Marc Weber\n
* The SiMBioSys group (CosmoLab)\n
* Parc Científic de Barcelona\n
* Barcelona, Spain.\n
* http://www.thesimbiosys.com
* \version 1.0
* \date 11/2009
*
* Copyright 2009 by Marc Weber
******************************************************************************/
#include "Output.h"
//------------------------------------------------------------------------------
Output::Output()
{}
//------------------------------------------------------------------------------
Output::~Output()
{
// Deleting dummy file indicating running simulation in output directory.
bf::path file1(simRunningFileNameString_);
bf::remove(file1);
}
//------------------------------------------------------------------------------
void Output::initialize(const Input& p)
{
// Write the new directory path into a string.
path_ = p.out.outputPath_;
// Output directory with name corresponding to the global parameters value.
stringstream pathSS;
pathSS << path_ << "/";
vector<string>::iterator it;
int i;
for (it=Input::listGlobalParametersName.begin(),i=0;
it!=Input::listGlobalParametersName.end();
it++,i++)
{
if (i!=0)
{
pathSS << "_";
}
// Write the name of the directory with the initial value of the global parameters.
//pathSS << *it << scientific << setprecision(3) << Input::globalParameter(i);
// Write the name of the directory with the final value of the global parameters.
int lastIndex = Input::globalParameterTimePoints.ubound(blitz::secondDim);
pathSS << *it << scientific << setprecision(3) << Input::globalParameterTimePoints( 0, lastIndex, i );
}
pathSS >> path_;
// Test if directory exists, create directory and check for existing files.
nExistingTrajFiles_ = 0;
bf::path dir1(path_); // Boost Filesystem path object.
if ( bf::exists(dir1) )
{
if ( bf::is_directory(dir1) )
{
// Find the number of trajectories already written in the directory.
bf::directory_iterator end_itr; // default construction yields past-the-end
// Iterate on the file list in directory
for ( bf::directory_iterator itr( dir1 ); itr != end_itr; ++itr )
{
if ( !is_directory(itr->status()) )
{
// Search for "xConc_trajXXX.dat" file and extract trajectory number
if ( itr->path().filename().string().substr(0,10).compare("xConc_traj") == 0)
{
int iTraj;
stringstream ssTraj(itr->path().filename().string().substr(10,3));
ssTraj >> iTraj;
nExistingTrajFiles_ = max(nExistingTrajFiles_,iTraj+1);
}
}
}
} else {
// dir1 is not a directory, delete it.
bf::remove(dir1);
// Create directory.
bf::create_directory(dir1);
}
} else {
// Create directory.
bf::create_directory(dir1);
}
if (nExistingTrajFiles_ > 0)
{
cout << "Found existing simulation output files in directory \"" << dir1.string() << "\"\n";
cout << "Number of existing trajectories: " << nExistingTrajFiles_ << endl;
cout << "Simulation output trajectories data files will start with number (counter starts at 0): " <<
nExistingTrajFiles_ << "\n\n\n" << endl;
}
// Writing a dummy file to indicate that simulation will write output in this directory.
simRunningFileNameString_ = path_ + "/waiting_for_simulation_output";
const char* simRunningFileName (simRunningFileNameString_.c_str());
ofstream simRunningOutputFile (simRunningFileName);
simRunningOutputFile.close();
xFileName_ = path_ + "/x_traj.dat";
xConcFileName_ = path_ + "/xConc_traj.dat";
volumeFileName_ = path_ + "/volumes_traj.dat";
cellsAvgFileName_ = path_ + "/cells_avg_traj.dat";
cellLineageFileName_ = path_ + "/cell_lineage_traj.dat";
positionFileName_ = path_ + "/position_traj.dat";
angleFileName_ = path_ + "/angle_traj.dat";
cellsTrajectoriesAvgFileName_ = path_ + "/cells_trajectories_avg.dat";
cellCyclePhasesFileName_ = path_ + "/cell_cycle_phases.dat";
firstPassageTimeFileName_ = path_ + "/firstPassageTime.dat";
}
//------------------------------------------------------------------------------
void Output::close()
{
// Deleting dummy file indicating running simulation in output directory.
bf::path file1(simRunningFileNameString_);
bf::remove(file1);
}
//------------------------------------------------------------------------------
void Output::writeTimeMeshTrajectory(list<TimeSlice>& timeMeshTrajectory,
int iTraj,
bool isEnabledComputeSpatialDynamics)
{
// Note: I should write a general method to write the data that has the
// same structure.
string filenameString2 (
insertNumberSuffixToFileName(xFileName_,iTraj + nExistingTrajFiles_) );
const char* filename2 (filenameString2.c_str());
ofstream outputfile2 (filename2);
string filenameString5 (
insertNumberSuffixToFileName(xConcFileName_,iTraj + nExistingTrajFiles_) );
const char* filename5 (filenameString5.c_str());
ofstream outputfile5 (filename5);
string filenameString4 (
insertNumberSuffixToFileName(positionFileName_,iTraj + nExistingTrajFiles_) );
const char* filename4 (filenameString4.c_str());
ofstream outputfile4 (filename4);
string filenameString6 (
insertNumberSuffixToFileName(angleFileName_,iTraj + nExistingTrajFiles_) );
const char* filename6 (filenameString6.c_str());
ofstream outputfile6 (filename6);
if ( outputfile2.is_open() && outputfile4.is_open() && outputfile5.is_open() && outputfile6.is_open())
{
list<TimeSlice>::iterator it;
for (it = timeMeshTrajectory.begin();
it!= timeMeshTrajectory.end();
it++)
{
outputfile2 << " 0, " << setw(7) << fixed << (*it).nCells_ << ", "
<< scientific << setprecision(10) << (*it).time_;
outputfile5 << " 0, " << setw(7) << fixed << (*it).nCells_ << ", "
<< scientific << setprecision(10) << (*it).time_;
if (isEnabledComputeSpatialDynamics)
{
outputfile4 << " 0, " << setw(7) << fixed << (*it).nCells_ << ", "
<< scientific << setprecision(10) << (*it).time_;
outputfile6 << " 0, " << setw(7) << fixed << (*it).nCells_ << ", "
<< scientific << setprecision(10) << (*it).time_;
}
int i;
for ( i=0; i < (*it).t0State_.size(); ++i)
{
outputfile2 << fixed << ", " << setw(16) << (*it).t0State_(i);
}
for ( i=0; i < (*it).xConc_.size(); ++i)
{
outputfile5 << fixed << ", " << setw(16) << (*it).xConc_(i);
}
if (isEnabledComputeSpatialDynamics)
{
for ( i=0; i < (*it).positionArray_.size(); ++i)
{
outputfile4 << fixed << ", " << setw(16) << (*it).positionArray_(i)(0);
outputfile4 << fixed << ", " << setw(16) << (*it).positionArray_(i)(1);
}
for ( i=0; i < (*it).angleArray_.size(); ++i)
{
outputfile6 << fixed << ", " << setw(16) << (*it).angleArray_(i);
}
}
outputfile2 << '\n';
outputfile5 << '\n';
if (isEnabledComputeSpatialDynamics)
{
outputfile4 << '\n';
outputfile6 << '\n';
}
}
outputfile2 << endl;
outputfile5 << endl;
if (isEnabledComputeSpatialDynamics)
{
outputfile4 << endl;
outputfile6 << '\n';
}
} else {
cout << "ERROR: Unable to open file \"" << filename2 << '\"' << " and \"" << filename4 << '\"'
<< " and \"" << filename5 << '\"' << " and \"" << filename6 << '\"' << endl;
}
if (!isEnabledComputeSpatialDynamics)
{
// Delete the files positions and angles, which are empty
outputfile4.close();
outputfile6.close();
bf::path file4(filenameString4);
bf::remove(file4);
bf::path file6(filenameString6);
bf::remove(file6);
}
#ifdef TIME_DEPENDENT_PROPENSITIES
string filenameString3 (
insertNumberSuffixToFileName(volumeFileName_,iTraj + nExistingTrajFiles_) );
const char* filename3 (filenameString3.c_str());
ofstream outputfile3 (filename3);
if ( outputfile3.is_open() )
{
list<TimeSlice>::iterator it;
for (it = timeMeshTrajectory.begin();
it!= timeMeshTrajectory.end();
it++)
{
outputfile3 << " 0, " << setw(7) << fixed << (*it).nCells_ << ", "
<< scientific << setprecision(10) << (*it).time_;
int i;
for ( i=0; i < (*it).volumeArray_.size(); i++)
{
outputfile3 << scientific << setprecision(10) << ", " << (*it).volumeArray_(i);
}
outputfile3 << '\n';
}
outputfile3 << endl;
} else {
cout << "ERROR: Unable to open file \"" << filename3 << '\"' << endl;
}
#endif
}
//------------------------------------------------------------------------------
void Output::writeTimeMeshCellsAvg
(
Array<TimeSliceCellsAvg,1>& timeMeshCellsAvg,
int iTraj
)
{
string filenameString1 (
insertNumberSuffixToFileName(cellsAvgFileName_,iTraj + nExistingTrajFiles_) );
const char* filename (filenameString1.c_str());
ofstream outputfile (filename);
if ( outputfile.is_open() )
{
int i;
for (i=0; i<timeMeshCellsAvg.size(); i++)
{
outputfile << setw(4) << fixed
<< timeMeshCellsAvg(i).nSamples_
<< ", " << setw(7) << setprecision(2)
<< timeMeshCellsAvg(i).nCellsAvg_ /
double(timeMeshCellsAvg(i).nSamples_)
<< ", " << scientific << setprecision(10)
<< timeMeshCellsAvg(i).timeStart_;
int j;
for ( j=0; j < timeMeshCellsAvg(i).stateAvg_.size(); j++)
{
outputfile << scientific << setprecision(10) << ", "
<< timeMeshCellsAvg(i).stateAvg_(j) /
double(timeMeshCellsAvg(i).nSamples_);
}
outputfile << '\n';
}
outputfile << endl;
} else {
cout << "ERROR: Unable to open file \"" << filename << endl;
}
}
//------------------------------------------------------------------------------
void Output::writeCellLineage
(
list<CellLineageGeneration> cellLineage,
int iTraj
)
{
string filenameString1 (
insertNumberSuffixToFileName(cellLineageFileName_,iTraj + nExistingTrajFiles_) );
const char* filename1 (filenameString1.c_str());
ofstream outputfile1 (filename1);
if ( outputfile1.is_open() )
{
list<CellLineageGeneration>::iterator it;
for (it = cellLineage.begin();
it!= cellLineage.end();
it++)
{
outputfile1
<< scientific << setprecision(10) << (*it).time_
<< ", " << setw(7) << fixed << (*it).nCells_
<< ", " << setw(7) << fixed << (*it).nCellsDelta_;
int i;
for ( i=0; i < (*it).motherCellsIndices_.size(); i++)
{
outputfile1 << ", " << setw(7) << fixed << (*it).motherCellsIndices_(i);
}
outputfile1 << '\n';
}
outputfile1 << endl;
} else {
cout << "ERROR: Unable to open file \"" << filename1 << '\"' << endl;
}
}
//------------------------------------------------------------------------------
void Output::writeTimeMeshCellsTrajectoriesAvg
(
Array<TimeSliceCellsAvg,1>& timeMeshCellsTrajectoriesAvg
)
{
const char* filename (cellsTrajectoriesAvgFileName_.c_str());
ofstream outputfile (filename);
if ( outputfile.is_open() )
{
int i;
for (i=0; i<timeMeshCellsTrajectoriesAvg.size(); i++)
{
outputfile << setw(4) << fixed
<< timeMeshCellsTrajectoriesAvg(i).nSamples_
<< ", " << setw(7) << setprecision(2)
<< timeMeshCellsTrajectoriesAvg(i).nCellsAvg_ /
double(timeMeshCellsTrajectoriesAvg(i).nSamples_)
<< ", " << scientific << setprecision(10)
<< timeMeshCellsTrajectoriesAvg(i).timeStart_;
int j;
for ( j=0; j < timeMeshCellsTrajectoriesAvg(i).stateAvg_.size(); j++)
{
outputfile << scientific << setprecision(10) << ", "
<< timeMeshCellsTrajectoriesAvg(i).stateAvg_(j) /
double(timeMeshCellsTrajectoriesAvg(i).nSamples_);
}
outputfile << '\n';
}
outputfile << endl;
} else {
cout << "ERROR: Unable to open file \"" << filename << "\"." << endl;
}
}
//------------------------------------------------------------------------------
void Output::writeCellCyclePhases(Array<double,1> cellCyclePhases)
{
const char* filename (cellCyclePhasesFileName_.c_str());
ofstream outputfile (filename);
if ( outputfile.is_open() )
{
int i;
for (i=0; i<cellCyclePhases.size(); i++)
{
outputfile
<< scientific << setprecision(10)
<< cellCyclePhases(i);
outputfile << '\n';
}
outputfile << endl;
} else {
cout << "ERROR: Unable to open file \"" << filename << "\"." << endl;
}
}
//------------------------------------------------------------------------------
void Output::writeFirstPassageTime
(
Array<double,1>& firstPassageTimeValues
)
{
const char* filename (firstPassageTimeFileName_.c_str());
ofstream outputfile (filename);
if ( outputfile.is_open() )
{
int i;
for (i=0; i<firstPassageTimeValues.size(); i++)
{
outputfile << scientific << setprecision(10) << firstPassageTimeValues(i) << '\n';
}
} else {
cout << "ERROR: Unable to open file \"" << filename << endl;
}
}
//------------------------------------------------------------------------------