Skip to content

Commit 9a70616

Browse files
author
C.A.P. Linssen
committed
change timestep adder to a transformer
1 parent d2f201e commit 9a70616

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed

pynestml/codegeneration/nest_code_generator.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ def analyse_neuron(self, neuron: ASTModel) -> Tuple[Dict[str, ASTAssignment], Di
344344
self.non_equations_state_variables[neuron.get_name()] = []
345345
self.non_equations_state_variables[neuron.get_name()].extend(
346346
ASTUtils.all_variables_defined_in_block(neuron.get_state_blocks()))
347-
ASTUtils.add_timestep_symbol(neuron)
348347

349348
return {}, {}, [], []
350349

@@ -402,7 +401,6 @@ def analyse_neuron(self, neuron: ASTModel) -> Tuple[Dict[str, ASTAssignment], Di
402401
ASTUtils.create_integrate_odes_combinations(neuron)
403402
ASTUtils.replace_variable_names_in_expressions(neuron, [analytic_solver, numeric_solver])
404403
ASTUtils.replace_convolution_aliasing_inlines(neuron)
405-
ASTUtils.add_timestep_symbol(neuron)
406404

407405
if self.analytic_solver[neuron.get_name()] is not None:
408406
neuron = ASTUtils.add_declarations_to_internals(
@@ -448,18 +446,14 @@ def analyse_synapse(self, synapse: ASTModel) -> Dict[str, ASTAssignment]:
448446
ASTUtils.create_initial_values_for_kernels(synapse, [analytic_solver, numeric_solver], kernels)
449447
ASTUtils.create_integrate_odes_combinations(synapse)
450448
ASTUtils.replace_variable_names_in_expressions(synapse, [analytic_solver, numeric_solver])
451-
ASTUtils.add_timestep_symbol(synapse)
452449
self.update_symbol_table(synapse)
453450
spike_updates, _ = self.get_spike_update_expressions(synapse, kernel_buffers, [analytic_solver, numeric_solver], delta_factors)
454451

455452
if not self.analytic_solver[synapse.get_name()] is None:
456453
synapse = ASTUtils.add_declarations_to_internals(
457454
synapse, self.analytic_solver[synapse.get_name()]["propagators"])
458455

459-
self.update_symbol_table(synapse)
460-
else:
461-
ASTUtils.add_timestep_symbol(synapse)
462-
self.update_symbol_table(synapse)
456+
self.update_symbol_table(synapse)
463457

464458
ASTUtils.update_blocktype_for_common_parameters(synapse)
465459

pynestml/codegeneration/nest_compartmental_code_generator.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,6 @@ def analyse_neuron(self, neuron: ASTModel) -> List[ASTAssignment]:
515515
# conventions of ODE-toolbox
516516
ASTUtils.replace_convolution_aliasing_inlines(neuron)
517517

518-
# add variable __h to internals block
519-
ASTUtils.add_timestep_symbol(neuron)
520-
521518
# add propagator variables calculated by odetoolbox into internal blocks
522519
if self.analytic_solver[neuron.get_name()] is not None:
523520
neuron = ASTUtils.add_declarations_to_internals(

pynestml/frontend/pynestml_frontend.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def transformers_from_target_name(target_name: str, options: Optional[Mapping[st
6161
if options is None:
6262
options = {}
6363

64+
if target_name.upper() in ["NEST", "PYTHON_STANDALONE", "NEST_COMPARTMENTAL", "NEST_DESKTOP"]:
65+
from pynestml.transformers.add_timestep_to_internals_transformer import AddTimestepToInternalsTransformer
66+
67+
add_timestep_to_internals_transformer = AddTimestepToInternalsTransformer()
68+
transformers.append(add_timestep_to_internals_transformer)
69+
6470
if target_name.upper() in ["NEST", "SPINNAKER"]:
6571
from pynestml.transformers.illegal_variable_name_transformer import IllegalVariableNameTransformer
6672

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# add_timestep_to_internals_transformer.py
4+
#
5+
# This file is part of NEST.
6+
#
7+
# Copyright (C) 2004 The NEST Initiative
8+
#
9+
# NEST is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 2 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# NEST is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with NEST. If not, see <http://www.gnu.org/licenses/>.
21+
22+
from __future__ import annotations
23+
24+
from typing import Optional, Mapping, Any, Union, Sequence
25+
from pynestml.meta_model.ast_model import ASTModel
26+
27+
from pynestml.meta_model.ast_node import ASTNode
28+
from pynestml.transformers.transformer import Transformer
29+
30+
31+
class AddTimestepToInternalsTransformer(Transformer):
32+
r"""
33+
Add timestep variable to the internals block
34+
"""
35+
36+
def __init__(self, options: Optional[Mapping[str, Any]] = None):
37+
super(Transformer, self).__init__(options)
38+
39+
def transform(self, models: Union[ASTNode, Sequence[ASTNode]]) -> Union[ASTNode, Sequence[ASTNode]]:
40+
single = False
41+
if isinstance(models, ASTNode):
42+
single = True
43+
models = [models]
44+
45+
for model in models:
46+
self.add_timestep_symbol(model)
47+
48+
if single:
49+
return models[0]
50+
51+
return models
52+
53+
@classmethod
54+
def add_timestep_symbol(cls, model: ASTModel) -> None:
55+
r"""
56+
Add timestep variable to the internals block
57+
"""
58+
from pynestml.utils.model_parser import ModelParser
59+
assert model.get_initial_value(
60+
"__h") is None, "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
61+
assert not "__h" in [sym.name for sym in model.get_internal_symbols(
62+
)], "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
63+
model.add_to_internals_block(ModelParser.parse_declaration('__h ms = resolution()'), index=0)

pynestml/utils/ast_utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,18 +2256,6 @@ def remove_kernel_definitions_from_equations_block(cls, model: ASTModel) -> ASTD
22562256

22572257
return decl_to_remove
22582258

2259-
@classmethod
2260-
def add_timestep_symbol(cls, model: ASTModel) -> None:
2261-
"""
2262-
Add timestep variable to the internals block
2263-
"""
2264-
from pynestml.utils.model_parser import ModelParser
2265-
assert model.get_initial_value(
2266-
"__h") is None, "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
2267-
assert not "__h" in [sym.name for sym in model.get_internal_symbols(
2268-
)], "\"__h\" is a reserved name, please do not use variables by this name in your NESTML file"
2269-
model.add_to_internals_block(ModelParser.parse_declaration('__h ms = resolution()'), index=0)
2270-
22712259
@classmethod
22722260
def generate_kernel_buffers(cls, model: ASTModel, equations_block: Union[ASTEquationsBlock, List[ASTEquationsBlock]]) -> Mapping[ASTKernel, ASTInputPort]:
22732261
"""

0 commit comments

Comments
 (0)