Skip to content

Commit dfe349b

Browse files
implement getNominalsOfContinuousStates
1 parent ec1613e commit dfe349b

File tree

6 files changed

+22
-47
lines changed

6 files changed

+22
-47
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ We currently support both the Model exchange and Cosimulation Interfaces.
1919

2020
For both interfaces event mode is not supported.
2121

22-
We currently do not support the following methods (beyond a default, trivial implementation)
23-
24-
- fmi3UpdateDiscreteStates
25-
- fmi3GetNumberOfEventindicators
26-
- fmi3GetNominalsOfContinuousStates
27-
- fmi3EvaluateDiscreteStates
28-
2922
### How do I build an FMU from python code?
3023

3124
1. Install `pythonfmu3` package:

examples/counter.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/runner.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/vanderpol_me.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ def get_continuous_state_derivatives(self) -> List[float]:
2929
self.derx0 = self.x1
3030
self.derx1 = self.mu * ((1 - self.x0**2) * self.x1) - self.x0
3131
return [self.derx0, self.derx1]
32-
32+
33+
def get_nominals_of_continuous_states(self, nStates: int) -> List[float]:
34+
return [1.0, 1.0]

pythonfmu3/modelexchange.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ def get_event_indicators(self):
4545

4646
def update_discrete_states(self):
4747
"""Update the discrete states of the model."""
48-
return Fmi3UpdateDiscreteStatesResult()
48+
return Fmi3UpdateDiscreteStatesResult()
49+
50+
def get_nominals_of_continuous_states(self, size: int) -> list[float]:
51+
"""Return the nominal values of the continuous states."""
52+
return [1.0] * size

pythonfmu3/pythonfmu-export/src/pythonfmu/PySlaveInstance.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,20 @@ void PySlaveInstance::GetContinuousStateDerivatives(cppfmu::FMIFloat64* continuo
567567

568568
void PySlaveInstance::GetNominalsOfContinuousStates(cppfmu::FMIFloat64* nominalsOfContinuousStates, std::size_t nStates) const
569569
{
570-
for (size_t i = 0u; i < nStates; i++) {
571-
nominalsOfContinuousStates[i] = 1.0;
572-
}
570+
py_safe_run([this, &nominalsOfContinuousStates, nStates](PyGILState_STATE gilState) {
571+
auto f = PyObject_CallMethod(pInstance_, "get_nominals_of_continuous_states", "(i)", static_cast<int>(nStates));
572+
// Check if f is a valid object
573+
if (f == nullptr) {
574+
handle_py_exception("[getNominalContinuousStates] PyObject_CallMethod", gilState);
575+
}
576+
// Assuming f is a list of floats
577+
for (std::size_t i = 0; i < nStates; i++) {
578+
PyObject* item = PyList_GetItem(f, i);
579+
nominalsOfContinuousStates[i] = static_cast<cppfmu::FMIFloat64>(PyFloat_AsDouble(item));
580+
}
581+
Py_DECREF(f);
582+
clearLogBuffer();
583+
});
573584
}
574585

575586
void PySlaveInstance::SetContinuousStates(const cppfmu::FMIFloat64* continuousStates, std::size_t nStates)

0 commit comments

Comments
 (0)