source: trunk/GSASIImpsubs.py @ 4021

Last change on this file since 4021 was 4021, checked in by toby, 2 years ago

implement filter for screen messages; start to replace print() with G2Print(); scripting changes for PDF

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
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: 2019-06-12 03:05:07 +0000 (Wed, 12 Jun 2019) $
22# $Author: toby $
23# $Revision: 4021 $
24# $URL: trunk/GSASIImpsubs.py $
25# $Id: GSASIImpsubs.py 4021 2019-06-12 03:05:07Z toby $
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: 4021 $")
33import GSASIIpwd as G2pwd
34import GSASIIfiles as G2fil
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 ResetMP():
46    '''Call after changing Config var 'Multiprocessing_cores' to force a resetting
47    of the useMP from the parameter.
48    '''
49    global ncores
50    ncores = None
51   
52def InitMP(allowMP=True):
53    '''Called to initialize use of Multiprocessing
54    '''
55    global useMP,ncores
56    if ncores is not None: return useMP,ncores
57    useMP = False
58    if not allowMP:
59        G2fil.G2Print('Multiprocessing disabled')
60        ncores = 0
61        return useMP,ncores
62    ncores = GSASIIpath.GetConfigValue('Multiprocessing_cores',0)
63    if ncores < 0: ncores = mp.cpu_count()//2
64    if ncores > 1:
65        useMP = True
66    if useMP:
67        G2fil.G2Print('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.