AGX Dynamics  2.35.0.1
export/Module.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx 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 
41 extern "C"
42 {
43  #include <external/fmi/2.0/fmi2Functions.h>
44 }
45 #if defined(_MSC_VER)
46 #pragma warning( pop )
47 #endif
48 
49 namespace agxOSG
50 {
51  class ExampleApplication;
52  class Group;
53 }
54 
55 
56 namespace agxIO
57 {
58  class ArgumentParser;
59 }
60 
61 
62 
63 
64 namespace 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;
262  };
263 
264 #ifndef SWIG
265  class Module::SteppingThread : public agx::BasicThread, public agx::Object
266  {
267  public:
268  SteppingThread(Module *module);
269 
270  void start();
271  void stop();
272 
273  void beginStep();
274  void continueStep();
275 
276  void setupStrongCoupling();
277 
278  protected:
279  virtual ~SteppingThread();
280 
281  private:
282  virtual void run() override;
283  void preSolveCallback(agx::Task *);
284  // void postSolveCallback(agx::Task *);
285 
286  private:
287  Module *m_module;
288  bool m_running;
289  bool m_stepping;
290  bool m_initialized;
291  agx::Block m_startBlock;
292  agx::Block m_activationBlock;
293  agx::Block m_solverBlock;
294  agx::Task::ExecutionEvent::CallbackType m_preSolveCallback;
295  agx::TaskRef m_reintegrateVelocitiesTask;
296  // agx::Task::ExecutionEvent::CallbackType m_postSolveCallback;
297  };
298 #endif
299 
300  AGX_FORCE_INLINE bool Module::getCreateOsgWindow() const { return m_createOsgWindow; }
301 
302  }
303 
304 }
305 
306 #endif /* AGX_USE_FMI */
307 
308 #endif /* AGXFMI2_EXPORT_MODULE_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#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:127
Basic wrapper class aroud std::thread.
Definition: Thread.h:82
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.
Definition: agx/HashSet.h:666
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:60
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
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:40
int32_t Int
Definition: Integer.h:35
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42