Skip to content

Commit

Permalink
Merge pull request #16 from chummels/particle_type_fix
Browse files Browse the repository at this point in the history
Changes all internal instances of particle_type to sampling_type
  • Loading branch information
chummels committed Nov 15, 2017
2 parents 54441fb + 479f1ec commit 19b7349
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
6 changes: 3 additions & 3 deletions trident/ion_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
LineDatabase, \
uniquify
from trident.utilities import \
_determine_particle_type
_determine_dataset_sampling_type
from trident.roman import \
from_roman

Expand Down Expand Up @@ -139,12 +139,12 @@ def _determine_sampling_type(ds, sampling_type, particle_type):
fields.
"""
if particle_type is not None:
warnings.Warn('The "particle_type" keyword is deprecated. '
warnings.warn('The "particle_type" keyword is deprecated. '
'Please use "sampling_type" instead.', stacklevel=2)
sampling_type = None

if sampling_type == 'auto' or particle_type == 'auto':
particle_type = _determine_particle_type(ds)
sampling_type = _determine_dataset_sampling_type(ds)

if particle_type is not None:
if particle_type:
Expand Down
35 changes: 12 additions & 23 deletions trident/ray_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
add_ion_number_density_field, \
atomic_number
from trident.utilities import \
_determine_particle_type
from yt.geometry.particle_geometry_handler import \
ParticleIndex
_determine_dataset_sampling_type, \
_check_sampling_types_match

def make_simple_ray(dataset_file, start_position, end_position,
lines=None, ftype="gas", fields=None,
Expand Down Expand Up @@ -238,22 +237,17 @@ def make_simple_ray(dataset_file, start_position, end_position,

ion_list = _determine_ions_from_lines(line_database, lines)

particle_type = _determine_particle_type(ds)

if (not particle_type) and \
(isinstance(ds.index, ParticleIndex)):
mylog.warning("Adding grid-based ion fields to SPH dataset. This is probably wrong.")
mylog.warning("To correct, change `ftype` in make_simple_ray() to SPH field type.")
sampling_type = _check_sampling_types_match(ds, ftype)

fields, fields_to_add_to_ds = _determine_fields_from_ions(ds, ion_list,
fields, ftype, particle_type)
fields, ftype, sampling_type)

# actually add the fields we need to add to the dataset
for atom, ion_state in fields_to_add_to_ds:
add_ion_number_density_field(atom, ion_state, ds,
ftype=ftype,
ionization_table=ionization_table,
particle_type=particle_type)
sampling_type=sampling_type)

# To assure there are no fields that are double specified or that collide
# based on being specified as "density" as well as ("gas", "density"),
Expand Down Expand Up @@ -516,7 +510,7 @@ def make_compound_ray(parameter_filename, simulation_type,
# to the field list for the ray. If not, we have to create them.

# We use the final dataset from the simulation in order to test it for
# the particle_type of the field, what fields are present, etc. This
# the sampling_type of the field, what fields are present, etc. This
# all assumes that the fields present in this output will be present in
# ALL outputs. Hopefully this is true, because testing each dataset
# is going to be slow and a pain.
Expand All @@ -529,15 +523,10 @@ def make_compound_ray(parameter_filename, simulation_type,

ion_list = _determine_ions_from_lines(line_database, lines)

particle_type = _determine_particle_type(ds)

if (not particle_type) and \
(isinstance(ds.index, ParticleIndex)):
mylog.warning("Adding grid-based ion fields to SPH dataset. This is probably wrong.")
mylog.warning("To correct, change `ftype` in make_compound_ray() to SPH field type.")
sampling_type = _check_sampling_types_match(ds, ftype)

fields, fields_to_add_to_ds = _determine_fields_from_ions(ds, ion_list,
fields, ftype, particle_type)
fields, ftype, sampling_type)

# actually add the fields we need to add to the dataset
# by adding the fields to the setup_function passed to each dataset
Expand All @@ -549,7 +538,7 @@ def setup_ds(ds):
add_ion_number_density_field(atom, ion_state, ds,
ftype=ftype,
ionization_table=ionization_table,
particle_type=particle_type)
sampling_type=sampling_type)
if setup_function is not None:
setup_function(ds)

Expand Down Expand Up @@ -601,12 +590,12 @@ def _determine_ions_from_lines(line_database, lines):

return uniquify(ion_list)

def _determine_fields_from_ions(ds, ion_list, fields, ftype, particle_type):
def _determine_fields_from_ions(ds, ion_list, fields, ftype, sampling_type):
"""
Figure out what fields need to be added based on the ions present
and the particle type
For sph (determined by particle_type):
For sph (determined by sampling_type):
Identify if the number_density fields for the desired ions already
exist on the dataset, and if so, just add them to the list
of fields to be added to the ray.
Expand Down Expand Up @@ -641,7 +630,7 @@ def _determine_fields_from_ions(ds, ion_list, fields, ftype, particle_type):
# type or not.
if (ftype, field) not in ds.derived_field_list:
if (ftype, alias_field) not in ds.derived_field_list:
if particle_type is True:
if sampling_type == 'particle':
fields_to_add_to_ds.append((atom, ion_state))
fields.append(("gas", alias_field))
else:
Expand Down
38 changes: 36 additions & 2 deletions trident/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
load
from yt.geometry.particle_geometry_handler import \
ParticleIndex
from yt.funcs import \
mylog

def ensure_directory(directory):
"""
Expand Down Expand Up @@ -471,7 +473,7 @@ def import_check():
The Trident package does not work correctly when imported from its
installation directory. Please try moving to another directory.""")

def _determine_particle_type(ds):
def _determine_dataset_sampling_type(ds):
"""
Determine whether the dataset is particle-based or grid-based.
Expand All @@ -488,4 +490,36 @@ def _determine_particle_type(ds):
# Sometimes data_type is not defined.
if getattr(ds, "data_type", None) == "yt_light_ray":
part_type = False
return part_type
if part_type:
return "particle"
else:
return "cell"

def _check_sampling_types_match(ds, ftype):
"""
Checks if sampling_type of field and dataset matches.
Users may not always put the correct ftype for their datasets, and this
can lead to errors if a user specifies a grid-based field for a
particle-dataset, which will lead to adding ion fields to deposited (and
already interpolated) fields with unexpected behavior. This alerts the
user when it thinks this has happened.
"""
# Determine the sampling type (e.g., "particle" or "cell") for the
# dataset as a whole.
sampling_type = _determine_dataset_sampling_type(ds)

# Determine the sampling type (e.g., "particle" or "cell") for the
# fields of "ftype" specified by the user.
ds.index
field_sampling_type = ds.field_info[ftype, 'density'].sampling_type

if sampling_type != field_sampling_type:
mylog.warning("===================================================")
mylog.warning("MISMATCH BETWEEN SAMPLING_TYPE OF FTYPE AND DATASET")
mylog.warning("sampling_type of (%s, 'density') = %s" % (ftype, field_sampling_type))
mylog.warning("sampling_type of dataset = %s" % sampling_type)
mylog.warning("THIS IS PROBABLY UNDESIRED BEHAVIOR. PLEASE CHOOSE A DIFFERENT FTYPE.")
mylog.warning("===================================================")

return sampling_type

0 comments on commit 19b7349

Please sign in to comment.