[yt-svn] commit/yt: xarthisius: Merged in yihaochen/yt (pull request #1988)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Wed Feb 17 09:04:36 PST 2016
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/a7cce4a6f076/
Changeset: a7cce4a6f076
Branch: yt
User: xarthisius
Date: 2016-02-17 17:04:30+00:00
Summary: Merged in yihaochen/yt (pull request #1988)
Fixing add_deposited_particle_field. Closes #1170
Affected #: 2 files
diff -r af128f1c867d7f72132699e5d243a37583c36570 -r a7cce4a6f076ddd2a1f122c5ea9cf8fde71aed8e yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -988,7 +988,8 @@
deps, _ = self.field_info.check_derived_fields([name])
self.field_dependencies.update(deps)
- def add_deposited_particle_field(self, deposit_field, method, kernel_name='cubic'):
+ def add_deposited_particle_field(self, deposit_field, method, kernel_name='cubic',
+ weight_field='particle_mass'):
"""Add a new deposited particle field
Creates a new deposited field based on the particle *deposit_field*.
@@ -1003,13 +1004,15 @@
method : string
This is the "method name" which will be looked up in the
`particle_deposit` namespace as `methodname_deposit`. Current
- methods include `count`, `simple_smooth`, `sum`, `std`, `cic`,
- `weighted_mean`, `mesh_id`, and `nearest`.
+ methods include `simple_smooth`, `sum`, `std`, `cic`, `weighted_mean`,
+ `mesh_id`, and `nearest`.
kernel_name : string, default 'cubic'
This is the name of the smoothing kernel to use. It is only used for
the `simple_smooth` method and is otherwise ignored. Current
supported kernel names include `cubic`, `quartic`, `quintic`,
`wendland2`, `wendland4`, and `wendland6`.
+ weight_field : string, default 'particle_mass'
+ Weighting field name for deposition method `weighted_mean`.
Returns
-------
@@ -1021,38 +1024,45 @@
ptype, deposit_field = deposit_field[0], deposit_field[1]
else:
raise RuntimeError
+
units = self.field_info[ptype, deposit_field].units
+ take_log = self.field_info[ptype, deposit_field].take_log
+ name_map = {"sum": "sum", "std":"std", "cic": "cic", "weighted_mean": "avg",
+ "nearest": "nn", "simple_smooth": "ss", "count": "count"}
+ field_name = "%s_" + name_map[method] + "_%s"
+ field_name = field_name % (ptype, deposit_field.replace('particle_', ''))
+
+ if method == "count":
+ field_name = "%s_count" % ptype
+ if ("deposit", field_name) in self.field_info:
+ mylog.warning("The deposited field %s already exists" % field_name)
+ return ("deposit", field_name)
+ else:
+ units = "dimensionless"
+ take_log = False
def _deposit_field(field, data):
"""
- Create a grid field for particle wuantities weighted by particle
- mass, using cloud-in-cell deposition.
+ Create a grid field for particle quantities using given method.
"""
pos = data[ptype, "particle_position"]
- # get back into density
- if method != 'count':
- pden = data[ptype, "particle_mass"]
- top = data.deposit(pos, [data[(ptype, deposit_field)]*pden],
- method=method, kernel_name=kernel_name)
- bottom = data.deposit(pos, [pden], method=method,
- kernel_name=kernel_name)
- top[bottom == 0] = 0.0
- bnz = bottom.nonzero()
- top[bnz] /= bottom[bnz]
- d = data.ds.arr(top, input_units=units)
+ if method == 'weighted_mean':
+ d = data.ds.arr(data.deposit(pos, [data[ptype, deposit_field],
+ data[ptype, weight_field]],
+ method=method, kernel_name=kernel_name),
+ input_units=units)
+ d[np.isnan(d)] = 0.0
else:
d = data.ds.arr(data.deposit(pos, [data[ptype, deposit_field]],
- method=method,
- kernel_name=kernel_name))
+ method=method, kernel_name=kernel_name),
+ input_units=units)
return d
- name_map = {"cic": "cic", "sum": "nn", "count": "count"}
- field_name = "%s_" + name_map[method] + "_%s"
- field_name = field_name % (ptype, deposit_field.replace('particle_', ''))
+
self.add_field(
("deposit", field_name),
function=_deposit_field,
units=units,
- take_log=False,
+ take_log=take_log,
validators=[ValidateSpatial()])
return ("deposit", field_name)
diff -r af128f1c867d7f72132699e5d243a37583c36570 -r a7cce4a6f076ddd2a1f122c5ea9cf8fde71aed8e yt/fields/tests/test_fields.py
--- a/yt/fields/tests/test_fields.py
+++ b/yt/fields/tests/test_fields.py
@@ -33,7 +33,7 @@
fields.append(("gas", fname))
units.append(code_units)
- base_ds = fake_random_ds(4, fields=fields, units=units, particles=10)
+ base_ds = fake_random_ds(4, fields=fields, units=units, particles=20)
base_ds.index
base_ds.cosmological_simulation = 1
@@ -195,12 +195,28 @@
yield TestFieldAccess(field, nproc)
def test_add_deposited_particle_field():
+ # NOT tested: "std", "mesh_id", "nearest" and "simple_smooth"
global base_ds
- fn = base_ds.add_deposited_particle_field(('io', 'particle_ones'), 'count')
- assert_equal(fn, ('deposit', 'io_count_ones'))
ad = base_ds.all_data()
+
+ # Test "count", "sum" and "cic" method
+ for method in ["count", "sum", "cic"]:
+ fn = base_ds.add_deposited_particle_field(('io', 'particle_mass'), method)
+ expected_fn = 'io_%s' if method == "count" else 'io_%s_mass'
+ assert_equal(fn, ('deposit', expected_fn % method))
+ ret = ad[fn]
+ if method == "count":
+ assert_equal(ret.sum(), ad['particle_ones'].sum())
+ else:
+ assert_almost_equal(ret.sum(), ad['particle_mass'].sum())
+
+ # Test "weighted_mean" method
+ fn = base_ds.add_deposited_particle_field(('io', 'particle_ones'), 'weighted_mean',
+ weight_field='particle_ones')
+ assert_equal(fn, ('deposit', 'io_avg_ones'))
ret = ad[fn]
- assert_equal(ret.sum(), ad['particle_ones'].sum())
+ # The sum should equal the number of cells that have particles
+ assert_equal(ret.sum(), np.count_nonzero(ad[("deposit", "io_count")]))
@requires_file('GadgetDiskGalaxy/snapshot_200.hdf5')
def test_add_smoothed_particle_field():
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list