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
29class Simu {
30
32
33public:
34
41
42 Simu(string params) : scenario() {
43 nlohmann::json configuration;
44 configuration = nlohmann::json::parse(params);
45 scenario.init(configuration);
47 }
48 Simu(string params, int seed) : scenario() {
49 nlohmann::json configuration;
50 configuration = nlohmann::json::parse(params);
52 scenario.init(configuration);
54 }
55
61 void update() {
62 scenario.loop();
63 }
64
72 void update(int nbSteps) {
73 for (int i = 0; i < nbSteps; i++)
74 scenario.loop();
75 }
76
84 }
85
96 double getTime(char format) {
97 if (format == 'd') {
98 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInH() / 24.0;
99 } else if (format == 'h') {
100 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInH();
101 } else if (format == 'm') {
102 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInMn();
103 } else if (format == 's') {
104 return (double)scenario.getWorld().getNbUpdates() * scenario.getWorld().getDtInS();
105 } else {
106 throw std::invalid_argument("received invalid format ['d': day, 'h': hours, 'm': minutes, 's': seconds]");
107 }
108 }
109
116 return &(scenario.getWorld().cells);
117 }
118
119
120 /*isigen:eval@SimuUtilityDeclarations'''
121 from string import Template
122 cellAttributeGettersTemplate = Template("""
123 vector<$type> get$nameUpper(){
124 \tvector<$type> ret;
125 \tret.resize(scenario.getWorld().cells.size());
126 \ttransform(scenario.getWorld().cells.begin(), scenario.getWorld().cells.end(), ret.begin(), [](auto* c){return $name;});
127 \treturn ret;
128 }
129 """)
130 attributesToIgnore = ['type', 'state']
131 validType = ['bool','double','int','void']
132 validType = [*validType,*[ 'vector<'+vt+'>' for vt in validType]]
133 validType = [*validType,*[ 'vector<'+vt+'>' for vt in validType]]
134 def checkValidType(type):
135 return type.replace(' ','') in validType
136 cellAttributeGetters = [a for a in builderData["cell"]["attributes"] if checkValidType(a['type']) and a['value'] not in attributesToIgnore]
137 if "enums" in builderData:
138 cellAttributeGettersEnum = [a for a in builderData["cell"]["attributes"] if a['type'] in builderData["enums"] and a['value'] not in attributesToIgnore]
139 '''*/
140
141 /*isigen:insert@functionsScenarioWrapper'''
142 import re
143 scenarioFunWrapper = ''
144 for fun in builderData["scenario"]["functions"].values():
145 res = re.search(r"(?P<ret>.+) +(?P<fun>.+) *\‍((?P<params>.*)\‍)",fun)
146 if checkValidType(res.group('ret')):
147 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(',')])+');')
148 print(scenarioFunWrapper)
149 '''*/
150};
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:193
double getDtInH() const
Definition: world.hpp:263
double getDtInS() const
Definition: world.hpp:261
double getDtInMn() const
Definition: world.hpp:262
cellPlugin_t cellPlugin
Definition: world.hpp:88
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
vector< PrimoCell< CellBody > * > * _getCells()
Gets the cells in the simulation.
Definition: simu.cpp:115
Simu(string params, int seed)
Definition: simu.cpp:48
int getCurrentStep()
Gets the current number of time steps.
Definition: simu.cpp:82
Simu(string params)
Definition: simu.cpp:42
CellPlugin< PrimoCell< CellBody > > * plugins
Cell plugins.
Definition: simu.cpp:40
void update(int nbSteps)
Updates the simulation by multiple time steps.
Definition: simu.cpp:72
scenario_t scenario
Definition: simu.cpp:31
double getTime(char format)
Gets the elapsed time in a given format.
Definition: simu.cpp:96
void update()
Updates the simulation by one time step.
Definition: simu.cpp:61
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:290
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
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:30