-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimManager.cpp
More file actions
1677 lines (1496 loc) · 49.6 KB
/
SimManager.cpp
File metadata and controls
1677 lines (1496 loc) · 49.6 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
//#define DEBUG
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <sstream>
#include "SimManager.h"
#include "Plot.h"
#include "BehaviorBase.h"
#include "TreePopulation.h"
#include "Output.h"
#include "Messages.h"
#include "Behaviors.h"
#include "Constants.h"
#include "Populations.h"
#include "Grids.h"
#include "Grid.h"
#include "PopulationBase.h"
#include "ParsingFunctions.h"
#include "ModelMath.h"
//XML includes
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
using namespace std;
//////////////////////////////////////////////////////////////////////////////
// Constructor()
//////////////////////////////////////////////////////////////////////////////
clSimManager::clSimManager( int iMajorVersion, int iMinorVersion, const string sAppPath)
{
try
{
m_iMajorVersion = iMajorVersion;
m_iMinorVersion = iMinorVersion;
m_sAppPath = sAppPath;
mp_oBehaviorManager = NULL;
mp_oPopulationManager = NULL;
mp_oGridManager = NULL;
mp_oPlot = NULL;
m_eSimState = No_Data;
m_iBatchNumber = 0;
m_iBatchGroup = 0;
//Prepare XML parsing tools
try {
XMLPlatformUtils::Initialize();
} catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
modelErr stcErr;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sFunction = "clSimManager::clSimManager";
stcErr.sMoreInfo = "Couldn't initialize XML parsing. Message: ";
stcErr.sMoreInfo += message;
XMLString::release(&message);
throw( stcErr );
}
mp_oXMLParser = new xercesc::XercesDOMParser();
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
mp_oXMLParser->setErrorHandler( errHandler );
GoToNoDataState();
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::clSimManager" ;
GoToNoDataState();
throw( stcErr );
}
}
/////////////////////////////////////////////////////////////////////////////
// Destructor()
//////////////////////////////////////////////////////////////////////////////
clSimManager::~clSimManager()
{
if ( No_Data != m_eSimState )
GoToNoDataState();
delete mp_oPopulationManager; mp_oPopulationManager = NULL;
delete mp_oGridManager; mp_oGridManager = NULL;
delete mp_oBehaviorManager; mp_oBehaviorManager = NULL;
delete mp_oPlot; mp_oPlot = NULL;
//Shut down the XML tools
delete mp_oXMLParser;
XMLPlatformUtils::Terminate();
}
/////////////////////////////////////////////////////////////////////////////
// GoToNoDataState()
/////////////////////////////////////////////////////////////////////////////
void clSimManager::GoToNoDataState()
{
try
{
//Create the object managers, if they haven't been already
if ( !mp_oPopulationManager )
mp_oPopulationManager = new clPopulationManager( this );
if ( !mp_oGridManager )
mp_oGridManager = new clGridManager( this );
if ( !mp_oBehaviorManager )
mp_oBehaviorManager = new clBehaviorManager( this );
//Set up the plot, if it hasn't been already
//if ( !mp_oPlot )
delete mp_oPlot; mp_oPlot = new clPlot( this );
//Have the object managers and plot clear their memory, in case we're coming
//from a data defined state
if ( m_eSimState > No_Data )
{
mp_oGridManager->FreeMemory();
mp_oPopulationManager->FreeMemory();
mp_oBehaviorManager->FreeMemory();
//delete mp_oPlot;
//mp_oPlot = new clPlot( this );
}
//Re-initialize variables that come from input files
m_iNumTimesteps = 0;
m_iRandomSeed = 0;
m_iCurrentTimestep = 0;
m_iTargetTimestep = 0;
m_iBatchNumber = 0;
//No - don't do batch group here
//m_iBatchGroup = 0;
m_eSimState = No_Data;
} //end of try block
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GoToNoDataState" ;
GoToNoDataState();
throw( stcErr );
}
}
/////////////////////////////////////////////////////////////////////////////
// ReadFile()
/////////////////////////////////////////////////////////////////////////////
void clSimManager::ReadFile(string sFileName )
{
try
{
DOMDocument * p_oDoc;
fileType iFileType;
//Check the file type
iFileType = GetFileType( sFileName );
//Parameter file - load it
if ( parfile == iFileType )
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
m_sParFilename = sFileName;
ReadParameterFile( p_oDoc, iFileType );
//Batch file - erase memory and run the batch
}
else if ( batchfile == iFileType )
{
GoToNoDataState();
RunBatch( sFileName );
//Detailed output file - load it like a par file
}
else if ( detailed_output == iFileType )
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
m_sParFilename = sFileName;
ReadParameterFile( p_oDoc, iFileType );
//Tree file - pass it to the tree population
}
else if ( tree == iFileType )
{
//This is only allowed if a parameter file is loaded (i.e. the model is
//ready to run)
if ( Initialized != m_eSimState )
{
modelErr stcErr;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sFunction = "clSimManager::ReadFile" ;
stcErr.sMoreInfo = "Parameter file must be loaded";
throw( stcErr );
}
else
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
ReadTreeFile( p_oDoc, iFileType );
}
//Tree map file - pass it to the tree population
}
else if ( treemap == iFileType )
{
//This is only allowed if a parameter file is loaded (i.e. the model is
//ready to run)
if ( Initialized != m_eSimState )
{
modelErr stcErr;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sFunction = "clSimManager::ReadFile" ;
stcErr.sMoreInfo = "Parameter file must be loaded";
throw( stcErr );
}
else
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
ReadTreeFile( p_oDoc, iFileType );
}
//Grid map file - pass it to the grid manager
}
else if ( map == iFileType )
{
//This is only allowed if a parameter file is loaded (i.e. the model is
//ready to run)
if ( Initialized != m_eSimState )
{
modelErr stcErr;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sFunction = "clSimManager::ReadFile" ;
stcErr.sMoreInfo = "Parameter file must be loaded";
throw( stcErr );
}
else
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
ReadMapFile( p_oDoc, iFileType );
}
//Detailed output timestep file - read it like tree and grid maps
}
else if ( detailed_output_timestep == iFileType )
{
//This is only allowed if a parameter file is loaded (i.e. the model is
//ready to run)
if ( Initialized != m_eSimState )
{
modelErr stcErr;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sFunction = "clSimManager::ReadFile" ;
stcErr.sMoreInfo = "Parameter file must be loaded";
throw( stcErr );
}
else
{
mp_oXMLParser->parse(sFileName.c_str());
p_oDoc = mp_oXMLParser->getDocument();
ReadMapFile( p_oDoc, iFileType );
ReadTreeFile( p_oDoc, iFileType );
}
}
else
{
//This is not a type we can handle - throw an error
modelErr stcErr;
stcErr.iErrorCode = BAD_FILE_TYPE;
stcErr.sFunction = "clSimManager::ReadFile" ;
stcErr.sMoreInfo = sFileName;
throw( stcErr );
}
//Free the DOM document's memory
mp_oXMLParser->resetDocumentPool();
}
catch (modelErr &e) {throw (e);}
catch (SAXParseException & err) {
modelErr stcErr;
char* message = XMLString::transcode(err.getMessage());
stcErr.iErrorCode = BAD_XML_FILE;
stcErr.sFunction = "Xerces parser";
stcErr.sMoreInfo = message;
XMLString::release(&message);
throw(stcErr);
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::ReadFile" ;
GoToNoDataState();
throw( stcErr );
}
}
/////////////////////////////////////////////////////////////////////////////
// GetFileType()
/////////////////////////////////////////////////////////////////////////////
fileType clSimManager::GetFileType(string sFileName)
{
try
{
using namespace std;
string strFileExtension, strFileNameCopy, strPlaybackExtension, strFileCodeTag = "fileCode", //the XML tag for the SORTIE file
//code
strLine, //for working with lines of a text file after converting
//them from char*
strFileCode; //for holding the text of the file code from an XML file
string::size_type pos; //for string parsing - holds string positions
fstream file;
FILE * filetest; //only this kind of file can be tested for existence of a
//file without creating
char cXMLTest[5], //for extracting the first 4 characters of a text file
cLine[200], //for extracting text from file and converting to string
cXMLDec[] = "<?xm", cXMLCom[] = "<!--";
int iFileType = 0; //for an int representation of the file type code
modelErr stcErr;
stcErr.sFunction = "clSimManager::GetFileType" ;
//Does this file exist and can it be opened?
filetest = fopen(sFileName.c_str(), "r");
if ( filetest == NULL )
{ //file does not exist
stcErr.iErrorCode = BAD_FILE;
stcErr.sMoreInfo = sFileName;
throw( stcErr );
}
else
fclose( filetest );
strFileNameCopy = sFileName;
//Find this file's extension
pos = strFileNameCopy.find_last_of( "." );
if ( pos == string::npos ) //no extension - return "not sortie"
return notrecognized;
strFileExtension = strFileNameCopy.substr( pos, strFileNameCopy.length() );
// IS THIS XML?
//Look at the first four characters of the file - should be either <!--
//(a comment) or <?xm (the xml declaration)
file.open(sFileName.c_str(), ios::in);
file.get( cXMLTest, sizeof( cXMLTest ) );
strcat( cXMLTest, "\0" );
if ( strcmp( cXMLTest, cXMLDec ) == 0 || strcmp( cXMLTest, cXMLCom ) == 0 )
{
//Yes - this is an xml file - so search the file text for the fileCode tag
while ( 1 )
{ //read all the file text until we find what we want
//search the line from the file for the file code tag
file.getline( cLine, sizeof( cLine ) );
strLine = cLine;
pos = strLine.find( strFileCodeTag );
if ( pos != string::npos )
{
//we found the file code tag - now extract the 8 char code after it
strFileCode = strLine.substr( pos + strFileCodeTag.length() + 2, 8 );
//the file type is the third pair of numbers
strFileCode = strFileCode.substr( 4, 2 );
iFileType = atoi( strFileCode.c_str() );
break;
}
if ( file.eof() ) break;
}
//if we don't have a file code this isn't an XML file we can read - exit
if ( 0 == iFileType ) return notrecognized;
//this is an XML file and we have a file type code - see if it's defined
if ( iFileType > 0 && iFileType < lastfile )
return (fileType) iFileType; //yep - it was found
else
return notrecognized; //we couldn't tell what file type it was
} //end of if (strcmp(cXMLTest, "<?xm") == 0 || strcmp(cXMLTest, "<!--") == 0)
/** IS THIS OLD SORTIE? This isn't an XML file so see if it's an old SORTIE text file */
//Check the file extension
if ( strFileExtension == ".sgli" || strFileExtension == ".par" || strFileExtension == ".out"
|| strFileExtension == ".stmf" || strFileExtension == ".sll" || strFileExtension == ".hvr"
|| strFileExtension == ".spb" || strFileExtension == ".hvs" ) return oldsortie;
return notrecognized;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetFileType" ;
GoToNoDataState();
throw( stcErr );
}
return notrecognized;
}
/////////////////////////////////////////////////////////////////////////////
// GetFileType()
/////////////////////////////////////////////////////////////////////////////
fileType clSimManager::GetFileType( DOMDocument * p_oDoc ) {
try {
DOMElement * p_oRoot; //for getting the root element
DOMAttr * p_oAttribute; //for getting the filecode node
XMLCh *sVal;
std::string strData; //for extracting the filecode text
char *cData;
int iFileType; //for storing the extracted file type
p_oRoot = p_oDoc->getDocumentElement();
sVal = XMLString::transcode( "fileCode" );
p_oAttribute = p_oRoot->getAttributeNode( sVal );
XMLString::release(&sVal);
if ( NULL == p_oAttribute ) return notrecognized;
cData = XMLString::transcode(p_oAttribute->getValue());
strData = cData;
delete[] cData;
strData = strData.substr( 4, 2 );
iFileType = atoi( strData.c_str() );
//if we don't have a file code this isn't an XML file we can read - exit
if ( 0 == iFileType ) return notrecognized;
//See if the file type code is defined
if ( iFileType > 0 && iFileType < lastfile )
return (fileType)iFileType; //yep - it was found
else
return notrecognized; //we couldn't tell what file type it was
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetFileType" ;
GoToNoDataState();
throw( stcErr );
}
return notrecognized;
}
/////////////////////////////////////////////////////////////////////////////
// RunSim()
/////////////////////////////////////////////////////////////////////////////
unsigned long clSimManager::RunSim( int iNumStepsToRun )
{
try
{
using namespace std;
DOMDocument * p_oDoc; //in case we need to parse and reload the par file
clWorkerBase * p_oTempObject = NULL;
clBehaviorBase * p_oBehavior = NULL;
clPopulationBase * p_oPopulation = NULL;
modelMsg stcMsg; //for checking for messages
int iNumBehaviors = 0, //number of behaviors - for looping through
//and calling their Action() functions
iNumPopulations = 0, //number of populations
iTimestep = 0, //current timestep - loop counter
iStartTimestep = 0, //the timestep to start on this time
iEndTimestep = 0, //the timestep to end on this time
iHrs,iMin;
clock_t startRun, endRun, now;
unsigned long iRunTime;
m_bUserQuit = false;
m_bUserPaused = false;
//Make sure that the number of timesteps is not less than zero
if ( 0 > iNumStepsToRun )
{
modelErr stcErr;
stcErr.sFunction = "clSimManager::RunSim" ;
stcErr.iErrorCode = BAD_DATA;
stcErr.sMoreInfo = "Number of timesteps to run must be greater than zero.";
throw( stcErr );
}
//If the status is "Run_Complete" - reset the model
if ( Run_Complete == m_eSimState )
{
mp_oXMLParser->parse(m_sParFilename.c_str());
p_oDoc = mp_oXMLParser->getDocument();
ReadParameterFile( p_oDoc, GetFileType( p_oDoc ) );
//Free DOM doc memory
mp_oXMLParser->resetDocumentPool();
}
//Only continue if the Simulation Manager state is Initialized or Paused
if ( m_eSimState != Initialized && m_eSimState != Paused )
{
modelErr stcErr;
stcErr.sFunction = "clSimManager::RunSim" ;
stcErr.iErrorCode = MODEL_NOT_READY;
stcErr.sMoreInfo = "Valid parameter file not loaded";
throw( stcErr );
}
startRun = clock();
//If the model status is paused and no timesteps are entered, pick up where
//we left off
if ( Paused == m_eSimState && 0 == iNumStepsToRun )
{
//If the current timestep is the same as the target timestep (minus 1),
//run to the end
if ( m_iCurrentTimestep == m_iTargetTimestep - 1 )
{
m_iTargetTimestep = 0;
iEndTimestep = m_iNumTimesteps + 1;
}
else
{
iEndTimestep = m_iTargetTimestep;
}
iStartTimestep = m_iCurrentTimestep + 1;
m_eSimState = Initialized; //no longer paused - running again
} //end of if (Paused == m_eSimState && 0 != iNumStepsToRun)
//Not paused, or we are supposed to start over - so figure out the start and
//stop timesteps
else
{
m_eSimState = Initialized; //erase paused status if present
iStartTimestep = m_iCurrentTimestep + 1;
if ( iNumStepsToRun < 1 || iNumStepsToRun >= ( m_iNumTimesteps - m_iCurrentTimestep ) )
iEndTimestep = m_iNumTimesteps + 1;
else
iEndTimestep = iStartTimestep + iNumStepsToRun;
m_iTargetTimestep = iEndTimestep;
} //end of else - not paused
//Query the behavior manager for the number of behaviors to run
//each timestep
iNumBehaviors = mp_oBehaviorManager->GetNumberOfObjects();
iNumPopulations = mp_oPopulationManager->GetNumberOfObjects();
//TIMESTEP LOOP
for ( iTimestep = iStartTimestep; iTimestep < iEndTimestep; iTimestep++ )
{
//Pass a message indicating the timestep we just started, and the elapsed time
now = clock();
iRunTime = (now - startRun) / CLOCKS_PER_SEC;
iHrs = (int)floor( iRunTime / 3600 );
iRunTime -= ( iHrs * 3600 );
//Calc number of minutes
iMin = (int)floor( iRunTime / 60 );
iRunTime -= ( iMin * 60 );
stcMsg.iMessageCode = INFO;
stringstream msg;
msg << "Starting timestep " << iTimestep << " of " << m_iNumTimesteps
<< ". Elapsed time: " << iHrs << ":" << iMin << ":" << iRunTime;
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
m_iCurrentTimestep = iTimestep;
//Run each behavior object
for ( int i = 0; i < iNumBehaviors; i++ )
{
p_oTempObject = mp_oBehaviorManager->PassObjectPointer( i );
p_oBehavior = dynamic_cast < clBehaviorBase * > ( p_oTempObject );
#ifdef DEBUG
stcMsg.iMessageCode = INFO;
stringstream msg;
fstream out( "debug log.txt", ios::out | ios::app );
msg << "Timestep " << iTimestep << ": Starting behavior " << p_oBehavior->GetName() << " at " << clock();
out << "Timestep " << iTimestep << ": Starting behavior " << p_oBehavior->GetName() << " at " << clock() << "\n";
out.close();
stcMsg.sMoreInfo = msg.str();
SendMessage( stcMsg );
#endif
p_oBehavior->Action();
for ( int j = 0; j < iNumPopulations; j++ )
{
p_oTempObject = mp_oPopulationManager->PassObjectPointer( j );
p_oPopulation = dynamic_cast < clPopulationBase * > ( p_oTempObject );
p_oPopulation->DoDataUpdates();
}
}
//Run the cleanup operations
TimestepCleanup();
//Check to see if we need to process any messages while we were waiting
stcMsg = CheckForMessage(m_sAppPath);
if ( NO_MESSAGE != stcMsg.iMessageCode )
{
if ( MODEL_PAUSED == stcMsg.iMessageCode )
{
//"pause"; set the timestep we were interrupted on so we know where
//to pick up
m_eSimState = Paused;
m_iTargetTimestep = iEndTimestep;
stcMsg.iMessageCode = MODEL_PAUSED;
stringstream s;
s << "Finished timestep " << m_iCurrentTimestep;
stcMsg.sMoreInfo = s.str();
SendMessage( stcMsg );
endRun = clock();
iRunTime = endRun - startRun;
m_bUserPaused = true;
return ( iRunTime / CLOCKS_PER_SEC ); //convert to seconds
}
else if ( QUIT == stcMsg.iMessageCode )
{
//quit - shut down everything
GoToNoDataState();
m_bUserQuit = true;
endRun = clock();
iRunTime = endRun - startRun;
return ( iRunTime / CLOCKS_PER_SEC ); //convert to seconds
}
}
} //end of timestep loop
//Set the sim state to run complete if we've run all the timesteps
if ( m_iCurrentTimestep == m_iNumTimesteps )
{
//Perform end-of-run cleanup
EndOfRunCleanup();
m_eSimState = Run_Complete;
m_iTargetTimestep = 0;
}
//Otherwise the status is paused
else {
m_eSimState = Paused;
stcMsg.iMessageCode = MODEL_PAUSED;
SendMessage( stcMsg );
}
endRun = clock();
iRunTime = endRun - startRun;
return ( iRunTime / CLOCKS_PER_SEC ); //convert to seconds
} //end of try block
catch ( modelErr & err )
{
char sMsg[30];
sprintf(sMsg, "%s%ld", " Random seed: ", m_iActualSeed);
err.sMoreInfo += sMsg;
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::RunSim" ;
GoToNoDataState();
throw( stcErr );
}
return -1;
}
/////////////////////////////////////////////////////////////////////////////
// TimestepCleanup()
/////////////////////////////////////////////////////////////////////////////
void clSimManager::TimestepCleanup()
{
try
{
//Run the timestep cleanup functions for each of the object managers
//and the plot
mp_oPlot->TimestepCleanup();
mp_oBehaviorManager->TimestepCleanup();
mp_oGridManager->TimestepCleanup();
mp_oPopulationManager->TimestepCleanup();
} //end of try block
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::TimestepCleanup" ;
GoToNoDataState();
throw( stcErr );
}
}
/////////////////////////////////////////////////////////////////////////////
// EndOfRunCleanup()
/////////////////////////////////////////////////////////////////////////////
void clSimManager::EndOfRunCleanup()
{
try
{
//Run the end-of-run cleanup functions for each of the object managers
//and the plot
mp_oPlot->EndOfRunCleanup();
mp_oBehaviorManager->EndOfRunCleanup();
mp_oGridManager->EndOfRunCleanup();
mp_oPopulationManager->EndOfRunCleanup();
} //end of try block
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::EndOfRunCleanup" ;
GoToNoDataState();
throw( stcErr );
}
}
/////////////////////////////////////////////////////////////////////////////
// GetPopulationObject()
/////////////////////////////////////////////////////////////////////////////
clPopulationBase *clSimManager::GetPopulationObject(string sPopName) {
try
{
clWorkerBase * p_oTempObject = NULL;
clPopulationBase * p_oPop = NULL;
//Make sure that the population manager has been created - if not, just
//return NULL
if ( !mp_oPopulationManager ) return p_oPop;
//Request the population from the Population Manager and return it
p_oTempObject = mp_oPopulationManager->PassObjectPointer(sPopName);
//Recast the pointer as a population pointer
if ( p_oTempObject )
p_oPop = dynamic_cast < clPopulationBase * > ( p_oTempObject );
return p_oPop;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetPopulationObject" ;
GoToNoDataState();
throw( stcErr );
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// GetPopulationObject()
/////////////////////////////////////////////////////////////////////////////
clPopulationBase * clSimManager::GetPopulationObject( int iIndex )
{
try
{
clWorkerBase * p_oTempObject = NULL;
clPopulationBase * p_oPop = NULL;
//Make sure that the population manager has been created - if not, just
//return NULL
if ( !mp_oPopulationManager ) return p_oPop;
//Request the population from the Population Manager and return it
p_oTempObject = mp_oPopulationManager->PassObjectPointer( iIndex );
//Recast the pointer as a population pointer
if ( p_oTempObject )
p_oPop = dynamic_cast < clPopulationBase * > ( p_oTempObject );
return p_oPop;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetPopulationObject" ;
GoToNoDataState();
throw( stcErr );
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// GetGridObject()
/////////////////////////////////////////////////////////////////////////////
clGrid * clSimManager::GetGridObject( const char * cGridName )
{
try
{
clWorkerBase * p_oTempObject = NULL;
clGrid * p_oGrid = NULL;
//Make sure the Grid Manager has been created - if not, just return NULL
if ( !mp_oGridManager ) return p_oGrid;
//Request the grid from the Grid Manager and return it
p_oTempObject = mp_oGridManager->PassObjectPointer( cGridName );
//Recast the pointer as a population pointer
if ( p_oTempObject )
p_oGrid = ( clGrid * ) p_oTempObject;
return p_oGrid;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetGridObject" ;
GoToNoDataState();
throw( stcErr );
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// GetGridObject()
/////////////////////////////////////////////////////////////////////////////
clGrid * clSimManager::GetGridObject( int iIndex ) {
try {
clWorkerBase * p_oTempObject = NULL;
clGrid * p_oGrid = NULL;
//Make sure the Grid Manager has been created - if not, just return NULL
if ( !mp_oGridManager ) return p_oGrid;
//Request the grid from the Grid Manager and return it
p_oTempObject = mp_oGridManager->PassObjectPointer( iIndex );
//Recast the pointer as a population pointer
if ( p_oTempObject )
p_oGrid = ( clGrid * ) p_oTempObject;
return p_oGrid;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetGridObject" ;
GoToNoDataState();
throw( stcErr );
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// GetBehaviorObject()
/////////////////////////////////////////////////////////////////////////////
clBehaviorBase *clSimManager::GetBehaviorObject(string sBehaviorName)
{
try
{
clWorkerBase * p_oTempObject = NULL;
clBehaviorBase * p_oBehavior = NULL;
//Make sure the Behavior Manager has been created - if not, just return NULL
if ( !mp_oBehaviorManager ) return p_oBehavior;
//Request the behavior from the Behavior Manager and return it
p_oTempObject = mp_oBehaviorManager->PassObjectPointer(sBehaviorName);
//Recast the pointer as a population pointer
if ( p_oTempObject )
p_oBehavior = dynamic_cast < clBehaviorBase * > ( p_oTempObject );
return p_oBehavior;
}
catch ( modelErr & err )
{
GoToNoDataState();
throw( err );
}
catch ( modelMsg & msg )
{ //non-fatal error
throw( msg );
}
catch ( ... )
{
modelErr stcErr;
stcErr.iErrorCode = UNKNOWN;
stcErr.sFunction = "clSimManager::GetBehaviorObject" ;
GoToNoDataState();
throw( stcErr );
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// GetBehaviorObject()
/////////////////////////////////////////////////////////////////////////////
clBehaviorBase * clSimManager::GetBehaviorObject( int iIndex )
{