VERBATIM and Gamma function

NMODL and the Channel Builder.
Post Reply
duboismathieu

VERBATIM and Gamma function

Post by duboismathieu »

Hello everybody,

I would like to implement the Gamma function in NEURON (it is needed for the model I'm working on and may be useful for other).

A nice algorithm can be found in "Numerical Recipes in C" by Press et al.

After looking at http://senselab.med.yale.edu/senselab/m ... 7/rand.mod to see how to embed C code in NMODL I have written the mod file gamma.mod:

Code: Select all

COMMENT
Algorithms are taken from Press et al "Numerical Recipes in C"
ENDCOMMENT

VERBATIM
#include <stdio.h>
#include <math.h>
#define ITMAX 100
#define EPS 3.0e-7
#define FPMIN 1.0e-30
ENDVERBATIM

COMMENT
/****************************************
 *	Log. of the Gamma function	*
 ****************************************/
ENDCOMMENT
FUNCTION lnGamma(xx) {
VERBATIM
	double x, y, ser, tmp;
	int j;

	static double cof[6] = {76.18009172947146,-86.50532032941677,
     24.01409824083091,-1.231739572450155,
     0.1208650973866179e-2,-0.5395239384953e-5};

	y = x = _lxx;
	tmp = x + 5.5;
	tmp -= (x+0.5)*log(tmp);
	ser=1.000000000190015;

	for (j=0;j<=5;j++) {
		ser += cof[j]/++y;
	}
	_llnGamma = -tmp+log(2.5066282746310005*ser/x);
ENDVERBATIM
}
The algorithm is a little bit complicated but it should work. I have also used the INCLUDE statement in my main mod file (called gIF4.mod). The two files reside in the same directory.

When I try to compile this mod file with nivmodl gIF4.mod I get:

Code: Select all

Translating gIF4.mod into gIF4.c
INCLUDEing gamma.mod
"/usr/local/nrn/share/nrn/libtool"  --mode=compile gcc -DHAVE_CONFIG_H  -I. -I.. -I"/usr/local/nrn/include/nrn" -I"/usr/local/nrn/i686/lib"    -g -O2 -c -o gIF4.lo gIF4.c
mkdir .libs
 gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/local/nrn/include/nrn -I/usr/local/nrn/i686/lib -g -O2 -c gIF4.c  -fPIC -DPIC -o .libs/gIF4.o
gIF4.c:313: error: conflicting types for '_hoc_lnGamma'
gIF4.c:77: error: previous declaration of '_hoc_lnGamma' was here
make: *** [gIF4.lo] Error 1
Looking at gIF4.c, one can read:
line 77: static double _hoc_lnGamma();
line 313: static int _hoc_lnGamma() {...

Is there any mistake in my code confusing nrnivmodl? Otherwise, how to do?

Thanks in advance,
Mathieu.
hines
Site Admin
Posts: 1713
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

I do not see anything wrong with the file and it can be used alone by appending

Code: Select all

NEURON { SUFFIX nothing }
I can't say why includeing it into gIF4.mod gives an error and would have to have both files to diagnose the problem.
duboismathieu

Post by duboismathieu »

Hello,

Thank you for the response.

Following your advice, I have added in gamma.mod:

Code: Select all

  NEURON {SUFFIX nothing}  
and I have removed the INCLUDE statement from gIF4.mod.

It works now! I have also corrected a bug in gIF4.mod: a variable sadly called eq1, causing a failure to load the libs.

I'm working now on testing the gamma function(s) to see if "my" implementation is correct.

If you are still interested I can send you the files gamma.mod and gIF4.mod.

Thanks again.
Mathieu.
ted
Site Admin
Posts: 6398
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

I would certainly appreciate seeing this code in ModelDB, after your work has been
accepted for publication of course.
duboismathieu

Post by duboismathieu »

Hello,

A little question.

The algorithm above uses Euler's constant (GAMMA=0.5772156). I will probably need PI too. These constants exist in hoc (I have found them "hardcoded" in the file src/oc/hoc_init.c).

I would bet that they are defined in NEURON's standard library. Is there any way to access these constants or do I need to define them with #DEFINE?

Mathieu.
duboismathieu

Post by duboismathieu »

The previous post is not very clear. Sorry.

I should add that I want to access PI and GAMMA in C code. I would like to write something like

Code: Select all

  sin(PI*x)  


There is probably a "#define PI" in NEURON that I can use to avoid duplicating values. And maybe a "#define GAMMA"...
ted
Site Admin
Posts: 6398
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Download and expand the gzipped source code.
In the subdirectories of nrn-x.x/src grep the *.c and *.h files to discover items of interest.
Example: nrn-x.x/src/oc/hoc_init.c contains this block of defined constants

Code: Select all

        "PI",   3.14159265358979323846,
        "E",    2.71828182845904523536,
        "GAMMA",0.57721566490153286060, /* Euler */
        "DEG", 57.29577951308232087680, /* deg/radian */
        "PHI",  1.61803398874989484820, /* golden ratio */
        "FARADAY", 96485.309,   /*coulombs/mole*/
        "R", 8.31441,           /*molar gas constant, joules/mole/deg-K*/
duboismathieu

Post by duboismathieu »

Sorry to reopen the subject (I have worked on something else for a while). I'm now using the latest NEURON 6.0 and I have some problem.

The model I want to implement is described Brette R. 'Exact Simulation of IF neurons with Synaptic Conductances'.
It uses a special function rho(a,x) defined after the incomplete gamma function:rho(a,x)=exp(x)*x^-a*gamma(a,x) .

I still have the gamma.mod file which defines all the functions needed. I have tested the implementation and the functions are correct. The block

Code: Select all

NEURON {
 SUFFIX nothing
}
is present.

The model is described in another mod file (no INCLUDE "gamma.mod") in the same directory.

When compiling with nrnivmodl, NEURON prints a warning:

Code: Select all

Translating gIF4mBrette.mod into gIF4mBrette.c
Warning: rho undefined. (declared within VERBATIM?)
In the model, the function rho always returns 1 (I print the result on ). But on the NEURON command line I can evaluate rho.

I have also tried to use nrnivmodl gamma.mod gIF4mBrette.mod but without success.

I don't know what to do.
I have tried to add gamma.mod to the source and recompile NEURON but without success.
I can send you the code but it is a little bit complicated...

Thanks in advance.
Mathieu.
ted
Site Admin
Posts: 6398
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Code: Select all

Warning: rho undefined.
That's a peculiar error message. Presumably your mod file contains a
FUNCTION rho
block that contains the code to calculate the value and assign it to rho,
or ends with
VERBATIM
return value_of_rho;
ENDVERBATIM
If, after reading this message, the solution doesn't jump out at you, perhaps
it is necessary to send me
ted dot carnevale at yale dot edu
the mod file plus just enough of a hoc file to be able to test it.
Post Reply