Page 1 of 1
User defined functions in VERBATIM
Posted: Thu Apr 01, 2010 1:27 am
by auatai
Hi ,
How can we define and use a user defined function in nmodl within a verbatim block ?
Re: User defined functions in VERBATIM
Posted: Thu Apr 01, 2010 9:32 am
by ted
VERBATIM blocks allow insertion of C source code, but are you sure you need to do that? NMODL allows you to define FUNCTIONs that contain general mathematical expressions.
Code: Select all
FUNCTION fname(arg (units)) {
fname = expression
}
Re: User defined functions in VERBATIM
Posted: Thu Apr 01, 2010 11:03 am
by hines
Put the VERBATIM...END_VERBATIM within a FUNCTION and call outside the function with c syntax.
This allows the translator to generate the boilerplate interface between hoc and the FUNCTION.
From HOC, call the FUNCTION. eg
VERBATIM
static double bar(double arg) {
return arg*arg;
}
ENDVERBATIM
FUNCTION foo(arg) {
VERBATIM
_lfoo = bar(_larg)
ENDVERBATIM
}
See nrn/src/ivoc/oc2iv.h and nrn/src/nrniv/nrnoc2iv.h
for c functions you can use to get args besides doubles.
Also see nrn/src/nrnoc/pattern.mod for a fairly elaborate use of VERBATIM
in FUNCTIONS (and elsewhere).
Look at the translated c code to get an idea of how a naming convention is used
and the detailed c code that connects hoc and the c code.
Bill Lytton's models in ModelDB often have a mod file that supplies utility functions
using VERBATIM.
Re: User defined functions in VERBATIM
Posted: Thu Apr 01, 2010 11:05 am
by hines
sorry I left out the ';' in
_lfoo = bar(_larg)
above. I did not execute this so there may be other syntax errors but the idea is sound.
Re: User defined functions in VERBATIM
Posted: Thu Apr 01, 2010 9:42 pm
by auatai
Thank you sir for showing me the approach. I'll work on it and in case of any problems will revert to you.