Skip to content

Commit dea8eb9

Browse files
committed
DIFFaX self-test; fix Layers imports & plotting
1 parent a7b6cae commit dea8eb9

File tree

4 files changed

+128
-17
lines changed

4 files changed

+128
-17
lines changed

GSASII/GSASIIphsGUI.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9199,7 +9199,7 @@ def AtomTypeSelect(event):
91999199

92009200
def OnDrawLayer(event):
92019201
drawLayer.SetValue(False)
9202-
G2plt.PlotLayers(G2frame,Layers,[il,],plotDefaults)
9202+
G2plt.PlotLayers(G2frame,Layers,[il,],plotDefaults,firstCall=True)
92039203

92049204
def OnSameAs(event):
92059205
Layer['SameAs'] = sameas.GetValue()
@@ -9268,7 +9268,7 @@ def PlotSelect(event):
92689268
Yi = Indx[Obj.GetId()]
92699269
Xi,c = event.GetRow(),event.GetCol()
92709270
if Xi >= 0 and c == 5: #plot column
9271-
G2plt.PlotLayers(G2frame,Layers,[Yi,Xi,],plotDefaults)
9271+
G2plt.PlotLayers(G2frame,Layers,[Yi,Xi,],plotDefaults,firstCall=True)
92729272
else:
92739273
Psum = 0.
92749274
for Xi in range(len(transArray)):
@@ -9365,7 +9365,7 @@ def OnPlotSeq(event):
93659365
except ValueError:
93669366
plotSeq.SetValue('Error in string '+plotSeq.GetValue())
93679367
return
9368-
G2plt.PlotLayers(G2frame,Layers,vals,plotDefaults)
9368+
G2plt.PlotLayers(G2frame,Layers,vals,plotDefaults,firstCall=True)
93699369

93709370
Names = [' %s: %d,'%(layer['Name'],iL+1) for iL,layer in enumerate(Layers['Layers'])]
93719371
plotSizer = wx.BoxSizer(wx.VERTICAL)
@@ -9640,20 +9640,20 @@ def OnSimulate(event):
96409640

96419641
if 'PWDR' in simCodes[0]: #powder pattern
96429642
data['Layers']['selInst'] = simCodes[1]
9643-
UseList = []
9644-
for item in data['Histograms']:
9645-
if 'PWDR' in item:
9646-
UseList.append(item)
9643+
UseList = [item for item in data['Histograms'] if 'PWDR' in item]
96479644
if not UseList:
96489645
wx.MessageBox('No PWDR data for this phase to simulate',caption='Data error',style=wx.ICON_EXCLAMATION)
96499646
return
9650-
dlg = wx.SingleChoiceDialog(G2frame,'Data to simulate','Select',UseList)
9651-
if dlg.ShowModal() == wx.ID_OK:
9652-
sel = dlg.GetSelection()
9653-
HistName = UseList[sel]
9647+
elif len(UseList) == 1: # don't ask questions when we know the answer!
9648+
HistName = UseList[0]
96549649
else:
9655-
return
9656-
dlg.Destroy()
9650+
dlg = wx.SingleChoiceDialog(G2frame,'Data to simulate','Select',UseList)
9651+
if dlg.ShowModal() == wx.ID_OK:
9652+
sel = dlg.GetSelection()
9653+
HistName = UseList[sel]
9654+
else:
9655+
return
9656+
dlg.Destroy()
96579657
G2frame.PatternId = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,HistName)
96589658
sample = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(
96599659
G2frame,G2frame.PatternId, 'Sample Parameters'))

GSASII/GSASIIplot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8159,11 +8159,11 @@ def UpdateDraw():
81598159
Draw('main')
81608160
Draw('main') #to fill both buffers so save works
81618161
if rbType == 'Vector': return UpdateDraw
8162+
81628163
#### Plot Layers ################################################################################
8163-
def PlotLayers(G2frame,Layers,laySeq,defaults):
8164+
def PlotLayers(G2frame,Layers,laySeq,defaults,firstCall=False):
81648165
'''Layer plotting package. Can show layer structures as balls & sticks
81658166
'''
8166-
81678167
global AtNames,AtTypes,XYZ,Bonds,Faces
81688168

81698169
def FindBonds(atTypes,XYZ):
@@ -8620,6 +8620,8 @@ def OnFocus(event):
86208620
Page.camera['backColor'] = np.array([0,0,0,0])
86218621
Page.context = wx.glcanvas.GLContext(Page.canvas)
86228622
Page.canvas.SetCurrent(Page.context)
8623+
if firstCall: # on Mac need to draw twice, but needs to be delayed
8624+
wx.CallAfter(Draw,'main')
86238625
wx.CallAfter(Draw,'main')
86248626

86258627
#### Plot Cluster Analysis ####################################################

GSASII/GSASIIpwd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4929,7 +4929,7 @@ def StackSim(Layers,ctrls,scale=0.,background={},limits=[],inst={},profile=[]):
49294929
49304930
Note that parameters all updated in place
49314931
'''
4932-
import atmdata
4932+
from . import atmdata
49334933
path = sys.path
49344934
for name in path:
49354935
if 'bin' in name:
@@ -5106,7 +5106,7 @@ def SetPWDRscan(inst,limits,profile):
51065106

51075107
def SetStackingSF(Layers,debug):
51085108
# Load scattering factors into DIFFaX arrays
5109-
import atmdata
5109+
from . import atmdata
51105110
atTypes = Layers['AtInfo'].keys()
51115111
aTypes = []
51125112
for atype in atTypes:

tests/test_diffax.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# perform a DIFFaX computation using the GSAS-II interface to the
2+
# pydiffax.so/.pyd module
3+
4+
import os
5+
import sys
6+
import tempfile
7+
import numpy as np
8+
import numpy.testing as npt
9+
home = os.path.dirname(__file__)
10+
work = tempfile.gettempdir()
11+
12+
import importlib.util
13+
G2loc = None
14+
try:
15+
G2loc = importlib.util.find_spec('GSASII.GSASIIpwd')
16+
except ModuleNotFoundError:
17+
print('ModuleNotFound for GSASII.GSASIIpwd')
18+
19+
if G2loc is None: # fixup path if GSASII not installed into Python
20+
print('GSAS-II not installed in Python; Hacking sys.path')
21+
sys.path.append(os.path.dirname(home))
22+
23+
import GSASII
24+
#import GSASII.GSASIIscriptable as G2sc # sets up access to binaries
25+
import GSASII.GSASIIpwd as G2pwd
26+
27+
def test_diffax():
28+
print('test_diffax(): test a small DIFFaX computation')
29+
# define input needed for computation
30+
layerinfo = {
31+
'Layers': [{'SameAs': '','Name': 'layer 1','Symm': '-1',
32+
'Atoms': [['C1','C',-1./3.,-3./18.,-1/8,1.0, 0.01]]},
33+
{'SameAs': '','Name': 'layer 2','Symm': '-1',
34+
'Atoms': [['C1','C', 1./3., 3./18.,-1/8,1.0, 0.01]]},],
35+
'AtInfo': {'C': {'Isotopes': {
36+
'13': {'SA': 0.00137,'Mass': 13.003,'SL': [0.619, 0]},
37+
'12': {'SA': 0.00353, 'Mass': 12.0, 'SL': [0.6654, 0]},
38+
'Nat. Abund.': {'SA': 0.0035, 'Mass': 12.011, 'SL': [0.6648, 0]}},
39+
'Vdrad': 1.95, 'Color': (144, 144, 144), 'Symbol': 'C', 'Lande g': 2.0,
40+
'Drad': 1.12, 'Mass': 12.011, 'Arad': 0.92, 'Z': 6, 'Hbrad': 0}},
41+
'allowedTrans': [['1', '1'], ['1', '2'], ['2', '1'], ['2', '2']],
42+
'seqCodes': ('TransP;0;0', [0.0, 1.0], 10),
43+
'SymTrans': True,
44+
'Stacking': ['recursive', 'infinite', ''],
45+
'Laue': '6/mmm',
46+
'Cell': [False, 2.522, 2.522, 2.059, 90.0, 90.0, 120.0, 11.341673551466423],
47+
'Width': [[1.0, 1.0], [False, False]],
48+
'Sadp': {},
49+
'seqResults': [],
50+
'Toler': 0.01,
51+
'Transitions': [
52+
[[0.7, 2./3., 1./3., 1.0, '', False],
53+
[0.3, 0.0, 0.0, 1.0, '', False]],
54+
[[0.3, 0.0, 0.0, 1.0, '', False],
55+
[0.7, -2./3., -1./3., 1.0, '', False]]],
56+
'selInst': 'Gaussian'}
57+
bkg = [['chebyschev', True, 3, 50.0, 0.0, 0.0],
58+
{'peaksList': [],
59+
'debyeTerms': [],
60+
'nPeaks': 0,
61+
'background PWDR': ['', -1.0],
62+
'nDebye': 0}]
63+
profile=[
64+
np.linspace(10,150,7001),
65+
np.zeros(7001), np.zeros(7001), np.zeros(7001),
66+
np.zeros(7001), np.zeros(7001),]
67+
inst = {'I(L2)/I(L1)': [0.5, 0.5, False],
68+
'SH/L': [0.002, 0.002, False],
69+
'V': [-2.0, -2.0, False],
70+
'Lam2': [1.5443, 1.5443, False],
71+
'Source': ['CuKa', '?'],
72+
'Zero': [0.0, 0.0, False],
73+
'Lam1': [1.5405, 1.5405, False],
74+
'U': [2.0, 2.0, False],
75+
'W': [5.0, 5.0, False],
76+
'Azimuth': [0.0, 0.0, False],
77+
'Y': [0.0, 0.0, False],
78+
'X': [0.0, 0.0, False],
79+
'Type': ['PXC', 'PXC', False],
80+
'Bank': [1.0, 1.0, False],
81+
'Polariz.': [0.7, 0.7, False]}
82+
83+
G2pwd.CalcStackingPWDR(layerinfo,1000.,bkg,[10.,150.],inst,profile,False)
84+
#import matplotlib.pyplot as plt
85+
#plt.plot(profile[0],profile[3])
86+
#plt.show()
87+
assert abs(profile[3][:7000].max()-5230.06) < 0.1, 'Max value off'
88+
assert abs(profile[3][:7000].min()-50.03) < 0.1, 'Min value off'
89+
msg = 'deviations in 42-43 deg section'
90+
npt.assert_allclose(profile[3][1600:1651],
91+
np.array([200.48310247, 200.63761559, 200.8637708 , 201.15861368,
92+
201.51951493, 201.94413305, 202.43038195, 202.97640301,
93+
203.58054064, 204.24132092, 204.95743283, 205.72771164,
94+
206.55112422, 207.42675591, 208.35379877, 209.33154098,
95+
210.35935728, 211.43670024, 212.56309224, 213.73811812,
96+
214.96141832, 216.23268245, 217.55164331, 218.91807113,
97+
220.33176811, 221.79256313, 223.30030665, 224.85486568,
98+
226.45611886, 228.10395155, 229.79825094, 231.53890116,
99+
233.32577831, 235.15874548, 237.03764759, 238.96230622,
100+
240.93251424, 242.94803037, 245.00857357, 247.11381723,
101+
249.26338327, 251.45683604, 253.69367607, 255.97333369,
102+
258.29516247, 260.6584326 , 263.06232408, 265.50591998,
103+
267.98819954, 270.5080314 , 273.06416684]), rtol=0.0001, err_msg=msg)
104+
105+
if __name__ == '__main__':
106+
#import time
107+
#start = time.time()
108+
test_diffax()
109+
#print('elapsed=',time.time()-start)

0 commit comments

Comments
 (0)