How to make a function written in C available to hoc

Particularly useful chunks of hoc and/or NMODL code. May be pedestrian or stunningly brilliant, may make you gasp or bring tears to your eyes, but always makes you think "I wish I had written that; I'm sure going to steal it."

How to make a function written in C available to hoc

Postby ted » Fri Aug 13, 2010 11:51 am

How can you make a function written in C available to hoc? Just put it in a mod file and compile the mod file. And if you declare
SUFFIX nothing
in the NEURON block of the mod file, none of the GUI menus will be changed--but the function will still be available to hoc.

For example, suppose you wanted to add cosh to hoc. cosh is part of the C library, so the mod file can be very simple.
Code: Select all
: dummy.mod
: Makes a function written in C available to hoc
  SUFFIX nothing
: call it gcosh to avoid naming conflicts
FUNCTION gcosh(z) {
  gcosh = cosh(z)

Compile this with mknrndll (or nrnivmodl for UNIX/Linux users), then test to confirm that it works.
Code: Select all
$ nrngui
. . .
Additional mechanisms from files

So far so good, but a more convincing test would be to write a cosh function in hoc and then do a visual comparison against the results generated by gcosh() over a range of arguments.

The Grapher is a good tool for this kind of task. The following hoc file defines proc cosh(), then loads the ses file for a Grapher that is configured to plot cosh(x) and gcosh(x) over the range [-3,3].*
Code: Select all

func cosh() {
        return (exp($1) + exp(-$1))/2

And here's
Code: Select all
ocbox_=new Grapher(1)"x","x", "", -3, 3, 0, 10, 100, -3, 3)
scene_vector_[2] = save_window_
ocbox_.g = save_window_
save_window_.xexpr("x", 0)
save_window_.addexpr("cosh(x)", 1, 3, 0.148243, 0.933506, 2)
save_window_.addexpr("gcosh(x)", 2, 1, 0.148243, 0.895213, 2)
ocbox_ = ocbox_.vbox"Grapher", 0, 105, 308.16, 437.76)
objref ocbox_
//End Grapher

objectvar scene_vector_[1]

*--To learn how to configure and use a Grapher, see
Site Admin
Posts: 5040
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine

Return to NEURON hacks

Who is online

Users browsing this forum: No registered users and 1 guest