CellModules
Model.hpp
Go to the documentation of this file.
1//'''
2#ifndef _MODEL_HPP
3#define _MODEL_HPP
4
5#include <mecacell/mecacell.h>
6#include <time.h>
7
8/*isigen:insert@enumDeclarations'''
9 print("enum State { " + ','.join(sorted([s['name'] for s in builderData["cell"]["States"]])) + "};")
10 print("enum Type { " + (','.join(sorted(builderData["cellTypes"])) if len(builderData["cellTypes"])>0 else ' Common ') + "};\n")
11 if "enums" in builderData:
12 for name, listV in builderData["enums"].items():
13 print("enum "+name+" { " + ','.join(sorted(listV)) + "};")
14 '''*/
15
16
17struct Protocol {
18 double defaultValue = 0;
20 int currentStep = -1;
21 int i = 0;
25 data(_data){
26 }
27 template <class World> double getValue(World &w){
29 double value = defaultValue;
30 double currentTime = w.getDt() * (double)w.getNbUpdates() / 3600.;
31 if(i<data.size() && currentTime>=data[i][0]-MecaCell::Config::DOUBLE_EPSILON){
32 value = data[i][1];
33 i++;
34 }
36 currentValue = value;
37 return value;
38 }
39 };
40
41class Model {
42
43 private :
44 struct PreSetCell {
45 /*isigen:insert@preSetCell'''
46 print('\n'.join(['{type} {value};'.format(type=a['type'],value=a['value']) for a in builderData["paramInputs"]["Cell"]]))
47 '''*/
48
50 PreSetCell(nlohmann::json cellConfig, std::string type){
51 /*isigen:insert@preSetCellInit'''
52 print('\n'.join(['try {{\n\tif(cellConfig[type].find("{value}") != cellConfig[type].end()) {value} = cellConfig[type]["{value}"];\n\telse {value} = cellConfig["Common"]["{value}"];\n}} catch (...) {{\n\tthrow std::invalid_argument( "problem with cell paramter \\"{value}\\", for cell type \\""+type+"\\"");\n}}'.format(value=a['value']) for a in builderData["paramInputs"]["Cell"]]))
53 '''*/
54 }
55
56 template <class Cell> void apply(Cell* c){
57 /*isigen:insert@preSetCellApply'''
58 print('\n'.join(['c->{attribute} = {attribute};'.format(attribute=a['value']) for a in builderData["paramInputs"]["Cell"]]))
59 '''*/
60 }
61 };
63 std::uniform_real_distribution<double> probaGen;
64 std::unordered_map<Type, PreSetCell> typeToPreSetCell;
65
66
67 public :
68
70 /*isigen:insert@loadPresets'''
71 print('\n'.join(['typeToPreSetCell[Type::{type}] = PreSetCell(config["Cell"],"{type}");'.format(type=ct) for ct in (builderData["cellTypes"] if len(builderData["cellTypes"])>0 else ['Common'])]))
72 '''*/
73 scenarConfig = config["Scenario"];
74 }
75
76 template <class Cell> void initCell(Cell* c){
77 typeToPreSetCell[c->getType()].apply(c);
78 }
79
80 template <class Scenario> void initScenario(Scenario* s){
81 /*isigen:insert@scenarioSettings'''
82 print('\n'.join(['s->{attribute} = {funcStart}scenarConfig[\"{attribute}\"]{funcEnd};'.format(attribute=a['value'],funcStart='Protocol(' if a['type']=='Protocol' else '',funcEnd='.get<std::vector<std::vector<double>>>())' if a['type']=='Protocol' else '') for a in builderData["paramInputs"]["Scenario"]]))
83 '''*/
84 }
85
86};
87
88#endif
89//'''
Where "everything" happens.
Definition: world.hpp:25
double getDt() const
Definition: world.hpp:260
size_t getNbUpdates() const
get the number of update since the creation of the world
Definition: world.hpp:193
Definition: Model.hpp:41
std::unordered_map< Type, PreSetCell > typeToPreSetCell
Definition: Model.hpp:64
nlohmann::json scenarConfig
Definition: Model.hpp:62
void initScenario(Scenario *s)
Definition: Model.hpp:80
void loadModel(nlohmann::json config)
Definition: Model.hpp:69
std::uniform_real_distribution< double > probaGen
Definition: Model.hpp:63
void initCell(Cell *c)
Definition: Model.hpp:76
Represents a cell in the simulation.
Definition: PrimoCell.hpp:53
Type getType() const
Gets the type of the cell.
Definition: PrimoCell.hpp:233
Manages the simulation scenario.
Definition: Scenario.hpp:24
a class to store JSON values
Definition: json.hpp:12931
A simple vector class template.
Definition: std.hpp:290
size_t size() const
Returns the number of elements in the vector.
Definition: std.hpp:320
static constexpr double DOUBLE_EPSILON
max distance for two doubles to be considered equals (only used for some geometric operations)
Definition: config.hpp:22
PreSetCell(nlohmann::json cellConfig, std::string type)
Definition: Model.hpp:50
void apply(Cell *c)
Definition: Model.hpp:56
int currentStep
Definition: Model.hpp:20
std::vector< std::vector< double > > data
Definition: Model.hpp:22
Protocol()
Definition: Model.hpp:23
int i
Definition: Model.hpp:21
double defaultValue
Definition: Model.hpp:18
Protocol(std::vector< std::vector< double > > _data)
Definition: Model.hpp:24
double getValue(World &w)
Definition: Model.hpp:27
double currentValue
Definition: Model.hpp:19
#define private
Definition: test.cpp:3