[yt-users] divergence of velocity field in yt

Suoqing Ji suoqing at physics.ucsb.edu
Thu Oct 27 14:29:03 PDT 2016


Hi Turhan,

Sorry for noticing your message late…

I attach some lines here of defining a (velocity) divergence field. You can change "velocity_x,y,z" to any vector field you want.

##################################################

import yt
from yt.fields.derived_field import ValidateSpatial

@yt.derived_field(name='div_velocity', units='1/code_time',
                           validators = [ValidateSpatial(1,
                           ['velocity_x','velocity_y','velocity_z'])])
def _div_velocity(field,data):
    sl_left = slice(None, -2, None)
    sl_right = slice(2, None, None)
    div_fac = 2.0
    ds = div_fac * data['dx'].flat[0]
    f  = data["velocity_x"][sl_right,1:-1,1:-1]/ds
    f -= data["velocity_x"][sl_left ,1:-1,1:-1]/ds
    if data.ds.dimensionality > 1:
        ds = div_fac * data['dy'].flat[0]
        f += data["velocity_y"][1:-1,sl_right,1:-1]/ds
        f -= data["velocity_y"][1:-1,sl_left ,1:-1]/ds
    if data.ds.dimensionality > 2:
        ds = div_fac * data['dz'].flat[0]
        f += data["velocity_z"][1:-1,1:-1,sl_right]/ds
        f -= data["velocity_z"][1:-1,1:-1,sl_left ]/ds
    new_field = np.zeros(data["velocity_x"].shape, dtype='float64')
    new_field = data.ds.arr(new_field,'1/code_time')
    new_field[1:-1,1:-1,1:-1] = f
    return new_field

##################################################

Best wishes,
--
Suoqing JI
Ph.D Candidate
Department of Physics
University of California, Santa Barbara
http://web.physics.ucsb.edu/~suoqing

> On Oct 27, 2016, at 2:24 PM, Britton Smith <brittonsmith at gmail.com> wrote:
> 
> Hi Turhan,
> 
> If you have a copy of the yt source, I suggest having a look the file yt/fields/vector_operations.py.  In there, you will find a function called _divergence which illustrates how to create a divergence field.  Additionally, I recommend looking for where that function is put to use in that file for an example of how to add such a field to the list of known fields.
> 
> Britton
> 
> On Tue, Oct 18, 2016 at 2:32 PM, turhan nasri <turhannasri at gmail.com <mailto:turhannasri at gmail.com>> wrote:
> Hi people, 
> 
> I wish to see the sliceplots of "divergence of velocity" fields in yt. I see yt has an example of how to derive gradient of a scalar field , is there any example to show how to derive  new field for divergence too ? 
> 
> Best
> Turhan
> 
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org <mailto:yt-users at lists.spacepope.org>
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org <http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org>
> 
> 
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20161027/fddb1ca2/attachment.html>


More information about the yt-users mailing list