Morpho-lang / morpho

The Morpho language 🦋. Morpho is a small embeddable language for scientific computing applications.
MIT License
33 stars 11 forks source link

Integrand for a single element #224

Closed joshichaitanya3 closed 1 year ago

joshichaitanya3 commented 1 year ago

This PR sets up a machinery for adding a integrandForElement method to functionals**. This can be really useful for applications that require point-wise operations (perturbing one vertex at a time in a Monte-Carlo simulation, for example).

This can be used like so:

import meshtools

var m = LineMesh(fn(t) [t^2, 0, 0], 0..10)

var l = Length()

print l.integrandForElement(m, 5) // Length of the edge with id 5

We define a small functional_mapintegrandforelement method in C that computes the value of the integrand at a given element id. The element type depends on the grade on which the functional is defined.

We further define a FUNCTIONAL_INTEGRANDFORELEMENT macro in functional.h that makes it easy to define this method for simpler functionals using a single line. The existing FUNCTIONAL_METHOD macro proves to be sufficient to define it for functionals that contain a reference (see the implementation for LineCurvatureSq in this PR).

In this PR, we have added this method for the following functionals:

If this looks good, we can add this method to all existing functionals.

** Upon discussions, the idea of having this as an optional behavior for the integrand method itself was rejected on the grounds of clarity and readability of the code.

softmattertheory commented 1 year ago

I really like this idea, @joshichaitanya3. Can you go ahead and add to other functionals, please?

softmattertheory commented 1 year ago

I will merge this in anyway as it's a valuable experimental feature. We may rethink some of the interface for functionals for 0.6 or beyond, but it's great to have this functionality exposed as we learn more about how it might be used.