CellModules
simu.cpp
Go to the documentation of this file.
1#include <mecacell/mecacell.h>
3#include <fstream>
4#include <numeric>
5
6using namespace std;
8
9
31class Simu {
32
35 int seed = -1;
38 }
39public:
40
47
48 Simu(string params) : scenario() {
50 seed = MecaCell::Config::globalRand().default_seed;
51 nlohmann::json configuration;
52 configuration = nlohmann::json::parse(params);
53 scenario.init(configuration);
55 }
56 Simu(string params, int _seed, bool benchmark) : scenario() {
58 seed = _seed;
60 nlohmann::json configuration;
61 configuration = nlohmann::json::parse(params);
62 scenario.getWorld().benchmark = benchmark;
63 scenario.init(configuration);
65 }
66
72 void update() {
74 scenario.loop();
75 }
76
84 void update(int nbSteps) {
86 for (int i = 0; i < nbSteps; i++)
87 scenario.loop();
88 }
89
97 }
98
109 double getTime(char format) {
110 if (format == 'd') {
111 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInH() / 24.0;
112 } else if (format == 'h') {
113 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInH();
114 } else if (format == 'm') {
115 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInMn();
116 } else if (format == 's') {
117 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInS();
118 } else {
119 throw std::invalid_argument("received invalid format ['d': day, 'h': hours, 'm': minutes, 's': seconds]");
120 }
121 }
122
123
129 int getSeed() const {
130 return seed;
131 }
132
140 return &(scenario.getWorld().cells);
141 }
150 }
151
152 string _getBenchmarksJSON() const {
154 }
155
156 string __repr__() {
157 std::ostringstream os;
158 os << "<Simu step=" << getCurrentStep() << " nbCells=" << _getCells()->size() << " seed=" << getSeed() << " benchmark=" << scenario.getWorld().benchmark << ">";
159 return os.str();
160 }
161
162 /*isigen:eval@SimuUtilityDeclarations'''
163 from string import Template
164 cellAttributeGettersTemplate = Template("""
165 vector<$type> get$nameUpper(){
166 \tvector<$type> ret;
167 \tret.resize(scenario.getWorld().cells.size());
168 \ttransform(scenario.getWorld().cells.begin(), scenario.getWorld().cells.end(), ret.begin(), [](auto* c){return $name;});
169 \treturn ret;
170 }
171 """)
172 attributesToIgnore = ['type', 'state']
173 validType = ['bool','double','int','void']
174 validType = [*validType,*[ 'vector<'+vt+'>' for vt in validType]]
175 validType = [*validType,*[ 'vector<'+vt+'>' for vt in validType]]
176 def checkValidType(type):
177 return type.replace(' ','') in validType
178 cellAttributeGetters = [a for a in builderData["cell"]["attributes"] if checkValidType(a['type']) and a['value'] not in attributesToIgnore]
179 if "enums" in builderData:
180 cellAttributeGettersEnum = [a for a in builderData["cell"]["attributes"] if a['type'] in builderData["enums"] and a['value'] not in attributesToIgnore]
181 '''*/
182
183 /*isigen:insert@functionsScenarioWrapper'''
184 import re
185 scenarioFunWrapper = ''
186 for fun in builderData["scenario"]["functions"].values():
187 res = re.search(r"(?P<ret>.+) +(?P<fun>.+) *\‍((?P<params>.*)\‍)",fun)
188 if checkValidType(res.group('ret')):
189 scenarioFunWrapper+="{ret} {funName}({params}){{\n\t{content}\n}}\n".format(ret=res.group('ret'),funName=res.group('fun'),params=res.group('params'),content=('return 'if res.group('ret')!='void' else '')+'scenario.'+res.group('fun')+'('+ ','.join([ s.split(' ')[-1] for s in res.group('params').split(',')])+');')
190 print(scenarioFunWrapper)
191 '''*/
192};
Defines the Scenario class for managing the simulation scenario.
size_t getNbUpdates() const
get the number of update since the creation of the world
Definition: world.hpp:220
bool benchmark
Definition: world.hpp:93
double getDtInH() const
Definition: world.hpp:296
double getDtInS() const
Definition: world.hpp:294
double getDtInMn() const
Definition: world.hpp:295
cellPlugin_t cellPlugin
Definition: world.hpp:97
Manages the simulation scenario.
Definition: Scenario.hpp:24
World & getWorld()
Gets the world of the scenario.
Definition: Scenario.hpp:51
void init(nlohmann::json config)
Initializes the scenario with a configuration.
Definition: Scenario.hpp:159
void loop()
Runs the main loop of the scenario.
Definition: Scenario.hpp:173
void showBenchmarksSummaries() const
Definition: Scenario.hpp:180
std::string getBenchmarksJSON() const
Definition: Scenario.hpp:184
vector< PrimoCell< CellBody > * > * _getCells()
Gets the cells in the simulation.
Definition: simu.cpp:138
string __repr__()
Definition: simu.cpp:156
MecaCell::Config::random_engine_t local_random_engine
Definition: simu.cpp:34
int getCurrentStep()
Gets the current number of time steps.
Definition: simu.cpp:95
string _getBenchmarksJSON() const
Definition: simu.cpp:152
Simu(string params)
Definition: simu.cpp:48
CellPlugin< PrimoCell< CellBody > > * plugins
Cell plugins.
Definition: simu.cpp:46
int getSeed() const
Gets the seed used for random number generation.
Definition: simu.cpp:129
void update(int nbSteps)
Updates the simulation by multiple time steps.
Definition: simu.cpp:84
scenario_t scenario
Definition: simu.cpp:33
int seed
Definition: simu.cpp:35
Simu(string params, int _seed, bool benchmark)
Definition: simu.cpp:56
void setStaticVariables()
Definition: simu.cpp:36
void showBenchmarksSummaries() const
prints the benchmark summaries.
Definition: simu.cpp:148
double getTime(char format)
Gets the elapsed time in a given format.
Definition: simu.cpp:109
void update()
Updates the simulation by one time step.
Definition: simu.cpp:72
a class to store JSON values
Definition: json.hpp:12931
static JSON_NODISCARD basic_json parse(detail::input_adapter &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
deserialize from a compatible input
Definition: json.hpp:18859
A simple vector class template.
Definition: std.hpp:324
static void initRandomSeed(int seed=clock())
Initializes the random seed.
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
static void setCurrentEngine(random_engine_t *eng)
Definition: config.hpp:44
std::mt19937 random_engine_t
Definition: config.hpp:8
static random_engine_t & globalRand()
access to the static global random engine.This pseudo - random generator is* used in random 3D vector...
Definition: config.hpp:39