AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
export/Module.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXFMI2_EXPORT_MODULE_H
18#define AGXFMI2_EXPORT_MODULE_H
19
20#include <agx/config/AGX_USE_FMI.h>
21
22#if AGX_USE_FMI()
23
24#include <agxFMI2/export.h>
25
28#include <agx/Object.h>
29#include <agx/Uuid.h>
30#include <agx/Xml.h>
31#include <agxSDK/Simulation.h>
32
33#include <agx/config/AGX_USE_OSG.h>
34#include <agx/RealTimeTrigger.h>
35#if AGX_USE_OSG()
37#include <agxOSG/Node.h>
38#endif
39
40
41extern "C"
42{
43 #include <FMI2/fmi2Functions.h>
44}
45#if defined(_MSC_VER)
46#pragma warning( pop )
47#endif
48
49namespace agxOSG
50{
51 class ExampleApplication;
52 class Group;
53}
54
55
56namespace agxIO
57{
58 class ArgumentParser;
59}
60
61
62
63
64namespace agxFMI2
65{
66 namespace Export
67 {
68
69 // AGX_DECLARE_POINTER_TYPES(Module);
70
74 class AGXFMI_EXPORT Module
75 {
76 public:
77 Module(const agx::Name& name, const char* GUID, bool createOsgWindow = false);
78 ~Module();
79
81 agx::UInt32 registerVariable(Variable* variable);
82
84 void registerStrongCoupling(agx::RigidBody* body);
85
86 void initRendering(agxIO::ArgumentParser* argParser = nullptr);
87 void initRemoteConnection();
88 void cleanup();
89
90 const agx::Name& getName() const;
91
92 Variable* getVariable(const agx::Name& name);
93
94 const char *getGUID() const;
95
96 const agx::String& getDescription() const;
97 void setDescription(const agx::String& description);
98
99 const agx::String& getResourcePath() const;
100 void setResourcePath(const agx::String& path);
101
102 // setSimulation can only be called _before_ the module is initialized!
103 void setSimulation(agxSDK::Simulation* simulation);
104 agxSDK::Simulation *getSimulation();
105
106 void setOsgApplication(agxOSG::ExampleApplication* app);
107 agxOSG::ExampleApplication *getOsgApplication();
108
109 void setOsgRoot(osg::Group *group);
110 osg::Group *getOsgRoot();
111
112 void exportModelDescriptionFile(const agx::String& filePath);
113
114 bool getCreateOsgWindow() const;
115
116 protected:
117 void setCreateOsgWindow(bool flag);
118 void setThrottleRealTime(bool flag);
119 void setUseDebugRendering(bool flag);
120 void updateViewer(agx::Real dt);
121 void setAutoStepCallback(bool flag);
122 void setRenderFrequency(agx::Real freq);
123 void setEnableJournalRecord(bool flag);
124 void setEnableTaskProfiling(bool flag);
125 void setEnableThreadTimeline(bool flag);
126 void setJournalPath(const agx::String& path);
127 void setJournalConfiguration(const agx::String& config);
128 void setJournalRecordFrequency(agx::Real frequency);
129 void setInternalStepMultiplier(agx::Int multiplier);
130 void setApplicationArguments(const agx::String& args);
131 void createRenderObjects();
132
133 /*
134 Enable automatic OSG rendering of all geometries.
135 */
136 void setEnableAutoRendering(bool enable);
137
138 public:
140#ifndef SWIG
141 void finalizeSetup();
142 fmi2Status fmiSetupExperiment(fmi2Boolean toleranceDefined, fmi2Real relativeTolerance, fmi2Real tStart, fmi2Boolean stopTimeDefined, fmi2Real tStop);
143 fmi2Status fmiEnterInitializationMode();
144 fmi2Status fmiExitInitializationMode();
145 fmi2Status fmiTerminate();
146 fmi2Status fmiReset();
147 fmi2Status fmiSetDebugLogging(fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[]);
148 fmi2Status fmiGetReal(const fmi2ValueReference vr[], size_t nvr, fmi2Real value[]);
149 fmi2Status fmiGetInteger(const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[]);
150 fmi2Status fmiGetBoolean(const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[]);
151 fmi2Status fmiGetString(const fmi2ValueReference vr[], size_t nvr, fmi2String value[]);
152 fmi2Status fmiSetReal(const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[]);
153 fmi2Status fmiSetInteger(const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[]);
154 fmi2Status fmiSetBoolean(const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[]);
155 fmi2Status fmiSetString(const fmi2ValueReference vr[], size_t nvr, const fmi2String value[]);
156 fmi2Status fmiSetRealInputDerivatives(const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], const fmi2Real value[]);
157 fmi2Status fmiGetRealOutputDerivatives(const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], fmi2Real value[]);
158 fmi2Status fmiCancelStep();
159 fmi2Status fmiDoStep(fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean commitStep);
160 fmi2Status fmiGetStatus(const fmi2StatusKind s, fmi2Status* value);
161 fmi2Status fmiGetRealStatus(const fmi2StatusKind s, fmi2Real* value);
162 fmi2Status fmiGetIntegerStatus(const fmi2StatusKind s, fmi2Integer* value);
163 fmi2Status fmiGetBooleanStatus(const fmi2StatusKind s, fmi2Boolean* value);
164 fmi2Status fmiGetStringStatus(const fmi2StatusKind s, fmi2String* value);
165 fmi2Status fmiGetDirectionalDerivative(const fmi2ValueReference vUnknown_ref[], size_t nUnknown, const fmi2ValueReference vKnown_ref[] , size_t nKnown, const fmi2Real dvKnown[], fmi2Real dvUnknown[]);
166
167 fmi2Status fmiGetFMUstate(fmi2FMUstate* state);
168 fmi2Status fmiSetFMUstate(fmi2FMUstate state);
169 fmi2Status fmiFreeFMUstate(fmi2FMUstate* state);
170#endif
171 private:
172 template<typename T>
173 T *createParameter(const agx::String& name, typename T::Type startValue = typename T::Type());
174
175 template <typename T>
176 void registerBodyAccessor(agx::RigidBody *body, agx::UInt numComponents, bool createMobilityVariable = false);
177
178 agx::TiXmlElement *exportModel();
179 agx::TiXmlElement *exportCoSimulation();
180 agx::TiXmlElement *exportUnitDefinitions();
181 agx::TiXmlElement *exportTypeDefinitions();
182 agx::TiXmlElement *exportModelVariables();
183 agx::TiXmlElement *exportModelStructure();
184
185 void createExportGeneralVariables();
186 void applyAccumulators();
187
188 template <typename T, typename VariableT>
189 fmi2Status fmiSetT(const fmi2ValueReference vr[], size_t nvr, const T value[]);
190
191 template <typename T, typename VariableT>
192 fmi2Status fmiGetT(const fmi2ValueReference vr[], size_t nvr, T value[]);
193
194 private:
195 AGX_DECLARE_POINTER_TYPES(SteppingThread);
196
197 private:
198 friend class SteppingThread;
199
200 void signalStepperDone(bool completedStep);
201
202 agx::UInt getInternalStepMultiplier() const;
203
204 private:
205 class RigidBodyMobilityOutputVariable;
206 using RigidBodyMobilityTable = agx::HashTable<Variable *, agx::Real>;
207 RigidBodyMobilityTable m_mobilityTable;
208
209 private:
210 agx::Name m_name;
211 const char *m_GUID;
212 bool m_initialized;
213 bool m_createOsgWindow;
214 bool m_throttleRealTime;
215 bool m_useDebugRendering;
216 bool m_enableJournalRecord;
217 bool m_enableTaskProfiling;
218 bool m_enableThreadTimeline;
219 bool m_initializedApplication;
220 agx::String m_journalPath;
221 agx::String m_journalConfiguration;
222 agx::Real m_journalRecordFrequency;
223 agx::UInt m_internalStepMultiplier;
224 bool m_sharedApplication;
225 agx::String m_description;
226 agx::String m_resourcePath;
227 agx::String m_applicationArguments;
228 bool m_enableAutoRendering;
229#if AGX_USE_OSG()
230 agxOSG::ExampleApplicationRef m_application;
231 osg::ref_ptr<osg::Group> m_renderRoot;
232#endif
233 agxSDK::SimulationRef m_simulation;
234 agx::Real m_rtThrottleAccumulation;
235 agx::Timer m_rtThrottleTimer;
236 agx::RealTimeTrigger m_rtTimer;
237 agx::Block m_stepBlock;
238 agx::Callback1<bool> m_autoStepCallback;
239
240
241 // fmi2ValueReference m_vrCounter;
242 bool m_useLogging;
243 agx::HashSet<agx::String> m_loggingCategories;
246
247 VariableRefVector m_variables;
248
249 agx::Real m_relativeTolerance;
250 agx::Real m_tStart;
251 agx::Real m_tStop;
252 bool m_hasStopDefined;
253 bool m_firstStep;
254 SteppingThreadRef m_steppingThread;
255 agx::Block m_steppingBlock;
256 bool m_strongCouplingStepping;
257 bool m_stepComplete;
258 bool m_preSolve;
259 bool m_enableDirectSolverDataSharing;
260 agx::MobilitySolverRef m_mobilityReferenceSolver;
261 agx::MobilitySolverRef m_savedState;
263 };
264
265#ifndef SWIG
266 class Module::SteppingThread : public agx::BasicThread, public agx::Object
267 {
268 public:
269 SteppingThread(Module *module);
270
271 void start();
272 void stop();
273
274 void beginStep();
275 void continueStep();
276
277 void setupStrongCoupling();
278
279 protected:
280 virtual ~SteppingThread();
281
282 private:
283 virtual void run() override;
284 void preSolveCallback(agx::Task *);
285 // void postSolveCallback(agx::Task *);
286
287 private:
288 Module *m_module;
289 bool m_running;
290 bool m_stepping;
291 bool m_initialized;
292 agx::Block m_startBlock;
293 agx::Block m_activationBlock;
294 agx::Block m_solverBlock;
296 agx::TaskRef m_reintegrateVelocitiesTask;
297 // agx::Task::ExecutionEvent::CallbackType m_postSolveCallback;
298 };
299#endif
300
301 AGX_FORCE_INLINE bool Module::getCreateOsgWindow() const { return m_createOsgWindow; }
302
303 }
304
305}
306
307#endif /* AGX_USE_FMI */
308
309#endif /* AGXFMI2_EXPORT_MODULE_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition Referenced.h:254
#define AGXFMI_EXPORT
A small argument parser class.
Class that encapsulates rendering and simulation using OpenSceneGraph.
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition Simulation.h:126
Basic wrapper class aroud std::thread.
Definition Thread.h:83
Block synchronization primitive.
Templated callback with one argument.
Definition Callback.h:101
T CallbackType
Definition Event.h:37
Inheritance with partial specialization due to bug with ref_ptr containers.
The HighAccurayTimer class can replace the regular Timer class in high level applications where the n...
Inheritance with partial specialization due to bug with ref_ptr containers.
Representation of a name string.
Definition Name.h:33
agx::Object is a refcounted object with a name.
Definition Object.h:59
The rigid body class, combining a geometric model and a frame of reference.
Definition RigidBody.h:52
A representation of a generic task.
Definition Task.h:58
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition Timer.h:75
Templated vector class.
Definition agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition ref_ptr.h:30
#define AGX_FORCE_INLINE
Definition macros.h:58
This namespace contains functionality for using AGX together with version 2 of the Functional Mockup ...
The agxIO namespace contains classes for reading, writing and finding files.
Definition Material.h:42
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
uint32_t UInt32
Definition Integer.h:32
uint64_t UInt
Definition Integer.h:27
double Real
Definition Real.h:42
int64_t Int
Definition Integer.h:28