Skip to content

Commit

Permalink
GeneratorProfile: added the external array string.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Aug 16, 2024
1 parent b1b7536 commit a46dbca
Show file tree
Hide file tree
Showing 42 changed files with 279 additions and 212 deletions.
19 changes: 19 additions & 0 deletions src/api/libcellml/generatorprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -3122,6 +3122,25 @@ class LIBCELLML_EXPORT GeneratorProfile
*/
void setAlgebraicArrayString(const std::string &algebraicArrayString);

/**
* @brief Get the @c std::string for the name of the external array.
*
* Return the @c std::string for the name of the external array.
*
* @return The @c std::string for the name of the external array.
*/
std::string externalArrayString() const;

/**
* @brief Set the @c std::string for the name of the external array.
*
* Set the @c std::string for the name of the external array.
*
* @param externalArrayString The @c std::string to use for the name of the
* external array.
*/
void setExternalArrayString(const std::string &externalArrayString);

/**
* @brief Get the @c std::string for the type definition of an external
* variable method.
Expand Down
6 changes: 6 additions & 0 deletions src/bindings/interface/generatorprofile.i
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,12 @@ and units of a variable respectively.";
%feature("docstring") libcellml::GeneratorProfile::setAlgebraicArrayString
"Sets the string for the name of the algebraic array.";

%feature("docstring") libcellml::GeneratorProfile::externalArrayString
"Returns the string for the name of the external array.";

%feature("docstring") libcellml::GeneratorProfile::setExternalArrayString
"Sets the string for the name of the external array.";

%feature("docstring") libcellml::GeneratorProfile::externalVariableMethodTypeDefinitionString
"Returns the string for the type definition of an external variable method.";

Expand Down
2 changes: 2 additions & 0 deletions src/bindings/javascript/generatorprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ EMSCRIPTEN_BINDINGS(libcellml_generatorprofile)
.function("setComputedConstantsArrayString", &libcellml::GeneratorProfile::setComputedConstantsArrayString)
.function("algebraicArrayString", &libcellml::GeneratorProfile::algebraicArrayString)
.function("setAlgebraicArrayString", &libcellml::GeneratorProfile::setAlgebraicArrayString)
.function("externalArrayString", &libcellml::GeneratorProfile::externalArrayString)
.function("setExternalArrayString", &libcellml::GeneratorProfile::setExternalArrayString)
.function("externalVariableMethodTypeDefinitionString", &libcellml::GeneratorProfile::externalVariableMethodTypeDefinitionString)
.function("setExternalVariableMethodTypeDefinitionString", &libcellml::GeneratorProfile::setExternalVariableMethodTypeDefinitionString)
.function("externalVariableMethodCallString", &libcellml::GeneratorProfile::externalVariableMethodCallString)
Expand Down
16 changes: 12 additions & 4 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,9 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
mProfile->constantsArrayString() :
(variable->type() == AnalyserVariable::Type::COMPUTED_CONSTANT) ?
mProfile->computedConstantsArrayString() :
mProfile->algebraicArrayString();
(variable->type() == AnalyserVariable::Type::ALGEBRAIC) ?
mProfile->algebraicArrayString() :
mProfile->externalArrayString();

methodBody += mProfile->indentString()
+ arrayString + mProfile->openArrayString() + convertToString(variable->index()) + mProfile->closeArrayString()
Expand Down Expand Up @@ -807,7 +809,9 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
for (const auto &variable : variables) {
auto arrayString = (variable->type() == AnalyserVariable::Type::STATE) ?
mProfile->ratesArrayString() :
mProfile->algebraicArrayString();
(variable->type() == AnalyserVariable::Type::ALGEBRAIC) ?
mProfile->algebraicArrayString() :
mProfile->externalArrayString();

methodBody += mProfile->indentString()
+ mProfile->uArrayString() + mProfile->openArrayString() + convertToString(++i) + mProfile->closeArrayString()
Expand All @@ -831,7 +835,9 @@ void Generator::GeneratorImpl::addNlaSystemsCode()
for (const auto &variable : variables) {
auto arrayString = (variable->type() == AnalyserVariable::Type::STATE) ?
mProfile->ratesArrayString() :
mProfile->algebraicArrayString();
(variable->type() == AnalyserVariable::Type::ALGEBRAIC) ?
mProfile->algebraicArrayString() :
mProfile->externalArrayString();

methodBody += mProfile->indentString()
+ arrayString + mProfile->openArrayString() + convertToString(variable->index()) + mProfile->closeArrayString()
Expand Down Expand Up @@ -914,8 +920,10 @@ std::string Generator::GeneratorImpl::generateVariableNameCode(const VariablePtr
arrayName = mProfile->constantsArrayString();
} else if (analyserVariable->type() == AnalyserVariable::Type::COMPUTED_CONSTANT) {
arrayName = mProfile->computedConstantsArrayString();
} else {
} else if (analyserVariable->type() == AnalyserVariable::Type::ALGEBRAIC) {
arrayName = mProfile->algebraicArrayString();
} else {
arrayName = mProfile->externalArrayString();
}

return arrayName + mProfile->openArrayString() + convertToString(analyserVariable->index()) + mProfile->closeArrayString();
Expand Down
12 changes: 12 additions & 0 deletions src/generatorprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ void GeneratorProfile::GeneratorProfileImpl::loadProfile(GeneratorProfile::Profi
mConstantsArrayString = "constants";
mComputedConstantsArrayString = "computedConstants";
mAlgebraicArrayString = "algebraic";
mExternalArrayString = "external";

mExternalVariableMethodTypeDefinitionFamString = "typedef double (* ExternalVariable)(double *variables, size_t index);\n";
mExternalVariableMethodTypeDefinitionFdmString = "typedef double (* ExternalVariable)(double voi, double *states, double *rates, double *variables, size_t index);\n";
Expand Down Expand Up @@ -740,6 +741,7 @@ void GeneratorProfile::GeneratorProfileImpl::loadProfile(GeneratorProfile::Profi
mConstantsArrayString = "constants";
mComputedConstantsArrayString = "computed_constants";
mAlgebraicArrayString = "algebraic";
mExternalArrayString = "external";

mExternalVariableMethodTypeDefinitionFamString = "";
mExternalVariableMethodTypeDefinitionFdmString = "";
Expand Down Expand Up @@ -2288,6 +2290,16 @@ void GeneratorProfile::setAlgebraicArrayString(const std::string &algebraicArray
mPimpl->mAlgebraicArrayString = algebraicArrayString;
}

std::string GeneratorProfile::externalArrayString() const
{
return mPimpl->mExternalArrayString;
}

void GeneratorProfile::setExternalArrayString(const std::string &externalArrayString)
{
mPimpl->mExternalArrayString = externalArrayString;
}

std::string GeneratorProfile::externalVariableMethodTypeDefinitionString(bool forDifferentialModel) const
{
if (forDifferentialModel) {
Expand Down
1 change: 1 addition & 0 deletions src/generatorprofile_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ struct GeneratorProfile::GeneratorProfileImpl
std::string mConstantsArrayString;
std::string mComputedConstantsArrayString;
std::string mAlgebraicArrayString;
std::string mExternalArrayString;

std::string mExternalVariableMethodTypeDefinitionFamString;
std::string mExternalVariableMethodTypeDefinitionFdmString;
Expand Down
4 changes: 2 additions & 2 deletions src/generatorprofilesha1values.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace libcellml {
* The content of this file is generated, do not edit this file directly.
* See docs/dev_utilities.rst for further information.
*/
static const char C_GENERATOR_PROFILE_SHA1[] = "2d4d14df0a454d95e32e1172503d529834c42588";
static const char PYTHON_GENERATOR_PROFILE_SHA1[] = "5ff1e7cc237b3e7eb1153b6d421df842731c6394";
static const char C_GENERATOR_PROFILE_SHA1[] = "54ee603459d7878ee469890e77de4f3dbb5452ad";
static const char PYTHON_GENERATOR_PROFILE_SHA1[] = "bf42c06955e401096d76d059a28ed1a5cb1ccc01";

} // namespace libcellml
3 changes: 2 additions & 1 deletion src/generatorprofiletools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ std::string generatorProfileAsString(const GeneratorProfilePtr &generatorProfile
+ generatorProfile->ratesArrayString()
+ generatorProfile->constantsArrayString()
+ generatorProfile->computedConstantsArrayString()
+ generatorProfile->algebraicArrayString();
+ generatorProfile->algebraicArrayString()
+ generatorProfile->externalArrayString();

profileContents += generatorProfile->externalVariableMethodTypeDefinitionString(false)
+ generatorProfile->externalVariableMethodTypeDefinitionString(true);
Expand Down
2 changes: 1 addition & 1 deletion tests/bindings/javascript/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("Generator tests", () => {
g.setModel(a.model())

const interface_lines = g.interfaceCode().split('\n')
expect(interface_lines.length).toBe(37)
expect(interface_lines.length).toBe(38)

const implementation_lines = g.implementationCode().split('\n')
expect(implementation_lines.length).toBe(97)
Expand Down
6 changes: 6 additions & 0 deletions tests/bindings/javascript/generatorprofile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,12 @@ describe("GeneratorProfile tests", () => {
x.setAlgebraicArrayString("something")
expect(x.algebraicArrayString()).toBe("something")
});
test("Checking GeneratorProfile.externalArrayString.", () => {
const x = new libcellml.GeneratorProfile(libcellml.GeneratorProfile.Profile.C)

x.setExternalArrayString("something")
expect(x.externalArrayString()).toBe("something")
});
test("Checking GeneratorProfile.externalVariableMethodTypeDefinitionString.", () => {
const x = new libcellml.GeneratorProfile(libcellml.GeneratorProfile.Profile.C)

Expand Down
9 changes: 9 additions & 0 deletions tests/bindings/python/test_generator_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,15 @@ def test_algebraic_array_string(self):
g.setAlgebraicArrayString(GeneratorProfileTestCase.VALUE)
self.assertEqual(GeneratorProfileTestCase.VALUE, g.algebraicArrayString())

def test_external_array_string(self):
from libcellml import GeneratorProfile

g = GeneratorProfile()

self.assertEqual('external', g.externalArrayString())
g.setExternalArrayString(GeneratorProfileTestCase.VALUE)
self.assertEqual(GeneratorProfileTestCase.VALUE, g.externalArrayString())

def test_external_variable_method_type_definition_string(self):
from libcellml import GeneratorProfile

Expand Down
3 changes: 3 additions & 0 deletions tests/generator/generatorprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ TEST(GeneratorProfile, defaultMiscellaneousValues)
EXPECT_EQ("constants", generatorProfile->constantsArrayString());
EXPECT_EQ("computedConstants", generatorProfile->computedConstantsArrayString());
EXPECT_EQ("algebraic", generatorProfile->algebraicArrayString());
EXPECT_EQ("external", generatorProfile->externalArrayString());

EXPECT_EQ("typedef double (* ExternalVariable)(double *variables, size_t index);\n", generatorProfile->externalVariableMethodTypeDefinitionString(false));
EXPECT_EQ("typedef double (* ExternalVariable)(double voi, double *states, double *rates, double *variables, size_t index);\n", generatorProfile->externalVariableMethodTypeDefinitionString(true));
Expand Down Expand Up @@ -938,6 +939,7 @@ TEST(GeneratorProfile, miscellaneous)
generatorProfile->setConstantsArrayString(value);
generatorProfile->setComputedConstantsArrayString(value);
generatorProfile->setAlgebraicArrayString(value);
generatorProfile->setExternalArrayString(value);

generatorProfile->setExternalVariableMethodTypeDefinitionString(false, value);
generatorProfile->setExternalVariableMethodTypeDefinitionString(true, value);
Expand Down Expand Up @@ -1084,6 +1086,7 @@ TEST(GeneratorProfile, miscellaneous)
EXPECT_EQ(value, generatorProfile->constantsArrayString());
EXPECT_EQ(value, generatorProfile->computedConstantsArrayString());
EXPECT_EQ(value, generatorProfile->algebraicArrayString());
EXPECT_EQ(value, generatorProfile->externalArrayString());

EXPECT_EQ(value, generatorProfile->externalVariableMethodTypeDefinitionString(false));
EXPECT_EQ(value, generatorProfile->externalVariableMethodTypeDefinitionString(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void deleteArray(double *array)

void initialiseVariables(double *constants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[1] = externalVariable(variables, 1);
external[1] = externalVariable(variables, 1);
}

void computeComputedConstants(double *constants, double *computedConstants)
Expand All @@ -87,6 +87,6 @@ void computeComputedConstants(double *constants, double *computedConstants)

void computeVariables(double *constants, double *computedConstants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[1] = externalVariable(variables, 1);
algebraic[0] = algebraic[1];
external[1] = externalVariable(variables, 1);
algebraic[0] = external[1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def create_externals_array():


def initialise_variables(constants, algebraic, external_variable):
algebraic[1] = external_variable(variables, 1)
external[1] = external_variable(variables, 1)


def compute_computed_constants(constants, computed_constants):
pass


def compute_variables(constants, computed_constants, algebraic, external_variable):
algebraic[1] = external_variable(variables, 1)
algebraic[0] = algebraic[1]
external[1] = external_variable(variables, 1)
algebraic[0] = external[1]
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void deleteArray(double *array)

void initialiseVariables(double *constants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[0] = externalVariable(variables, 0);
external[0] = externalVariable(variables, 0);
}

void computeComputedConstants(double *constants, double *computedConstants)
Expand All @@ -92,5 +92,5 @@ void computeComputedConstants(double *constants, double *computedConstants)

void computeVariables(double *constants, double *computedConstants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[0] = externalVariable(variables, 0);
external[0] = externalVariable(variables, 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def create_externals_array():


def initialise_variables(constants, algebraic, external_variable):
algebraic[0] = external_variable(variables, 0)
external[0] = external_variable(variables, 0)


def compute_computed_constants(constants, computed_constants):
Expand All @@ -56,4 +56,4 @@ def compute_computed_constants(constants, computed_constants):


def compute_variables(constants, computed_constants, algebraic, external_variable):
algebraic[0] = external_variable(variables, 0)
external[0] = external_variable(variables, 0)
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ void deleteArray(double *array)

void initialiseVariables(double *constants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[0] = externalVariable(variables, 0);
algebraic[1] = externalVariable(variables, 1);
algebraic[2] = externalVariable(variables, 2);
external[0] = externalVariable(variables, 0);
external[1] = externalVariable(variables, 1);
external[2] = externalVariable(variables, 2);
}

void computeComputedConstants(double *constants, double *computedConstants)
Expand All @@ -90,7 +90,7 @@ void computeComputedConstants(double *constants, double *computedConstants)

void computeVariables(double *constants, double *computedConstants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[0] = externalVariable(variables, 0);
algebraic[1] = externalVariable(variables, 1);
algebraic[2] = externalVariable(variables, 2);
external[0] = externalVariable(variables, 0);
external[1] = externalVariable(variables, 1);
external[2] = externalVariable(variables, 2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def create_externals_array():


def initialise_variables(constants, algebraic, external_variable):
algebraic[0] = external_variable(variables, 0)
algebraic[1] = external_variable(variables, 1)
algebraic[2] = external_variable(variables, 2)
external[0] = external_variable(variables, 0)
external[1] = external_variable(variables, 1)
external[2] = external_variable(variables, 2)


def compute_computed_constants(constants, computed_constants):
pass


def compute_variables(constants, computed_constants, algebraic, external_variable):
algebraic[0] = external_variable(variables, 0)
algebraic[1] = external_variable(variables, 1)
algebraic[2] = external_variable(variables, 2)
external[0] = external_variable(variables, 0)
external[1] = external_variable(variables, 1)
external[2] = external_variable(variables, 2)
10 changes: 5 additions & 5 deletions tests/resources/generator/cell_geometry_model/model.external.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void deleteArray(double *array)

void initialiseVariables(double *constants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[1] = externalVariable(variables, 1);
algebraic[2] = externalVariable(variables, 2);
external[1] = externalVariable(variables, 1);
external[2] = externalVariable(variables, 2);
}

void computeComputedConstants(double *constants, double *computedConstants)
Expand All @@ -90,8 +90,8 @@ void computeComputedConstants(double *constants, double *computedConstants)

void computeVariables(double *constants, double *computedConstants, double *algebraic, ExternalVariable externalVariable)
{
algebraic[1] = externalVariable(variables, 1);
algebraic[2] = externalVariable(variables, 2);
algebraic[0] = 1000.0*3.14*algebraic[2]*algebraic[2]*algebraic[1];
external[1] = externalVariable(variables, 1);
external[2] = externalVariable(variables, 2);
algebraic[0] = 1000.0*3.14*external[2]*external[2]*external[1];
algebraic[3] = 0.02*algebraic[0];
}
10 changes: 5 additions & 5 deletions tests/resources/generator/cell_geometry_model/model.external.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ def create_externals_array():


def initialise_variables(constants, algebraic, external_variable):
algebraic[1] = external_variable(variables, 1)
algebraic[2] = external_variable(variables, 2)
external[1] = external_variable(variables, 1)
external[2] = external_variable(variables, 2)


def compute_computed_constants(constants, computed_constants):
pass


def compute_variables(constants, computed_constants, algebraic, external_variable):
algebraic[1] = external_variable(variables, 1)
algebraic[2] = external_variable(variables, 2)
algebraic[0] = 1000.0*3.14*algebraic[2]*algebraic[2]*algebraic[1]
external[1] = external_variable(variables, 1)
external[2] = external_variable(variables, 2)
algebraic[0] = 1000.0*3.14*external[2]*external[2]*external[1]
algebraic[3] = 0.02*algebraic[0]
Loading

0 comments on commit a46dbca

Please sign in to comment.