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 1ea18c5
Show file tree
Hide file tree
Showing 7 changed files with 85 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 run(const std::map<std::string, double> &pInitialConditions); //---ISAN---
#endif

/**
* @brief Return the tasks.
Expand Down
4 changes: 4 additions & 0 deletions src/bindings/javascript/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ EMSCRIPTEN_BINDINGS(libOpenCOR)
emscripten::register_vector<libOpenCOR::SedStylePtr>("SedStylePtrs");
emscripten::register_vector<std::string>("Strings");

// Maps.

emscripten::register_map<std::string, double>("StringDoubleMap"); //---ISAN---

// APIs.

loggerApi(); // Note: it needs to be first since it is used by some other APIs.
Expand Down
4 changes: 4 additions & 0 deletions src/bindings/javascript/sed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ void sedApi()

emscripten::class_<libOpenCOR::SedInstance, emscripten::base<libOpenCOR::Logger>>("SedInstance")
.smart_ptr<libOpenCOR::SedInstancePtr>("SedInstance")
/*---ISAN---
.function("run", &libOpenCOR::SedInstance::run)
*/
.function("run", emscripten::select_overload<void()>(&libOpenCOR::SedInstance::run)) //---ISAN---
.function("run", emscripten::select_overload<void(const std::map<std::string, double> &)>(&libOpenCOR::SedInstance::run)) //---ISAN---
.function("tasks", &libOpenCOR::SedInstance::tasks);

// SedInstanceTask API.
Expand Down
13 changes: 13 additions & 0 deletions src/sed/sedinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ SedInstance::Impl::Impl(const SedDocumentPtr &pDocument, bool pCompiled)
}
}

/*---ISAN---
void SedInstance::Impl::run()
*/
void SedInstance::Impl::run(const std::map<std::string, double> &pInitialConditions) //---ISAN---
{
// Run all the tasks associated with this instance unless they have some issues.

for (const auto &task : mTasks) {
if (!task->hasIssues()) {
/*---ISAN---
task->pimpl()->run();
*/
task->pimpl()->run(pInitialConditions); //---ISAN---

if (task->hasIssues()) {
addIssues(task);
Expand Down Expand Up @@ -118,6 +124,13 @@ void SedInstance::run()
pimpl()->run();
}

#ifdef __EMSCRIPTEN__
void SedInstance::run(const std::map<std::string, double> &pInitialConditions) //---ISAN---
{
pimpl()->run(pInitialConditions);
}
#endif

SedInstanceTaskPtrs SedInstance::tasks() const
{
return pimpl()->tasks();
Expand Down
3 changes: 3 additions & 0 deletions src/sed/sedinstance_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class SedInstance::Impl: public Logger::Impl

explicit Impl(const SedDocumentPtr &pDocument, bool pCompiled);

/*---ISAN---
void run();
*/
void run(const std::map<std::string, double> &pInitialConditions = {}); //---ISAN---

SedInstanceTaskPtrs tasks() const;
};
Expand Down
52 changes: 52 additions & 0 deletions src/sed/sedinstancetask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,48 @@ 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

/*---ISAN---
void SedInstanceTask::Impl::initialise()
*/
void SedInstanceTask::Impl::applyInitialConditions(const std::map<std::string, double> &pInitialConditions) //---ISAN---
{
for (const auto &[key, value] : pInitialConditions) {
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(const std::map<std::string, double> &pInitialConditions)
{
// Initialise our model, which means that for an ODE/DAE model we need to initialise our states, rates, and
// variables, compute computed constants, rates, and variables, while for an algebraic/NLA model we need to
Expand All @@ -177,6 +218,9 @@ void SedInstanceTask::Impl::initialise()
} else {
#endif
mRuntime->initialiseInterpretedVariablesForDifferentialModel()(mStates, mRates, mVariables);

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

mRuntime->computeInterpretedComputedConstants()(mVariables);
mRuntime->computeInterpretedRates()(mVoi, mStates, mRates, mVariables);
mRuntime->computeInterpretedVariablesForDifferentialModel()(mVoi, mStates, mRates, mVariables);
Expand Down Expand Up @@ -216,12 +260,18 @@ void SedInstanceTask::Impl::initialise()
#endif
}

/*---ISAN---
void SedInstanceTask::Impl::run()
*/
void SedInstanceTask::Impl::run(const std::map<std::string, double> &pInitialConditions) //---ISAN---
{
// (Re)initialise our model.
// Note: reinitialise our model in case we are running our model multiple times.

/*---ISAN---
initialise();
*/
initialise(pInitialConditions); //---ISAN---

// Compute our model, unless it's an algebraic/NLA model in which case we are already done.

Expand Down Expand Up @@ -319,6 +369,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 +380,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 @@ -62,8 +62,14 @@ class SedInstanceTask::Impl: public Logger::Impl

void trackResults(size_t pIndex);

void applyInitialConditions(const std::map<std::string, double> &pInitialConditions); //---ISAN---

/*---ISAN---
void initialise();
void run();
*/
void initialise(const std::map<std::string, double> &pInitialConditions = {}); //---ISAN---
void run(const std::map<std::string, double> &pInitialConditions); //---ISAN---

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

0 comments on commit 1ea18c5

Please sign in to comment.