source: trunk/GSASIImpsubs.py @ 3136

Last change on this file since 3136 was 3136, checked in by vondreele, 4 years ago

make GSAS-II python 3.6 compliant & preserve python 2.7 use;changes:
do from future import division, print_function for all GSAS-II py sources
all menu items revised to be py 2.7/3.6 compliant
all wx.OPEN --> wx.FD_OPEN in file dialogs
all integer divides (typically for image pixel math) made explicit with ; ambiguous ones made floats as appropriate
all print "stuff" --> print (stuff)
all print >> pFile,'stuff' --> pFile.writeCIFtemplate('stuff')
all read file opens made explicit 'r' or 'rb'
all cPickle imports made for py2.7 or 3.6 as cPickle or _pickle; test for '2' platform.version_tuple[0] for py 2.7
define cPickleload to select load(fp) or load(fp,encoding='latin-1') for loading gpx files; provides cross compatibility between py 2.7/3.6 gpx files
make dict.keys() as explicit list(dict.keys()) as needed (NB: possible source of remaining py3.6 bugs)
make zip(a,b) as explicit list(zip(a,b)) as needed (NB: possible source of remaining py3.6 bugs)
select unichr/chr according test for '2' platform.version_tuple[0] for py 2.7 (G2pwdGUI * G2plot) for special characters
select wg.EVT_GRID_CELL_CHANGE (classic) or wg.EVT_GRID_CELL_CHANGED (phoenix) in grid Bind
maxint --> maxsize; used in random number stuff
raise Exception,"stuff" --> raise Exception("stuff")
wx 'classic' sizer.DeleteWindows?() or 'phoenix' sizer.Clear(True)
wx 'classic' SetToolTipString?(text) or 'phoenix' SetToolTip?(wx.ToolTip?(text)); define SetToolTipString?(self,text) to handle the choice in plots
status.SetFields? --> status.SetStatusText?
'classic' AddSimpleTool? or 'phoenix' self.AddTool? for plot toolbar; Bind different as well
define GetItemPydata? as it doesn't exist in wx 'phoenix'
allow python versions 2.7 & 3.6 to run GSAS-II
Bind override commented out - no logging capability (NB: remove all logging code?)
all import ContentsValidator? open filename & test if valid then close; filepointer removed from Reader
binary importers (mostly images) test for 'byte' type & convert as needed to satisfy py 3.6 str/byte rules

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1# -*- coding: utf-8 -*-
2'''
3*GSASIImpsubs - routines used in multiprocessing*
4-------------------------------------------------
5
6The routines here are called either directly when GSAS-II is used without multiprocessing
7or in separate cores when multiprocessing is used.
8
9These routines are designed to be used in one of two ways:
10
11 * when multiprocessing is
12   enabled (see global variable useMP) the computational routines are called in
13   separate Python interpreter that is created and then deleted after use.
14
15 * when useMP is False, these routines are called directly from the main "thread".
16
17Note that :func:`GSASIImpsubs.InitMP` should be called before any of the other routines
18in this module are used.
19'''
20########### SVN repository information ###################
21# $Date: $
22# $Author: $
23# $Revision: $
24# $URL: $
25# $Id: $
26########### SVN repository information ###################
27from __future__ import division, print_function
28import multiprocessing as mp
29import numpy as np
30import numpy.ma as ma
31import GSASIIpath
32GSASIIpath.SetVersionNumber("$Revision: 2895 $")
33import GSASIIpwd as G2pwd
34
35sind = lambda x: np.sin(x*np.pi/180.)
36cosd = lambda x: np.cos(x*np.pi/180.)
37tand = lambda x: np.tan(x*np.pi/180.)
38#asind = lambda x: 180.*np.arcsin(x)/np.pi
39#acosd = lambda x: 180.*np.arccos(x)/np.pi
40#atan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi
41   
42ncores = None
43
44def ResetMP():
45    '''Call after changing Config var 'Multiprocessing_cores' to force a resetting
46    of the useMP from the parameter.
47    '''
48    global ncores
49    ncores = None
50   
51def InitMP(allowMP=True):
52    '''Called to initialize use of Multiprocessing
53    '''
54    global useMP,ncores
55    if ncores is not None: return useMP,ncores
56    useMP = False
57    if not allowMP:
58        print('Multiprocessing disabled')
59        ncores = 0
60        return useMP,ncores
61    ncores = GSASIIpath.GetConfigValue('Multiprocessing_cores',0)
62    if ncores < 0: ncores = mp.cpu_count()/2
63    if ncores > 1:
64        useMP = True
65    #if GSASIIpath.GetConfigValue('debug') and useMP:
66    if useMP:
67        print('Multiprocessing with {} cores enabled'.format(ncores))
68    return useMP,ncores
69
70################################################################################
71# Fobs Squared computation
72################################################################################       
73def InitFobsSqGlobals(x1,ratio1,shl1,xB1,xF1,im1,lamRatio1,kRatio1,xMask1,Ka21):
74    '''Initialize for the computation of Fobs Squared for powder histograms.
75    Puts lots of junk into the global namespace in this module.
76    '''
77    global x,ratio,shl,xB,xF,im,lamRatio,kRatio,xMask,Ka2
78    x = ma.getdata(x1)
79    ratio = ratio1
80    shl = shl1
81    xB = xB1
82    xF = xF1
83    im = im1
84    lamRatio = lamRatio1
85    kRatio = kRatio1
86    xMask = xMask1
87    Ka2 = Ka21
88
89def ComputeFobsSqCWbatch(profList):
90    sInt = 0
91    resList = []
92    for refl,iref in profList:
93        icod = ComputeFobsSqCW(refl,iref)
94        if type(icod) is tuple:
95            resList.append((icod[0],iref))
96            sInt += icod[1]
97        elif icod == -1:
98            resList.append((None,iref))
99        elif icod == -2:
100            break
101    return sInt,resList
102
103def ComputeFobsSqTOFbatch(profList):
104    sInt = 0
105    resList = []
106    for refl,iref in profList:
107        icod = ComputeFobsSqTOF(refl,iref)
108        if type(icod) is tuple:
109            resList.append((icod[0],iref))
110            sInt += icod[1]
111        elif icod == -1:
112            resList.append((None,iref))
113        elif icod == -2:
114            break
115    return sInt,resList
116       
117def ComputeFobsSqCW(refl,iref):
118    yp = np.zeros(len(x)) # not masked
119    sInt = 0
120    refl8im = 0
121    Wd,fmin,fmax = G2pwd.getWidthsCW(refl[5+im],refl[6+im],refl[7+im],shl)
122    iBeg = max(xB,np.searchsorted(x,refl[5+im]-fmin))
123    iFin = max(xB,min(np.searchsorted(x,refl[5+im]+fmax),xF))
124    iFin2 = iFin
125    if not iBeg+iFin:       #peak below low limit - skip peak
126        return 0
127    if ma.all(xMask[iBeg:iFin]):    #peak entirely masked - skip peak
128        return -1
129    elif not iBeg-iFin:     #peak above high limit - done
130        return -2
131    elif iBeg < iFin:
132        yp[iBeg:iFin] = refl[11+im]*refl[9+im]*G2pwd.getFCJVoigt3(
133            refl[5+im],refl[6+im],refl[7+im],shl,x[iBeg:iFin])
134        sInt = refl[11+im]*refl[9+im]
135        if Ka2:
136            pos2 = refl[5+im]+lamRatio*tand(refl[5+im]/2.0)       # + 360/pi * Dlam/lam * tan(th)
137            Wd,fmin,fmax = G2pwd.getWidthsCW(pos2,refl[6+im],refl[7+im],shl)
138            iBeg2 = max(xB,np.searchsorted(x,pos2-fmin))
139            iFin2 = min(np.searchsorted(x,pos2+fmax),xF)
140            if iFin2 > iBeg2: 
141                yp[iBeg2:iFin2] += refl[11+im]*refl[9+im]*kRatio*G2pwd.getFCJVoigt3(
142                    pos2,refl[6+im],refl[7+im],shl,x[iBeg2:iFin2])
143                sInt *= 1.+kRatio
144    refl8im = np.sum(np.where(ratio[iBeg:iFin2]>0.,yp[iBeg:iFin2]*ratio[iBeg:iFin2]/(refl[11+im]*(1.+kRatio)),0.0))
145    return refl8im,sInt
146
147def ComputeFobsSqTOF(refl,iref):
148    yp = np.zeros(len(x)) # not masked
149    refl8im = 0
150    Wd,fmin,fmax = G2pwd.getWidthsTOF(refl[5+im],refl[12+im],refl[13+im],refl[6+im],refl[7+im])
151    iBeg = max(xB,np.searchsorted(x,refl[5+im]-fmin))
152    iFin = max(xB,min(np.searchsorted(x,refl[5+im]+fmax),xF))
153    if not iBeg+iFin:       #peak below low limit - skip peak
154        return 0
155    if ma.all(xMask[iBeg:iFin]):    #peak entirely masked - skip peak
156        return -1
157    elif not iBeg-iFin:     #peak above high limit - done
158        return -2
159    if iBeg < iFin:
160        yp[iBeg:iFin] = refl[11+im]*refl[9+im]*G2pwd.getEpsVoigt(
161            refl[5+im],refl[12+im],refl[13+im],refl[6+im],refl[7+im],x[iBeg:iFin])
162    refl8im = np.sum(np.where(ratio[iBeg:iFin]>0.,yp[iBeg:iFin]*ratio[iBeg:iFin]/refl[11+im],0.0))
163    return refl8im,refl[11+im]*refl[9+im]
164################################################################################
165# Powder Profile computation
166################################################################################       
167def InitPwdrProfGlobals(im1,shl1,x1):
168    '''Initialize for the computation of Fobs Squared for powder histograms.
169    Puts lots of junk into the global namespace in this module.
170    '''
171    global im,shl,x
172    im = im1
173    shl = shl1
174    x = ma.getdata(x1)
175    global cw
176    cw = np.diff(x)
177    cw = np.append(cw,cw[-1])
178    # create local copies of ycalc array
179    global yc
180    yc = np.zeros_like(x)
181
182
183def ComputePwdrProfCW(profList):
184    'Compute the peaks profile for a set of CW peaks and add into the yc array'
185    for pos,refl,iBeg,iFin,kRatio in profList:
186        yc[iBeg:iFin] += refl[11+im]*refl[9+im]*kRatio*G2pwd.getFCJVoigt3(
187            pos,refl[6+im],refl[7+im],shl,x[iBeg:iFin])
188    return yc
189
190def ComputePwdrProfTOF(profList):
191    'Compute the peaks profile for a set of TOF peaks and add into the yc array'
192    for pos,refl,iBeg,iFin in profList:
193        yc[iBeg:iFin] += refl[11+im]*refl[9+im]*G2pwd.getEpsVoigt(
194            pos,refl[12+im],refl[13+im],refl[6+im],refl[7+im],x[iBeg:iFin])/cw[iBeg:iFin]
195    return yc
196   
Note: See TracBrowser for help on using the repository browser.