Skip to content

Commit

Permalink
add pi predefined constant (#1075)
Browse files Browse the repository at this point in the history
Co-authored-by: C.A.P. Linssen <charl@turingbirds.com>
  • Loading branch information
clinssen and C.A.P. Linssen authored Jul 10, 2024
1 parent 761ba33 commit 6e2b725
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 51 deletions.
2 changes: 2 additions & 0 deletions doc/nestml_language/nestml_language_concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ The following variables and constants are predefined in NESTML and can be used o
- The current simulation time (read only)
* - ``e``
- Euler's constant (2.718...)
* - ``pi``
- pi (3.14159...)
* - ``inf``
- Floating point infinity

Expand Down
41 changes: 0 additions & 41 deletions pynestml/codegeneration/printers/c_variable_printer.py

This file was deleted.

5 changes: 4 additions & 1 deletion pynestml/codegeneration/printers/cpp_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def print_variable(self, node: ASTVariable) -> str:
assert isinstance(node, ASTVariable)

if node.get_name() == PredefinedVariables.E_CONSTANT:
return "numerics::e"
return "2.718281828459045235360287471352" # not defined in C++11 stdlib

if node.get_name() == PredefinedVariables.E_CONSTANT:
return "M_PI" # from <cmath>

return CppVariablePrinter._print_cpp_name(node.get_complete_name())
5 changes: 4 additions & 1 deletion pynestml/codegeneration/printers/nest_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def print_variable(self, variable: ASTVariable) -> str:
return "((post_neuron_t*)(__target))->get_" + _name + "(_tr_t)"

if variable.get_name() == PredefinedVariables.E_CONSTANT:
return "numerics::e"
return "numerics::e" # from nest::

if variable.get_name() == PredefinedVariables.PI_CONSTANT:
return "numerics::pi" # from nest::

if variable.get_name() == PredefinedVariables.TIME_CONSTANT:
return "get_t()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def print_variable(self, node: ASTVariable) -> str:
if node.get_name() == PredefinedVariables.E_CONSTANT:
return "np.e"

if node.get_name() == PredefinedVariables.PI_CONSTANT:
return "np.pi"

symbol = node.get_scope().resolve_to_symbol(node.get_complete_name(), SymbolKind.VARIABLE)

if symbol.is_state() and not symbol.is_inline_expression:
Expand Down
3 changes: 3 additions & 0 deletions pynestml/codegeneration/printers/python_variable_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def print_variable(self, variable: ASTVariable) -> str:
if variable.get_name() == PredefinedVariables.E_CONSTANT:
return "math.e"

if variable.get_name() == PredefinedVariables.PI_CONSTANT:
return "math.pi"

symbol = variable.get_scope().resolve_to_symbol(variable.get_complete_name(), SymbolKind.VARIABLE)
if symbol is None:
# test if variable name can be resolved to a type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def print_variable(self, variable: ASTVariable) -> str:
if variable.get_name() == PredefinedVariables.E_CONSTANT:
return "REAL_CONST(2.718282)"

if variable.get_name() == PredefinedVariables.PI_CONSTANT:
return "REAL_CONST(3.14159)"

symbol = variable.get_scope().resolve_to_symbol(variable.get_complete_name(), SymbolKind.VARIABLE)
if symbol is None:
# test if variable name can be resolved to a type
Expand Down
25 changes: 17 additions & 8 deletions pynestml/symbols/predefined_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class PredefinedVariables:
"""
This class is used to store all predefined variables as generally available.
"""
name2variable = {} # type: Mapping[str, VariableSymbol]
E_CONSTANT = 'e' # type: str
TIME_CONSTANT = 't' # type: str
name2variable: Mapping[str, VariableSymbol] = {}
E_CONSTANT: str = 'e'
PI_CONSTANT: str = 'pi'
TIME_CONSTANT: str = 't'

@classmethod
def register_variables(cls):
Expand All @@ -40,6 +41,7 @@ def register_variables(cls):
"""
cls.name2variable = {}
cls.__register_euler_constant()
cls.__register_pi_constant()
cls.__register_time_constant()

@classmethod
Expand All @@ -53,7 +55,6 @@ def __register_predefined_type_variables(cls):
type_symbol=PredefinedTypes.get_type(name),
variable_type=VariableType.TYPE)
cls.name2variable[name] = symbol
return

@classmethod
def __register_euler_constant(cls):
Expand All @@ -64,7 +65,16 @@ def __register_euler_constant(cls):
is_predefined=True, type_symbol=PredefinedTypes.get_real_type(),
variable_type=VariableType.VARIABLE)
cls.name2variable[cls.E_CONSTANT] = symbol
return

@classmethod
def __register_pi_constant(cls):
"""
Adds the pi constant.
"""
symbol = VariableSymbol(name='pi', block_type=BlockType.STATE,
is_predefined=True, type_symbol=PredefinedTypes.get_real_type(),
variable_type=VariableType.VARIABLE)
cls.name2variable[cls.PI_CONSTANT] = symbol

@classmethod
def __register_time_constant(cls):
Expand All @@ -75,7 +85,6 @@ def __register_time_constant(cls):
is_predefined=True, type_symbol=PredefinedTypes.get_type('ms'),
variable_type=VariableType.VARIABLE)
cls.name2variable[cls.TIME_CONSTANT] = symbol
return

@classmethod
def get_time_constant(cls):
Expand Down Expand Up @@ -106,8 +115,8 @@ def get_variable(cls, name):
"""
if name in cls.name2variable.keys():
return cls.name2variable[name]
else:
return None

return None

@classmethod
def get_variables(cls):
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/ExpressionCollection.nestml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ model ExpressionCollection:
testerf real = erf(0.)
testerfc real = erfc(0.)

test_e real = e
test_pi real = pi

equations:
#neuron aeif_cond_alpha_neuron
test0 = E_L
Expand Down

0 comments on commit 6e2b725

Please sign in to comment.