Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #148 NaNs in boolean formulas (also in If-Formulas and MinMax-Formulas) #154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 72 additions & 63 deletions src/OSPSuite.SimModelNative/include/SimModel/BooleanFormula.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _BooleanFormula_H_
#define _BooleanFormula_H_

#include <functional>

#include "SimModel/Formula.h"

namespace SimModelNative
Expand All @@ -10,151 +12,158 @@ class BooleanFormula :
public Formula
{
protected:
Formula * m_SecondOperandFormula;
Formula * m_FirstOperandFormula;
Formula * m_SecondOperandFormula;
Formula * m_FirstOperandFormula;

std::vector <double> SwitchTimePointFromComparisonFormula();
std::vector <double> SwitchTimePointFromComparisonFormula() const;

//this function is called by all logical formulas with 2 arguments
// it checks if one of the arguments is NaN and returns NaN in such a case;
// otherwise it performs the logical operation passed as the last argument
double calculate_binaryOperation(const double* y, const double time, ScaleFactorUsageMode scaleFactorMode, const std::function<double(double,double)> & logicalOperation) const;

//same as calculate_binaryOperation, but for logical formulas with ONE argument
double calculate_unaryOperation(const double* y, const double time, ScaleFactorUsageMode scaleFactorMode, const std::function<double(double)>& logicalOperation) const;

public:
BooleanFormula ();
virtual ~BooleanFormula ();
BooleanFormula ();
virtual ~BooleanFormula ();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to reformat the whole SimModel code once to have the proper indentation...
Will do it after 11.1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please. Let's createa an issue for this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done #155


virtual void LoadFromXMLNode (const XMLNode & pNode);
virtual void XMLFinalizeInstance (const XMLNode & pNode, Simulation * sim);
virtual void SetQuantityReference (const QuantityReference & quantityReference);
virtual void DE_Jacobian (double * * jacobian, const double * y, const double time, const int iEquation, const double preFactor);
virtual Formula * DE_Jacobian(const int iEquation);
virtual Formula * clone() = 0;
virtual Formula * RecursiveSimplify();
void setFormula(Formula* FirstOperandFormula, Formula* SecondOperandFormula);
virtual void LoadFromXMLNode (const XMLNode & pNode);
virtual void XMLFinalizeInstance (const XMLNode & pNode, Simulation * sim);
virtual void SetQuantityReference (const QuantityReference & quantityReference);
virtual void DE_Jacobian (double * * jacobian, const double * y, const double time, const int iEquation, const double preFactor);
virtual Formula * DE_Jacobian(const int iEquation);
virtual Formula * clone() = 0;
virtual Formula * RecursiveSimplify();
void setFormula(Formula* FirstOperandFormula, Formula* SecondOperandFormula);

virtual void Finalize();
virtual void Finalize();

virtual std::vector <double> SwitchTimePoints();
virtual std::vector <double> SwitchTimePoints();

virtual bool IsZero(void);
virtual bool IsZero(void);

virtual void AppendUsedVariables(std::set<int> & usedVariablesIndices, const std::set<int> & variablesIndicesUsedInSwitchAssignments);
virtual void AppendUsedParameters(std::set<int> & usedParameterIDs);
void SwitchFormulaFromComparisonFormula(std::vector<Formula*> &vecExplicit, std::vector<Formula*> &vecImplicit);
virtual void AppendUsedVariables(std::set<int> & usedVariablesIndices, const std::set<int> & variablesIndicesUsedInSwitchAssignments);
virtual void AppendUsedParameters(std::set<int> & usedParameterIDs);
void SwitchFormulaFromComparisonFormula(std::vector<Formula*> &vecExplicit, std::vector<Formula*> &vecImplicit) const;

virtual void UpdateIndicesOfReferencedVariables();
virtual void UpdateIndicesOfReferencedVariables();
};

class AndFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual bool IsZero(void); //special treatment for the AND-formula
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual bool IsZero(void); //special treatment for the AND-formula

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class EqualFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class GreaterEqualFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class GreaterFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class LessEqualFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class LessFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class NotFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class OrFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

class UnequalFormula :
public BooleanFormula
{
public:
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();
virtual double DE_Compute (const double * y, const double time, ScaleFactorUsageMode scaleFactorMode);
virtual Formula* clone();
virtual std::vector <double> SwitchTimePoints();

protected:
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
virtual void WriteFormulaMatlabCode (std::ostream & mrOut);
virtual void WriteFormulaCppCode (std::ostream & mrOut);
};

}//.. end "namespace SimModelNative"
Expand Down
Loading