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.

Element weighting

More
4 years 5 months ago #2005 by NilsHM
Element weighting was created by NilsHM
Hello everyone!

I'm working on a hyper-reduction method where each element in the mesh has a unique weight.

The weights must be applied to the local element stiffness matrices during the assembly to the bilinear form.
Is there a way of doing that given a vector of element weights?

Or else is there a way to get single element stiffness matrices in global coordinates?

Thank you
Nils
More
4 years 5 months ago - 4 years 5 months ago #2006 by christopher
Replied by christopher on topic Element weighting
You can do this quite easily with a c++ extension. Basically you need to follow the first two steps of the c++ tutorial here: ngsolve.org/docu/latest/mylittlengs/1_Basic/README.html

You can for example parse the elementwise coefficients as a list or a numpy array and store them in a c++ array. Then in the evaluate function of your coefficient you query the element number by
Code:
int elnr = mip.GetTransformation().GetElementNr();
Then return the corresponding value from the array.

You can then use this CoefficientFunction in your stiffness bilinearform.

I hope this helps, if you run into problems write again. This part of the documentation is not often used yet.

Best
Christopher
Last edit: 4 years 5 months ago by christopher.
The following user(s) said Thank You: NilsHM
More
4 years 5 months ago #2057 by NilsHM
Replied by NilsHM on topic Element weighting
Hello Christopher,

thank you for the help.
I got as far as making a CoefficientFunction that gives the element number.

But I need the CoefficientFunction to know the element weights. You said to store the weights in an array. At what point can I do that?

I tried to initialize with the array.
The idea was to initialize my custom CoefficientFunction like this:
Code:
cf = ElementWeightCF(weights)
But that didn't work:
Code:
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. CppExtension.cppextension.ElementWeightCF()
It seems like the issue is to initialize the ngsolve CoefficientFunction with to many arguments.
You can find my .hpp file in the attachments.

How can I fix that issue or is there a better way of passing the element weights to my c++ CoefficientFunction?

Thanks in advance
Nils


File Attachment:

File Name: ElementWeightCF.hpp
File Size:1 KB
More
4 years 5 months ago #2058 by christopher
Replied by christopher on topic Element weighting
Hi Nils,
the code in your hpp seems ok, I expect the problem is in the binding code. Can you send the cpp file as well?
Best
More
4 years 5 months ago - 4 years 5 months ago #2059 by NilsHM
Replied by NilsHM on topic Element weighting
You can find the remaining code in the attachments.

File Attachment:

File Name: ElementWeightCF.cpp
File Size:1 KB

File Attachment:

File Name: cppextension.cpp
File Size:1 KB
Last edit: 4 years 5 months ago by NilsHM.
More
4 years 5 months ago #2060 by christopher
Replied by christopher on topic Element weighting
The export of the constructor is wrong, the template arguments for the py::init must be the arguments for the constructor:
Code:
.def(py::init<py::array_t<double>>())
best
The following user(s) said Thank You: NilsHM
Time to create page: 0.163 seconds