Changeset 1243


Ignore:
Timestamp:
Mar 10, 2014 2:06:56 PM (8 years ago)
Author:
toby
Message:

fix constraints use in sequential refinements

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1236 r1243  
    31133113            constrDict,fixedList,ignored = G2stIO.ProcessConstraints(constList)
    31143114            groups,parmlist = G2mv.GroupConstraints(constrDict)
    3115             G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList)
     3115            G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList,parmValDict)
    31163116            G2mv.Map2Dict(parmValDict,varyList)
    31173117        except:
  • trunk/GSASIIIO.py

    r1236 r1243  
    20892089        try:
    20902090            groups,parmlist = G2mv.GroupConstraints(constDict)
    2091             G2mv.GenerateConstraints(groups,parmlist,varyList,constDict,fixedList)
     2091            G2mv.GenerateConstraints(groups,parmlist,varyList,constDict,fixedList,self.parmDict)
    20922092        except:
    20932093            # this really should not happen
  • trunk/GSASIImapvars.py

    r1160 r1243  
    8585parameter from being varied. Note that all parameters in a constraint relationship
    8686must specified as varied (appear in varyList) or none can be varied. This is
    87 checked in GenerateConstraints (as well as for generated relationships in SetVaryFlags).
     87checked in GenerateConstraints. Likewise, if all parameters in a constraint are
     88not referenced in a refinement, the constraint is ignored, but if some parameters
     89in a constraint group are not referenced in a refinement, but others are this
     90constitutes and error.
    8891
    8992* When a new variable is created, the variable is assigned the name associated
     
    508511    return errmsg,warnmsg
    509512
    510 def GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList):
     513def GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList,parmDict=None):
    511514    '''Takes a list of relationship entries comprising a group of
    512515    constraints and builds the relationship lists and their inverse
     
    545548    # process equivalences: make a list of dependent and independent vars
    546549    #    and check for repeated uses (repetition of a parameter as an
    547     #    independent var is OK)
     550    #    independent var is OK [A=B; A=C], but chaining: [A=B; B=C] is not good)
    548551    indepVarList = []
    549552    depVarList = []
    550553    multdepVarList = []
    551     for varlist,mapvars,multarr,invmultarr in zip(
     554    for varlist,mapvars,multarr,invmultarr in zip(       # process equivalences
    552555        dependentParmList,indParmList,arrayList,invarrayList):
    553         if multarr is None: # do only if an equivalence
     556        if multarr is None: # true only if an equivalence
    554557            zeromult = False
    555558            for mv in mapvars:
     
    563566                    notvaried += mv
    564567                if mv not in indepVarList: indepVarList.append(mv)
     568                if parmDict is not None and mv not in parmDict:
     569                    msg += "\nCannot equivalence to variable "+str(mv)+". Not defined in this refinement"
     570                    continue
    565571                for v,m in zip(varlist,invmultarr):
     572                    if parmDict is not None and v not in parmDict:
     573                        print "Dropping equivalence for variable "+str(v)+". Not defined in this refinement"
     574                        continue
    566575                    if m == 0: zeromult = True
    567576                    if v in varyList:
     
    617626            varied = 0
    618627            notvaried = ''
     628            unused = 0
     629            notused = ''
    619630            for var in constrDict[rel]:
    620631                if var.startswith('_'): continue
     632                if parmDict is not None and var not in parmDict:
     633                    unused += 1
     634                    if notvaried: notused += ', '
     635                    notused += var
    621636                if var in varyList:
    622637                    varied += 1
     
    627642                    msg += '\nError: parameter '+var+" is Fixed and used in a constraint:\n\t"
    628643                    msg += _FormatConstraint(constrDict[rel],fixedList[rel])+"\n"
     644            #if unused > 0:# and unused != len(VarKeys(constrDict[rel])):
     645            if unused > 0 and unused != len(VarKeys(constrDict[rel])):
     646                msg += "\nSome (but not all) variables in constraint are not defined:\n\t"
     647                msg += _FormatConstraint(constrDict[rel],fixedList[rel])
     648                msg += '\nNot used: ' + notused + '\n'
    629649            if varied > 0 and varied != len(VarKeys(constrDict[rel])):
    630650                msg += "\nNot all variables refined in constraint:\n\t"
     
    651671        for rel in group:
    652672            varied = 0
     673            unused = 0
    653674            for var in VarKeys(constrDict[rel]):
     675                if parmDict is not None and var not in parmDict:
     676                    unused += 1                   
    654677                if var not in varsList: varsList.append(var)
    655678                if var in varyList: varied += 1
     
    659682            msg = 'too many relationships'
    660683            break
     684        # Since we checked before, if any variables are unused, then all must be.
     685        # If so, this set of relationships can be ignored
     686        if unused:
     687            if debug: print('Constraint ignored (all variables undefined)')
     688            if debug: print ('    '+_FormatConstraint(constrDict[rel],fixedList[rel]))
     689            continue
    661690        # fill in additional degrees of freedom
    662691        try:
     
    704733                unused = False
    705734                mapvar.append(fixedval)
    706         if unused: continue # skip over constraints that do not have a fixed value or a refined variable
     735        if unused: # skip over constraints that don't matter (w/o fixed value or any refined variables)
     736            if debug: print('Constraint ignored (all variables unrefined)')
     737            if debug: print ('   '+_FormatConstraint(constrDict[rel],fixedList[rel]))
     738            continue
    707739        dependentParmList.append(varlist)
    708740        arrayList.append(constrArr)
  • trunk/GSASIIstrMain.py

    r1242 r1243  
    155155    try:
    156156        groups,parmlist = G2mv.GroupConstraints(constrDict)
    157         G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList)
     157        G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList,parmDict)
    158158    except:
    159159        print ' *** ERROR - your constraints are internally inconsistent ***'
    160         errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
    161         print 'Errors',errmsg
    162         if warnmsg: print 'Warnings',warnmsg
     160        #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
     161        #print 'Errors',errmsg
     162        #if warnmsg: print 'Warnings',warnmsg
    163163        raise Exception(' *** Refine aborted ***')
    164     # # check to see which generated parameters are fully varied
    165     # msg = G2mv.SetVaryFlags(varyList)
    166     # if msg:
    167     #     print ' *** ERROR - you have not set the refine flags for constraints consistently! ***'
    168     #     print msg
    169     #     raise Exception(' *** Refine aborted ***')
    170164    #print G2mv.VarRemapShow(varyList)
    171165   
     
    218212   
    219213    printFile = open(ospath.splitext(GPXfile)[0]+'.lst','w')
    220     print ' Sequential Refinement'
     214    print 'Starting Sequential Refinement'
    221215    G2stIO.ShowBanner(printFile)
    222216    Controls = G2stIO.GetControls(GPXfile)
     
    252246    NewparmDict = {}
    253247    for ihst,histogram in enumerate(histNames):
     248        print('Refining with '+str(histogram))
    254249        ifPrint = False
    255250        if dlg:
     
    260255        calcControls['FFtables'] = FFtables
    261256        calcControls['BLtables'] = BLtables
    262         varyList = []
    263         parmDict = {}
    264257        Histo = {histogram:Histograms[histogram],}
    265258        hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histo,Print=False)
     
    269262        varyList = rbVary+phaseVary+hapVary+histVary
    270263        if not ihst:
     264            # subsequent refinements
    271265            saveVaryList = varyList[:]
    272266            for i,item in enumerate(saveVaryList):
     
    278272            SeqResult['varyList'] = saveVaryList
    279273        else:
     274            # first histogram to refine against
    280275            newVaryList = varyList[:]
    281276            for i,item in enumerate(newVaryList):
     
    286281                newVaryList[i] = item
    287282            if newVaryList != SeqResult['varyList']:
    288                 #print 'histogram',histogram,len(newVaryList),'variables'
    289                 #print newVaryList
    290                 #print 'previous',len(SeqResult['varyList']),'variables'
    291                 #print SeqResult['varyList']
     283                # variable lists are expected to match between sequential refinements
    292284                print '**** ERROR - variable list for this histogram does not match previous'
    293285                print '\ncurrent histogram',histogram,'has',len(newVaryList),'variables'
     
    317309                print line
    318310                raise Exception
     311        parmDict = {}
    319312        parmDict.update(phaseDict)
    320313        parmDict.update(hapDict)
     
    324317        G2stIO.GetFprime(calcControls,Histo)
    325318        # do constraint processing
     319        #reload(G2mv) # debug
    326320        G2mv.InitVars()   
    327321        constrDict,fixedList = G2stIO.GetConstraints(GPXfile)
     
    329323        try:
    330324            groups,parmlist = G2mv.GroupConstraints(constrDict)
    331             G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList)
     325            G2mv.GenerateConstraints(groups,parmlist,varyList,constrDict,fixedList,parmDict)
    332326        except:
    333327            print ' *** ERROR - your constraints are internally inconsistent ***'
     328            #errmsg, warnmsg = G2mv.CheckConstraints(varyList,constrDict,fixedList)
     329            #print 'Errors',errmsg
     330            #if warnmsg: print 'Warnings',warnmsg
    334331            raise Exception(' *** Refine aborted ***')
    335         # check to see which generated parameters are fully varied
    336         # msg = G2mv.SetVaryFlags(varyList)
    337         # if msg:
    338         #     print ' *** ERROR - you have not set the refine flags for constraints consistently! ***'
    339         #     print msg
    340         #     raise Exception(' *** Refine aborted ***')
    341         #print G2mv.VarRemapShow(varyList)
    342332       
    343333        ifPrint = False
Note: See TracChangeset for help on using the changeset viewer.