Skip to content

Commit

Permalink
SedInstance: allow to pass some initial conditions to the JavaScript …
Browse files Browse the repository at this point in the history
…binding version of the `run()` method.

This is to be reverted after ISAN.
  • Loading branch information
agarny committed Jul 2, 2024
1 parent 9eed30c commit f310a08
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/libopencor/sedinstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class LIBOPENCOR_EXPORT SedInstance: public Logger
*/

void run();
#ifdef __EMSCRIPTEN__
void addInitialCondition(const std::string &pParameter, double pValue); //---ISAN---
#endif

/**
* @brief Return the tasks.
Expand Down
1 change: 1 addition & 0 deletions src/bindings/javascript/sed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void sedApi()
emscripten::class_<libOpenCOR::SedInstance, emscripten::base<libOpenCOR::Logger>>("SedInstance")
.smart_ptr<libOpenCOR::SedInstancePtr>("SedInstance")
.function("run", &libOpenCOR::SedInstance::run)
.function("addInitialCondition", &libOpenCOR::SedInstance::addInitialCondition) //---ISAN---
.function("tasks", &libOpenCOR::SedInstance::tasks);

// SedInstanceTask API.
Expand Down
16 changes: 16 additions & 0 deletions src/sed/sedinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ void SedInstance::Impl::run()
}
}

#ifdef __EMSCRIPTEN__
void SedInstance::Impl::addInitialCondition(const std::string &pParameter, double pValue) //---ISAN---
{
for (const auto &task : mTasks) {
task->pimpl()->addInitialCondition(pParameter, pValue);
}
}
#endif

SedInstanceTaskPtrs SedInstance::Impl::tasks() const
{
return mTasks;
Expand Down Expand Up @@ -118,6 +127,13 @@ void SedInstance::run()
pimpl()->run();
}

#ifdef __EMSCRIPTEN__
void SedInstance::addInitialCondition(const std::string &pParameter, double pValue) //---ISAN---
{
pimpl()->addInitialCondition(pParameter, pValue);
}
#endif

SedInstanceTaskPtrs SedInstance::tasks() const
{
return pimpl()->tasks();
Expand Down
1 change: 1 addition & 0 deletions src/sed/sedinstance_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SedInstance::Impl: public Logger::Impl
explicit Impl(const SedDocumentPtr &pDocument, bool pCompiled);

void run();
void addInitialCondition(const std::string &pParameter, double pValue); //---ISAN---

SedInstanceTaskPtrs tasks() const;
};
Expand Down
48 changes: 48 additions & 0 deletions src/sed/sedinstancetask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,44 @@ void SedInstanceTask::Impl::trackResults(size_t pIndex)
}
}

namespace {

std::string name(const libcellml::VariablePtr &pVariable, char pSeparator = '.') //---ISAN---
{
auto component = std::dynamic_pointer_cast<libcellml::Component>(pVariable->parent());

return component->name() + pSeparator + pVariable->name();
}

} // namespace

void SedInstanceTask::Impl::applyInitialConditions() //---ISAN---
{
for (const auto &[key, value] : mInitialConditions) {
bool initialConditionSet = false;

for (const auto &state : mAnalyserModel->states()) {
if (name(state->variable(), '/') == key) {
mStates[state->index()] = value;

initialConditionSet = true;

break;
}
}

if (!initialConditionSet) {
for (const auto &variable : mAnalyserModel->variables()) {
if (name(variable->variable(), '/') == key) {
mVariables[variable->index()] = value;

break;
}
}
}
}
}

void SedInstanceTask::Impl::initialise()
{
// Initialise our model, which means that for an ODE/DAE model we need to initialise our states, rates, and
Expand All @@ -177,6 +215,9 @@ void SedInstanceTask::Impl::initialise()
} else {
#endif
mRuntime->initialiseInterpretedVariablesForDifferentialModel()(mStates, mRates, mVariables);

applyInitialConditions(); //---ISAN---

mRuntime->computeInterpretedComputedConstants()(mVariables);
mRuntime->computeInterpretedRates()(mVoi, mStates, mRates, mVariables);
mRuntime->computeInterpretedVariablesForDifferentialModel()(mVoi, mStates, mRates, mVariables);
Expand Down Expand Up @@ -292,6 +333,11 @@ void SedInstanceTask::Impl::run()
}
}

void SedInstanceTask::Impl::addInitialCondition(const std::string &pParameter, double pValue) //---ISAN---
{
mInitialConditions[pParameter] = pValue;
}

Doubles SedInstanceTask::Impl::state(size_t pIndex) const
{
if (pIndex >= mAnalyserModel->stateCount()) {
Expand Down Expand Up @@ -319,6 +365,7 @@ Doubles SedInstanceTask::Impl::variable(size_t pIndex) const
return mResults.variables[pIndex];
}

/*---ISAN---
namespace {
std::string name(const libcellml::VariablePtr &pVariable)
Expand All @@ -329,6 +376,7 @@ std::string name(const libcellml::VariablePtr &pVariable)
}
} // namespace
*/

Doubles SedInstanceTask::Impl::voi() const
{
Expand Down
6 changes: 6 additions & 0 deletions src/sed/sedinstancetask_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class SedInstanceTask::Impl: public Logger::Impl
SolverNlaPtr mNlaSolver;
bool mCompiled = true;

std::map<std::string, double> mInitialConditions; //---ISAN---

double mVoi = 0.0;
double *mStates = nullptr;
double *mRates = nullptr;
Expand All @@ -62,9 +64,13 @@ class SedInstanceTask::Impl: public Logger::Impl

void trackResults(size_t pIndex);

void applyInitialConditions(); //---ISAN---

void initialise();
void run();

void addInitialCondition(const std::string &pParameter, double pValue); //---ISAN---

Doubles state(size_t pIndex) const;
Doubles rate(size_t pIndex) const;
Doubles variable(size_t pIndex) const;
Expand Down

0 comments on commit f310a08

Please sign in to comment.