Changeset 415


Ignore:
Timestamp:
Nov 15, 2011 9:07:55 AM (11 years ago)
Author:
toby
Message:

implement refinement of constrained parameters; fix minor bugs; add fast interpolate

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r411 r415  
    15091509        screenSize = wx.ClientDisplayRect()
    15101510        Size = dlg.GetSize()
     1511        Size = (int(Size[0]*1.2),Size[1]) # increase size a bit along x
    15111512        dlg.SetPosition(wx.Point(screenSize[2]-Size[0]-305,screenSize[1]+5))
     1513        dlg.SetSize(Size)
    15121514        try:
    15131515            G2str.Refine(self.GSASprojectfile,dlg)
     
    15441546        screenSize = wx.ClientDisplayRect()
    15451547        Size = dlg.GetSize()
     1548        Size = (int(Size[0]*1.2),Size[1]) # increase size a bit along x
    15461549        dlg.SetPosition(wx.Point(screenSize[2]-Size[0]-305,screenSize[1]+5))
     1550        dlg.SetSize(Size)
    15471551        try:
    15481552            G2str.SeqRefine(self.GSASprojectfile,dlg)
     
    16171621def main():
    16181622    application = GSASIImain(0)
     1623    #application.main.OnRefine(None)
    16191624    application.MainLoop()
    1620 
     1625   
    16211626if __name__ == '__main__':
    16221627    main()
  • trunk/GSASIIgrid.py

    r413 r415  
    1818import GSASIIphsGUI as G2phG
    1919import GSASIIstruct as G2str
    20 import GSASIImapvars as G2mv
     20#import GSASIImapvars as G2mv
    2121
    2222[ wxID_ATOMSEDITADD, wxID_ATOMSEDITINSERT, wxID_ATOMSEDITDELETE, wxID_ATOMSREFINE,
  • trunk/GSASIImapvars.py

    r411 r415  
    9090     each group. This contains both parameters used in parameter
    9191     redefinitions as well as names of generated new parameters.
    92    
     92
     93   fixedVarList: a list of variables that have been 'fixed'
     94     by defining them as equal to a constant (::var: = 0). Note that
     95     the constant value is ignored at present. These variables are
     96     later removed from varyList which prevents them from being refined.
     97     Unlikely to be used externally.
    9398   arrayList: a list by group of relationship matrices to relate
    9499     parameters in dependentParmList to those in indParmList. Unlikely
     
    115120fixedDict = {} # a dictionary containing the fixed values corresponding to defined parameter equations
    116121               # key is original ascii string, value is float
     122fixedVarList = [] # List of variables that should not be refined
    117123
    118124# compile regular expressions used for parsing input
    119125rex_mult = re.compile('[+-]?[0-9.]+[eE]?[+-]?[0-9]*')
    120126rex_star = re.compile('\s*\*\s*')
    121 rex_var = re.compile('\w+')
     127rex_var = re.compile('[a-zA-Z0-9_:]+')
    122128rex_plusminus = re.compile('\s*[+-=]\s*')
    123129rex_vary = re.compile('\s*Vary\s*', re.IGNORECASE)
     
    137143    fixedDict = {} # a dictionary containing the fixed values corresponding to defined parameter equations
    138144    consNum = 0 # number of the next constraint to be created
     145    fixedVarList = []
    139146
    140147def InputParse(mapList):
     
    185192    if debug: print 50*'-','\n(Input)'
    186193    for line in mapList:
     194        inputline = line[:]
    187195        line = line.strip()
    188196        if len(line) == 0: continue # empty lines are ignored
     
    243251                else:
    244252                    # something else is on the line, but not a keyword
    245                     raise Exception
     253                    raise Exception,'Error in line '+str(inputline)+'\nat '+str(line)
    246254        except SyntaxError:
    247255            if debug: print 'Error in line',i,'token',j,'@','"'+line+'"'
     
    307315        for row in group:
    308316            if 'VaryFree' in constrFlag[row]: VaryFree = True
     317        if len(varlist) == 1:
     318            #print "Debug: Fixing parameter (%s)" % (varlist[0])
     319            fixedVarList.append(varlist[0])
     320            continue
    309321        arr = MakeArray(constrDict, group, varlist)
    310322        constrArr = FillInMissingRelations(arr,len(group))
     
    382394    return
    383395
     396def SetVaryFlags(varyList):
     397    '''Adds independent variables to the varyList provided that all
     398    dependent variables are being varied.
     399    Ignores independent variables where no dependent variables are
     400    being varied.
     401    Returns a non-empty text message where some but not all dependent
     402    variables are being varied.
     403    '''
     404    global dependentParmList,arrayList,invarrayList,indParmList,fixedDict
     405    for varlist,mapvars,multarr in zip(dependentParmList,indParmList,arrayList):
     406        msg = ""
     407        for mv in mapvars:
     408            varied = []
     409            notvaried = []
     410            if mv in fixedDict: continue
     411            if multarr is None:
     412                if mv in varyList:
     413                    varied.append(mv)
     414                else:
     415                    notvaried.append(mv)
     416            for v in varlist:
     417                if v in varyList:
     418                    varied.append(v)
     419                else:
     420                    notvaried.append(v)
     421            if len(varied) > 0 and len(notvaried) > 0:
     422                if msg != "": msg += "\n"
     423                msg += "Error: inconsistent use of parameter " + mv
     424                msg += "\n  varied: "
     425                for v in varied: msg += v
     426                msg += "\n  not varied: "
     427                for v in notvaried: msg += v   
     428            elif len(varied) > 0 and multarr is not None:
     429                varyList.append(mv)
     430    return msg
     431
     432
    384433def VarRemapShow(varyList):
    385434    '''List out the saved relationships.
    386435    Returns a string containing the details.
    387436    '''
    388     s = 'Mapping relations:\n'
     437    s = ''
     438    if len(fixedVarList) > 0:
     439        s += 'Fixed Variables:\n'
     440        for v in fixedVarList:
     441            s += '    ' + v + '\n'
     442    s += 'Variable mapping relations:\n'
    389443    global dependentParmList,arrayList,invarrayList,indParmList,fixedDict
    390444    for varlist,mapvars,multarr in zip(dependentParmList,indParmList,arrayList):
     
    410464            s += '\n'
    411465            i += 1
    412     s += 'Inverse mapping relations:\n'
     466    s += 'Inverse variable mapping relations:\n'
    413467    for varlist,mapvars,invmultarr in zip(dependentParmList,indParmList,invarrayList):
    414468        i = 0
     
    429483    set of Parameters
    430484
     485    Removes dependent variables from the varyList
     486
    431487    This should be done once, before any variable refinement is done
    432488    to complete the parameter dictionary with the Independent Parameters
    433489    '''
     490    # process the Independent vars: remove dependent ones from varylist
     491    # and then compute values for the Independent ones from their dependents
    434492    global dependentParmList,arrayList,invarrayList,indParmList,fixedDict
    435493    for varlist,mapvars,multarr in zip(dependentParmList,indParmList,arrayList):
     
    444502                            np.dot(multarr,np.array(valuelist)))
    445503                        )
    446     # overwrite dict with constraints - why not parmDict.update(fixDict)?
     504    # now remove fixed variables from the varyList
     505    global fixedVarList
     506    for item in fixedVarList:
     507        try:
     508            varyList.remove(item)
     509        except ValueError:
     510            pass
     511    # Set constrained parameters to their fixed values
    447512    parmDict.update(fixedDict)
    448513
     
    537602        raise Exception,'The number of relationships (%d) exceeds the number of parameters (%d):\n\t%s\n\t%s'% (
    538603            len(group),len(varlist),group,varlist)
    539     if len(varlist) == 1: # one to one mapping -- something is probably wrong
    540         raise Exception,'Why remap a single parameter? (%s)'% (varlist[0])
    541604    # put the coefficients into an array
    542605    multarr = np.zeros(2*[len(varlist),])
     
    637700    parmdict = {}
    638701    StoreEquivalence('2::atomx:3',('2::atomy:3', ('2::atomz:3',2,), ))
    639     print VarRemapShow()
    640 
    641     parmdict = {
    642         '2::atomx:3':.1 ,
    643         '2::atomy:3':.2 , # conflicts with constraint
    644         '2::atomz:3':.3 ,
    645     }
    646 
     702    varylist = ['2::atomx:3',]
     703    print VarRemapShow(varylist)
     704    msg = SetVaryFlags(varylist)
     705    if msg != "": print msg
     706    varylist = ['2::atomx:3', '2::atomy:3', '2::atomz:3',]
     707    print VarRemapShow(varylist)
     708    msg = SetVaryFlags(varylist)
     709    if msg != "": print msg
     710    #parmdict = {
     711    #    '2::atomx:3':.1 ,
     712    #    '2::atomy:3':.2 , # conflicts with constraint
     713    #    '2::atomz:3':.3 ,
     714    #}
     715
     716    varylist = []
     717    mapList1 = [
     718        "1 * 2::atomz:3 = 0",
     719        "1*p1 + 2e0*p2 - 1.0*p3",
     720        "1*p4 + 1*p7",
     721        "1*p1+2*p2-3.0*p2 VARY",
     722        ]
    647723    mapList = [
     724        "1 * 2::atomz:3 = 0",
    648725        "1*p1 + 2e0*p2 - 1.0*p3",
    649726        "1*p4 + 1*p7",
     
    654731        "-10e-1 * p1 - -2*p2 + 3.0*p4",
    655732        ]
    656     constrDict,constrFlag,fixedList = InputParse(mapList)
    657     print constrDict
    658     print constrFlag
    659     print fixedList
     733    constrDict,constrFlag,fixedList = InputParse(mapList1)
     734    #print constrDict
     735    #print constrFlag
     736    #print fixedList
    660737    groups,parmlist = GroupConstraints(constrDict)
    661     GenerateConstraints(groups,parmlist,constrDict,constrFlag,fixedList)
    662     print VarRemapShow()
     738    GenerateConstraints(groups,parmlist,varylist,constrDict,constrFlag,fixedList)
     739    print VarRemapShow(varylist)
     740    sys.exit()
    663741    parmdict.update( {'p1':1,'p2':2,'p3':3,'p4':4,
    664742                      'p6':6,'p5':5,  # conflicts with constraint
  • trunk/GSASIIstruct.py

    r411 r415  
    236236    import distutils.file_util as dfu
    237237    GPXpath,GPXname = ospath.split(GPXfile)
     238    if GPXpath == '': GPXpath = '.'
    238239    Name = ospath.splitext(GPXname)[0]
    239240    files = os.listdir(GPXpath)
     
    24532454        print ' *** ERROR - you have no data to refine with! ***'
    24542455        print ' *** Refine aborted ***'
    2455         raise Exception
     2456        raise Exception       
    24562457    Natoms,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables = GetPhaseData(Phases)
    24572458    calcControls['Natoms'] = Natoms
     
    24672468    parmDict.update(histDict)
    24682469    GetFprime(calcControls,Histograms)
    2469     groups,parmlist = G2mv.GroupConstraints(constrDict)
    2470     G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,constrFlag,fixedList)
     2470    # do constraint processing
     2471    try:
     2472        groups,parmlist = G2mv.GroupConstraints(constrDict)
     2473        G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,constrFlag,fixedList)
     2474    except:
     2475        print ' *** ERROR - your constraints are internally inconsistent ***'
     2476        print ' *** Refine aborted ***'
     2477        raise Exception
     2478    # check to see which generated parameters are fully varied
     2479    msg = G2mv.SetVaryFlags(varyList)
     2480    if msg:
     2481        print ' *** ERROR - you have not set the refine flags for constraints consistently! ***'
     2482        print msg
     2483        print ' *** Refine aborted ***'
     2484        raise Exception       
     2485    G2mv.Map2Dict(parmDict,varyList)
    24712486    print G2mv.VarRemapShow(varyList)
    2472     G2mv.Map2Dict(parmDict,varyList)
    24732487
    24742488    while True:
  • trunk/fsource/pypowder.for

    r355 r415  
    126126      END
    127127
     128C Fortran (fast) linear interpolation -- B.H. Toby 9/2011
     129      SUBROUTINE PYFINTERP(NIN,XIN,YIN,NOUT,XOUT,YOUT)
     130C XIN(1:NIN) and YIN(1:NIN) are arrays of (x,y) points to be interpolated
     131C     Values must be sorted increasing in XIN
     132C XOUT(1:NOUT) is an array of x values, must also be sorted increasing in x
     133C    XOUT may contain values smaller than XIN(1) or larger than XIN(NIN)
     134C RETURNS interpolated y values corresponding to XOUT. Values outside the
     135C   range of XIN are set to zero.
     136C Needs a way to signal an error if XIN or XOUT is not sorted -- for now stops
     137Cf2py intent(in) NIN
     138Cf2py intent(in)  XIN
     139cf2py depend(NIN) XIN
     140Cf2py intent(in)  YIN
     141cf2py depend(NIN) YIN
     142Cf2py intent(in) NOUT
     143Cf2py intent(in)   XOUT
     144cf2py depend(NOUT) XOUT
     145Cf2py intent(out)  YOUT
     146cf2py depend(NOUT) YOUT
     147
     148      REAL XIN(NIN),YIN(NIN)
     149      REAL XOUT(NOUT),YOUT(NOUT)
     150      INTEGER IERROR
     151      REAL X,F
     152      INTEGER IIN,I
     153
     154      IERROR = 1
     155      IIN = 1
     156      X = XOUT(1)
     157      DO I=1,NOUT
     158         IF (X .GT. XOUT(I)) STOP ! test if Xout not sorted
     159         X = XOUT(I)
     160         IF (X .LT. XIN(1) .OR. X .GT. XIN(NIN) ) THEN
     161            YOUT(I) = 0.0
     162         ELSE
     163            DO WHILE (X .GT.  XIN(IIN+1))
     164               IF (XIN(IIN) .GT. XIN(IIN+1)) STOP ! test if Xin not sorted
     165               IIN = IIN + 1
     166             ENDDO
     167             F = (X - XIN(IIN)) / (XIN(IIN+1) - XIN(IIN))
     168             YOUT(I) = (1.-F)*YIN(IIN) + F*YIN(IIN+1)
     169          ENDIF
     170          !write (*,*) xout(i),iin,f,yout(i)
     171      END DO
     172      IERROR = 0
     173      RETURN
     174      END
Note: See TracChangeset for help on using the changeset viewer.