Ignore:
Timestamp:
Sep 8, 2017 5:56:57 PM (6 years ago)
Author:
toby
Message:

more work on constraints: prevent use of perviously constrained variables in new equivalences; include sym. generated constraints in checks; do not allow error-generating constraints to be added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIconstrGUI.py

    r3056 r3057  
    382382                        varList += [var]
    383383                        choices.append([var,hlbl,meaning])
    384         elif page[1] == 'glb':
     384        elif page[1] == 'glb' or page[1] == 'sym':
    385385            pass
    386386        else:
     
    498498        return []
    499499
     500    def CheckConstraints(constraintSet):
     501        '''Check for errors in a set of constraints. Constraints based on symmetry (etc.)
     502        are generated by running :func:`GSASIIstrIO.GetPhaseData`.
     503        '''
     504        G2mv.InitVars()   
     505        constDictList,fixedList,ignored = G2stIO.ProcessConstraints(constraintSet)
     506        # generate symmetry constraints to check for conflicts
     507        rigidbodyDict = G2frame.GPXtree.GetItemPyData(   
     508            G2gd.GetGPXtreeItemId(G2frame,G2frame.root,'Rigid bodies'))
     509        rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
     510        rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,Print=False)
     511        Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,maxSSwave = G2stIO.GetPhaseData(
     512            Phases,RestraintDict=None,rbIds=rbIds,Print=False) # generates atom symmetry constraints
     513        return G2mv.CheckConstraints('',constDictList,fixedList)
     514
    500515    def CheckAddedConstraint(newcons):
    501516        '''Check a new constraint that has just been input.
    502         If there is an error display a message and give the user a
    503         choice to keep or discard the last entry (why keep? -- they
    504         may want to delete something else or edit multipliers).
     517        If there is an error display a message and discard the last entry
     518
    505519        Since the varylist is not available, no warning messages
    506         should be generated.
     520        should be generated here
    507521
    508522        :returns: True if constraint should be added
    509523        '''
    510         allcons = []
     524        allcons1 = []
    511525        for key in data:
    512526            if key.startswith('_'): continue
    513             allcons += data[key]
     527            allcons1 += data[key]
     528        allcons = allcons1[:]
    514529        allcons += newcons
    515530        if not len(allcons): return True
    516         G2mv.InitVars()   
    517         constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
    518         errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
     531        errmsg,warnmsg = CheckConstraints(allcons)
    519532        if errmsg:
    520533            res = G2frame.ErrorDialog('Constraint Error',
    521534                'Error with newly added constraint:\n'+errmsg+
    522                 '\n\nDiscard newly added constraint?',parent=G2frame,
    523                 wtype=wx.YES_NO)
    524             return res != wx.ID_YES
     535                '\nIgnoring newly added constraint',parent=G2frame)
     536            # reset error status
     537            errmsg,warnmsg = CheckConstraints(allcons1)
     538            if errmsg:
     539                print errmsg
     540                print G2mv.VarRemapShow([],True)
     541            return False
    525542        elif warnmsg:
    526543            print 'Unexpected contraint warning:\n',warnmsg
     
    529546    def CheckChangedConstraint():
    530547        '''Check all constraints after an edit has been made.
    531         If there is an error display a message and give the user a
    532         choice to keep or discard the last edit.
     548        If there is an error display a message and reject the change.
     549
    533550        Since the varylist is not available, no warning messages
    534551        should be generated.
     
    541558            allcons += data[key]
    542559        if not len(allcons): return True
    543         G2mv.InitVars()   
    544         constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
    545         errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
     560        errmsg,warnmsg = CheckConstraints(allcons)
    546561        if errmsg:
    547562            res = G2frame.ErrorDialog('Constraint Error',
    548563                'Error after editing constraint:\n'+errmsg+
    549                 '\n\nDiscard last constraint edit?',parent=G2frame,
    550                 wtype=wx.YES_NO)
    551             return res != wx.ID_YES
     564                '\nDiscarding last constraint edit',parent=G2frame)
     565            # reset error status
     566            errmsg,warnmsg = CheckConstraints(allcons1)
     567            if errmsg:
     568                print errmsg
     569                print G2mv.VarRemapShow([],True)
     570            return False
    552571        elif warnmsg:
    553572            print 'Unexpected contraint warning:\n',warnmsg
     
    572591            varList = globalList
    573592            constrDictEnt = 'Global'
     593        elif page[1] == "sym":
     594            return None,None,None
    574595        else:
    575596            raise Exception,'Should not happen!'
     
    584605        page = G2frame.Page
    585606        vartype,varList,constrDictEnt = PageSelection(page)
     607        if vartype is None: return
    586608        title1 = "Hold "+vartype+" variable"
    587609        if not varList:
     
    615637        page = G2frame.Page
    616638        vartype,varList,constrDictEnt = PageSelection(page)
     639        if vartype is None: return
    617640        title1 = "Setup equivalent "+vartype+" variables"
    618641        title2 = "Select additional "+vartype+" variable(s) to be equivalent with "
     
    628651        page = G2frame.Page
    629652        vartype,varList,constrDictEnt = PageSelection(page)
     653        if vartype is None: return
    630654        title1 = "Setup equivalent atom variables"
    631655        title2 = "Select additional atoms(s) to be equivalent with "
     
    641665        page = G2frame.Page
    642666        vartype,varList,constrDictEnt = PageSelection(page)
     667        if vartype is None: return
    643668        title1 = "Setup riding atoms "
    644669        title2 = "Select additional atoms(s) to ride on "
     
    654679        page = G2frame.Page
    655680        vartype,varList,constrDictEnt = PageSelection(page)
     681        if vartype is None: return
    656682        title1 = "Setup new variable based on "+vartype+" variables"
    657683        title2 = "Include additional "+vartype+" variable(s) to be included with "
     
    667693        page = G2frame.Page
    668694        vartype,varList,constrDictEnt = PageSelection(page)
     695        if vartype is None: return
    669696        title1 = "Setup constraint on "+vartype+" variables"
    670697        title2 = "Select additional "+vartype+" variable(s) to include in constraint with "
     
    687714            l2 = max(l2,len(loc))
    688715        fmt = "{:"+str(l1)+"s} {:"+str(l2)+"s} {:s}"
    689         varListlbl = [fmt.format(i,*G2obj.VarDescr(i)) for i in varList]       
     716        if constType == 'equivalence':
     717            omitVars = G2mv.GetDependentVars()
     718        else:
     719            omitVars = []
     720        varList = [i for i in varList if i not in omitVars]
     721        varListlbl = [fmt.format(i,*G2obj.VarDescr(i)) for i in varList]
    690722        dlg = G2G.G2SingleChoiceDialog(G2frame,'Select 1st variable:',
    691723            title1,varListlbl,monoFont=True,size=(625,400))
    692724        dlg.CenterOnParent()
    693725        if dlg.ShowModal() == wx.ID_OK:
     726            if constType == 'equivalence':
     727                omitVars = G2mv.GetDependentVars() + G2mv.GetIndependentVars()
    694728            sel = dlg.GetSelection()
    695729            FrstVarb = varList[sel]
    696730            VarObj = G2obj.G2VarObj(FrstVarb)
    697             moreVarb = FindEquivVarb(FrstVarb,varList)
     731            moreVarb = FindEquivVarb(FrstVarb,[i for i in varList if i not in omitVars])
    698732            newcons = SelectVarbs(page,VarObj,moreVarb,title2+FrstVarb,constType)
    699733            if len(newcons) > 0:
     
    10731107                 G2G.wxID_CONSHAP,
    10741108                 G2G.wxID_CONSHIST,
    1075                  G2G.wxID_CONSGLOBAL).index(event.GetId())
     1109                 G2G.wxID_CONSGLOBAL,
     1110                 G2G.wxID_CONSSYM,
     1111                ).index(event.GetId())
    10761112            G2frame.constr.SetSelection(i)
    10771113            wx.CallAfter(OnPageChanged,None)
     
    10971133               G2G.wxID_CONSHAP,
    10981134               G2G.wxID_CONSHIST,
    1099                G2G.wxID_CONSGLOBAL):
     1135               G2G.wxID_CONSGLOBAL,
     1136               G2G.wxID_CONSSYM,
     1137               ):
    11001138        G2frame.Bind(wx.EVT_MENU, RaisePage,id=id)
    11011139
     
    11221160        allcons += data[key]
    11231161    if not len(allcons): return
    1124     #reload(G2mv) # TODO: for testing
    1125     G2mv.InitVars()   
    1126     constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
    1127     # generate symmetry constraints to check for conflicts
    1128     rigidbodyDict = G2frame.GPXtree.GetItemPyData(   
    1129             G2gd.GetGPXtreeItemId(G2frame,G2frame.root,'Rigid bodies'))
    1130     rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
    1131     rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,Print=False)
    1132     Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtables,BLtables,MFtables,maxSSwave = G2stIO.GetPhaseData(
    1133         Phases,RestraintDict=None,rbIds=rbIds,Print=False) # generates atom symmetry constraints
    1134     errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
     1162    errmsg,warnmsg = CheckConstraints(allcons)
    11351163    if errmsg:
    11361164        G2frame.ErrorDialog('Constraint Error',
Note: See TracChangeset for help on using the changeset viewer.