### C++ dolfin::norm only on part of function

184
views
0
6 months ago by
Hello,

I've got a function from which I need to calculate a L1-norm. I tried it like this:

dolfin::norm(func.vector(), std::string("l1));

Unfortunately the function contains at least one element which is "NaN". Is there a way to create a new function with the same content despite the "NaNs" or to calculate the norm only on a part of the function?

Justus
Community: FEniCS Project

4
6 months ago by
You can calculate the norm manually by using func.vector().array() to access numpy array.  You can then replace any nans of the array with numpy too:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html
is there a similar way to do this in C++?
written 6 months ago by Justus Schock
1
You have access to the dolfin::Function.vector()->get_local(), so from there I don't know if there is something in std to replace Nan's, perhaps just a for loop?
written 6 months ago by pf4d
std::vector<double> values;
diff_func.vector()->get_local(values);

for(unsigned int i = 0; i < values.size(); i++){
if (values[i] != values[i]){
values[i] = 0.0;
}
}

did the trick. I only did not knew how to access the vectors content. Thanks a lot.

written 6 months ago by Justus Schock
Happy to be of service!
written 6 months ago by pf4d