pythonflashyt-project

python syntax error and usage of yt package


I need a little help in defining a new derived field in yt. my goal is to find if a stellar system eject some mass or not.

I am attaching the derived_field_list from the file I have. I am trying to define the total energy of the system E=0.5*m*v**2+gravitational potential energy*m

I am completely new to python and trying to get hold of the language through yt. I am attaching my efforts and the errors. please help me fix that.

Thanks in advance.

from yt import derived_field
@derived_field(name = 'mass_ejected')
def shock(field,data):
    dm = data["gas",'cell_mass']
    xv = data["gas","velocity_x"]
    yv = data["gas","velocity_y"]
    zv = data["gas","velocity_z"]
    grav_pot= data["flash", u'gpot']
#define total energy
    E_total=0.5*dm*(xv**2+yv**2+zv**2)+grav_pot*dm

if E_total > 0:
    return 0
else :
    return 1

and the corresponding error

 File "<ipython-input-14-2cb04cd0ad81>", line 13
    return 0
SyntaxError: 'return' outside function

and the file contains these following derived fields already

[('flash', u'dens'),
 ('flash', u'eint'),
 ('flash', u'gamc'),
 ('flash', u'gpot'),
 ('flash', u'pres'),
 ('flash', u'temp'),
 ('flash', u'velx'),
 ('flash', u'vely'),
 ('flash', u'velz'),
 ('gas', 'H_nuclei_density'),
 ('gas', 'He_nuclei_density'),
 ('gas', 'angular_momentum_magnitude'),
 ('gas', 'angular_momentum_x'),
 ('gas', 'angular_momentum_y'),
 ('gas', 'angular_momentum_z'),
 ('gas', 'averaged_density'),
 ('gas', 'baroclinic_vorticity_magnitude'),
 ('gas', 'baroclinic_vorticity_x'),
 ('gas', 'baroclinic_vorticity_y'),
 ('gas', 'baroclinic_vorticity_z'),
 ('gas', 'cell_mass'),
 ('gas', 'courant_time_step'),
 ('gas', 'cutting_plane_velocity_x'),
 ('gas', 'cutting_plane_velocity_y'),
 ('gas', 'cutting_plane_velocity_z'),
 ('gas', 'cylindrical_radial_velocity'),
 ('gas', 'cylindrical_radial_velocity_absolute'),
 ('gas', 'cylindrical_tangential_velocity'),
 ('gas', 'cylindrical_tangential_velocity_absolute'),
 ('gas', 'density'),
 ('gas', 'density_gradient_magnitude'),
 ('gas', 'density_gradient_x'),
 ('gas', 'density_gradient_y'),
 ('gas', 'density_gradient_z'),
 ('gas', 'dynamical_time'),
 ('gas', 'entropy'),
 ('gas', 'gravitational_potential'),
 ('gas', 'kT'),
 ('gas', 'kinetic_energy'),
 ('gas', 'mach_number'),
 ('gas', 'mazzotta_weighting'),
 ('gas', 'pressure'),
 ('gas', 'pressure_gradient_magnitude'),
 ('gas', 'pressure_gradient_x'),
 ('gas', 'pressure_gradient_y'),
 ('gas', 'pressure_gradient_z'),
 ('gas', 'radial_mach_number'),
 ('gas', 'radial_velocity'),
 ('gas', 'radial_velocity_absolute'),
 ('gas', 'shear'),
 ('gas', 'shear_criterion'),
 ('gas', 'shear_mach'),
 ('gas', 'sound_speed'),
 ('gas', 'specific_angular_momentum_magnitude'),
 ('gas', 'specific_angular_momentum_x'),
 ('gas', 'specific_angular_momentum_y'),
 ('gas', 'specific_angular_momentum_z'),
 ('gas', 'sz_kinetic'),
 ('gas', 'szy'),
 ('gas', 'tangential_over_velocity_magnitude'),
 ('gas', 'tangential_velocity'),
 ('gas', 'temperature'),
 ('gas', 'thermal_energy'),
 ('gas', 'total_energy'),
 ('gas', 'velocity_cylindrical_radius'),
 ('gas', 'velocity_cylindrical_theta'),
 ('gas', 'velocity_cylindrical_z'),
 ('gas', 'velocity_divergence'),
 ('gas', 'velocity_divergence_absolute'),
 ('gas', 'velocity_magnitude'),
 ('gas', 'velocity_spherical_phi'),
 ('gas', 'velocity_spherical_radius'),
 ('gas', 'velocity_spherical_theta'),
 ('gas', 'velocity_x'),
 ('gas', 'velocity_y'),
 ('gas', 'velocity_z'),
 ('gas', 'vorticity_growth_magnitude'),
 ('gas', 'vorticity_growth_magnitude_absolute'),
 ('gas', 'vorticity_growth_timescale'),
 ('gas', 'vorticity_growth_x'),
 ('gas', 'vorticity_growth_y'),
 ('gas', 'vorticity_growth_z'),
 ('gas', 'vorticity_magnitude'),
 ('gas', 'vorticity_squared'),
 ('gas', 'vorticity_stretching_magnitude'),
 ('gas', 'vorticity_stretching_x'),
 ('gas', 'vorticity_stretching_y'),
 ('gas', 'vorticity_stretching_z'),
 ('gas', 'vorticity_x'),
 ('gas', 'vorticity_y'),
 ('gas', 'vorticity_z'),
 ('gas', 'xray_emissivity'),
 ('index', 'cell_volume'),
 ('index', 'cylindrical_r'),
 ('index', 'cylindrical_radius'),
 ('index', 'cylindrical_theta'),
 ('index', 'cylindrical_z'),
 ('index', 'disk_angle'),
 ('index', 'dx'),
 ('index', 'dy'),
 ('index', 'dz'),
 ('index', 'grid_indices'),
 ('index', 'grid_level'),
 ('index', 'height'),
 ('index', 'ones'),
 ('index', 'ones_over_dx'),
 ('index', 'path_element_x'),
 ('index', 'path_element_y'),
 ('index', 'path_element_z'),
 ('index', 'radius'),
 ('index', 'spherical_phi'),
 ('index', 'spherical_r'),
 ('index', 'spherical_radius'),
 ('index', 'spherical_theta'),
 ('index', 'virial_radius_fraction'),
 ('index', 'x'),
 ('index', 'y'),
 ('index', 'z'),
 ('index', 'zeros')]

Solution

  • Indent the if and else code blocks at the end of the listing so they align with the code above. Indentation has significance in Python, as opposed to most other languages. The interpreter thinks the E_total= line is the last one in the function body.