Changeset 1138 for trunk/GSASIIstrIO.py


Ignore:
Timestamp:
Nov 7, 2013 12:12:55 PM (8 years ago)
Author:
toby
Message:

major constraints revision

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIstrIO.py

    r1137 r1138  
    2929import GSASIIlattice as G2lat
    3030import GSASIIspc as G2spc
     31import GSASIIobj as G2obj
    3132import GSASIImapvars as G2mv
    3233import GSASIImath as G2mth
     
    7980    constDict,fixedList,ignored = ProcessConstraints(constList)
    8081    if ignored:
    81         print ignored,'old-style Constraints were rejected'
     82        print ignored,'Constraints were rejected. Was a constrained phase, histogram or atom deleted?'
    8283    return constDict,fixedList
    8384   
    8485def ProcessConstraints(constList):
    8586    """Interpret the constraints in the constList input into a dictionary, etc.
     87    All :class:`GSASIIobj.G2VarObj` objects are mapped to the appropriate
     88    phase/hist/atoms based on the object internals (random Ids). If this can't be
     89    done (if a phase has been deleted, etc.), the variable is ignored.
     90    If the constraint cannot be used due to too many dropped variables,
     91    it is counted as ignored.
    8692   
    8793    :param list constList: a list of lists where each item in the outer list
    88       specifies a constraint of some form. The last item in each inner list
    89       determines which of the four constraints types has been input:
    90 
    91         * h (hold): a single variable that will not be varied. It
    92           will be removed from the varyList later.
    93         * c (constraint): specifies a linear relationship that
    94           can be varied as a new grouped variable
    95           a fixed value.
    96         * f (fixed): specifies a linear relationship that is assigned
    97           a fixed value.
    98         * e (equivalence): specifies a series of variables where the
    99           first variable in the last can be used to generate the
    100           values for all the remaining variables.
     94      specifies a constraint of some form, as described in the :mod:`GSASIIobj`
     95      :ref:`Constraint definition <Constraint_definitions_table>`.
    10196
    10297    :returns:  a tuple of (constDict,fixedList,ignored) where:
    10398     
    104       * constDict (list) contains the constraint relationships
     99      * constDict (list of dicts) contains the constraint relationships
    105100      * fixedList (list) contains the fixed values for type
    106101        of constraint.
    107102      * ignored (int) counts the number of invalid constraint items
    108103        (should always be zero!)
    109 
    110104    """
    111105    constDict = []
     
    116110            # process a hold
    117111            fixedList.append('0')
    118             constDict.append({item[0][1]:0.0})
     112            var = str(item[0][1])
     113            if '?' not in var:
     114                constDict.append({var:0.0})
     115            else:
     116                ignored += 1
    119117        elif item[-1] == 'f':
    120118            # process a new variable
    121119            fixedList.append(None)
    122             constDict.append({})
     120            D = {}
     121            varyFlag = item[-2]
     122            name = item[-3]
    123123            for term in item[:-3]:
    124                 constDict[-1][term[1]] = term[0]
     124                var = str(term[1])
     125                if '?' not in var:
     126                    D[var] = term[0]
     127            if len(D) > 1:
     128                # add extra dict terms for input variable name and vary flag
     129                #if name is not None:
     130                #    D['_name'] = name
     131                #D['_vary'] = varyFlag == True # force to bool
     132                constDict.append(D)
     133            else:
     134                ignored += 1
    125135            #constFlag[-1] = ['Vary']
    126136        elif item[-1] == 'c':
    127137            # process a contraint relationship
    128             fixedList.append(str(item[-3]))
    129             constDict.append({})
     138            D = {}
    130139            for term in item[:-3]:
    131                 constDict[-1][term[1]] = term[0]
    132             #constFlag[-1] = ['VaryFree']
     140                var = str(term[1])
     141                if '?' not in var:
     142                    D[var] = term[0]
     143            if len(D) >= 1:
     144                fixedList.append(str(item[-3]))
     145                constDict.append(D)
     146            else:
     147                ignored += 1
    133148        elif item[-1] == 'e':
    134149            # process an equivalence
     
    137152            for term in item[:-3]:
    138153                if term[0] == 0: term[0] = 1.0
     154                var = str(term[1])
     155                if '?' in var: continue
    139156                if firstmult is None:
    140                     firstmult,firstvar = term
     157                    firstmult = term[0]
     158                    firstvar = var
    141159                else:
    142                     eqlist.append([term[1],firstmult/term[0]])
    143             G2mv.StoreEquivalence(firstvar,eqlist)
     160                    eqlist.append([var,firstmult/term[0]])
     161            if len(eqlist) > 0:
     162                G2mv.StoreEquivalence(firstvar,eqlist)
     163            else:
     164                ignored += 1
    144165        else:
    145166            ignored += 1
     
    156177    if not Histograms:
    157178        return 'Error: no diffraction data',''
     179    G2obj.IndexAllIds(Histograms=Histograms,Phases=Phases)
    158180    rigidbodyDict = GetRigidBodies(GPXfile)
    159181    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
     
    335357def GetUsedHistogramsAndPhases(GPXfile):
    336358    ''' Returns all histograms that are found in any phase
    337     and any phase that uses a histogram
     359    and any phase that uses a histogram. This also
     360    assigns numbers to used phases and histograms by the
     361    order they appear in the file.
    338362
    339363    :param str GPXfile: full .gpx file name
    340     :return: (Histograms,Phases)
     364    :returns: (Histograms,Phases)
    341365
    342366     * Histograms = dictionary of histograms as {name:data,...}
     
    363387                    Phase['Histograms'][hist]['Use'] = True         
    364388                if hist not in Histograms and Phase['Histograms'][hist]['Use']:
    365                     Histograms[hist] = allHistograms[hist]
    366                     hId = histoList.index(hist)
    367                     Histograms[hist]['hId'] = hId
     389                    try:
     390                        Histograms[hist] = allHistograms[hist]
     391                        hId = histoList.index(hist)
     392                        Histograms[hist]['hId'] = hId
     393                    except KeyError: # would happen if a referenced histogram were
     394                        # renamed or deleted
     395                        print('For phase "'+str(phase)+
     396                              '" unresolved reference to histogram "'+str(hist)+'"')
    368397    return Histograms,Phases
    369398   
Note: See TracChangeset for help on using the changeset viewer.