Ignore:
Timestamp:
Nov 5, 2018 5:30:35 AM (3 years ago)
Author:
toby
Message:

restore lost Hold list; new tutorials; cleanup naming in constraint docs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIImapvars.py

    r3713 r3718  
    3131references
    3232to numbered objects are resolved using the appropriate random ids and the
    33 variable object is expressed as a string of form ``ph:hst:VARNAM:at``.
     33parameter object is converted to a string of form ``ph:hst:VARNAM:at``.
    3434
    3535Alternate parameters (New Var)
     
    5555degree of freedom where parameter Px and Py, etc. are varied to keep
    5656their ratio
    57 fixed according the expression. A new variable is assigned to each degree of
     57fixed according the expression. A new variable parameter is assigned to each degree of
    5858freedom when refined. An example where this can be valuable is when
    5959two parameters, P1 and P2, have similar values and are highly correlated. It is often better to create a new variable, Ps = P1 + P2, and refine Ps.
     
    8181a formula, but this can be expanded if needed.
    8282
    83 These equations set an interdependence between variables.
     83These equations set an interdependence between parameters.
    8484Common uses of parameter constraints are to set rules that decrease the number of parameters,
    8585such as restricting the sum of fractional occupancies for atoms that share
     
    154154^^^^^^^^^^^^^^^^^^^^^^^^
    155155
    156 When variables are refined where a single refinement flag determines that several variables
     156When parameters are refined where a single refinement flag determines that several variables
    157157are refined at the same time (examples are: cell parameters, atom positions, anisotropic
    158158displacement parameters, magnetic moments,...) it can be useful to specify that a
    159159specific parameter should not be varied. These will most commonly be generated due to symmetry,
    160 but under specific conditions, there may be other good reasons to constrain a variable.
     160but under specific conditions, there may be other good reasons to constrain a parameter.
    161161
    162162A "Hold" constraint is stored as described for type "h" in the
     
    361361
    362362Rows in the matrix corresponding to "New Var" constraints and those that
    363 were generated by the Gram-Schmidt process are provided with variable names
     363were generated by the Gram-Schmidt process are provided with parameter names
    364364(this can be specified if a "New Var" entry by using a ``"_name"`` element
    365365in the constraint dict, but at present this is not implemented.) Names are
     
    488488
    489489fixedVarList:
    490      a list of variables that have been 'fixed'
     490     a list of parameters that have been 'fixed'
    491491     by defining them as equal to a constant (::var: = 0). Note that
    492      the constant value is ignored at present. These variables are
     492     the constant value is ignored at present. These parameters are
    493493     later removed from varyList which prevents them from being refined.
    494494     Unlikely to be used externally.
     
    504504     
    505505problemVars:
    506      a list containing variables that show up in constraints producing errors
     506     a list containing parameters that show up in constraints producing errors
    507507
    508508
     
    548548'''
    549549fixedVarList = []
    550 '''List of variables that should not be refined.
     550'''List of parameters that should not be refined.
    551551'''
    552552symGenList = []
     
    554554'''
    555555problemVars = []
    556 '''a list of variables causing errors
     556'''a list of parameters causing errors
    557557'''
    558558dependentVars = []
     
    561561'A list of dependent variables, taken from (:data:`indParmList`).'
    562562genVarLookup = {}
    563 'provides a list of variables that are related to each generated variable'
     563'provides a list of parameters that are related to each generated parameter'
    564564paramPrefix = "::constr"
    565565'A prefix for generated parameter names'
     
    586586
    587587def VarKeys(constr):
    588     """Finds the keys in a constraint that represent variables
     588    """Finds the keys in a constraint that represent parameters
    589589    e.g. eliminates any that start with '_'
    590590
     
    687687    # varied, create a warning message.
    688688    for group,varlist in zip(groups,parmlist):
    689         if len(varlist) == 1: continue
     689        if len(varlist) == 1:   # process fixed (held) variables
     690            var = varlist[0]
     691            if var not in fixedVarList:
     692                fixedVarList.append(var)
     693            continue
    690694        for rel in group:
    691695            varied = 0
     
    694698                if var.startswith('_'): continue
    695699                if not re.match('[0-9]*:[0-9\*]*:',var):
    696                     warnmsg += "\nVariable "+str(var)+" does not begin with a ':'"
     700                    warnmsg += "\nParameter "+str(var)+" does not begin with a ':'"
    697701                if var in varyList:
    698702                    varied += 1
     
    705709                    errmsg += _FormatConstraint(constrDict[rel],fixedList[rel])+"\n"
    706710            if varied > 0 and varied != len(VarKeys(constrDict[rel])):
    707                 warnmsg += "\nNot all variables refined in constraint:\n\t"
     711                warnmsg += "\nNot all parameters refined in constraint:\n\t"
    708712                warnmsg += _FormatConstraint(constrDict[rel],fixedList[rel])
    709713                warnmsg += '\nNot refined: ' + notvaried + '\n'
     
    714718    # non-singular square matrix
    715719    for group,varlist in zip(groups,parmlist):
    716         if len(varlist) == 1: continue # a constraint group with a single variable can be ignored
     720        if len(varlist) == 1: continue # a constraint group with a single parameter can be ignored
    717721        if len(varlist) < len(group): # too many relationships -- no can do
    718722            errmsg += "\nOver-constrained input. "
    719723            errmsg += "There are more constraints " + str(len(group))
    720             errmsg += "\n\tthan variables " + str(len(varlist)) + "\n"
     724            errmsg += "\n\tthan parameters " + str(len(varlist)) + "\n"
    721725            for rel in group:
    722726                errmsg += _FormatConstraint(constrDict[rel],fixedList[rel])
     
    744748        mapvar = []
    745749        group = group[:]
    746         # scan through all generated and input variables
    747         # Check again for inconsistent variable use
    748         # for new variables -- where varied and unvaried parameters get grouped
     750        # scan through all generated and input parameters
     751        # Check again for inconsistent parameter use
     752        # for new parameters -- where varied and unvaried parameters get grouped
    749753        # together. I don't think this can happen when not flagged before, but
    750754        # it does not hurt to check again.
     
    764768                fixedval = None
    765769            if fixedval is None:
    766                 varname = paramPrefix + str(consNum) # assign a name to a variable
     770                varname = paramPrefix + str(consNum) # assign a name to a parameter
    767771                mapvar.append(varname)
    768772                consNum += 1
     
    770774                mapvar.append(fixedval)
    771775            if varied > 0 and notvaried != '':
    772                 warnmsg += "\nNot all variables refined in generated constraint"
     776                warnmsg += "\nNot all parameters refined in generated constraint"
    773777                warnmsg += '\nPlease report this unexpected error\n'
    774778                for rel in group:
     
    793797    '''Takes a list of relationship entries comprising a group of
    794798    constraints and builds the relationship lists and their inverse
    795     and stores them in global variables Also checks for internal
     799    and stores them in global parameters Also checks for internal
    796800    conflicts or inconsistencies in parameter/variable definitions.
    797801
     
    810814
    811815    :param int SeqHist: number of current histogram, when used in a sequential
    812       refinement. None (default) otherwise. Wildcard variable names are
     816      refinement. None (default) otherwise. Wildcard parameter names are
    813817      set to the current histogram, when found if not None.
    814818    '''
     
    818822    shortmsg = ''
    819823    changed = False
     824   
    820825    # Process the equivalences
    821826    #    If there are conflicting parameters, move them into constraints. This
     
    840845    groups,parmlist = GroupConstraints(constrDict)
    841846    for group,varlist in zip(groups,parmlist):
    842         if len(varlist) == 1: continue
     847        if len(varlist) == 1:   # process fixed (held) variables
     848            var = varlist[0]
     849            if var not in fixedVarList:
     850                fixedVarList.append(var)
     851            continue
    843852        for rel in group:
    844853            varied = 0
     
    868877            #if unused > 0:# and unused != len(VarKeys(constrDict[rel])):
    869878            if unused > 0 and unused != len(VarKeys(constrDict[rel])):
    870                 #msg += "\nSome (but not all) variables in constraint are not defined:\n\t"
     879                #msg += "\nSome (but not all) parameters in constraint are not defined:\n\t"
    871880                #msg += _FormatConstraint(constrDict[rel],fixedList[rel])
    872881                #msg += '\nNot used: ' + notused + '\n'
    873882                shortmsg += notused+" not used in constraint "+_FormatConstraint(constrDict[rel],fixedList[rel])
    874883            elif varied > 0 and varied != len(VarKeys(constrDict[rel])):
    875                 #msg += "\nNot all variables refined in constraint:\n\t"
     884                #msg += "\nNot all parameters refined in constraint:\n\t"
    876885                #msg += _FormatConstraint(constrDict[rel],fixedList[rel])
    877886                #msg += '\nNot refined: ' + notvaried + '\n'
     
    899908    for group,varlist in zip(groups,parmlist):
    900909        if len(varlist) == 1: continue
    901         # for constraints, if all included variables are refined,
     910        # for constraints, if all included parameters are refined,
    902911        # set the VaryFree flag, and remaining degrees of freedom will be
    903         # varied (since consistency was checked, if any one variable is
     912        # varied (since consistency was checked, if any one parameter is
    904913        # refined, then assume that all are)
    905         varsList = [] # make a list of all the referenced variables as well
     914        varsList = [] # make a list of all the referenced parameters as well
    906915        VaryFree = False
    907916        for rel in group:
     
    919928            msg = 'too many relationships'
    920929            break
    921         # Since we checked before, if any variables are unused, then all must be.
     930        # Since we checked before, if any parameters are unused, then all must be.
    922931        # If so, this set of relationships can be ignored
    923932        if unused:
    924             if debug: print('Constraint ignored (all variables undefined)')
     933            if debug: print('Constraint ignored (all parameters undefined)')
    925934            if debug: print ('    '+_FormatConstraint(constrDict[rel],fixedList[rel]))
    926935            continue
     
    953962                varname = ''
    954963                fixedval = None
    955             if fixedval is None: # this is a new variable, not a constraint
     964            if fixedval is None: # this is a new parameter, not a constraint
    956965                if not varname:
    957966                    varname = paramPrefix + str(consNum) # no assigned name, create one
    958967                    consNum += 1
    959968                mapvar.append(varname)
    960                 genVarLookup[varname] = varlist # save list of variables related to this new var
     969                genVarLookup[varname] = varlist # save list of parameters related to this new var
    961970                # vary the new relationship if it is a degree of freedom in
    962971                # a set of contraint equations or if a New Var is flagged to be varied.
     
    964973                    unused = False
    965974                    varyList.append(varname)
    966                     # fix (prevent varying) of all the variables inside the constraint group
     975                    # fix (prevent varying) of all the parameters inside the constraint group
    967976                    # (dependent vars)
    968977                    for var in varsList:
     
    971980                unused = False
    972981                mapvar.append(fixedval)
    973         if unused: # skip over constraints that don't matter (w/o fixed value or any refined variables)
    974             if debug: print('Constraint ignored (all variables unrefined)')
     982        if unused: # skip over constraints that don't matter (w/o fixed value or any refined parameters)
     983            if debug: print('Constraint ignored (all parameters unrefined)')
    975984            if debug: print ('   '+_FormatConstraint(constrDict[rel],fixedList[rel]))
    976985            continue
     
    11321141    errmsg = ''
    11331142    problemVars = []
    1134     # process fixed variables (holds)
     1143    # process fixed parameters (holds)
    11351144    fixVlist = [] # list of Fixed vars
    11361145    constrVars = [] # list of vars in constraint expressions
     
    11481157    multdepVarList = []
    11491158    dropVarList = []
    1150     translateTable = {} # lookup table for wildcard referenced variables
     1159    translateTable = {} # lookup table for wildcard referenced parameters
    11511160    for varlist,mapvars,multarr,invmultarr in zip(
    11521161        dependentParmList,indParmList,arrayList,invarrayList):
     
    11681177                    notvaried += mv
    11691178                if parmDict is not None and mv not in parmDict:
    1170                     print ("Dropping equivalence for variable "+str(mv)+". Not defined in this refinement")
     1179                    print ("Dropping equivalence for parameter "+str(mv)+". Not defined in this refinement")
    11711180                    if mv not in dropVarList: dropVarList.append(mv)
    11721181                if mv not in indepVarList: indepVarList.append(mv)
     
    11881197                    notvaried += v
    11891198                if v in indepVarList:
    1190                     errmsg += '\nVariable '+v+' is used to set values in a constraint before its value is set in another constraint\n'
     1199                    errmsg += '\nParameter '+v+' is used to set values in a constraint before its value is set in another constraint\n'
    11911200                    if v not in problemVars: problemVars.append(v)
    11921201                if v in depVarList:
     
    11951204                    depVarList.append(v)
    11961205            if varied > 0 and varied != len(varlist)+1:
    1197                 warnmsg += "\nNot all variables refined in equivalence:\n\t"
     1206                warnmsg += "\nNot all parameters refined in equivalence:\n\t"
    11981207                s = ""
    11991208                for v in varlist:
     
    13801389
    13811390def GetDependentVars():
    1382     '''Return a list of dependent variables: e.g. variables that are
    1383     constrained in terms of other variables
    1384 
    1385     :returns: a list of variable names
     1391    '''Return a list of dependent variables: e.g. parameters that are
     1392    constrained in terms of other parameters
     1393
     1394    :returns: a list of parameter names
    13861395
    13871396    '''
     
    13901399
    13911400def GetIndependentVars():
    1392     '''Return a list of independent variables: e.g. variables that are
    1393     created by constraints of other variables
    1394 
    1395     :returns: a list of variable names
     1401    '''Return a list of independent variables: e.g. parameters that are
     1402    slaved to other parameters by constraints
     1403
     1404    :returns: a list of parameter names
    13961405
    13971406    '''
     
    14001409
    14011410def PrintIndependentVars(parmDict,varyList,sigDict,PrintAll=False,pFile=None):
    1402     '''Print the values and uncertainties on the independent variables'''
     1411    '''Print the values and uncertainties on the independent parameters'''
    14031412    global dependentParmList,arrayList,invarrayList,indParmList,fixedDict
    14041413    printlist = []
     
    14141423    s3 = ''
    14151424    pFile.write(130*'-'+'\n')
    1416     pFile.write("Variables generated by constraints\n")
     1425    pFile.write("Parameters generated by constraints\n")
    14171426    printlist.append(3*[None])
    14181427    for name,val,esd in printlist:
     
    14931502    s = ''
    14941503    if len(fixedVarList) > 0:
    1495         s += 'Fixed Variables:\n'
     1504        s += 'Fixed Parameters:\n'
    14961505        for v in fixedVarList:
    14971506            s += '    ' + v + '\n'
    14981507    if not inputOnly:
    1499         s += 'User-supplied variable mapping relations:\n'
     1508        s += 'User-supplied parameter mapping relations:\n'
    15001509    symout = ''
    15011510    global dependentParmList,arrayList,invarrayList,indParmList,fixedDict,symGenList
     
    15361545        s += 'Symmetry-generated relations:\n' + symout
    15371546    if inputOnly: return s
    1538     s += 'Inverse variable mapping relations:\n'
     1547    s += 'Inverse parameter mapping relations:\n'
    15391548    for varlist,mapvars,invmultarr in zip(dependentParmList,indParmList,invarrayList):
    15401549        for i,mv in enumerate(varlist):
     
    16031612    for varlist,mapvars,multarr,invmultarr in zip(dependentParmList,indParmList,arrayList,invarrayList):
    16041613        for i,name in enumerate(mapvars):
    1605             # grouped variables: need to add in the derv. w/r
     1614            # grouped parameters: need to add in the derv. w/r
    16061615            # dependent variables to the independent ones
    16071616            if name not in varyList: continue # skip if independent var not varied
     
    16281637    defined using :func:`StoreEquivalence`,
    16291638    :func:`GroupConstraints` and :func:`GenerateConstraints` and
    1630     before any variable refinement is done. This completes the parameter
     1639    before any parameter refinement is done. This completes the parameter
    16311640    dictionary by defining independent parameters and it satisfies the
    16321641    constraint equations in the initial parameters
     
    16571666                            np.dot(multarr,np.array(valuelist)))
    16581667                        )
    1659     # now remove fixed variables from the varyList
     1668    # now remove fixed parameters from the varyList
    16601669    global fixedVarList
    16611670    for item in fixedVarList:
Note: See TracChangeset for help on using the changeset viewer.