RayZaler 0.1
The free opto-mechanical simulation framework
GenericCompositeModel.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _GENERIC_COMPOSITE_MODEL_H
20#define _GENERIC_COMPOSITE_MODEL_H
21
22#include <Vector.h>
23#include <list>
24#include <string>
25#include <vector>
26#include <map>
27#include <Random.h>
28
29namespace RZ {
30 struct ParamAssignExpression;
31 struct RecipeContext;
32 struct RecipeParameter;
33 struct RecipeElementStep;
34
35 class Recipe;
36 class ReferenceFrame;
37 class RotatedFrame;
38 class TranslatedFrame;
39 class Element;
40 class OMModel;
41 class ExprRandomState;
42 class Detector;
43
44#ifdef PYTHON_SCRIPT_SUPPORT
45 class Script;
46#endif // PYTHON_SCRIPT_SUPPORT
47
48 enum GenericModelParamType {
49 GENERIC_MODEL_PARAM_TYPE_ELEMENT,
50 GENERIC_MODEL_PARAM_TYPE_ROTATED_FRAME,
51 GENERIC_MODEL_PARAM_TYPE_TRANSLATED_FRAME,
52 GENERIC_MODEL_PARAM_TYPE_VARIABLE
53 };
54
55 class GenericModelParam;
56 typedef std::map<std::string, GenericModelParam *> GenericEvaluatorSymbolDict;
57
59 std::string name;
60 unsigned argc;
61 virtual Real evaluate (Real const *args, unsigned argc) = 0;
62 };
63
65 const GenericEvaluatorSymbolDict *m_dict = nullptr;
66 ExprRandomState *m_randState = nullptr;
67 ExprRandomState *m_ownState = nullptr;
68 std::list<GenericCustomFunction *> m_funcList; // Borrowed
69
70 protected:
71 std::list<std::string> symbols() const;
72 std::list<GenericCustomFunction *> functions() const;
73 Real *resolve(std::string const &);
74 ExprRandomState *randState() const;
75
76 public:
77 GenericEvaluator(const GenericEvaluatorSymbolDict *, ExprRandomState *);
78 virtual ~GenericEvaluator();
79 virtual bool registerCustomFunction(GenericCustomFunction *);
80 virtual std::list<std::string> dependencies() const = 0;
81 virtual bool compile(std::string const &) = 0;
82 virtual Real evaluate() = 0;
83 };
84
85 // This describes how a parameter of an element or a frame is calculated
87 GenericModelParamType type; // What type of object we need to update
88 ParamAssignExpression *description = nullptr; // Already contains an index
89 GenericModelParam *storage = nullptr; // Where to store this result
90 GenericEvaluator *evaluator = nullptr; // Owned
91 std::string assignString;
92 int position = -1;
93
94 // What to do with this result
95 union {
96 Element *element = nullptr;
97 RotatedFrame *rotation;
98 TranslatedFrame *translation;
99 };
100
101 void assign();
103 };
104
105 // This describes what depends on what. This is what is exposed
107 const RecipeParameter *description = nullptr;
108 Real value = 0.; // Current value
109
110 // What needs to be evaluated
111 std::list<GenericComponentParamEvaluator *> dependencies;
112
113 bool test(Real val);
114 };
115
116 // Serves as storage
118 class ElementFactory;
119
121 std::string m_givenName = "(no name)";
122 OMModel *m_model = nullptr; // Borrowed
123 Recipe *m_recipe = nullptr; // Borrowed
124 Element *m_parent = nullptr; // Borrowed
125 GenericCompositeModel *m_parentModel = nullptr; // Borrowed
126 bool m_ownsRecipe = false;
127
128 GenericEvaluatorSymbolDict m_global;
129 std::vector<ReferenceFrame *> m_frames; // Borrowed (m_model)
130 std::vector<Element *> m_elements; // Borrowed (m_model)
131
132 std::list<CompositeElementFactory *> m_customFactoryList; // Owned
133 std::map<std::string, CompositeElementFactory *> m_customFactories;
134 std::list<GenericComponentParamEvaluator *> m_expressions; // Owned
135 ExprRandomState m_ownState;
136 ExprRandomState *m_randState;
137
138#ifdef PYTHON_SCRIPT_SUPPORT
139 std::list<Script *> m_scripts;
140#endif // PYTHON_SCRIPT_SUPPORT
141
142 unsigned int m_completedFrames = 0;
143 unsigned int m_completedElements = 0;
144
145 // Generic parameter storage (dofs and params)
146 std::list<GenericModelParam *> m_genParamStorage; // Owned
147
148 // What is dof and what is param
149 std::map<std::string, GenericModelParam *> m_params; // Borrowed
150 std::map<std::string, GenericModelParam *> m_dofs; // Borrowed
151
152 std::string m_prefix;
153
154 bool m_constructed = false;
155
156 bool registerCustomFactory(CompositeElementFactory *);
157 ElementFactory *lookupElementFactory(const std::string &, bool &) const;
158
159 void registerGlobalsRecursive(GenericCompositeModel *);
160 void initGlobalScope();
161 void registerCustomElements();
162 void createFrames(ReferenceFrame *);
163 void loadScripts();
164 void resolvePorts();
165 void createDelayedElements();
166 void createElementInside(RecipeElementStep *step, ReferenceFrame *pFrame);
167 void createElements(ReferenceFrame *);
168 void createParams();
169 void createScopedExpressions(
170 GenericEvaluatorSymbolDict &,
171 RecipeContext *);
172 void createScopedVariables(
173 GenericEvaluatorSymbolDict &,
174 RecipeContext *);
175
176 void delayedCreationLoop();
177 void createExpressions();
178 void exposeOpticalPaths();
179 void exposePorts();
180
181 GenericComponentParamEvaluator *makeExpression(
182 std::string const &expr,
183 const GenericEvaluatorSymbolDict *dict);
184
185 static bool getLastDottedElement(
186 std::string const &,
187 std::string &prefix,
188 std::string &suffix);
189
190 protected:
191 GenericModelParam *allocateParam();
192 GenericEvaluatorSymbolDict const &symbolDict() const;
193 void setName(std::string const &);
194
195 // Interface methods
196 virtual void registerDof(
197 std::string const &name,
198 GenericModelParam *) = 0;
199
200 virtual void registerParam(
201 std::string const &name,
202 GenericModelParam *) = 0;
203
204 virtual void registerOpticalPath(
205 std::string const &name,
206 std::list<std::string> &params) = 0;
207
208 virtual GenericEvaluator *allocateEvaluator(
209 std::string const &expr,
210 const GenericEvaluatorSymbolDict *dict,
211 std::list<GenericCustomFunction *> const &functions,
212 ExprRandomState *state) = 0;
213
214 virtual void exposePort(
215 std::string const &name,
216 ReferenceFrame *frame);
217
218 ReferenceFrame *getFrameOfContext(const RecipeContext *) const;
219
220 public:
222 Recipe *,
223 OMModel *,
224 GenericCompositeModel *parentModel = nullptr,
225 Element *parent = nullptr);
226 virtual ~GenericCompositeModel();
227
228 std::string givenName() const;
229 std::list<std::string> params() const;
230 std::list<std::string> dofs() const;
231
232 GenericModelParam *lookupParam(std::string const &);
233 GenericModelParam *lookupDof(std::string const &);
234 GenericCompositeModel *parentCompositeModel() const;
235
236 bool loadScript(std::string const &path);
237 bool setParam(std::string const &, Real);
238 bool setDof(std::string const &, Real);
239
240 std::string resolveFilePath(std::string const &) const;
241
242 void assignEverything();
243 void updateRandState();
244 void setRandomState(ExprRandomState * state);
245 ExprRandomState *randState() const;
246
247 // Takes the recipe and constructs elements
248 void build(ReferenceFrame *, std::string const &prefix = "");
249 void transferRecipeOwnership();
250
251 virtual void notifyDetector(
252 std::string const &preferredName,
253 Detector *det) = 0;
254 };
255}
256
257#endif // _GENERIC_COMPOSITE_MODEL_H
Definition: CompositeElement.h:97
Definition: Detector.h:116
Definition: Element.h:393
Definition: Element.h:173
Definition: Random.h:28
Definition: GenericCompositeModel.h:120
Definition: GenericCompositeModel.h:64
Definition: OMModel.h:204
Definition: Recipe.h:142
Definition: ReferenceFrame.h:59
Definition: RotatedFrame.h:25
Definition: TranslatedFrame.h:25
Definition: GenericCompositeModel.h:86
Definition: GenericCompositeModel.h:58
Definition: GenericCompositeModel.h:106
Definition: Recipe.h:33
Definition: Recipe.h:94
Definition: Recipe.h:67
Definition: Recipe.h:81