15 #ifndef CT_WRAPPERS_HPP 16 #define CT_WRAPPERS_HPP 26 #include "fmi2/fmi2.h" 27 #include "fmi2/sim_support.h" 58 const std::string& fmu_file,
59 const unsigned int& input_index,
60 const unsigned int& output_index,
61 const sc_time& sample_period
62 ) :
ct_process(_name), iport1(
"iport1"), oport1(
"oport1"),
63 fmuFileName(fmu_file), input_index(input_index),
64 output_index(output_index), h(sample_period)
66 #ifdef FORSYDE_INTROSPECTION 67 arg_vec.push_back(std::make_tuple(
"fmuFileName",fmuFileName));
70 arg_vec.push_back(std::make_tuple(
"input_index", ss.str()));
73 arg_vec.push_back(std::make_tuple(
"output_index", ss.str()));
76 arg_vec.push_back(std::make_tuple(
"sample_period", ss.str()));
89 std::string fmuFileName;
90 unsigned int input_index, output_index;
96 const char *instanceName;
99 char *fmuResourceLocation;
101 fmi2CallbackFunctions callbacks = {fmuLogger, calloc, free, NULL, &fmu};
102 ModelDescription* md;
103 fmi2Boolean toleranceDefined;
112 fmuResourceLocation = getTempResourcesLocation();
115 toleranceDefined = fmi2False;
120 loadFMU(fmuFileName.c_str(), &fmu);
124 md = fmu.modelDescription;
125 guid = getAttributeValue((Element *)md, att_guid);
126 instanceName = getAttributeValue((Element *)getCoSimulation(md),
127 att_modelIdentifier);
128 c = fmu.instantiate(instanceName, fmi2CoSimulation, guid,
129 fmuResourceLocation, &callbacks, visible, fmi2False);
130 free(fmuResourceLocation);
131 if (!c)
return SC_REPORT_ERROR(name(),
"could not instantiate model");
133 defaultExp = getDefaultExperiment(md);
134 if (defaultExp) tolerance = getAttributeDouble(defaultExp, att_tolerance, &vs);
135 if (vs == valueDefined) {
136 toleranceDefined = fmi2True;
139 fmi2Flag = fmu.setupExperiment(c, toleranceDefined, tolerance, 0, fmi2True, 1000);
140 if (fmi2Flag > fmi2Warning) {
141 return SC_REPORT_ERROR(name(),
"could not initialize model; failed FMI setup experiment");
144 fmi2Flag = fmu.enterInitializationMode(c);
145 if (fmi2Flag > fmi2Warning) {
146 return SC_REPORT_ERROR(name(),
"could not initialize model; failed FMI enter initialization mode");
149 fmi2Flag = fmu.exitInitializationMode(c);
150 if (fmi2Flag > fmi2Warning) {
151 return SC_REPORT_ERROR(name(),
"could not initialize model; failed FMI exit initialization mode");
166 ival1 = iport1.read();
171 while (time >= get_end_time(ival1)) ival1 = iport1.read();
172 setRealInput(&fmu, c, input_index, ival1(time));
177 fmi2Flag = fmu.doStep(c, time.to_seconds(), h.to_seconds(), fmi2True);
178 if (fmi2Flag == fmi2Discard) {
181 if (fmi2OK != fmu.getBooleanStatus(c, fmi2Terminated, &b)) {
182 return SC_REPORT_ERROR(name(),
"could not complete simulation of the model. getBooleanStatus return other than fmi2OK");
185 return SC_REPORT_ERROR(name(),
"the model requested to end the simulation");
187 return SC_REPORT_ERROR(name(),
"could not complete simulation of the model");
189 if (fmi2Flag != fmi2OK)
190 return SC_REPORT_ERROR(name(),
"could not complete simulation of the model");
193 auto res = getRealOutput(&fmu, c, output_index);
195 [
this,res](
const sc_time& t)
204 WRITE_MULTIPORT(oport1, oval)
206 wait(time - sc_time_stamp());
216 FreeLibrary(fmu.dllHandle);
218 dlclose(fmu.dllHandle);
220 freeModelDescription(fmu.modelDescription);
221 deleteUnzippedFiles();
224 #ifdef FORSYDE_INTROSPECTION 227 boundInChans.resize(1);
228 boundInChans[0].port = &iport1;
229 boundOutChans.resize(1);
230 boundOutChans[0].port = &oport1;
242 template <
class OIf,
class I1If>
244 const std::string& fmu_file,
245 const unsigned& input_index,
246 const unsigned& output_index,
247 const sc_time& sample_period,
252 auto p =
new fmi2cswrap(pName.c_str(), fmu_file, input_index, output_index, sample_period);
The sub-signal type used to construct a CT signal.
Definition: sub_signal.hpp:38
The namespace for ForSyDe.
Definition: abssemantics.hpp:30
The CT_out port is used for output ports of CT processes.
Definition: ct_process.hpp:64
fmi2cswrap * make_fmi2cswrap(const std::string &pName, const std::string &fmu_file, const unsigned &input_index, const unsigned &output_index, const sc_time &sample_period, OIf &outS, I1If &inp1S)
Helper function to construct a pipewrap process.
Definition: ct_wrappers.hpp:243
Implements the abstract process in the CT Model of Computation.
The process constructor which defines the abstract semantics of execution.
Definition: abssemantics.hpp:212
Process constructor for a co-simulation FMU wrapper with one input and one output.
Definition: ct_wrappers.hpp:46
CT_out oport1
port for the output channel
Definition: ct_wrappers.hpp:50
The CT_in port is used for input ports of CT processes.
Definition: ct_process.hpp:53
fmi2cswrap(sc_module_name _name, const std::string &fmu_file, const unsigned int &input_index, const unsigned int &output_index, const sc_time &sample_period)
The constructor requires the module name.
Definition: ct_wrappers.hpp:57
std::string forsyde_kind() const
Specifying from which process constructor is the module built.
Definition: ct_wrappers.hpp:81
Implements the sub-components of a CT signal.
CT_in iport1
port for the input channel
Definition: ct_wrappers.hpp:49