BlueBrain / nmodl

Code Generation Framework For NEURON MODeling Language
https://bluebrain.github.io/nmodl/
Apache License 2.0
57 stars 15 forks source link

Remove noisy `static_cast<int>(0)`. #1434

Closed 1uc closed 17 hours ago

alkino commented 2 months ago

It has been added because sometime this is a double and this is forbidden to use this for indexing

1uc commented 2 months ago

It has been added because sometime this is a double

Okay, so it's not just for readability, but it's a bug fix, even better.

alkino commented 2 months ago

https://github.com/BlueBrain/nmodl/issues/776

1uc commented 2 months ago

We have code that looks like this:

            nmodl_eigen_j[static_cast<int>(120)] =  -nmodl_eigen_x[static_cast<int>(0)] * _nt->_dt * kf11_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(0)]);
            nmodl_eigen_j[static_cast<int>(132)] = _nt->_dt * kb11_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(0)]);
            nmodl_eigen_f[static_cast<int>(1)] = (nmodl_eigen_x[static_cast<int>(0)] * _nt->_dt * kf0_ - nmodl_eigen_x[static_cast<int>(1)] * _nt->_dt * kb0_ - nmodl_eigen_x[static_cast<int>(1)] * _nt->_dt * kf1_ + nmodl_eigen_x[static_cast<int>(2)] * _nt->_dt * kb1_ + pow((*inst.diam[id]), 2.0) * ( -nmodl_eigen_x[static_cast<int>(1)] + old_ca_1) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)]) / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)]);
            nmodl_eigen_j[static_cast<int>(1)] = _nt->_dt * kf0_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)]);
            nmodl_eigen_j[static_cast<int>(13)] = ( -inst.global->beta * pow((*inst.diam[id]), 2.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)] - pow((*inst.diam[id]), 2.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)] - _nt->_dt * kb0_ - _nt->_dt * kf1_) / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)]);
            nmodl_eigen_j[static_cast<int>(25)] = _nt->_dt * kb1_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(1)]);

In order to be able to read this I need to apply a sequence of substitutions, first one it to remove all the static_cast<int>, they're just too much. Then we need to strip the nmodl_eigen_ which adds more clutter.

For example compare:

            nmodl_eigen_j[static_cast<int>(120)] =  -nmodl_eigen_x[static_cast<int>(0)] * _nt->_dt * kf11_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[static_cast<int>(0)]);

            nmodl_eigen_j[120] =  -nmodl_eigen_x[0] * _nt->_dt * kf11_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[0]);

            j[120] =  -x[0] * _nt->_dt * kf11_ / (pow((*inst.diam[id]), 2.0) * (inst.global->beta + 1.0) * (_thread_vars.vol_ptr(id))[0]);

There's still some problems with inst.diam, inst.global->beta and _thread_vars.vol_ptr that make it difficult to read; and arguably NOCMODL generated output is superior in that regard.