|
4 | 4 | import os |
5 | 5 | import sys |
6 | 6 | import tempfile |
| 7 | +import numpy.testing as npt |
7 | 8 | home = os.path.dirname(__file__) |
8 | 9 | work = tempfile.gettempdir() |
| 10 | +work='/tmp/' |
9 | 11 |
|
10 | 12 | import importlib # fixup path if GSASII not installed into Python |
11 | 13 | if importlib.util.find_spec('GSASII') is None: |
12 | | - print('Path hacking!') |
| 14 | + print('GSAS-II not installed in Python: Hacking sys.path') |
13 | 15 | os.environ["GSASII_YOLO_PATH"] = "True" |
14 | 16 | sys.path.append(os.path.dirname(home)) |
15 | 17 |
|
16 | 18 | import GSASII.GSASIIscriptable as G2sc |
17 | 19 |
|
18 | | -dataloc = lambda fil: os.path.join(home,'testinp',fil) |
19 | | -workloc = lambda fil: os.path.join(work,fil) |
20 | | -gpx = G2sc.G2Project(newgpx=workloc('test_scripting.gpx')) |
21 | | -# setup step 1: add two histograms to the project |
22 | | -hist1 = gpx.add_powder_histogram(dataloc("PBSO4.XRA"),dataloc("INST_XRY.PRM"), |
23 | | - fmthint='GSAS powder') |
24 | | -hist2 = gpx.add_powder_histogram(dataloc("PBSO4.CWN"),dataloc("inst_d1a.prm"), |
25 | | - fmthint='GSAS powder') |
26 | | -# setup step 2: add a phase and link it to the previous histograms |
27 | | -phase0 = gpx.add_phase(dataloc("PbSO4-Wyckoff.cif"), |
28 | | - phasename="PbSO4", histograms=[hist1,hist2]) |
29 | | -gpx.set_Controls('cycles', 0) |
30 | | -gpx.refine() |
31 | | -print(hist1.residuals['wR']) |
32 | | -print(hist2.residuals['wR']) |
33 | | -gpx.set_Controls('cycles', 8) |
34 | | -refdict0 = {"set": {"Background": { "no. coeffs": 3, "refine": True }}} |
35 | | -gpx.do_refinements([refdict0]) |
36 | | -print(hist1.residuals['wR']) |
37 | | -print(hist2.residuals['wR']) |
| 20 | +def test_refine(): |
| 21 | + def testR(msg,w1,w2): |
| 22 | + print(f"*** {msg}: Rwp(h1)={h1.residuals['wR']:.5f}, Rwp(h2)={h2.residuals['wR']:.5f}") |
| 23 | + npt.assert_allclose([h1.residuals['wR'],h2.residuals['wR']], |
| 24 | + [w1,w2], rtol=0.0001) |
| 25 | + |
| 26 | + print('test_refine(): test a small refinement') |
| 27 | + dataloc = lambda fil: os.path.join(home,'testinp',fil) |
| 28 | + workloc = lambda fil: os.path.join(work,fil) |
| 29 | + gpx = G2sc.G2Project(newgpx=workloc('test_scripting.gpx')) |
| 30 | + # setup step 1: add two histograms to the project |
| 31 | + h1 = gpx.add_powder_histogram(dataloc("PBSO4.XRA"),dataloc("INST_XRY.PRM"), |
| 32 | + fmthint='GSAS powder') |
| 33 | + h2 = gpx.add_powder_histogram(dataloc("PBSO4.CWN"),dataloc("inst_d1a.prm"), |
| 34 | + fmthint='GSAS powder') |
| 35 | + # setup step 2: add a phase and link it to the previous histograms |
| 36 | + phase0 = gpx.add_phase(dataloc("PbSO4-Wyckoff.cif"), |
| 37 | + phasename="PbSO4", histograms=[h1,h2]) |
| 38 | + gpx.set_Controls('cycles', 0) |
| 39 | + gpx.refine() |
| 40 | + testR('Before fitting',96.681098,99.748994) |
| 41 | + # |
| 42 | + h1.set_refinements({'Limits': [16.,158.4]}) |
| 43 | + h2.set_refinements({'Limits': [19.,153.]}) |
| 44 | + gpx.set_Controls('cycles', 8) |
| 45 | + h1.set_refinements({"Background": { "no. coeffs": 6, "refine": True }}) |
| 46 | + h2.set_refinements({"Background": { "no. coeffs": 3, "refine": True }}) |
| 47 | + gpx.refine() |
| 48 | + testR('Fit scale & bkg',40.64193551740201,18.6189841945785) |
| 49 | + # |
| 50 | + phase0.set_refinements({'Cell':True}) |
| 51 | + phase0.set_HAP_refinements({'HStrain':True},[h2]) |
| 52 | + gpx.refine() |
| 53 | + testR('Fit cells',30.072804646662338,15.014744642359773) |
| 54 | + # |
| 55 | + phase0.set_HAP_refinements({'Mustrain':{'refine':True}},[h1]) |
| 56 | + #phase0.set_HAP_refinements({'Size':{'refine':True}},[h1]) |
| 57 | + h1.set_refinements({"Sample Parameters": {"Shift": True}}) |
| 58 | + h2.set_refinements({"Sample Parameters":["DisplaceX","DisplaceY"]}) |
| 59 | + phase0.set_refinements({"Atoms":{"all":"XU"}}) |
| 60 | + gpx.refine() |
| 61 | + testR('add Mustrain, Shift, Displace[XY], atomic X & Uiso', |
| 62 | + 12.66845815113383,6.695761603085025) |
| 63 | + # |
| 64 | + h1.set_refinements({'Instrument Parameters': ['U', 'V', 'W']}) |
| 65 | + h2.set_refinements({'Instrument Parameters': ['U', 'V', 'W']}) |
| 66 | + gpx.refine() |
| 67 | + testR('add UVW',10.518485346229324,4.495180030877684) |
| 68 | + print('OK') |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + test_refine() |
0 commit comments