libcellml icon indicating copy to clipboard operation
libcellml copied to clipboard

Using a computed constant as an initialisation for a state variable throws an warning in libcellml check

Open FinbarArgus opened this issue 9 months ago • 6 comments

When running libcellml checking of this model

SN_to_cAMP_02_03_25.zip

with these commands https://github.com/FinbarArgus/circulatory_autogen/blob/812a09f7c0f04b2ee0e175d14dda067cf90bf9db/src/generators/CVSCellMLGenerator.py#L83C9-L107:

I get the following warning:

Checking Status of Model
The method "parse_model" found 1 issues:
    - Given model is a CellML 1.1 model, the parser will try to represent this model in CellML 2.0.
Recorded 1 issues:
Issue 0 is a MESSAGE:
    Description: Given model is a CellML 1.1 model, the parser will try to represent this model in CellML 2.0.
    More information is available at: ?issue=
    Stored item type: model

The method "resolve_imports" found 3 issues:
    - Given model is a CellML 1.1 model, the parser will try to represent this model in CellML 2.0.
    - Given model is a CellML 1.1 model, the parser will try to represent this model in CellML 2.0.
    - Given model is a CellML 1.1 model, the parser will try to represent this model in CellML 2.0.
no unresolved imports.
Recorded 24 issues:
Issue 0 is an ERROR:
    Description: Variable 'm' in component 'soma_SN_module' is initialised using variable 'm_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 1 is an ERROR:
    Description: Variable 'h' in component 'soma_SN_module' is initialised using variable 'h_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 2 is an ERROR:
    Description: Variable 'c' in component 'soma_SN_module' is initialised using variable 'c_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 3 is an ERROR:
    Description: Variable 'hc' in component 'soma_SN_module' is initialised using variable 'hc_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 4 is an ERROR:
    Description: Variable 'n1' in component 'soma_SN_module' is initialised using variable 'n1_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 5 is an ERROR:
    Description: Variable 'a' in component 'soma_SN_module' is initialised using variable 'a_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 6 is an ERROR:
    Description: Variable 'ah' in component 'soma_SN_module' is initialised using variable 'ah_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 7 is an ERROR:
    Description: Variable 'm_BK' in component 'soma_SN_module' is initialised using variable 'm_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 8 is an ERROR:
    Description: Variable 'h_BK' in component 'soma_SN_module' is initialised using variable 'h_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 9 is an ERROR:
    Description: Variable 'z_BK' in component 'soma_SN_module' is initialised using variable 'z_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 10 is an ERROR:
    Description: Variable 'm_SK' in component 'soma_SN_module' is initialised using variable 'm_SK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 11 is an ERROR:
    Description: Variable 'w' in component 'soma_SN_module' is initialised using variable 'w_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 12 is an ERROR:
    Description: Variable 'm' in component 'soma_SN' is initialised using variable 'm_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 13 is an ERROR:
    Description: Variable 'h' in component 'soma_SN' is initialised using variable 'h_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 14 is an ERROR:
    Description: Variable 'c' in component 'soma_SN' is initialised using variable 'c_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 15 is an ERROR:
    Description: Variable 'hc' in component 'soma_SN' is initialised using variable 'hc_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 16 is an ERROR:
    Description: Variable 'n1' in component 'soma_SN' is initialised using variable 'n1_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 17 is an ERROR:
    Description: Variable 'a' in component 'soma_SN' is initialised using variable 'a_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 18 is an ERROR:
    Description: Variable 'ah' in component 'soma_SN' is initialised using variable 'ah_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 19 is an ERROR:
    Description: Variable 'm_BK' in component 'soma_SN' is initialised using variable 'm_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 20 is an ERROR:
    Description: Variable 'h_BK' in component 'soma_SN' is initialised using variable 'h_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 21 is an ERROR:
    Description: Variable 'z_BK' in component 'soma_SN' is initialised using variable 'z_BK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 22 is an ERROR:
    Description: Variable 'm_SK' in component 'soma_SN' is initialised using variable 'm_SK_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable
Issue 23 is an ERROR:
    Description: Variable 'w' in component 'soma_SN' is initialised using variable 'w_init', which is not a constant.
    More information is available at: https://libcellml.org/documentation/guides/latest/runtime_codes/index?issue=ANALYSER_VARIABLE_NON_CONSTANT_INITIALISATION
    Stored item type: variable

The model runs fine. Is there a reason that we need a warning for using computed constants as initialisation for states?

FinbarArgus avatar Mar 02 '25 05:03 FinbarArgus

Hi @FinbarArgus - the warning is just there to let the user know that they are initializing a state variable with a non-constant value, which is often, but not always, a bug in the model which can result in unexpected behavior. It is also to try and communicate to the user that its only the initialization of the state variable, it won't have any impact as the model is evaluated...unless the user chooses to call the generated initialization routine again, in which case the current value of the computed constant will be used.

Warnings in the analyzer are designed to provide information to the user on potential issues with their model(s), they won't (shouldn't) stop the model from being simulate-able.

@agarny might correct me on this one :)

@agarny @hsorby - I also see the errors in this example point to broken URLs for more information. And that the current version of the docs is missing any additional information for the analyser run time codes (https://libcellml.org/documentation/v0.6.3/runtimecodes/runtime_codes/index#analyser-codes)

nickerso avatar Mar 02 '25 19:03 nickerso

Those are not warnings but errors (see here) although they could probably be made warnings.

Anyway, the issue here is that a variable is indeed initialised using a variable which is not a constant. So, the analyser cannot (currently) tell whether the variable is safely used. For instance, say that you have a^b with b initialised using a variable which is not a constant. This will therefore affect the units of a^b and render the model inconsistent when it comes to units. On the other hand, if b is used in something like a+b then we would be good (assuming that a and b have the same units). It's just that the analyser doesn't (currently) check where and how b is used, hence the error even it might actually be fine. We could probably do that check though, if really needed.

Argh re the broken links... :/

agarny avatar Mar 02 '25 23:03 agarny

Hi @agarny and @nickerso ,

Thanks for the replies.

"For instance, say that you have a^b with b initialised using a variable which is not a constant. This will, therefore, affect the units of a^b and render the model inconsistent when it comes to units."

  • But the units of b are specified and checked in the calculation of b right? I agree there should be a warning/error if the calculation of the units of b are inconsistent but I don't see why this is an issue with using computed constants for b. I'm probably missing something.

Often when creating an ODE model it is beneficial to calculate the initializations. This allows faster convergence to a steady state or periodic steady state. Therefore, I would argue that using computed constants in this way shouldn't be discouraged. Let me know your thoughts.

FinbarArgus avatar Mar 03 '25 00:03 FinbarArgus

  • But the units of b are specified and checked in the calculation of b right? I agree there should be a warning/error if the calculation of the units of b are inconsistent but I don't see why this is an issue with using computed constants for b. I'm probably missing something.

When it comes to a^b, the analyser will expect b to be dimensionless. If it is not, then the analyser will complain about it. Still, even if b is dimensionless the fact that it is initialised by a non-constant variable means that a^b may be expressed in different units. For instance, say that at some point b is equal to 2 and that later on it is equal to 3. This means that if a is expressed in, say, seconds then you end up with a^b being expressed in either s^2 or s^3, hence the error message generated by libCellML.

Often when creating an ODE model it is beneficial to calculate the initializations. This allows faster convergence to a steady state or periodic steady state. Therefore, I would argue that using computed constants in this way shouldn't be discouraged. Let me know your thoughts.

I am certainly not discouraging anything (and I don't think @nickerso is either). All the analyser does is to make you aware of some potential issues with your model. If you think you are doing the right thing then just ignore them. It's similar to some compilation warnings that we sometimes get in C/C++. Some of them are really about making sure that the developer knows what he's doing.

agarny avatar Mar 03 '25 01:03 agarny

Got it! That makes sense, thank you.

Understood. Would we be able to switch specific warnings off?

I want to use libCellMLs error checking in Circulatory Autogen for users to debug errors in their generated models, but I think having the above warnings is overload for a user.

FinbarArgus avatar Mar 03 '25 01:03 FinbarArgus

Got it! That makes sense, thank you.

Understood. Would we be able to switch specific warnings off?

I want to use libCellMLs error checking in Circulatory Autogen for users to debug errors in their generated models, but I think having the above warnings is overload for a user.

Remember, the above are not warnings, but errors. What I suggested is that they could be made warnings instead. Then, in your code, you could ignore warnings and just let users know if there are errors. That's what I do when I want to run a model, I "ignore" warnings.

Regarding switching specific warnings off, that's something that you could easily do at your end, if really needed?

agarny avatar Mar 03 '25 02:03 agarny