Skip to content

Commit

Permalink
Generator: specify tracked/untracked variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Oct 17, 2024
1 parent 9acc693 commit b5392e0
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/api/libcellml/analysermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace libcellml {
class LIBCELLML_EXPORT AnalyserModel
{
friend class Analyser;
friend class Generator;

public:
/**
Expand Down
68 changes: 68 additions & 0 deletions src/api/libcellml/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,74 @@ class LIBCELLML_EXPORT Generator
*/
void setProfile(const GeneratorProfilePtr &profile);

/**
* @brief Track all the variables in the given @p model.
*
* Track all the variables in the given @p model. This will add all the variables in the model to the list of
* tracked variables.
*
* @param model The pointer to the @ref AnalyserModel which all the variables are to be tracked.
*
* @return @c true if all the variables in the model were tracked, @c false otherwise.
*/
bool trackAllVariables(const AnalyserModelPtr &model);

/**
* @brief Untrack all the variables in the given @p model.
*
* Untrack all the variables in the given @p model. This will remove all the variables in the model from the list of
* tracked variables.
*
* @param model The pointer to the @ref AnalyserModel which all the variables are to be untracked.
*
* @return @c true if all the variables in the model were untracked, @c false otherwise.
*/
bool untrackAllVariables(const AnalyserModelPtr &model);

/**
* @brief Track the given @p variable.
*
* Track the given @p variable. This will add the variable to the list of tracked variables.
*
* @param variable The pointer to the @ref Variable to track.
*
* @return @c true if the variable was tracked, @c false otherwise.
*/
bool trackVariable(const VariablePtr &variable);

/**
* @brief Untrack the given @p variable.
*
* Untrack the given @p variable. This will remove the variable from the list of tracked variables.
*
* @param variable The pointer to the @ref Variable to untrack.
*
* @return @c true if the variable was untracked, @c false otherwise.
*/
bool untrackVariable(const VariablePtr &variable);

/**
* @brief Get the number of tracked variables in the given @p model.
*
* Get the number of tracked variables in the given @p model.
*
* @param model The pointer to the @ref AnalyserModel for which to get the number of tracked variables.
*
* @return The number of tracked variables in the model.
*/
size_t trackedVariableCount(const AnalyserModelPtr &model);

/**
* @brief Get the number of untracked variables in the given @p model.
*
* Get the number of untracked variables in the given @p model.
*
* @param model The pointer to the @ref AnalyserModel for which to get the number of untracked variables.
*
* @return The number of untracked variables in the model.
*/
size_t untrackedVariableCount(const AnalyserModelPtr &model);

/**
* @brief Get the interface code for the @ref AnalyserModel.
*
Expand Down
110 changes: 110 additions & 0 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ limitations under the License.
#include "libcellml/units.h"
#include "libcellml/version.h"

#include "analysermodel_p.h"
#include "commonutils.h"
#include "generator_p.h"
#include "generatorprofilesha1values.h"
Expand All @@ -42,6 +43,85 @@ void Generator::GeneratorImpl::reset()
mCode = {};
}

bool Generator::GeneratorImpl::doTrackVariable(const ModelPtr &model, const VariablePtr &variable, bool tracked)
{
mTrackedVariables[model][variable] = tracked;

return true;
}

bool Generator::GeneratorImpl::trackVariable(const VariablePtr &variable)
{
if (variable == nullptr) {
return false;
}

return doTrackVariable(owningModel(variable), variable, true);
}

bool Generator::GeneratorImpl::untrackVariable(const VariablePtr &variable)
{
if (variable == nullptr) {
return false;
}

return doTrackVariable(owningModel(variable), variable, false);
}

bool Generator::GeneratorImpl::doTrackAllVariables(const AnalyserModelPtr &model, bool tracked)
{
if (model == nullptr) {
return false;
}

for (const auto &variable : variables(model, true)) {
doTrackVariable(model->mPimpl->mModel, variable->variable(), tracked);
}

return true;
}

bool Generator::GeneratorImpl::trackAllVariables(const AnalyserModelPtr &model)
{
return doTrackAllVariables(model, true);
}

bool Generator::GeneratorImpl::untrackAllVariables(const AnalyserModelPtr &model)
{
return doTrackAllVariables(model, false);
}

size_t Generator::GeneratorImpl::doTrackedVariableCount(const AnalyserModelPtr &model, bool tracked)
{
if (model == nullptr) {
return 0;
}

size_t res = 0;

for (const auto &variable : variables(model, true)) {
if (mTrackedVariables[model->mPimpl->mModel].find(variable->variable()) == mTrackedVariables[model->mPimpl->mModel].end()) {
mTrackedVariables[model->mPimpl->mModel][variable->variable()] = true;
}

if (mTrackedVariables[model->mPimpl->mModel][variable->variable()] == tracked) {
++res;
}
}

return res;
}

size_t Generator::GeneratorImpl::trackedVariableCount(const AnalyserModelPtr &model)
{
return doTrackedVariableCount(model, true);
}

size_t Generator::GeneratorImpl::untrackedVariableCount(const AnalyserModelPtr &model)
{
return doTrackedVariableCount(model, false);
}

bool Generator::GeneratorImpl::modelHasOdes(const AnalyserModelPtr &model) const
{
switch (model->type()) {
Expand Down Expand Up @@ -2022,6 +2102,36 @@ void Generator::setProfile(const GeneratorProfilePtr &profile)
mPimpl->mProfile = profile;
}

bool Generator::trackVariable(const VariablePtr &variable)
{
return mPimpl->trackVariable(variable);
}

bool Generator::untrackVariable(const VariablePtr &variable)
{
return mPimpl->untrackVariable(variable);
}

bool Generator::trackAllVariables(const AnalyserModelPtr &model)
{
return mPimpl->trackAllVariables(model);
}

bool Generator::untrackAllVariables(const AnalyserModelPtr &model)
{
return mPimpl->untrackAllVariables(model);
}

size_t Generator::trackedVariableCount(const AnalyserModelPtr &model)
{
return mPimpl->trackedVariableCount(model);
}

size_t Generator::untrackedVariableCount(const AnalyserModelPtr &model)
{
return mPimpl->untrackedVariableCount(model);
}

std::string Generator::interfaceCode(const AnalyserModelPtr &model) const
{
if ((model == nullptr)
Expand Down
17 changes: 17 additions & 0 deletions src/generator_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,25 @@ struct Generator::GeneratorImpl

GeneratorProfilePtr mProfile = GeneratorProfile::create();

std::map<ModelPtr, std::map<VariablePtr, bool>> mTrackedVariables;

void reset();

bool doTrackVariable(const ModelPtr &model, const VariablePtr &variable, bool tracked);

bool trackVariable(const VariablePtr &variable);
bool untrackVariable(const VariablePtr &variable);

bool doTrackAllVariables(const AnalyserModelPtr &model, bool tracked);

bool trackAllVariables(const AnalyserModelPtr &model);
bool untrackAllVariables(const AnalyserModelPtr &model);

size_t doTrackedVariableCount(const AnalyserModelPtr &model, bool tracked);

size_t trackedVariableCount(const AnalyserModelPtr &model);
size_t untrackedVariableCount(const AnalyserModelPtr &model);

bool modelHasOdes(const AnalyserModelPtr &model) const;
bool modelHasNlas(const AnalyserModelPtr &model) const;

Expand Down
14 changes: 9 additions & 5 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,21 +1317,25 @@ XmlNodePtr mathmlChildNode(const XmlNodePtr &node, size_t index)
return res;
}

std::vector<AnalyserVariablePtr> variables(const AnalyserModelPtr &model)
std::vector<AnalyserVariablePtr> variables(const AnalyserModelPtr &model, bool onlyUntrackableVariables)
{
std::vector<AnalyserVariablePtr> res;

if (model->voi() != nullptr) {
res.push_back(model->voi());
if (!onlyUntrackableVariables) {
if (model->voi() != nullptr) {
res.push_back(model->voi());
}

auto states = model->states();

res.insert(res.end(), states.begin(), states.end());
}

auto states = model->states();
auto constants = model->constants();
auto computedConstants = model->computedConstants();
auto algebraic = model->algebraic();
auto externals = model->externals();

res.insert(res.end(), states.begin(), states.end());
res.insert(res.end(), constants.begin(), constants.end());
res.insert(res.end(), computedConstants.begin(), computedConstants.end());
res.insert(res.end(), algebraic.begin(), algebraic.end());
Expand Down
3 changes: 2 additions & 1 deletion src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,11 @@ XmlNodePtr mathmlChildNode(const XmlNodePtr &node, size_t index);
* Return the variables in the given model.
*
* @param model The model for which we want the variables.
* @param onlyUntrackableVariables If @c true, only return untrackable variables.
*
* @return The variables in the given model.
*/
std::vector<AnalyserVariablePtr> variables(const AnalyserModelPtr &model);
std::vector<AnalyserVariablePtr> variables(const AnalyserModelPtr &model, bool onlyUntrackableVariables = false);

/**
* @brief Return the variables in the given equation.
Expand Down
Loading

0 comments on commit b5392e0

Please sign in to comment.