neuronsimulator / nrn

NEURON Simulator
http://nrn.readthedocs.io
Other
381 stars 114 forks source link

CONSTANT variables are not constant. #2976

Closed 1uc closed 1 month ago

1uc commented 1 month ago

The following MOD files demonstrates the question:

NEURON {
  SUFFIX constant_mod
}

CONSTANT {
  a = 2.3
}

INITIAL {
  a = 24
}

FUNCTION foo() {
  foo = a
}

This haves as expected in that the value is 2.3 before h.stdinit() and 24 afterwards.

The question: Is this is intentional or just an artifact of the global being implemented as:

static double a = 2.3;

instead of:

static const double a = 2.3;

Note that the MOD file is considered thread-safe:

$ nocmodl constant.mod
Translating constant.mod into constant.cpp
Thread Safe
nrnhines commented 1 month ago

In the beginning, we were thinking of the word CONSTANT as almost a synonym for PARAMETER but local to the mod file same for all instances More in the realm of what the users expect but not demanded by the language. I guess one could think of it as "constant in space". In the end, this is just an artifact of the limitation of K&R C. If we change to static const double, what would the user do if they wanted to change the value in the mod file (e.g. during a call to a function from the interpreter). At the moment I'm leaning toward keeping it as static double but am easily persuadeable to go the other way, especially if there is no modeldb example that breaks. I have a faint feeling that people (I?) used CONSTANT because it was at hand for use as static double and never considered the possibilty of the language enforcing true const ness. To this moment one can

$ nrniv
NEURON -- VERSION 9.0a-295-g3a2336ec6 master (3a2336ec6) 2024-07-12
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2022
See http://neuron.yale.edu/neuron/credits

oc>PI
    3.1415927 
oc>PI=4
oc>PI
    4 
oc>

and

$ python
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pi
3.141592653589793
>>> math.pi=4
>>> math.pi
4
>>> 
1uc commented 1 month ago

Thank you for explaining, I'll add the appropriate test to NMODL. I merely wanted to double check that it's working as intended and not a recent regression.

1uc commented 1 month ago

I've not had any new questions and it's behaving like intended. From my side the issue has served it's purpose. Thanks for clarifying.