Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions diffpy/pdfmorph/morphs/morphishape.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MorphISpheroid -- apply inverse spheroidal shape function

from diffpy.pdfmorph.morphs.morph import *
from diffpy.pdfmorph.morphs.morphshape import _sphericalCF, _spheroidalCF
import numpy


class MorphISphere(Morph):
Expand All @@ -44,7 +45,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
"""Apply a scale factor."""
Morph.morph(self, x_morph, y_morph, x_target, y_target)
f = _sphericalCF(x_morph, 2 * self.iradius)
self.y_morph_out /= f
with numpy.errstate(divide='ignore', invalid='ignore'):
self.y_morph_out /= f
self.y_morph_out[f == 0] = 0
return self.xyallout

Expand Down Expand Up @@ -74,7 +76,8 @@ def morph(self, x_morph, y_morph, x_target, y_target):
"""Apply a scale factor."""
Morph.morph(self, x_morph, y_morph, x_target, y_target)
f = _spheroidalCF(x_morph, self.iradius, self.ipradius)
self.y_morph_out /= f
with numpy.errstate(divide='ignore', invalid='ignore'):
self.y_morph_out /= f
self.y_morph_out[f == 0] = 0
return self.xyallout

Expand Down
68 changes: 55 additions & 13 deletions diffpy/pdfmorph/tests/test_morphshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
testdata_dir = os.path.join(tests_dir, 'testdata')

from diffpy.pdfmorph.morphs.morphshape import MorphSphere, MorphSpheroid
from diffpy.pdfmorph.morphs.morphishape import MorphISphere, MorphISpheroid


class TestMorphSphere(unittest.TestCase):
def setUp(self):
morph_file = os.path.join(testdata_dir, "ni_qmax25.cgr")
self.x_morph, self.y_morph = numpy.loadtxt(morph_file, unpack=True)
target_file = os.path.join(testdata_dir, "ni_qmax25_psize30.cgr")
target_file = os.path.join(testdata_dir, "ni_qmax25_psize35.cgr")
self.x_target, self.y_target = numpy.loadtxt(target_file, unpack=True)
return

Expand All @@ -38,24 +39,48 @@ def test_morph(self):


class TestMorphSpheroid(unittest.TestCase):
# Common configs for testing MorphSpheroid and MorphISpheroid
# FIXME: add test data for prolate spheroids
config_sphere = {"radius": 17.5, "pradius": 17.5}
config_oblate = {"radius": 17.5, "pradius": 5.0}
spheroid_configs = [config_sphere, config_oblate]
iconfig_sphere = {"iradius": 17.5, "ipradius": 17.5}
iconfig_oblate = {"iradius": 17.5, "ipradius": 5.0}
ispheroid_configs = [iconfig_sphere, iconfig_oblate]

# Files used for testing
flag_inverse = 0 # Indicates whether we are testing MorphSpheroid or MorphISpheroid
testfiles = [
["ni_qmax25.cgr", "ni_qmax25_psize35.cgr"], # Sphere
["ni_qmax25.cgr", "ni_qmax25_e17.5_p5.0.cgr"], # Oblate spheroid
]
testfile = [] # Initialize testfile array

def setUp(self):
morph_file = os.path.join(testdata_dir, "ni_qmax25.cgr")
if len(self.testfile) == 0:
# Ignore first init
return
morph_file = os.path.join(testdata_dir, self.testfile[0 - self.flag_inverse])
self.x_morph, self.y_morph = numpy.loadtxt(morph_file, unpack=True)
target_file = os.path.join(testdata_dir, "ni_qmax25_e17.5_p5.0.cgr")
target_file = os.path.join(testdata_dir, self.testfile[1 - self.flag_inverse])
self.x_target, self.y_target = numpy.loadtxt(target_file, unpack=True)
return

def test_morph(self):
"""check MorphSphere.morph()"""
config_oblate = {
"radius": 17.5,
"pradius": 5.0,
}
# FIXME: add test data for prolate spheroids and spheres
configs = [config_oblate]

for config in configs:
self.shape_test_helper(config)
"""check MorphSpheroid.morph() and MorphISpheroid.morph()"""

for idx in range(len(self.testfiles)):
self.testfile = self.testfiles[idx]

# Test MorphSpheroid.morph()
self.flag_inverse = 0
self.setUp()
self.shape_test_helper(self.spheroid_configs[idx])

# Test MorphISpheroid.morph()
self.flag_inverse = 1
self.setUp()
self.ishape_test_helper(self.ispheroid_configs[idx])
return

def shape_test_helper(self, config):
Expand All @@ -67,6 +92,23 @@ def shape_test_helper(self, config):
self.assertTrue(numpy.allclose(y_morph, y_target))
return

def ishape_test_helper(self, config):
morph = MorphISpheroid(config)

x_morph, y_morph, x_target, y_target = morph(self.x_morph, self.y_morph, self.x_target, self.y_target)

self.assertTrue(numpy.allclose(self.y_target, y_target))

psize = 2 * max(config["iradius"], config["ipradius"])
for idx in range(len(x_morph)):
if x_morph[idx] < psize: # Within the particle
self.assertTrue(numpy.isclose(y_morph[idx], y_target[idx]))
elif x_morph[idx] == psize:
pass # FIXME: determine behavior at boundary
else: # Outside the particle morph should be zero
self.assertTrue(y_morph[idx] == 0)
return


# End of class TestMorphSpheroid

Expand Down
4 changes: 4 additions & 0 deletions diffpy/pdfmorph/tests/testdata/ni_qmax25.cgr
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# qmax=25

##### start data
#L r(A) G(r)
0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000021e-02 -1.243355089687663353e-02
2.000000000000000042e-02 -2.480463559665002649e-02
Expand Down
6 changes: 6 additions & 0 deletions diffpy/pdfmorph/tests/testdata/ni_qmax25_e17.5_p5.0.cgr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# qmax = 25
# erad = 17.5
# prad = 5.0

##### start data
#L r(A) G(r)
0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000021e-02 -1.242269673220141278e-02
2.000000000000000042e-02 -2.476132802191605836e-02
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# qmax = 25
# psize = 35

##### start data
#L r(A) G(r)
0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000021e-02 -1.242822223235153997e-02
2.000000000000000042e-02 -2.478337448273846069e-02
Expand Down
5 changes: 5 additions & 0 deletions diffpy/pdfmorph/tests/testdata/ni_qmax25_qdamp0.01.cgr
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# qmax = 25
# qdamp = 0.01

##### start data
#L r(A) G(r)
0.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000021e-02 -1.243355083470887995e-02
2.000000000000000042e-02 -2.480463510055731791e-02
Expand Down