Skip to content

Commit

Permalink
Use regex to remove units
Browse files Browse the repository at this point in the history
  • Loading branch information
JCGoran committed Dec 2, 2024
1 parent a851a1b commit 8a2cd24
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/visitors/cvode_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utils/logger.hpp"
#include "visitors/visitor_utils.hpp"
#include <optional>
#include <regex>
#include <utility>

namespace pywrap = nmodl::pybind_wrappers;
Expand All @@ -35,6 +36,22 @@ static void remove_conserve_statements(ast::StatementBlock& node) {
}
}

// remove units using state-of-the-art technology
static void remove_units(ast::BinaryExpression& node) {
std::regex unit_pattern(R"((\d*\.?\d+)\([a-zA-Z]+\))");
auto rhs_string = to_nmodl(node.get_rhs());
auto rhs_string_no_units = fmt::format("{} = {}",
to_nmodl(node.get_lhs()),
std::regex_replace(rhs_string, unit_pattern, "$1"));
logger->debug("CvodeVisitor :: removing units from statement {}", to_nmodl(node));
logger->debug("CvodeVisitor :: result: {}", rhs_string_no_units);
auto expr_statement = std::dynamic_pointer_cast<ast::ExpressionStatement>(
create_statement(rhs_string_no_units));
const auto bin_expr = std::dynamic_pointer_cast<const ast::BinaryExpression>(
expr_statement->get_expression());
node.set_rhs(std::shared_ptr<ast::Expression>(bin_expr->get_rhs()->clone()));
}

static std::pair<std::string, std::optional<int>> parse_independent_var(
std::shared_ptr<ast::Identifier> node) {
auto variable = std::make_pair(node->get_node_name(), std::optional<int>());
Expand Down Expand Up @@ -152,7 +169,10 @@ class StiffVisitor: public CvodeHelperVisitor {
program_symtab->insert(symbol);
}

remove_units(node);

auto rhs = node.get_rhs();

// all indexed variables (need special treatment in SymPy)
auto indexed_variables = get_indexed_variables(*rhs, name->get_node_name());
auto diff2c = pywrap::EmbeddedPythonLoader::get_instance().api().diff2c;
Expand Down

0 comments on commit 8a2cd24

Please sign in to comment.