From 1efedefdf75fbbc41f0db322e2fa40da20ce285d Mon Sep 17 00:00:00 2001 From: Christopher LaChance <christopher.t.lachance@gmail.com> Date: Tue, 10 Oct 2017 10:30:28 -0500 Subject: [PATCH] Added additional variable information to the Monte_Runs data file. --- include/trick/MonteVar.hh | 3 ++ include/trick/MonteVarCalculated.hh | 3 ++ include/trick/MonteVarFile.hh | 7 ++- include/trick/MonteVarFixed.hh | 5 +- include/trick/MonteVarRandom.hh | 3 ++ include/trick/StlRandomGeneratorSub.hh | 6 --- .../MonteCarlo/MonteCarlo_c_intf.cpp | 2 +- .../MonteCarlo/MonteCarlo_funcs.cpp | 4 +- .../MonteCarlo/MonteCarlo_master_file_io.cpp | 48 +++++++++++-------- .../MonteCarlo/MonteVarCalculated.cpp | 13 +++++ .../sim_services/MonteCarlo/MonteVarFile.cpp | 15 +++++- .../sim_services/MonteCarlo/MonteVarFixed.cpp | 13 ++++- .../MonteCarlo/MonteVarRandom.cpp | 22 +++++++++ 13 files changed, 112 insertions(+), 32 deletions(-) diff --git a/include/trick/MonteVar.hh b/include/trick/MonteVar.hh index 00ea56bb..3cfc471d 100644 --- a/include/trick/MonteVar.hh +++ b/include/trick/MonteVar.hh @@ -42,6 +42,9 @@ namespace Trick { */ void set_unit(std::string in_unit) { this->unit = in_unit; } + // Composite the various elements of this MonteVar. + virtual std::string describe_variable() = 0; + /** Class MonteCarlo is a friend so it can use the get_next_value method. * The get_next_value method needs to be protected so users cannot use it in the input file */ diff --git a/include/trick/MonteVarCalculated.hh b/include/trick/MonteVarCalculated.hh index 335c9b71..f2f843db 100644 --- a/include/trick/MonteVarCalculated.hh +++ b/include/trick/MonteVarCalculated.hh @@ -56,6 +56,9 @@ namespace Trick { */ MonteVarCalculated(std::string name, std::string unit = ""); + // Describes the various properties of this variable. + std::string describe_variable(); + protected: virtual std::string get_next_value(); diff --git a/include/trick/MonteVarFile.hh b/include/trick/MonteVarFile.hh index fd45ad94..a924f3b3 100644 --- a/include/trick/MonteVarFile.hh +++ b/include/trick/MonteVarFile.hh @@ -94,14 +94,19 @@ namespace Trick { * * @param in_file_name the name of the file containing this variable's values */ - void set_file_name(std::string in_file_name); + + void set_file_name(std::string in_file_name); + /** * Sets the column * * @param in_column the column (starting at 1) within the file corresponding to this variable */ void set_column(unsigned int in_column); + + // Describes the various properties of this variable. + std::string describe_variable(); protected: virtual std::string get_next_value(); diff --git a/include/trick/MonteVarFixed.hh b/include/trick/MonteVarFixed.hh index 0e90665e..3f356703 100644 --- a/include/trick/MonteVarFixed.hh +++ b/include/trick/MonteVarFixed.hh @@ -48,8 +48,11 @@ namespace Trick { * @param unit this variable's units */ MonteVarFixed(std::string name, double value, std::string unit = ""); - + void set_value(double in_value); + + // Describes the properties of this variable. + std::string describe_variable(); protected: virtual std::string get_next_value(); diff --git a/include/trick/MonteVarRandom.hh b/include/trick/MonteVarRandom.hh index 2f4ba718..33aaf550 100644 --- a/include/trick/MonteVarRandom.hh +++ b/include/trick/MonteVarRandom.hh @@ -185,6 +185,9 @@ namespace Trick { */ double get_absolute_max() const { return (randist.rel_max) ? (randist.mu + randist.max) : randist.max; } + // Describes the properties of this variable. + std::string describe_variable(); + protected: virtual std::string get_next_value(); diff --git a/include/trick/StlRandomGeneratorSub.hh b/include/trick/StlRandomGeneratorSub.hh index 3f709eb7..aa93e996 100644 --- a/include/trick/StlRandomGeneratorSub.hh +++ b/include/trick/StlRandomGeneratorSub.hh @@ -87,7 +87,6 @@ public: }; #endif - ///@brief Sub class for object that includes a <random> engine and a distribution /// ///@note partial specialization exists for Distribution = std::poisson_distribution<int> @@ -134,7 +133,6 @@ public: virtual ~StlRandomGeneratorSub() { } - ///@brief return next pseudo-random number virtual TRICK_GSL_RETURN_TYPE operator()() { @@ -148,7 +146,6 @@ public: #endif } - ///@brief reset seed for underlying uniform pseudo-random number generator virtual void set_seed(unsigned long in_seed) { @@ -156,7 +153,6 @@ public: engine.seed(initialSeed); } - ///@brief reset parameters for the distribution /// ///@param a is min for FLAT, mean for GAUSSION and POISSON @@ -210,8 +206,6 @@ private: }; - - #ifdef _HAVE_TR1_RANDOM // implement one of these template FULL specializations diff --git a/trick_source/sim_services/MonteCarlo/MonteCarlo_c_intf.cpp b/trick_source/sim_services/MonteCarlo/MonteCarlo_c_intf.cpp index a1ae80d9..572b6899 100644 --- a/trick_source/sim_services/MonteCarlo/MonteCarlo_c_intf.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteCarlo_c_intf.cpp @@ -16,7 +16,7 @@ extern "C" int mc_get_enabled() { if ( the_mc != NULL ) { return the_mc->get_enabled(); } - return 0 ; + return 0 ; } extern "C" void mc_set_dry_run(int dry_run) { diff --git a/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp b/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp index 68f15400..107ae65d 100644 --- a/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp @@ -440,10 +440,10 @@ int Trick::MonteCarlo::prepare_run(MonteRun *curr_run) { } } /** <li> Create the data file </ul>*/ - fprintf(run_data_file, "%05u ", curr_run->id); + fprintf(run_data_file, "%05u\t", curr_run->id); for (std::vector<std::string>::size_type i = 0; i < variables.size(); ++i) { if (i>0) { - fprintf(run_data_file, " "); + fprintf(run_data_file, "\t"); } fprintf(run_data_file, "%s", variables[i]->value.c_str()); } diff --git a/trick_source/sim_services/MonteCarlo/MonteCarlo_master_file_io.cpp b/trick_source/sim_services/MonteCarlo/MonteCarlo_master_file_io.cpp index 00e0ec66..f0acf19b 100644 --- a/trick_source/sim_services/MonteCarlo/MonteCarlo_master_file_io.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteCarlo_master_file_io.cpp @@ -1,4 +1,3 @@ - #include <sys/stat.h> #include <libgen.h> @@ -57,25 +56,36 @@ int Trick::MonteCarlo::open_file(std::string file_name, FILE** file_ptr) { return 0; } -void Trick::MonteCarlo::write_to_run_files(std::string file_name) { - - /** <li> Write the number of runs. */ - fprintf(run_header_file, "trick.mc_set_num_runs(%d)\n", num_runs); - - /** <li> Write the variables. */ - fprintf(run_data_file, "#run_num "); - for (std::vector<std::string>::size_type i = 0; i < variables.size(); ++i) { - if (variables[i]->unit.empty()) { - fprintf(run_header_file, "\nvar%zu = trick.MonteVarFile(\"%s\", \"%s\", %zu)\n", - i, variables[i]->name.c_str(), file_name.c_str(), i + 2); - } - else { - fprintf(run_header_file, "\nvar%zu = trick.MonteVarFile(\"%s\", \"%s\", %zu, \"%s\")\n", - i, variables[i]->name.c_str(), file_name.c_str(), i + 2, variables[i]->unit.c_str()); - } - fprintf(run_header_file, "trick_mc.mc.add_variable(var%zu)\n", i); - fprintf(run_data_file, "%s ", variables[i]->name.c_str()); +void Trick::MonteCarlo::write_to_run_files(std::string file_name) +{ + // Write a description of each variable. + for(std::vector<std::string>::size_type i = 0; i < variables.size(); ++i) + { + fprintf(run_data_file, "%s\n", variables[i]->describe_variable().c_str()); } + // Data file header. + fprintf(run_data_file, "# RUN\t"); + for(std::vector<std::string>::size_type i = 0; i < variables.size(); ++i) + { + fprintf(run_data_file, "%s\t", variables[i]->name.c_str()); + } fprintf(run_data_file, "\n"); + + // Write the input file lines that configured the initial state of the Monte Carlo simulation. + fprintf(run_header_file, "trick.mc_set_num_runs(%d)\n", num_runs); + for (std::vector<std::string>::size_type i = 0; i < variables.size(); ++i) + { + if (variables[i]->unit.empty()) + { + fprintf(run_header_file, "\nvar%zu = trick.MonteVarFile(\"%s\", \"%s\", %zu)\n", + i, variables[i]->name.c_str(), file_name.c_str(), i + 2); + } + else + { + fprintf(run_header_file, "\nvar%zu = trick.MonteVarFile(\"%s\", \"%s\", %zu, \"%s\")\n", + i, variables[i]->name.c_str(), file_name.c_str(), i + 2, variables[i]->unit.c_str()); + } + fprintf(run_header_file, "trick_mc.mc.add_variable(var%zu)\n", i); + } } diff --git a/trick_source/sim_services/MonteCarlo/MonteVarCalculated.cpp b/trick_source/sim_services/MonteCarlo/MonteVarCalculated.cpp index f7a231d4..7d39cb1f 100644 --- a/trick_source/sim_services/MonteCarlo/MonteVarCalculated.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteVarCalculated.cpp @@ -1,4 +1,5 @@ #include <iostream> +#include <sstream> #include "trick/MonteVarCalculated.hh" #include "trick/memorymanager_c_intf.h" @@ -15,6 +16,18 @@ Trick::MonteVarCalculated::MonteVarCalculated(std::string in_name, std::string i } } +// Composite the various properties of this MonteVarCalculated. +std::string Trick::MonteVarCalculated::describe_variable() +{ + std::stringstream ss; + + ss << "#NAME:\t" << this->name << "\n" + << "#TYPE:\tCALCULATED\n" + << "#UNIT:\t" << this->unit << "\n"; + + return ss.str(); +} + std::string Trick::MonteVarCalculated::get_next_value() { char buffer[128]; if (ref2 != NULL) { diff --git a/trick_source/sim_services/MonteCarlo/MonteVarFile.cpp b/trick_source/sim_services/MonteCarlo/MonteVarFile.cpp index 94265a18..ceeedf53 100644 --- a/trick_source/sim_services/MonteCarlo/MonteVarFile.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteVarFile.cpp @@ -17,7 +17,6 @@ Trick::MonteVarFile::MonteVarFile(std::string in_name, std::string in_file_name, set_file_name(in_file_name); buffer = new char[4096]; - } Trick::MonteVarFile::~MonteVarFile() { @@ -25,6 +24,20 @@ Trick::MonteVarFile::~MonteVarFile() { delete buffer; } +// Composite the various properties of this MonteVarFile. +std::string Trick::MonteVarFile::describe_variable() +{ + std::stringstream ss; + + ss << "#NAME:\t\t" << this->name << "\n" + << "#TYPE:\t\tFILE\n" + << "#UNIT:\t\t" << this->unit << "\n" + << "#FILE:\t\t" << this->file_name << "\n" + << "#COLUMN:\t" << this->column << "\n"; + + return ss.str(); +} + std::string Trick::MonteVarFile::get_next_value() { if (input_file_stream->good()) { double file_value; diff --git a/trick_source/sim_services/MonteCarlo/MonteVarFixed.cpp b/trick_source/sim_services/MonteCarlo/MonteVarFixed.cpp index 49bd1923..4fc77161 100644 --- a/trick_source/sim_services/MonteCarlo/MonteVarFixed.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteVarFixed.cpp @@ -9,13 +9,24 @@ Trick::MonteVarFixed::MonteVarFixed(std::string in_name, double in_value, std::s this->unit = in_unit; } +// Composite the various properties of this MonteVarFixed. +std::string Trick::MonteVarFixed::describe_variable() +{ + std::stringstream ss; + + ss << "#NAME:\t" << this->name << "\n" + << "#TYPE:\tFIXED\n" + << "#UNIT:\t" << this->unit << "\n"; + + return ss.str(); +} + void Trick::MonteVarFixed::set_value(double in_value) { std::ostringstream string_stream; string_stream << std::setprecision(15) << in_value ; this->value = string_stream.str(); } - std::string Trick::MonteVarFixed::get_next_value() { std::ostringstream string_stream; if (unit.empty()) { diff --git a/trick_source/sim_services/MonteCarlo/MonteVarRandom.cpp b/trick_source/sim_services/MonteCarlo/MonteVarRandom.cpp index 77e77f15..398b28b7 100644 --- a/trick_source/sim_services/MonteCarlo/MonteVarRandom.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteVarRandom.cpp @@ -4,6 +4,7 @@ #include <cmath> #include <limits> +#include "trick/MonteVar.hh" #include "trick/MonteVarRandom.hh" #include "trick/exec_proto.h" @@ -66,6 +67,27 @@ Trick::MonteVarRandom::~MonteVarRandom() delete stlGenPtr; } +// Composite the various properties of this MonteVarRandom. +std::string Trick::MonteVarRandom::describe_variable() +{ + std::string dist_list[] = {"GAUSSIAN", "FLAT", "POISSON"}; + std::stringstream ss; + + ss << "#NAME:\t\t\t" << this->name << "\n" + << "#TYPE:\t\t\tRANDOM\n" + << "#UNIT:\t\t\t" << this->unit << "\n" + << "#DISTRIBUTION:\t" << dist_list[this->randist.type] << "\n" + << "#SEED:\t\t\t" << this->randist.seed << "\n" + << "#SIGMA:\t\t\t" << this->randist.sigma << "\n" + << "#MU:\t\t\t" << this->randist.mu << "\n" + << "#MIN:\t\t\t" << this->randist.min << "\n" + << "#MAX:\t\t\t" << this->randist.max << "\n" + << "#REL_MIN:\t\t" << this->randist.rel_min << "\n" + << "#REL_MAX:\t\t" << this->randist.rel_max << "\n"; + + return ss.str(); +} + void Trick::MonteVarRandom::set_seed(unsigned long seed) { randist.seed = seed; if (engineType != NO_ENGINE && stlGenPtr) {