Forum Message

 

 

We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

Notice

The forum is in read only mode.

CoefficientFunction from interpolation list

More
4 years 1 month ago #2473 by NilsHM
Hello Everyone,

I want to define a CoefficientFunction from another CoefficientFunction using an interpolation list for values.

Here is a short example:
Code:
import numpy as np from ngsolve import * B_list = [0.0, 0.211862, 0.265665, 0.332377] mu_list = [2.66233640, 2.62172474, 2.59376841, 2.54489696] B_cf = x
Here I tried to define the CoefficientFunction as follows:
Code:
mu_cf = CoefficientFunction( [ np.interp(B, B_list, mu_list) for B in B_cf ] )
But this does not work.

Is there a way of making the CoefficientFunction make use of the np.interp() function or is there another way?

Thanks
Nils
More
4 years 1 month ago #2474 by christopher
You can use the BSpline coefficient function for this. It gets an order, a list of nodes and a list of values.
Best
Christopher
The following user(s) said Thank You: NilsHM
More
4 years 1 month ago #2486 by NilsHM
Thanks! The BSpline CF probably what I was looking for.

The value of that CF however follows the data not very closely.
I'm setting up the CF like this:
Code:
spline = BSpline(n, B_list, mu_list) mu_cf = spline(B_cf)
I put the output value of that CF for different orders compared to the interpolation data in the attachments. Order 1 is mapping the the data in a acceptable way but since I need to differentiate this cf order 1 is out of question.

Is there a way of making the CF just interpolate the data or something else to make this work?

Best regards
Nils

Data: blue dots
BSpline result: red dashed line
Order n=1

Order n=2

Order n=3
More
4 years 1 month ago #2487 by joachim
you have to duplicate the first control point (value of B_list)
then the n=2 B-spline will be linear interpolation.

Joachim
The following user(s) said Thank You: NilsHM
More
4 years 1 month ago #2490 by NilsHM
Thanks!

Now I have a different problem:

I'm using this CF in a nonlinear BilinearForm:
Code:
u,v = fes.TnT() a = BilinearForm(fes) a += SymbolicBFI(1.0/spline(Norm(grad(u))) * grad(u) * grad(v))
When assembling a linearized matrix of the system the matrix has a lot of nan entries.
This should not happen since the values of spline() are always nonzero positive.

Do you know why there are nan entries in the matrix and how to avoid it?

Best regards
Nils
More
4 years 1 month ago #2491 by joachim
difficult to say without testing, but maybe you try to differentiate Norm at grad(u) = 0 ?

Replace Norm(grad(u)) by
sqrt( InnerProduct(grad(u),grad(u)) + 1e-15 )

Joachim
The following user(s) said Thank You: NilsHM
Time to create page: 0.169 seconds