Skip to content

Commit

Permalink
adding these for now but not quite finished
Browse files Browse the repository at this point in the history
  • Loading branch information
skeating committed Dec 8, 2024
1 parent bdba1fb commit 67b2eef
Show file tree
Hide file tree
Showing 11 changed files with 1,785 additions and 237 deletions.
911 changes: 736 additions & 175 deletions src/sbml/conversion/ExpressionAnalyser.cpp

Large diffs are not rendered by default.

78 changes: 70 additions & 8 deletions src/sbml/conversion/ExpressionAnalyser.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ typedef enum
} ExpressionType_t;

/*
*
|*/
* the structure to contain the substitution values
*/
struct SubstitutionValues_t {
std::string k_value;
double k_real_value;
Expand All @@ -89,6 +89,7 @@ struct SubstitutionValues_t {
ExpressionType_t type;
ASTNode* current;
std::string z_value;
ASTNode* z_expression;
unsigned int odeIndex;
};

Expand Down Expand Up @@ -155,23 +156,60 @@ class LIBSBML_EXTERN ExpressionAnalyser
int setModel(Model* m);


SubstitutionValues_t* createBlankSubstitutionValues();
unsigned int getNumExpressions();
SubstitutionValues_t* getExpression(unsigned int index);


void detectHiddenSpecies(List * hiddenSpecies);

bool analyseNode(ASTNode* node, SubstitutionValues_t* value);

void analyse(bool minusXPlusYOnly = false);

void orderExpressions();

void printSubstitutionValues(const SubstitutionValues_t* values1);
bool areIdenticalSubstitutionValues(SubstitutionValues_t* values1, SubstitutionValues_t* values2);

bool expressionExists(SubstitutionValues_t* values1, SubstitutionValues_t* values2);

void substituteParametersForExpressions(List* hiddenSpecies);

/*
* identify instances of - x + y within formula and create expressions
*
*/
void detect_minusXPlusYOnly();

void reorderMinusXPlusYIteratively();

private:
/** @cond doxygenLibsbmlInternal */
bool matchesK(SubstitutionValues_t* values1, SubstitutionValues_t* values2);

bool matchesKValue(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesKRealValue(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesXValue(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesYValue(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesVExpression(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesWExpression(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesDxdtExpression(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesDydtExpression(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
bool matchesCurrentNode(SubstitutionValues_t* values1, SubstitutionValues_t* values2);


void substituteParameters(List* hiddenSpecies, SubstitutionValues_t* values);

bool areIdenticalSubstitutionValues(SubstitutionValues_t* values1, SubstitutionValues_t* values2);
SubstitutionValues_t* getSubstitutionValuesByType(ExpressionType_t type, size_t index = 0);

void printSubstitutionValues(const SubstitutionValues_t* values1);

//void printSubstitutionValues(const SubstitutionValues_t* values1);

// functions that represents steps of algo 3.1

void reorderMinusXPlusYIteratively();


void analyse(bool minusXPlusYOnly = false);
bool analyseNode(ASTNode* node, SubstitutionValues_t* value);

/*
* Return the ODE for the given variable
Expand Down Expand Up @@ -199,7 +237,7 @@ class LIBSBML_EXTERN ExpressionAnalyser
/*
* Loops through expressions already recorded and checks for exact matches
*/
bool hasExpressionAlreadyRecorded(SubstitutionValues_t* value);
bool hasExpressionAlreadyBeenRecorded(SubstitutionValues_t* value);

/**
* Searches for a node's parent and its index as the parent's child in a one-directional tree (nodes know their children, but not their parent).
Expand Down Expand Up @@ -235,12 +273,36 @@ class LIBSBML_EXTERN ExpressionAnalyser
*/
bool isParameterAlreadyCreated(std::string& name);

bool isTypeKminusXminusY(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeKminusX(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeKplusVminusX(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeKplusV(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeKplusVminusXminusY(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeKminusXplusWminusY(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

bool isTypeWplusKminusX(unsigned int numChildren, ASTNode* rightChild,
ASTNode* leftChild, ASTNodeType_t type, SubstitutionValues_t* value);

// member variables populated during analysis
pairODEs mODEs;

Model* mModel;

std::vector <SubstitutionValues_t*> mExpressions;

// list of hidden species that are identified during the analysis
List* mHiddenSpecies;

// variables to ensure unique new parameter name

Expand Down
57 changes: 33 additions & 24 deletions src/sbml/conversion/SBMLRateRuleConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ SBMLRateRuleConverter::convert()
}

// Fages algo 3.6 Steps 1-2
populateInitialODEinfo();
populateODEinfo();

if (getMathNotSupportedFlag() == true)
Expand Down Expand Up @@ -835,37 +836,45 @@ SBMLRateRuleConverter::addToTerms(ASTNode* node, bool isToplevel)


void
SBMLRateRuleConverter::populateODEinfo()
SBMLRateRuleConverter::populateInitialODEinfo()
{
Model* model = mDocument->getModel();
Model* model = mDocument->getModel();

// Fages algo 3.6 create set O
// create pairs of variables and their corresponding ODE
// eg. ODEs[0] = [S1, -k1*S1]
// ODES[1] = [S2, k1*S1]
// ODES[2] = [S3, k2*S3]
//
for (unsigned int n = 0; n < model->getNumSpecies(); n++)
{
Species *s = model->getSpecies(n);
if (s->getConstant() == false)
// Fages algo 3.6 create set O
// create pairs of variables and their corresponding ODE
// eg. ODEs[0] = [S1, -k1*S1]
// ODES[1] = [S2, k1*S1]
// ODES[2] = [S3, k2*S3]
//
for (unsigned int n = 0; n < model->getNumSpecies(); n++)
{
addODEPair(s->getId(), model);
Species* s = model->getSpecies(n);
if (s->getConstant() == false)
{
addODEPair(s->getId(), model);
}
}
}
for (unsigned int n = 0; n < model->getNumParameters(); n++)
{
Parameter *p = model->getParameter(n);
if (p->getConstant() == false)
for (unsigned int n = 0; n < model->getNumParameters(); n++)
{
addODEPair(p->getId(), model);
Parameter* p = model->getParameter(n);
if (p->getConstant() == false)
{
addODEPair(p->getId(), model);
}
}
}

for (unsigned int odeIndex = 0; odeIndex < mODEs.size(); odeIndex++)
{
cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
}
for (unsigned int odeIndex = 0; odeIndex < mODEs.size(); odeIndex++)
{
cout << mODEs[odeIndex].first << ": " << SBML_formulaToL3String(mODEs[odeIndex].second) << endl;
}
}


void
SBMLRateRuleConverter::populateODEinfo()
{
Model* model = mDocument->getModel();

// implement Algo 3.1 here (hidden variables!)
// check for hidden variables, and add an appropriate ODE if a hidden variable is found
ExpressionAnalyser *ea = new ExpressionAnalyser(model, mODEs);
Expand Down
6 changes: 4 additions & 2 deletions src/sbml/conversion/SBMLRateRuleConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class LIBSBML_EXTERN SBMLRateRuleConverter : public SBMLConverter
typedef std::vector< std::vector<double> > setRnCoeffs;
typedef std::vector< std::pair< std::string, ASTNode*> >::iterator odeIt;

pairODEs getOde() { return mODEs; };
pairODEs getOdePairs() { return mODEs; };

setCoeff getCoeff() { return mCoefficients; };
setRnCoeffs getReactants() { return mReactants; };
Expand All @@ -200,6 +200,9 @@ class LIBSBML_EXTERN SBMLRateRuleConverter : public SBMLConverter
std::vector< std::vector<bool> > getNegDer() { return mNegDerivative; };
bool getMathNotSupportedFlag() const;

void populateInitialODEinfo();

void populateODEinfo();

private:
/** @cond doxygenLibsbmlInternal */
Expand All @@ -208,7 +211,6 @@ class LIBSBML_EXTERN SBMLRateRuleConverter : public SBMLConverter

bool isDocumentAppropriate(OperationReturnValues_t& returnVal);

void populateODEinfo();

void populateReactionCoefficients();

Expand Down
Loading

0 comments on commit 67b2eef

Please sign in to comment.