Changeset 415 for trunk/GSASIImapvars.py


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.