diff --git a/Stochastic/Tests/statstest.cpp b/Stochastic/Tests/statstest.cpp index 94de1ccc..73c40ab2 100644 --- a/Stochastic/Tests/statstest.cpp +++ b/Stochastic/Tests/statstest.cpp @@ -7,6 +7,8 @@ #include <iostream> #include <cstdio> +#include "VCELL/SimulationMessaging.h" + const char* input_file_contents = R"INPUT_FILE( <control> STARTING_TIME 0.0 @@ -98,70 +100,68 @@ const std::map<int, double> expected_S0 = { TEST(statstest,test1) { std::cout << "Checkpoint 0" << std::endl; - char *temp_input_file_name = new char[1000] {0}; - char *temp_output_file_name = new char[1000] {0}; - std::string in_prefix = testing::TempDir() + "input_XXXXXX"; - std::string out_prefix = testing::TempDir() + "output_XXXXXX"; - strncpy(temp_input_file_name, in_prefix.c_str(), in_prefix.length()); - strncpy(temp_output_file_name, out_prefix.c_str(), out_prefix.length()); + //std::string inputFileName = std::tmpnam(nullptr); + std::string inputFileName {R"(C:\CustomTemp\testme_vcell.txt)"}; + std::string outputFileName = std::tmpnam(nullptr); + std::map<int,double> results; + std::fstream inputFileStream{inputFileName}; + ASSERT_TRUE(inputFileStream.fail()); + std::cout << std::system_category().message(::GetLastError()) << std::endl; + std::cout << "Supposed file: " << inputFileName << std::endl; + return; + std::fstream outputFileStream{outputFileName}; + ASSERT_FALSE(outputFileStream.fail()); + + // Setup the Gibson Solver input file std::cout << "Checkpoint 1" << std::endl; - assert(mkstemp(temp_input_file_name) != -1); - assert(mkstemp(temp_output_file_name) != -1); - std::ofstream input_file (temp_input_file_name); - bool bWroteFile = false; - if (input_file.is_open()){ - input_file << input_file_contents; - input_file.close(); - bWroteFile = true; - } - ASSERT_TRUE(bWroteFile); + std::cout << " >> " << "Input: <" << inputFileName << ">;"<< std::endl; + std::cout << " >> " << "Output: <" << outputFileName << ">;"<< std::endl; + if (outputFileStream.is_open()) outputFileStream.close(); + inputFileStream << input_file_contents; + inputFileStream.close(); + return; + + // Create the Gibson Solver std::cout << "Checkpoint 2" << std::endl; - Gibson *gb= new Gibson(temp_input_file_name, temp_output_file_name); + auto *gb = new Gibson(inputFileName.c_str(), outputFileName.c_str()); + + // Launch the test std::cout << "Checkpoint 3" << std::endl; gb->march(); + // Verify file contents std::cout << "Checkpoint 4" << std::endl; - // verify file contents - std::ifstream outfile(temp_output_file_name); - string line; - getline(outfile, line); // remove header line -// std::cout << line << std::endl; -// std::cout.flush(); - + outputFileStream.open(outputFileName); + std::string line; + std::getline(outputFileStream, line); // remove header line + for (int i = 0; !outputFileStream.eof(); i++) { + std::getline(outputFileStream, line); + // if index found in expected_S0 map, store in results map + if (expected_S0.find(i) != expected_S0.end()){ + float t, s0, s1, s2; + // extract space separated values for t, s0, s1 and s2 from line + std::stringstream line_stream(line); + line_stream >> t >> s0 >> s1 >> s2; + results[i] = s0; + } + } + outputFileStream.close(); + + // compare the expected and actual values std::cout << "Checkpoint 5" << std::endl; - std::map<int,double> results; - int i = 0; - while (getline(outfile, line)){ - // if index found in expected_S0 map, store in results map - if (expected_S0.find(i) != expected_S0.end()){ - float t, s0, s1, s2; - // extract space separated values for t, s0, s1 and s2 from line - std::stringstream line_stream(line); - line_stream >> t >> s0 >> s1 >> s2; - results[i] = s0; -// std::cout << line << std::endl; -// std::cout.flush(); - } - i++; - } - std::cout << "Checkpoint 6" << std::endl; - // compare the expected and actual values - double accum_error = 0.0; - double max_error = 0.0; + double accumulatedError = 0.0, maxIndividualError = 0.0; for (auto const& expected : expected_S0){ - double s0_given = expected.second; - double s0_computed = results[expected.first]; - double abserr = std::abs(s0_given - s0_computed); -// std::cout << "t=" << expected.first << " expected=" << s0_given << " computed=" << s0_computed << " abserr=" << abserr << std::endl; - accum_error += abserr; - max_error = std::max(max_error, abserr); + double absoluteError = std::abs(expected.second - results[expected.first]); + // std::cout << "t=" << expected.first << " expected=" << expected.second << " computed=" << results[expected.first] << " abserr=" << abserr << std::endl; + accumulatedError += absoluteError; + maxIndividualError = std::max(maxIndividualError, absoluteError); } - assert(accum_error < 0.015); - assert(max_error < 0.005); + assert(accumulatedError < 0.015); + assert(maxIndividualError < 0.005); std::cout << "Checkpoint 7" << std::endl; delete gb; - delete[] temp_input_file_name; - delete[] temp_output_file_name; + if (inputFileStream.is_open()) inputFileStream.close(); + if (outputFileStream.is_open()) outputFileStream.close(); std::cout << "Checkpoint 8" << std::endl; } \ No newline at end of file diff --git a/Stochastic/VCellStoch/src/Gibson.cpp b/Stochastic/VCellStoch/src/Gibson.cpp index 9f50aa25..4a677209 100644 --- a/Stochastic/VCellStoch/src/Gibson.cpp +++ b/Stochastic/VCellStoch/src/Gibson.cpp @@ -10,13 +10,13 @@ #include <string> #include <iomanip> #include <vector> -#include <stdint.h> -#include <math.h> -#include <stdlib.h> +#include <cstdint> +#include <cmath> +#include <cstdlib> #include <random> using namespace std; -#include <time.h> +#include <ctime> #include "../include/IndexedTree.h" #ifdef USE_MESSAGING @@ -26,14 +26,12 @@ using namespace std; const double double_infinity = numeric_limits<double>::infinity(); const double EPSILON = 1E-12; const string Gibson::MY_T_STR = "t"; + /* *Empty constructor of Gibson. It will use the defalt settings in StochModel. */ Gibson::Gibson() - :StochModel(), - savedSampleCount(1), - lastTime (std::numeric_limits<long>::min( )) -{ + : savedSampleCount(1), lastTime (std::numeric_limits<long>::min()) { Tree=NULL; currvals=NULL; generator = new std::mt19937_64(); @@ -47,16 +45,13 @@ Gibson::Gibson() * string, the output file(name), where the results are saved. */ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) - :StochModel(), - savedSampleCount(1), - lastTime (std::numeric_limits<long>::min( )) -{ - Tree=NULL; - currvals=NULL; - generator = new std::mt19937_64(); - distribution = new std::uniform_real_distribution<double>(0.0,1.0); - - outfilename=arg_outfilename; + : savedSampleCount(1), lastTime (std::numeric_limits<long>::min( )) { + // TODO: Call basic constructor rather than duplicate lines + this->Tree = NULL; + this->currvals = NULL; + this->generator = new std::mt19937_64(); + this->distribution = new std::uniform_real_distribution<>(0.0,1.0); + this->outfilename = arg_outfilename; ifstream infile; string instring; @@ -69,56 +64,33 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) while(infile >> instring) { //load control info. - if (instring == "STARTING_TIME") - { + if (instring == "STARTING_TIME"){ infile >> STARTING_TIME; - } - else if (instring == "BMULTIBUTNOTHISTO") - { + } else if (instring == "BMULTIBUTNOTHISTO"){ infile >> bMultiButNotHisto; - } - else if (instring == "ENDING_TIME") - { + } else if (instring == "ENDING_TIME"){ infile >> ENDING_TIME; - } - else if (instring == "SAVE_PERIOD") - { + } else if (instring == "SAVE_PERIOD"){ infile >> SAVE_PERIOD; flag_savePeriod=true; - } - else if (instring == "MAX_ITERATION") - { + } else if (instring == "MAX_ITERATION"){ infile >> MAX_ITERATION; - } - else if (instring == "TOLERANCE") - { + } else if (instring == "TOLERANCE"){ infile >> TOLERANCE; - } - else if (instring == "SAMPLE_INTERVAL") - { + } else if (instring == "SAMPLE_INTERVAL"){ infile >> SAMPLE_INTERVAL; - } - else if (instring == "MAX_SAVE_POINTS") - { + } else if (instring == "MAX_SAVE_POINTS"){ infile >> MAX_SAVE_POINTS; - } - else if (instring == "NUM_TRIAL") - { + } else if (instring == "NUM_TRIAL"){ infile >> NUM_TRIAL; - } - else if (instring == "SEED") - { + } else if (instring == "SEED"){ infile >> SEED; - } - //load listofvars - else if (instring == "TotalVars") - { + } else if (instring == "TotalVars"){ //load listofvars int varCount; infile >> varCount; string name; double amount; - for(int i=0;i<varCount;i++) - { + for(int i=0;i<varCount;i++){ infile >> name; infile >> amount; listOfVarNames.push_back(name); @@ -126,10 +98,7 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) StochVar *var=new StochVar((uint64_t)amount); listOfVars.push_back(var); } - } - //load listOfProcesses - else if (instring == "TotalProcesses") - { + } else if (instring == "TotalProcesses"){ //load listOfProcesses int pCount; infile >> pCount; string name; @@ -140,15 +109,11 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) Jump *jp=new Jump(); listOfProcesses.push_back(jp); } - } - //load process description to set up processes - else if (instring == "TotalDescriptions") - { + } else if (instring == "TotalDescriptions") { //load process description to set up processes int dCount,idx; infile >> dCount; string name,str; - for(int i=0;i<dCount;i++)//loop through each process description - { + for(int i=0;i<dCount;i++){ //loop through each process description infile >> name >> name;// "process name" //find the process in listOfProcesses using it's name idx=getProcessIndex(name); @@ -201,10 +166,9 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) } } //setup IndexedTree - Tree=new IndexedTree(); - for(int i=0;i<listOfProcesses.size();i++) - { - Tree->addProcess(listOfProcesses[i]); + Tree = new IndexedTree(); + for(auto & listOfProcesse : listOfProcesses){ + Tree->addProcess(listOfProcesse); } infile.close(); if (NUM_TRIAL > MAX_ALLOWED_POINTS) { @@ -214,7 +178,6 @@ Gibson::Gibson(const char* arg_infilename, const char* arg_outfilename) VCELL_EXCEPTION(invalid_argument,"Stochastic initialization: Server save points " << MAX_SAVE_POINTS << " exceeds limit of " << MAX_ALLOWED_POINTS); } - if(NUM_TRIAL > 1 && !bMultiButNotHisto){ //this must be a gibson 'histogram' sim, //java gui not allow setting of MAX_SAVE_POINTS and default is too low @@ -270,12 +233,12 @@ Gibson::~Gibson() //delete indexedTree delete Tree; //delete variables - for(int i=0;i<listOfVars.size();i++) - delete listOfVars[i]; + for(auto & listOfVar : listOfVars) + delete listOfVar; listOfVars.clear(); //delete proccesses - for(int i=0;i<listOfProcesses.size();i++) - delete listOfProcesses[i]; + for(auto & listOfProcesse : listOfProcesses) + delete listOfProcesse; listOfProcesses.clear(); //delete vectors listOfVarNames.clear();