python yt 包的语法错误和用法

python syntax error and usage of yt package

我需要一些帮助来定义 yt 中的新派生字段。我的目标是确定恒星系统是否会喷射出一些质量。

我附上了我的文件中的 derived_field_list。我试图定义系统的总能量 E=0.5*m*v**2+gravitational potential energy*m

我对 python 完全陌生,正试图通过 yt 掌握这门语言。我附上我的努力和错误。请帮我解决这个问题。

提前致谢。

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

和相应的错误

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

并且该文件已经包含以下派生字段

[('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')]

缩进列表末尾的 ifelse 代码块,以便它们与上面的代码对齐。与大多数其他语言相反,缩进在 Python 中很重要。解释器认为 E_total= 行是函数体中的最后一行。