source: trunk/GSASIImpsubs.py @ 3041

Last change on this file since 3041 was 3041, checked in by toby, 8 years ago

Fully implement multiprocessing on reflection loops, but currently disabled, when enabled default is still not to use unless turned on in config; add timing code

  • Property svn:eol-style set to native
File size: 6.7 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 ###################
27import multiprocessing as mp
28import numpy as np
29import numpy.ma as ma
30import numpy.linalg as nl
31import GSASIIpath
32GSASIIpath.SetVersionNumber("$Revision: 2895 $")
33import GSASIIpwd as G2pwd
34import GSASIIstrMath as G2stMth
35
36sind = lambda x: np.sin(x*np.pi/180.)
37cosd = lambda x: np.cos(x*np.pi/180.)
38tand = lambda x: np.tan(x*np.pi/180.)
39#asind = lambda x: 180.*np.arcsin(x)/np.pi
40#acosd = lambda x: 180.*np.arccos(x)/np.pi
41#atan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi
42   
43ncores = None
44
45def InitMP(allowMP=True):
46    '''Called to initialize use of Multiprocessing
47    '''
48    global useMP,ncores
49    if ncores is not None: return useMP,ncores
50    useMP = False
51    if not allowMP:
52        print('Multiprocessing disabled')
53        ncores = 0
54        return useMP,ncores
55    ncores = GSASIIpath.GetConfigValue('Multiprocessing_cores',0)
56    if ncores < 0: ncores = mp.cpu_count()/2
57    if ncores > 1:
58        useMP = True
59    #if GSASIIpath.GetConfigValue('debug') and useMP:
60    if useMP:
61        print('Multiprocessing with {} cores enabled'.format(ncores))
62    return useMP,ncores
63
64################################################################################
65# Fobs Squared computation
66################################################################################       
67def InitFobsSqGlobals(x1,ratio1,shl1,xB1,xF1,im1,lamRatio1,kRatio1,xMask1,Ka21):
68    '''Initialize for the computation of Fobs Squared for powder histograms.
69    Puts lots of junk into the global namespace in this module.
70    '''
71    global x,ratio,shl,xB,xF,im,lamRatio,kRatio,xMask,Ka2
72    x = ma.getdata(x1)
73    ratio = ratio1
74    shl = shl1
75    xB = xB1
76    xF = xF1
77    im = im1
78    lamRatio = lamRatio1
79    kRatio = kRatio1
80    xMask = xMask1
81    Ka2 = Ka21
82
83def ComputeFobsSqCWbatch(profList):
84    sInt = 0
85    resList = []
86    for refl,iref in profList:
87        icod = ComputeFobsSqCW(refl,iref)
88        if type(icod) is tuple:
89            resList.append((icod[0],iref))
90            sInt += icod[1]
91        elif icod == -1:
92            res.append((None,iref))
93        elif icod == -2:
94            break
95    return sInt,resList
96
97def ComputeFobsSqTOFbatch(profList):
98    sInt = 0
99    resList = []
100    for refl,iref in profList:
101        icod = ComputeFobsSqTOF(refl,iref)
102        if type(icod) is tuple:
103            resList.append((icod[0],iref))
104            sInt += icod[1]
105        elif icod == -1:
106            res.append((None,iref))
107        elif icod == -2:
108            break
109    return sInt,resList
110       
111def ComputeFobsSqCW(refl,iref):
112    yp = np.zeros(len(x)) # not masked
113    sInt = 0
114    refl8im = 0
115    Wd,fmin,fmax = G2pwd.getWidthsCW(refl[5+im],refl[6+im],refl[7+im],shl)
116    iBeg = max(xB,np.searchsorted(x,refl[5+im]-fmin))
117    iFin = max(xB,min(np.searchsorted(x,refl[5+im]+fmax),xF))
118    iFin2 = iFin
119    if not iBeg+iFin:       #peak below low limit - skip peak
120        return 0
121    if ma.all(xMask[iBeg:iFin]):    #peak entirely masked - skip peak
122        return -1
123    elif not iBeg-iFin:     #peak above high limit - done
124        return -2
125    elif iBeg < iFin:
126        yp[iBeg:iFin] = refl[11+im]*refl[9+im]*G2pwd.getFCJVoigt3(
127            refl[5+im],refl[6+im],refl[7+im],shl,x[iBeg:iFin])
128        sInt = refl[11+im]*refl[9+im]
129        if Ka2:
130            pos2 = refl[5+im]+lamRatio*tand(refl[5+im]/2.0)       # + 360/pi * Dlam/lam * tan(th)
131            Wd,fmin,fmax = G2pwd.getWidthsCW(pos2,refl[6+im],refl[7+im],shl)
132            iBeg2 = max(xB,np.searchsorted(x,pos2-fmin))
133            iFin2 = min(np.searchsorted(x,pos2+fmax),xF)
134            if iFin2 > iBeg2: 
135                yp[iBeg2:iFin2] += refl[11+im]*refl[9+im]*kRatio*G2pwd.getFCJVoigt3(
136                    pos2,refl[6+im],refl[7+im],shl,x[iBeg2:iFin2])
137                sInt *= 1.+kRatio
138    refl8im = np.sum(np.where(ratio[iBeg:iFin2]>0.,yp[iBeg:iFin2]*ratio[iBeg:iFin2]/(refl[11+im]*(1.+kRatio)),0.0))
139    return refl8im,sInt
140
141def ComputeFobsSqTOF(refl,iref):
142    yp = np.zeros(len(x)) # not masked
143    refl8im = 0
144    Wd,fmin,fmax = G2pwd.getWidthsTOF(refl[5+im],refl[12+im],refl[13+im],refl[6+im],refl[7+im])
145    iBeg = max(xB,np.searchsorted(x,refl[5+im]-fmin))
146    iFin = max(xB,min(np.searchsorted(x,refl[5+im]+fmax),xF))
147    if not iBeg+iFin:       #peak below low limit - skip peak
148        return 0
149    if ma.all(xMask[iBeg:iFin]):    #peak entirely masked - skip peak
150        return -1
151    elif not iBeg-iFin:     #peak above high limit - done
152        return -2
153    if iBeg < iFin:
154        yp[iBeg:iFin] = refl[11+im]*refl[9+im]*G2pwd.getEpsVoigt(
155            refl[5+im],refl[12+im],refl[13+im],refl[6+im],refl[7+im],x[iBeg:iFin])
156    refl8im = np.sum(np.where(ratio[iBeg:iFin]>0.,yp[iBeg:iFin]*ratio[iBeg:iFin]/refl[11+im],0.0))
157    return refl8im,refl[11+im]*refl[9+im]
158################################################################################
159# Powder Profile computation
160################################################################################       
161def InitPwdrProfGlobals(im1,shl1,x1):
162    '''Initialize for the computation of Fobs Squared for powder histograms.
163    Puts lots of junk into the global namespace in this module.
164    '''
165    global im,shl,x
166    im = im1
167    shl = shl1
168    x = ma.getdata(x1)
169    global cw
170    cw = np.diff(x)
171    cw = np.append(cw,cw[-1])
172    # create local copies of ycalc array
173    global yc
174    yc = np.zeros_like(x)
175
176
177def ComputePwdrProfCW(profList):
178    'Compute the peaks profile for a set of CW peaks and add into the yc array'
179    for pos,refl,iBeg,iFin,kRatio in profList:
180        yc[iBeg:iFin] += refl[11+im]*refl[9+im]*kRatio*G2pwd.getFCJVoigt3(
181            pos,refl[6+im],refl[7+im],shl,x[iBeg:iFin])
182    return yc
183
184def ComputePwdrProfTOF(profList):
185    'Compute the peaks profile for a set of TOF peaks and add into the yc array'
186    for pos,refl,iBeg,iFin in profList:
187        yc[iBeg:iFin] += refl[11+im]*refl[9+im]*G2pwd.getEpsVoigt(
188            pos,refl[12+im],refl[13+im],refl[6+im],refl[7+im],x[iBeg:iFin])/cw[iBeg:iFin]
189    return yc
190   
Note: See TracBrowser for help on using the repository browser.