Skip to content

Commit

Permalink
#4 rename reset to resetAll, add new reset
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergmann committed May 2, 2024
1 parent aa79691 commit 49a5a50
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/copasi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface ModelInfo {
export default class COPASI {
constructor(module: any);
reset(): void;
resetAll(): void;
readonly version: string;
_vectorToArray(v: any): any[];
loadExample(path: string) : ModelInfo;
Expand Down
1 change: 1 addition & 0 deletions js/copasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class COPASI {
this.destroy = Module.destroy;
this.getValue = Module.getValue;
this.setValue = Module.setValue;
this.resetAll = Module.resetAll;

// init runtime
this.initCps();
Expand Down
47 changes: 47 additions & 0 deletions src/copasijs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,35 @@ std::string getModelInfo()
return buildModelInfo().dump(2);
}

void _removeFixedElementsFromSet(CModelParameterGroup* group)
{
if (!group)
return;

std::vector< CModelParameter * > toBeRemoved;
for (auto it = group->begin(); it != group->end(); ++it)
{
auto* p = *it;
if (p == NULL)
continue;

auto *pGroup = dynamic_cast<CModelParameterGroup*>(p);
if (pGroup)
{
_removeFixedElementsFromSet(pGroup);
continue;
}
if (p->getSimulationType() == CModelEntity::Status::FIXED)
{
toBeRemoved.push_back(p);
}
}

for (auto* p : toBeRemoved)
{
group->remove(p);
}
}

void loadCommon()
{
Expand All @@ -793,6 +822,12 @@ void loadCommon()
auto* newSet = new CModelParameterSet("_initial_state");
sets.add(newSet, true);
newSet->createFromModel();

// now create a second one with only the state variables
newSet = new CModelParameterSet("_initial_state_variables_only");
sets.add(newSet, true);
newSet->createFromModel();
_removeFixedElementsFromSet(newSet);
}

std::string loadFromFile(const std::string& modelFile)
Expand Down Expand Up @@ -874,6 +909,17 @@ std::string loadModel(const std::string& cpsCode)
}

void reset()
{
if (pDataModel == NULL)
initCps();

auto* pModel = pDataModel->getModel();
auto& set = pModel->getModelParameterSets()["_initial_state_variables_only"];
set.updateModel();
pModel->applyInitialValues();
}

void resetAll()
{
if (pDataModel == NULL)
initCps();
Expand Down Expand Up @@ -1336,6 +1382,7 @@ EMSCRIPTEN_BINDINGS(copasi_binding)
emscripten::function("loadFromFile", &loadFromFile);
emscripten::function("loadModel", &loadModel);
emscripten::function("reset", &reset);
emscripten::function("resetAll", &resetAll);
emscripten::function("simulate", &simulate);
emscripten::function("simulateYaml", &simulateYaml);
emscripten::function("simulateEx", &simulateEx);
Expand Down
9 changes: 8 additions & 1 deletion src/copasijs.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ std::string loadModel(const std::string& modelString);

/// @brief resets the model
///
/// This function resets the models floating species and entities with
/// ODEs back to their initial values.
void reset();

/// @brief resets the model completely
///
/// This function resets the model to the state after loading
/// the model.
void reset();
void resetAll();


/// @brief applies a yaml string to the model
/// @param yaml the yaml string to apply
Expand Down
19 changes: 19 additions & 0 deletions test/copasijs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ TEST_CASE("Load SBML Model", "[copasijs][sbml]")
REQUIRE(json["titles"][1] == "S1");
REQUIRE(json["columns"][1][0] == 0);

setValue("J0_v0", 8);

// test difference between reset and restAll by changing the value of S1 and J0_v0
setValue("S1", 1);
setValue("J0_v0", 10);

reset();
// now S1 should be 0 and J0_v0 should be 10
REQUIRE(getValue("S1") == 0);
REQUIRE(getValue("J0_v0") == 10);

// now run resetAll
setValue("S1", 1);
setValue("J0_v0", 10);
resetAll();
// now S1 should be 0 and J0_v0 should be 8
REQUIRE(getValue("S1") == 0);
REQUIRE(getValue("J0_v0") == 8);

}


Expand Down

0 comments on commit 49a5a50

Please sign in to comment.