Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompressible random vector field: add zero mean velocity feature #258

Open
joshua-benabou opened this issue Aug 14, 2022 · 2 comments
Open
Assignees
Milestone

Comments

@joshua-benabou
Copy link

joshua-benabou commented Aug 14, 2022

Hello,

I believe I found a bug with the incompressible random vector field algorithm. Generating a 3d incompressible field A (3 space dimensions, 3-component vectors), I find that the quantity dA_z/dy-dA_y/dz is always zero in the limit of large grid sizes.

A priori there is no reason this should be true for a generic incompressible field, and is unphysical, as it means the curl of A always has x-component equal to zero. This could be an artefact of the projector used to ensure incompressibility. The projector appears to introduce a preferential direction along the x-axis...

So as is, the incompressible random vector field code is not able to simulate an incompressible fluid with non-zero curl along the x-axis. For a fluid with zero mean velocity this is especially problematic.

If this was by design, it would be very useful to have an algorithm which generates a random 3d vector field which is not incompressible, so that an incompressible one may be obtained by then taking the curl. This is as simple as removing the projector in the summate_incompr method. Edit: Actually, I'm wondering if there is any downside with this method? For a Gaussian field, the curl of the field should have the same statistics (e.g correlation length) as the field itself (don't know a proof of this statement, but seems plausible).

I would do this myself, however I would need some help understanding your workflow. I am guessing something like: write cython in the summator.pyx file and then compile summator.c which I think is done automatically in setup.py. However must I rerun 'setup.py' everytime after making changes? Some details on your workflow here so I can try to make some contributions would be great!

Here is the minimal example showing the issue:

from B_field_generation import *
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import h5py
import gstools
import gstools.covmodel
import time
print("Done!")



############################################################################################
# generate incompressible random vector field in 3D

dim_x=40

x, y, z = np.arange(dim_x)/dim_x, np.arange(dim_x)/dim_x, np.arange(dim_x)/dim_x
cor_x=0.1
model = gs.Gaussian(dim=3, var=1, len_scale=[cor_x, cor_x, cor_x]) # has mean zero

srf = gs.SRF(model, generator='VectorField') # incompressible vector field #, seed=19841203

start = time.time()
A_field = srf((x, y, z), mesh_type='structured') # draw 1 realization for all our time points
end = time.time()

# get field components and derivatives
Az = A_field[2,:,:,:]
dAz_dy = np.gradient(Az, axis=1)

Ay = A_field[1,:,:,:]
dAy_dz = np.gradient(Ay, axis=2)

# plot field components

plt.pcolormesh(Az[:,:,dim_x//2])
plt.title("Az")
plt.figure()

plt.pcolormesh(Ay[:,:,dim_x//2])
plt.title("Ay")

plt.figure()

plt.pcolormesh(dAy_dz[:,:,dim_x//2])
plt.title("dAy_dz")

plt.figure()

plt.pcolormesh(dAz_dy[:,:,dim_x//2])
plt.title("dAz_dy")


plt.figure()

plt.pcolormesh(dAz_dy[:,:,dim_x//2]-dAy_dz[:,:,dim_x//2]) # this approaches zero for large grid sizes
plt.title("dAz_dy-dAy_dz")
@LSchueler
Copy link
Member

Hi Joshua,

this is indeed by design and not a bug. The algorithm was designed to study the diffusion of particles in random velocity fields with a mean velocity greater than 0 in the x-direction. However, random velocity fields without a mean velocity would be great to include. So, please go ahead! Do you know the Kraichnan 1970 paper "Diffusion by a Random Velocity Field"? He uses a more general approach for the incomprehensibility by using the cross product. In case you don't have access to the paper, I can send it to you.

I think the easiest way for you to compile the Cython code, is to use pip. The -e flag is for "editable" or something along that line and helps with quick development:
pip install -e . in the main directory. But you can also run the setup.py. That is the downside of a compiled language ;-)
In case you happen to have some experience with the language Rust, you could also make your contribution in this repository:
https://github.com/GeoStat-Framework/GSTools-Core
We hope to someday deprecate the Cython code, because especially parallel programming can be a real pain with it. You can already use 100% of GSTools with the Rust code. But in case you have no experience, just stick to Cython and we will convert your code to Rust sometime in the future.

@joshua-benabou
Copy link
Author

joshua-benabou commented Aug 16, 2022

Dear Lennart,

Thanks for your reply. I was able to get setup in editable mode and implement the Kraichnan method for a zero-velocity fluid. It is fairly simple. Will open pull request for the nd-vector-fields branch asap, still running some tests to make sure everything is in order. Will continue discussion on the other open discussion. #251

@MuellerSeb MuellerSeb added this to the v1.5 milestone Oct 29, 2022
@MuellerSeb MuellerSeb changed the title Incompressible random vector field appears to have a bug! Incompressible random vector field: add zero mean velocity feature Jun 6, 2023
@MuellerSeb MuellerSeb removed this from the v1.5 milestone Jun 6, 2023
@MuellerSeb MuellerSeb added this to the 1.6 milestone Jun 15, 2023
@MuellerSeb MuellerSeb modified the milestones: 1.6, 1.7 Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants