From 98632663b23a253895e630b9d19a8a7994d3db98 Mon Sep 17 00:00:00 2001 From: Goran Jelic-Cizmek Date: Tue, 3 Dec 2024 14:36:24 +0100 Subject: [PATCH] Fix units in CVODE with an improved method --- src/visitors/cvode_visitor.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/visitors/cvode_visitor.cpp b/src/visitors/cvode_visitor.cpp index 13ef1e819..b4d23d8a5 100644 --- a/src/visitors/cvode_visitor.cpp +++ b/src/visitors/cvode_visitor.cpp @@ -13,7 +13,6 @@ #include "utils/logger.hpp" #include "visitors/visitor_utils.hpp" #include -#include #include namespace pywrap = nmodl::pybind_wrappers; @@ -38,18 +37,14 @@ static void remove_conserve_statements(ast::StatementBlock& node) { // remove units from CVODE block so sympy can parse it properly static void remove_units(ast::BinaryExpression& node) { - // matches either an int or a float, followed by any (including zero) - // number of spaces, followed by an expression in parentheses, that only - // has letters of the alphabet - std::regex unit_pattern(R"((\d+\.?\d*|\.\d+)\s*\([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")); + auto statement = + fmt::format("{} = {}", + to_nmodl(node.get_lhs()), + to_nmodl(node.get_rhs(), {ast::AstNodeType::UNIT, ast::AstNodeType::UNIT_DEF})); logger->debug("CvodeVisitor :: removing units from statement {}", to_nmodl(node)); - logger->debug("CvodeVisitor :: result: {}", rhs_string_no_units); + logger->debug("CvodeVisitor :: result: {}", statement); auto expr_statement = std::dynamic_pointer_cast( - create_statement(rhs_string_no_units)); + create_statement(statement)); const auto bin_expr = std::dynamic_pointer_cast( expr_statement->get_expression()); node.set_rhs(std::shared_ptr(bin_expr->get_rhs()->clone()));