Hi He Yifeng,

So I am not *sure*, but I *think* that you may need to change how it sets up the number of ghost zones that it acquires.  I think that the nested nature won't increase the requested ghost zones quite right.  What I would actually suggest is, instead of doing the gradient-of-the-gradient, is to implement this yourself with a derived field.

The original code that gets used is in:

https://github.com/yt-project/yt/blob/8c9193965329f536cba35c85fa23e9447f90cfbe/yt/fields/fluid_fields.py#L244

For your purposes, there's a lot that isn't necessary -- including stuff related to non-Cartesian axes, block ordering, etc.  The essence is in defining the slice_3d, the manipulation of the 'f' variable, and setting `new_field`.  And, when it gets added, note that it sets 'ValidateSpatial' to have an argument of 1 -- this is the tricky bit, as it'll need to be 2, to get an extra ghost zone for your results.

(You *could* try setting that to 2 and seeing if it 'magically fixes everything' but I'm not sure, and it'll definitely be a lot slower.)

What you'll want to do is write the stencil to operate on each individual grid, add the field with the ValidateSpatial argument set to 2 instead of 1, and name it something like second_order_gradient_x or something.  If you run into trouble, hit us back here on the list, or on slack!

On Wed, May 10, 2023 at 9:07 AM <hyf@stu.pku.edu.cn> wrote:
Hi everyone,

I have tried to use the function 'add_gradient_fields' to calculate
the second-order derivative of density 'rho' with x-direction, i.e., 'rho_gradient_x_gradient_x'.
Here is the code:

rho_gradientx = ds.add_gradient_fields(('boxlib','rho'))
ds.periodicity = (True, True, True)
rho_gradientxx = ds.add_gradient_fields(('boxlib','rho_gradient_x'))

Unfortunately, nan occurs when I tried to plot the field 'rho_gradient_x_gradient_x'

I will be appreciate if anyone can give me suggestions about how to fix it
or provide other solutions to calculate higher order(more than 1st order)  gradient fields.

Best wishes,

He Yifeng
_______________________________________________
yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org
https://mail.python.org/mailman3/lists/yt-users.python.org/
Member address: matthewturk@gmail.com