On Wed, Aug 5, 2009 at 12:52 PM, Jon Olav Vik<[email protected]> wrote:
> Lisandro Dalcin <dalc...@...> writes:
>
>> Could you tell me what language are you going to use for your "main"
>> application? I mean, are you coding in Python, and then want to reuse
>> the Fortran library? Or perhaps you are coding in C/Fortran, but want
>> to be able to write your functions in Python? Depending on that, the
>> approach is quite different (at least from my POV).
>
> * Python is my main language.
> * C is used for a rate function in a differential equation (provided to me
> from
> elsewhere).
> * Fortran has the numerical differentiation library, which I'd like to use for
> the following reasons:
> - Don't have to code it myself 8-)
> - Presumably robust, efficient and numerically accurate.
> - Citable publication.
> - Parallel implementations are available (though I'm unsure how that will play
> with a Cython callback)
>
> I'd be very grateful if you could recommend a strategy for this use case, or
> suggest other options. I include some more detail below in case you're
> interested. As for the mixed-language programming, I can see the following
> scenarios:
>
> == Ideal case: f2cy lets me call Fortran library from Cython ==
> (f2cy is described at fortrancython.wordpress.com.)
> Then I could do, in principle:
Hi, Jon. I'm the 'maintaner' of fwrap, which is the new name of
'f2cy' (I clearly need to put an update post on the blog!). It's
still in development, and will have basic functionality ready to go in
a few weeks. I don't know if we'll have C (i.e. cdef) callbacks
implemented by then, but it will be high on the priority list. It
will certainly help you wrap a Fortran library in Cython, as you
desire, although the api is similar to f2py where the wrapping is
automated, rather than the ctypes-like example you have below. If you
can't wait that long then you'll have to find another way. I'm sorry
that I can't devote the time to your particular mixed language
programming questions, since I have to put all my time into fwrap. If
I can carve out a chunk of time in the next few days I'll take a look.
Kurt
>
> cimport f2cy
> import my_model # heavy initialization goes here
> ndl = f2cy("NDL") # make Fortran library callable from Cython
> cdef double phenotype(double* parameters, int number_of_parameters):
> """The function I want to differentiate,
> with an argument list matching the NDL specification."""
> return my_model.phenotype(parameters) # expensive function evaluation
> # calling an NDL function which makes callbacks to my function:
> print ndl.hessian(phenotype, ...) # returns a 2-d array of double
>
> (In real life, the Fortran names are six characters long and devoid of vowels,
> of course.)
>
> == "Practicality beats elegance" case: do whatever it takes... ==
> "cdef double phenotype..." as above, but calling phenotype() from Fortran
> (supposedly as easy as calling it from C). Then the main program will have to
> be in Fortran:
>
> ! This is how I make compiled C functions in my_model_cy.o
> ! available to Fortran. Incredibly simple:
> external my_model_cy
> ! Would need to figure out a way
> ! to make the answer available for further processing...
> print *, hessian(phenotype, ...)
>
> (Maybe I could even make a reusable, flexible Fortran function that could be
> called from Cython, but I wouldn't know how to pass function pointers to
> it...and if I could, I should be able to call the NDL library directly.)
>
>
> Thanks a lot for your help!
>
> Jon Olav
>
>
> == Details about my use case ==
>
> My overall goal is to estimate parameter interaction in a detailed model of
> heart cell physiology. The parameters represent conductances of cellular ion
> channels and lots of other stuff. The model simulates the change over time in
> transmembrane voltage and concentrations of various ions in the cell. I will
> aggregate this to a single scalar-valued cellular phenotype, e.g. a measure of
> the duration of the voltage peak. This gives me a scalar function H of an n-
> dimensional parameter vector p.
>
> The derivative d(H)/d(p_i) is a measure of the "sensitivity" of the chosen
> phenotype to each model parameter p_j. However, this sensitivity may depend on
> other parameters p_j. The second derivatives d^2 H / [d(p_j) d(p_i)] is a
> measure of how a (small) change in parameter p_j modifies the effect of a
> (small) change in parameter p_j on the chosen phenotype.
>
> I will evaluate these derivatives at different points in parameter space and
> study various phenotypic measures, orchestrating it all in Python/Numpy:
> iterating over parameter scenarios, computing aggregate phenotypic measures,
> storing results, making graphs (Matplotlib) and exporting for statistical
> analysis (rpy2). Also, I'll want to concentrate on just a few parameters p_i
> and p_j; this could be neatly done with ad hoc wrappers like:
>
> p = ... # Full n-dimensional parameter vector
> i = ... # Indices to the parameters I'll be looking at
> def f(x, n):
> p0 = p # copy of original parameter values
> p0[i] = x # substitute the given parameters
> return phenotype(p0) # call with full parameter vector
>
> Having to code each of these in Fortran would probably make me cry.
>
>
> Because the function evaluations are rather expensive, I'd like to use an
> efficient, robust, and perhaps parallel library for the differentation.
>
>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev