Skip to content

Commit

Permalink
Remove unitless printers
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbabu committed Jan 7, 2025
1 parent 61a890c commit 75b2492
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pynestml/codegeneration/nest_compartmental_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from jinja2 import TemplateRuntimeError

from odetoolbox import analysis
from pynestml.codegeneration.printers.sympy_simple_expression_printer import SympySimpleExpressionPrinter

import pynestml
from pynestml.cocos.co_cos_manager import CoCosManager
Expand Down Expand Up @@ -180,7 +181,7 @@ def setup_printers(self):
self._ode_toolbox_variable_printer = ODEToolboxVariablePrinter(None)
self._ode_toolbox_function_call_printer = ODEToolboxFunctionCallPrinter(None)
self._ode_toolbox_printer = ODEToolboxExpressionPrinter(
simple_expression_printer=CppSimpleExpressionPrinter(
simple_expression_printer=SympySimpleExpressionPrinter(
variable_printer=self._ode_toolbox_variable_printer,
constant_printer=self._constant_printer,
function_call_printer=self._ode_toolbox_function_call_printer))
Expand Down
17 changes: 15 additions & 2 deletions pynestml/codegeneration/printers/gsl_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
#
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from pynestml.codegeneration.nest_unit_converter import NESTUnitConverter
from pynestml.codegeneration.printers.cpp_variable_printer import CppVariablePrinter
from pynestml.meta_model.ast_variable import ASTVariable
from pynestml.symbols.predefined_units import PredefinedUnits
from pynestml.symbols.symbol import SymbolKind
from pynestml.utils.ast_utils import ASTUtils
from pynestml.utils.logger import Logger, LoggingLevel
from pynestml.utils.messages import Messages


class GSLVariablePrinter(CppVariablePrinter):
r"""
Reference converter for C++ syntax and using the GSL (GNU Scientific Library) API from inside the ``extern "C"`` stepping function.
Variable printer for C++ syntax and using the GSL (GNU Scientific Library) API from inside the ``extern "C"`` stepping function.
"""

def print_variable(self, node: ASTVariable) -> str:
Expand All @@ -39,6 +42,16 @@ def print_variable(self, node: ASTVariable) -> str:
assert isinstance(node, ASTVariable)
symbol = node.get_scope().resolve_to_symbol(node.get_complete_name(), SymbolKind.VARIABLE)

if symbol is None:
# test if variable name can be resolved to a type
if PredefinedUnits.is_unit(node.get_complete_name()):
return str(NESTUnitConverter.get_factor(PredefinedUnits.get_unit(node.get_complete_name()).get_unit()))

code, message = Messages.get_could_not_resolve(node.get_name())
Logger.log_message(log_level=LoggingLevel.ERROR, code=code, message=message,
error_position=node.get_source_position())
return ""

if node.is_delay_variable():
return self._print_delay_variable(node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from pynestml.codegeneration.nest_unit_converter import NESTUnitConverter
from pynestml.codegeneration.printers.cpp_simple_expression_printer import CppSimpleExpressionPrinter
from pynestml.codegeneration.printers.simple_expression_printer import SimpleExpressionPrinter
from pynestml.meta_model.ast_function_call import ASTFunctionCall
from pynestml.meta_model.ast_node import ASTNode
Expand All @@ -29,7 +30,7 @@
from pynestml.symbols.symbol import SymbolKind


class SympySimpleExpressionPrinter(SimpleExpressionPrinter):
class SympySimpleExpressionPrinter(CppSimpleExpressionPrinter):
r"""
Printer for ASTSimpleExpressions in Sympy syntax.
"""
Expand Down
4 changes: 3 additions & 1 deletion pynestml/utils/mechanism_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import copy

from pynestml.codegeneration.printers.sympy_simple_expression_printer import SympySimpleExpressionPrinter

from pynestml.codegeneration.printers.cpp_simple_expression_printer import CppSimpleExpressionPrinter
from pynestml.codegeneration.printers.nestml_printer import NESTMLPrinter
from pynestml.codegeneration.printers.constant_printer import ConstantPrinter
Expand Down Expand Up @@ -56,7 +58,7 @@ class MechanismProcessing:
_ode_toolbox_variable_printer = ODEToolboxVariablePrinter(None)
_ode_toolbox_function_call_printer = ODEToolboxFunctionCallPrinter(None)
_ode_toolbox_printer = ODEToolboxExpressionPrinter(
simple_expression_printer=CppSimpleExpressionPrinter(
simple_expression_printer=SympySimpleExpressionPrinter(
variable_printer=_ode_toolbox_variable_printer,
constant_printer=_constant_printer,
function_call_printer=_ode_toolbox_function_call_printer))
Expand Down

0 comments on commit 75b2492

Please sign in to comment.