Skip to content

Commit

Permalink
add timestep() predefined function akin to resolution()
Browse files Browse the repository at this point in the history
  • Loading branch information
C.A.P. Linssen committed Sep 22, 2024
1 parent ed55a2c commit cf0363f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 18 deletions.
20 changes: 8 additions & 12 deletions pynestml/cocos/co_co_timestep_function_legally_used.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from pynestml.cocos.co_co import CoCo
from pynestml.meta_model.ast_block_with_variables import ASTBlockWithVariables
from pynestml.meta_model.ast_equations_block import ASTEquationsBlock
from pynestml.meta_model.ast_function import ASTFunction
from pynestml.meta_model.ast_model import ASTModel
from pynestml.meta_model.ast_on_condition_block import ASTOnConditionBlock
from pynestml.meta_model.ast_on_receive_block import ASTOnReceiveBlock
from pynestml.meta_model.ast_simple_expression import ASTSimpleExpression
from pynestml.meta_model.ast_update_block import ASTUpdateBlock
from pynestml.symbols.predefined_functions import PredefinedFunctions
from pynestml.utils.logger import LoggingLevel, Logger
from pynestml.utils.messages import Messages
from pynestml.visitors.ast_visitor import ASTVisitor
from pynestml.symbols.predefined_functions import PredefinedFunctions


class CoCoTimestepFuncLegallyUsed(CoCo):
Expand Down Expand Up @@ -68,12 +64,12 @@ def visit_simple_expression(self, node):
while _node:
_node = _node.get_parent()

if isinstance(_node, ASTEquationsBlock) \
or isinstance(_node, ASTBlockWithVariables) \
or isinstance(_node, ASTOnReceiveBlock) \
or isinstance(_node, ASTOnConditionBlock) \
or isinstance(_node, ASTFunction) \
or isinstance(_node, ASTModel):
if isinstance(_node, ASTUpdateBlock):
# function was used inside an ``update`` block; everything is OK
return

if isinstance(_node, ASTModel):
# we reached the top-level block without running into an ``update`` block on the way --> incorrect usage of the function
code, message = Messages.get_timestep_function_legally_used()
Logger.log_message(code=code, message=message, error_position=node.get_source_position(),
log_level=LoggingLevel.ERROR)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _print_function_call(self, node: ASTFunctionCall) -> str:
"""
function_name = node.get_name()

if function_name == PredefinedFunctions.TIME_RESOLUTION:
if function_name in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
# context dependent; we assume the template contains the necessary definitions
return r'\Delta{}t'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _print_function_call_format_string(self, function_call: ASTFunctionCall) ->
"""
function_name = function_call.get_name()

if function_name == PredefinedFunctions.TIME_RESOLUTION:
if function_name in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
# context dependent; we assume the template contains the necessary definitions
return '__resolution'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _print_function_call_format_string(self, function_call: ASTFunctionCall) ->
if function_call.get_name() == PredefinedFunctions.TIME_STEPS:
return "steps({!s}, self._timestep)"

if function_call.get_name() == PredefinedFunctions.TIME_RESOLUTION:
if function_call.get_name() in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
return "self._timestep"

if function_call.get_name() == PredefinedFunctions.EMIT_SPIKE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _print_function_call_format_string(self, function_call: ASTFunctionCall) ->
if function_call.get_name() == PredefinedFunctions.TIME_STEPS:
return "steps({!s}, node._timestep)"

if function_call.get_name() == PredefinedFunctions.TIME_RESOLUTION:
if function_call.get_name() in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
return "node._timestep"

if function_call.get_name() == PredefinedFunctions.EMIT_SPIKE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def print_function_call(self, node: ASTFunctionCall) -> str:
"""
function_name = node.get_name()

if function_name == PredefinedFunctions.TIME_RESOLUTION:
if function_name in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
# context dependent; we assume the template contains the necessary definitions
return 'parameter->__h'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _print_function_call_format_string(self, function_call: ASTFunctionCall) ->
if function_call.get_name() == PredefinedFunctions.TIME_STEPS:
return " SpynnakerDataView.get_simulation_time_step_per_ms() * {!s}"

if function_call.get_name() == PredefinedFunctions.TIME_RESOLUTION:
if function_call.get_name() in [PredefinedFunctions.TIME_RESOLUTION, PredefinedFunctions.TIME_TIMESTEP]:
return "timestep"

if function_call.get_name() == PredefinedFunctions.EMIT_SPIKE:
Expand Down

0 comments on commit cf0363f

Please sign in to comment.