RayZaler 0.1
The free opto-mechanical simulation framework
Recipe.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 _RECIPE_H
20#define _RECIPE_H
21
22#include <string>
23#include <list>
24#include <map>
25#include <OpticalElement.h>
26#include <cmath>
27
28namespace RZ {
29 class OMModel;
30 class RecipeContext;
31 class Recipe;
32
34 int s_target = -1;
35 int s_index = -1;
36
37 std::string parameter;
38 std::string expression;
39
40 RecipeContext *parent = nullptr;
41 };
42
44 std::vector<std::string> params;
45 std::map<std::string, std::string> values;
46 std::list<std::string> defined;
47
48 unsigned int positionalNdx = 0;
49 unsigned int nonPositionalNdx = 0;
50
51 void pushParam(std::string const &name, std::string const &value = "") {
52 params.push_back(name);
53 values[name] = value;
54 }
55
56 std::string &
57 operator[](std::string const &name)
58 {
59 return values[name];
60 }
61
62 bool isSet(std::string const &name) const;
63
64 void set(std::string const &, std::string const &);
65 };
66
68 std::string name;
69 std::string factory;
70 std::list<ParamAssignExpression *> positionalParams;
71 std::map<std::string, ParamAssignExpression *> params;
72
73 int s_index = -1;
74 bool delayedCreation = false;
75 RecipeContext *parent = nullptr;
76 Recipe *owner = nullptr;
77
78 void set(std::string const &name, std::string const &expr);
79 };
80
82 Real defaultVal;
83 Real min;
84 Real max;
85 };
86
87 enum RecipeContextType {
88 RECIPE_CONTEXT_TYPE_ROOT,
89 RECIPE_CONTEXT_TYPE_ROTATION,
90 RECIPE_CONTEXT_TYPE_TRANSLATION,
91 RECIPE_CONTEXT_TYPE_PORT
92 };
93
95 std::string name; // May be empty
96 RecipeContextType type;
97
98 std::list<RecipeContext *> contexts;
99 std::list<RecipeElementStep *> elements;
100
101 std::list<std::string> varNames;
102 std::map<std::string, ParamAssignExpression *> variables;
103 std::map<std::string, ParamAssignExpression *> params;
104
105 int s_index = -1;
106
107 bool delayed = false;
108 RecipeContext *parent = nullptr;
109 Recipe *owner = nullptr;
110 std::string parentNS;
111
112 // Only set if type == PORT
113 RecipeElementStep *element;
114 std::string port;
115
116 inline std::string
117 currNS() const
118 {
119 if (parentNS.size() == 0)
120 return name;
121
122 return name.size() > 0
123 ? parentNS + "." + name
124 : parentNS;
125 }
126
127 std::string to_string() const;
128 };
129
131 std::string name;
132 std::list<std::string> steps;
133 RecipeContext *parent = nullptr;
134
135 inline void
136 plug(std::string const &element)
137 {
138 steps.push_back(element);
139 }
140 };
141
142 class Recipe {
143 RecipeContext *m_rootContext = nullptr;
144 Recipe *m_parent = nullptr;
145
146 // Search paths (forwarded to the composite model)
147 std::list<std::string> m_searchPaths;
148
149 // Allocation
150 std::vector<RecipeContext *> m_contexts; // Tracks frames
151 std::vector<RecipeElementStep *> m_elementSteps; // Tracks elements
152 std::vector<RecipeOpticalPath *> m_pathSteps; // Optical paths
153 std::vector<Recipe *> m_subRecipes; // Subrecipes to construct elements
154 std::list<std::string> m_scripts; // Python scripts
155
156 std::vector<ParamAssignExpression *> m_elemParameters; // Tracks how to configure elements
157 std::vector<ParamAssignExpression *> m_frameParameters; // Tracks how to configure frames
158 std::vector<ParamAssignExpression *> m_variables; // Tracks how to update variables
159
160 std::map<std::string, RecipeContext *> m_frames;
161 std::map<std::string, RecipeElementStep *> m_elements;
162 std::map<std::string, Recipe *> m_customElements; // Tracks composite elements
163 std::map<std::string, RecipeOpticalPath *> m_paths; // Tracks optical paths
164 std::map<std::string, RecipeParameter> m_parameters;
165 std::map<std::string, RecipeParameter> m_dofs;
166 std::map<std::string, RecipeContext *> m_ports;
167
168 // Set during edition
169 RecipeContext *m_currContext = nullptr;
170 unsigned int m_nestedPorts = 0;
171
172 std::string currNS() const;
173
174 private:
175 std::string genElementName(std::string const &type);
176 std::string genReferenceFrameName(std::string const &type);
177
178 std::string genElementName(std::string const &parent, std::string const &type);
179 std::string genReferenceFrameName(std::string const &parent, std::string const &type);
180
181 RecipeContext *makeContext(RecipeContext *);
182 RecipeElementStep *makeElementStep(RecipeContext *);
183 RecipeOpticalPath *makeOpticalPathStep(RecipeContext *);
184 RecipeParameter *makeRecipeParam(std::map<std::string, RecipeParameter> &, std::string const &);
185
186 ParamAssignExpression *makeElementParameter(
187 RecipeElementStep *elem,
188 std::string const &name,
189 std::string const &expression);
190
191 ParamAssignExpression *makeReferenceFrameParameter(
192 RecipeContext *ctx,
193 std::string const &name,
194 std::string const &expression);
195
196 ParamAssignExpression *makeVariable(
197 RecipeContext *ctx,
198 std::string const &name,
199 std::string const &expression);
200
201 void push(RecipeContext *ctx);
202
203 public:
204 Recipe(std::string const &, Recipe *);
205 Recipe();
206 ~Recipe();
207
208 inline std::list<std::string> const &
209 scripts() const
210 {
211 return m_scripts;
212 }
213
214 inline std::list<std::string> const &
215 searchPaths() const
216 {
217 return m_searchPaths;
218 }
219
220 inline RecipeContext *
221 currentContext() const
222 {
223 return m_currContext;
224 }
225
226 Recipe *parent() const;
227
228 RecipeContext *rootContext() const;
229 std::vector<RecipeContext *> const &contexts() const;
230 std::vector<RecipeElementStep *> const &elements() const;
231 std::vector<RecipeOpticalPath *> const &paths() const;
232 std::map<std::string, RecipeContext *> const &ports() const;
233
234 std::map<std::string, Recipe *> const &customElements() const;
235 std::map<std::string, RecipeParameter> const &dofs() const;
236 std::map<std::string, RecipeParameter> const &params() const;
237
238 RecipeContext *lookupReferenceFrame(std::string const &) const;
239 RecipeElementStep *lookupElement(std::string const &) const;
240 RecipeOpticalPath *lookupOpticalPath(std::string const & = "") const;
241 RecipeElementStep *resolveElement(std::string const &) const;
242 Recipe *makeCustomElement(std::string const &);
243
244 bool addScript(std::string const &scriptPath);
245 void pushSearchPath(std::string const &path);
246 void pushVariable(std::string const &name, std::string const &value);
247
248 void pushRotation(
249 std::string const &angle,
250 std::string const &eX,
251 std::string const &eY,
252 std::string const &eZ,
253 std::string const &name = "");
254
255 void pushTranslation(
256 std::string const &dX,
257 std::string const &dY,
258 std::string const &dZ,
259 std::string const &name = "");
260
261 void pushPortContext(RecipeElementStep *, std::string const &port);
262
263 bool pop();
264
265 RecipeElementStep *addElement(
266 std::string const &name,
267 std::string const &factory,
268 std::map<std::string, std::string> const &parameters
269 = std::map<std::string, std::string>());
270
271 RecipeOpticalPath *allocatePath(std::string const &name = "");
272
273 void addPort(std::string const &name);
274
275 bool addDof(
276 std::string const &name,
277 Real defVal,
278 Real min = -INFINITY,
279 Real max = +INFINITY);
280
281 bool addParam(
282 std::string const &name,
283 Real defVal,
284 Real min = -INFINITY,
285 Real max = +INFINITY);
286
287 void debug();
288
289 friend class RecipeElementStep;
290 };
291
293}
294#endif // _RECIPE_H
Definition: GenericCompositeModel.h:120
Definition: Recipe.h:142
Definition: Recipe.h:33
Definition: Recipe.h:94
Definition: Recipe.h:67
Definition: Recipe.h:130
Definition: Recipe.h:43
Definition: Recipe.h:81