source: branch/2frame/GSASIIconstrGUI.py @ 2914

Last change on this file since 2914 was 2914, checked in by vondreele, 6 years ago

fix G2constrGUI; dataDisplay --> rbBook & fix a parent issue
similarly in G2restrGUI dataDisplay --> restrBook
G2ctrlGUI; missing G2obj import & fix a parent issue on a couple of dialogs
put the wxID definitions into _initMenus - need to be sorted into each section
remove the tree title - redundant info
tree root text changed
G2plot; change dataDisplay to restrBook for torsion & Ramachandran plots
some dataDisplay --> phaseDisplay in various places

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 91.1 KB
Line 
1# -*- coding: utf-8 -*-
2#GSASIIconstrGUI - constraint GUI routines
3########### SVN repository information ###################
4# $Date: 2017-07-07 17:32:09 +0000 (Fri, 07 Jul 2017) $
5# $Author: vondreele $
6# $Revision: 2914 $
7# $URL: branch/2frame/GSASIIconstrGUI.py $
8# $Id: GSASIIconstrGUI.py 2914 2017-07-07 17:32:09Z vondreele $
9########### SVN repository information ###################
10'''
11*GSASIIconstrGUI: Constraint GUI routines*
12------------------------------------------
13
14Used to define constraints and rigid bodies.
15
16'''
17import sys
18import wx
19import wx.grid as wg
20import random as ran
21import numpy as np
22import numpy.ma as ma
23import numpy.linalg as nl
24import os.path
25import GSASIIpath
26GSASIIpath.SetVersionNumber("$Revision: 2914 $")
27import GSASIIElem as G2elem
28import GSASIIElemGUI as G2elemGUI
29import GSASIIstrIO as G2stIO
30import GSASIImapvars as G2mv
31import GSASIImath as G2mth
32import GSASIIlattice as G2lat
33import GSASIIdataGUI as G2gd
34import GSASIIctrlGUI as G2G
35import GSASIIplot as G2plt
36import GSASIIobj as G2obj
37import GSASIIspc as G2spc
38VERY_LIGHT_GREY = wx.Colour(235,235,235)
39
40class MultiIntegerDialog(wx.Dialog):
41    '''Input a series of integers based on prompts
42    '''
43    def __init__(self,parent,title,prompts,values):
44        wx.Dialog.__init__(self,parent,-1,title, 
45            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
46        self.panel = wx.Panel(self)         #just a dummy - gets destroyed in Draw!
47        self.values = values
48        self.prompts = prompts
49        self.Draw()
50       
51    def Draw(self):
52       
53        def OnValItem(event):
54            event.Skip()
55            Obj = event.GetEventObject()
56            ind = Indx[Obj.GetId()]
57            try:
58                val = int(Obj.GetValue())
59                if val <= 0:
60                    raise ValueError
61            except ValueError:
62                val = self.values[ind]
63            self.values[ind] = val
64            Obj.SetValue('%d'%(val))
65           
66        self.panel.Destroy()
67        self.panel = wx.Panel(self)
68        mainSizer = wx.BoxSizer(wx.VERTICAL)
69        Indx = {}
70        for ival,[prompt,value] in enumerate(zip(self.prompts,self.values)):
71            mainSizer.Add(wx.StaticText(self.panel,-1,prompt),0,wx.ALIGN_CENTER)
72            valItem = wx.TextCtrl(self.panel,-1,value='%d'%(value),style=wx.TE_PROCESS_ENTER)
73            mainSizer.Add(valItem,0,wx.ALIGN_CENTER)
74            Indx[valItem.GetId()] = ival
75            valItem.Bind(wx.EVT_TEXT_ENTER,OnValItem)
76            valItem.Bind(wx.EVT_KILL_FOCUS,OnValItem)
77        OkBtn = wx.Button(self.panel,-1,"Ok")
78        OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
79        CancelBtn = wx.Button(self.panel,-1,'Cancel')
80        CancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
81        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
82        btnSizer.Add((20,20),1)
83        btnSizer.Add(OkBtn)
84        btnSizer.Add(CancelBtn)
85        btnSizer.Add((20,20),1)
86        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
87        self.panel.SetSizer(mainSizer)
88        self.panel.Fit()
89        self.Fit()
90
91    def GetValues(self):
92        return self.values
93       
94    def OnOk(self,event):
95        parent = self.GetParent()
96        parent.Raise()
97        self.EndModal(wx.ID_OK)             
98       
99    def OnCancel(self,event):
100        parent = self.GetParent()
101        parent.Raise()
102        self.EndModal(wx.ID_CANCEL)
103
104class ConstraintDialog(wx.Dialog):
105    '''Window to edit Constraint values
106    '''
107    def __init__(self,parent,title,text,data,separator='*',varname="",varyflag=False):
108        wx.Dialog.__init__(self,parent,-1,'Edit '+title, 
109            pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE)
110        self.data = data[:]
111        self.newvar = [varname,varyflag]
112        panel = wx.Panel(self)
113        mainSizer = wx.BoxSizer(wx.VERTICAL)
114        topLabl = wx.StaticText(panel,-1,text)
115        mainSizer.Add((10,10),1)
116        mainSizer.Add(topLabl,0,wx.ALIGN_CENTER_VERTICAL|wx.LEFT,10)
117        mainSizer.Add((10,10),1)
118        dataGridSizer = wx.FlexGridSizer(cols=3,hgap=2,vgap=2)
119        self.OkBtn = wx.Button(panel,wx.ID_OK)
120        for id in range(len(self.data)):
121            lbl1 = lbl = str(self.data[id][1])
122            if lbl[-1] != '=': lbl1 = lbl + ' ' + separator + ' '
123            name = wx.StaticText(panel,wx.ID_ANY,lbl1,
124                                 style=wx.ALIGN_RIGHT)
125            scale = G2G.ValidatedTxtCtrl(panel,self.data[id],0,
126                                          typeHint=float,
127                                          OKcontrol=self.DisableOK)
128            dataGridSizer.Add(name,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,5)
129            dataGridSizer.Add(scale,0,wx.RIGHT,3)
130            if ':' in lbl:
131                dataGridSizer.Add(
132                    wx.StaticText(panel,-1,G2obj.fmtVarDescr(lbl)),
133                    0,wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,3)
134            else:
135                dataGridSizer.Add((-1,-1))
136        if title == 'New Variable':
137            name = wx.StaticText(panel,wx.ID_ANY,"New variable's\nname (optional)",
138                                 style=wx.ALIGN_CENTER)
139            scale = G2G.ValidatedTxtCtrl(panel,self.newvar,0,
140                                          typeHint=str,notBlank=False)
141            dataGridSizer.Add(name,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,5)
142            dataGridSizer.Add(scale,0,wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,3)
143            self.refine = wx.CheckBox(panel,label='Refine?')
144            self.refine.SetValue(self.newvar[1]==True)
145            self.refine.Bind(wx.EVT_CHECKBOX, self.OnCheckBox)
146            dataGridSizer.Add(self.refine,0,wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,3)
147           
148        mainSizer.Add(dataGridSizer,0,wx.EXPAND)
149        self.OkBtn.Bind(wx.EVT_BUTTON, self.OnOk)
150        self.OkBtn.SetDefault()
151        cancelBtn = wx.Button(panel,wx.ID_CANCEL)
152        cancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
153        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
154        btnSizer.Add((20,20),1)
155        btnSizer.Add(self.OkBtn)
156        btnSizer.Add((20,20),1)
157        btnSizer.Add(cancelBtn)
158        btnSizer.Add((20,20),1)
159
160        mainSizer.Add(btnSizer,0,wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
161        panel.SetSizer(mainSizer)
162        panel.Fit()
163        self.Fit()
164        self.CenterOnParent()
165       
166    def DisableOK(self,setting):
167        if setting:
168            self.OkBtn.Enable()
169        else:
170            self.OkBtn.Disable()
171
172    def OnCheckBox(self,event):
173        self.newvar[1] = self.refine.GetValue()
174
175    def OnOk(self,event):
176        parent = self.GetParent()
177        parent.Raise()
178        self.EndModal(wx.ID_OK)             
179
180    def OnCancel(self,event):
181        parent = self.GetParent()
182        parent.Raise()
183        self.EndModal(wx.ID_CANCEL)             
184
185    def GetData(self):
186        return self.data
187       
188################################################################################
189#####  Constraints
190################################################################################           
191       
192def UpdateConstraints(G2frame,data):
193    '''Called when Constraints tree item is selected.
194    Displays the constraints in the data window
195    '''
196    if not data:
197        data.update({'Hist':[],'HAP':[],'Phase':[],'Global':[]})       #empty dict - fill it
198    if 'Global' not in data:                                            #patch
199        data['Global'] = []
200    # DEBUG code ########################################
201    #import GSASIIconstrGUI
202    #reload(GSASIIconstrGUI)
203    #reload(G2obj)
204    #reload(G2stIO)
205    #import GSASIIstrMain
206    #reload(GSASIIstrMain)   
207    #reload(G2mv)
208    #reload(G2gd)
209    ###################################################
210    Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
211    G2obj.IndexAllIds(Histograms,Phases)
212    ##################################################################################
213    # patch: convert old-style (str) variables in constraints to G2VarObj objects
214    for key,value in data.items():
215        if key.startswith('_'): continue
216        j = 0
217        for cons in value:
218            #print cons             # DEBUG
219            for i in range(len(cons[:-3])):
220                if type(cons[i][1]) is str:
221                    cons[i][1] = G2obj.G2VarObj(cons[i][1])
222                    j += 1
223        if j:
224            print str(key) + ': '+str(j)+' variable(s) as strings converted to objects'
225    ##################################################################################
226    rigidbodyDict = G2frame.GPXtree.GetItemPyData(
227        G2gd.GetGPXtreeItemId(G2frame,G2frame.root,'Rigid bodies'))
228    rbIds = rigidbodyDict.get('RBIds',{'Vector':[],'Residue':[]})
229    rbVary,rbDict = G2stIO.GetRigidBodyModels(rigidbodyDict,Print=False)
230    badPhaseParms = ['Ax','Ay','Az','Amul','AI/A','Atype','SHorder','mV0','mV1','mV2','waveType','Vol','isMag',]
231    globalList = rbDict.keys()
232    globalList.sort()
233    try:
234        AtomDict = dict([Phases[phase]['pId'],Phases[phase]['Atoms']] for phase in Phases)
235    except KeyError:
236        G2frame.ErrorDialog('Constraint Error','You must run least squares at least once before setting constraints\n'+ \
237            'We suggest you refine scale factor first')
238        return
239
240    # create a list of the phase variables
241    Natoms,atomIndx,phaseVary,phaseDict,pawleyLookup,FFtable,BLtable,MFtable,maxSSwave = G2stIO.GetPhaseData(Phases,rbIds=rbIds,Print=False)
242    phaseList = []
243    for item in phaseDict:
244        if item.split(':')[2] not in badPhaseParms:
245            phaseList.append(item)
246    phaseList.sort()
247    phaseAtNames = {}
248    phaseAtTypes = {}
249    TypeList = []
250    for item in phaseList:
251        Split = item.split(':')
252        if Split[2][:2] in ['AU','Af','dA','AM']:
253            Id = int(Split[0])
254            phaseAtNames[item] = AtomDict[Id][int(Split[3])][0]
255            phaseAtTypes[item] = AtomDict[Id][int(Split[3])][1]
256            if phaseAtTypes[item] not in TypeList:
257                TypeList.append(phaseAtTypes[item])
258        else:
259            phaseAtNames[item] = ''
260            phaseAtTypes[item] = ''
261             
262    # create a list of the hist*phase variables; include wildcards here
263    hapVary,hapDict,controlDict = G2stIO.GetHistogramPhaseData(Phases,Histograms,Print=False)
264    hapList = [i for i in hapDict.keys() if i.split(':')[2] not in ('Type',)]
265    hapList.sort()
266    wildList = [] # list of variables with "*" for histogram number
267    for i in hapList:
268        s = i.split(':')
269        if s[1] == "": continue
270        s[1] = '*'
271        sj = ':'.join(s)
272        if sj not in wildList: wildList.append(sj)
273    #wildList.sort() # unneeded
274    hapList += wildList
275    histVary,histDict,controlDict = G2stIO.GetHistogramData(Histograms,Print=False)
276    histList = []
277    for item in histDict:
278        if item.split(':')[2] not in ['Omega','Type','Chi','Phi',
279                                      'Azimuth','Gonio. radius',
280                                      'Lam1','Lam2','Back','Temperature','Pressure',
281                                      'FreePrm1','FreePrm2','FreePrm3',
282                                      ]:
283            histList.append(item)
284    histList.sort()
285    wildList = []
286    # for i in histList: # any reason to have this for hist constraints?
287    #     s = i.split(':')
288    #     if s[1] == "": continue
289    #     s[1] = '*'
290    #     sj = ':'.join(s)
291    #     if sj not in wildList: wildList.append(sj)
292    # histList += wildList
293    Indx = {}
294    G2frame.Page = [0,'phs']
295       
296    def FindEquivVarb(name,nameList):
297        'Creates a list of variables appropriate to constrain with name'
298        outList = []
299        #phlist = []
300        items = name.split(':')
301        namelist = [items[2],]
302        if 'dA' in name:
303            namelist = ['dAx','dAy','dAz']
304        elif 'AU' in name:
305            namelist = ['AUiso','AU11','AU22','AU33','AU12','AU13','AU23']
306        elif 'AM' in name:
307            namelist = ['AMx','AMy','AMz']
308        elif items[-1] in ['A0','A1','A2','A3','A4','A5']:
309            namelist = ['A0','A1','A2','A3','A4','A5']
310        elif items[-1] in ['D11','D22','D33','D12','D13','D23']:
311            namelist = ['D11','D22','D33','D12','D13','D23']
312        elif 'Tm' in name:
313            namelist = ['Tmin','Tmax']
314        elif 'RB' in name:
315            rbfx = 'RB'+items[2][2]
316            if 'T' in name and 'Tr' not in name:
317                namelist = [rbfx+'T11',rbfx+'T22',rbfx+'T33',rbfx+'T12',rbfx+'T13',rbfx+'T23']
318            if 'L' in name:
319                namelist = [rbfx+'L11',rbfx+'L22',rbfx+'L33',rbfx+'L12',rbfx+'L13',rbfx+'L23']
320            if 'S' in name:
321                namelist = [rbfx+'S12',rbfx+'S13',rbfx+'S21',rbfx+'S23',rbfx+'S31',rbfx+'S32',rbfx+'SAA',rbfx+'SBB']
322            if 'U' in name:
323                namelist = [rbfx+'U',]
324
325        for item in nameList:
326            keys = item.split(':')
327            #if keys[0] not in phlist:
328            #    phlist.append(keys[0])
329            if items[1] == '*' and keys[2] in namelist: # wildcard -- select only sequential options
330                keys[1] = '*'
331                mitem = ':'.join(keys)
332                if mitem == name: continue
333                if mitem not in outList: outList.append(mitem)
334            elif keys[2] in namelist and item != name:
335                outList.append(item)
336        return outList
337       
338    def SelectVarbs(page,FrstVarb,varList,legend,constType):
339        '''Select variables used in constraints after one variable has
340        been selected. This routine determines the appropriate variables to be
341        used based on the one that has been selected and asks for more to be added.
342
343        It then creates the constraint and adds it to the constraints list.
344       
345        Called from OnAddEquivalence, OnAddFunction & OnAddConstraint (all but
346        OnAddHold)
347
348        :param list page: defines calling page -- type of variables to be used
349        :parm GSASIIobj.G2VarObj FrstVarb: reference to first selected variable
350        :param list varList: list of other appropriate variables to select from
351        :param str legend: header for selection dialog
352        :param str constType: type of constraint to be generated
353        :returns: a constraint, as defined in
354          :ref:`GSASIIobj <Constraint_definitions_table>`
355        '''
356        choices = [[i]+list(G2obj.VarDescr(i)) for i in varList]
357        meaning = G2obj.getDescr(FrstVarb.name)
358        if not meaning:
359            meaning = "(no definition found!)"
360        l = str(FrstVarb).split(':')
361        # make lists of phases & histograms to iterate over
362        phaselist = [l[0]]
363        if l[0]:
364            phaselbl = ['phase #'+l[0]]
365            if len(Phases) > 1:
366                phaselist += ['all'] 
367                phaselbl += ['all phases']
368        else:
369            phaselbl = ['']
370        histlist = [l[1]]
371        if l[1] == '*':
372            pass
373        elif l[1]:
374            histlbl = ['histogram #'+l[1]]
375            if len(Histograms) > 1:
376                histlist += ['all']
377                histlbl += ['all histograms']
378                typ = Histograms[G2obj.LookupHistName(l[1])[0]]['Instrument Parameters'][0]['Type'][1]
379                i = 0
380                for hist in Histograms:
381                    if Histograms[hist]['Instrument Parameters'][0]['Type'][1] == typ: i += 1
382                if i > 1:
383                    histlist += ['all='+typ]
384                    histlbl += ['all '+typ+' histograms']
385        else:
386            histlbl = ['']
387        # make a list of equivalent parameter names
388        nameList = [FrstVarb.name]
389        for var in varList:
390            nam = var.split(":")[2]
391            if nam not in nameList: nameList += [nam]
392        # add "wild-card" names to the list of variables
393        if l[1] == '*':
394            pass
395        elif page[1] == 'phs':
396            if 'RB' in FrstVarb.name:
397                pass
398            elif FrstVarb.atom is None:
399                for nam in nameList:
400                    for ph,plbl in zip(phaselist,phaselbl):
401                        if plbl: plbl = 'For ' + plbl
402                        var = ph+"::"+nam
403                        if var == str(FrstVarb) or var in varList: continue
404                        varList += [var]
405                        choices.append([var,plbl,meaning])
406            else:
407                for nam in nameList:
408                    for ph,plbl in zip(phaselist,phaselbl):
409                        if plbl: plbl = ' in ' + plbl
410                        for atype in ['']+TypeList:
411                            if atype:
412                                albl = "For "+atype+" atoms"
413                                akey = "all="+atype                       
414                            else:
415                                albl = "For all atoms"
416                                akey = "all"
417                            var = ph+"::"+nam+":"+akey
418                            if var == str(FrstVarb) or var in varList: continue
419                            varList += [var]
420                            choices.append([var,albl+plbl,meaning])
421        elif page[1] == 'hap':
422            if FrstVarb.name == "Scale":
423                meaning = "Phase fraction"
424            for nam in nameList:
425                for ph,plbl in zip(phaselist,phaselbl):
426                    if plbl: plbl = 'For ' + plbl
427                    for hst,hlbl in zip(histlist,histlbl):
428                        if hlbl:
429                            if plbl:
430                                hlbl = ' in ' + hlbl
431                            else:
432                                hlbl = 'For ' + hlbl                               
433                            var = ph+":"+hst+":"+nam
434                            if var == str(FrstVarb) or var in varList: continue
435                            varList += [var]
436                            choices.append([var,plbl+hlbl,meaning])
437        elif page[1] == 'hst':
438            if FrstVarb.name == "Scale":
439                meaning = "Scale factor"
440            for nam in nameList:
441                for hst,hlbl in zip(histlist,histlbl):
442                    if hlbl:
443                        hlbl = 'For ' + hlbl                               
444                        var = ":"+hst+":"+nam
445                        if var == str(FrstVarb) or var in varList: continue
446                        varList += [var]
447                        choices.append([var,hlbl,meaning])
448        elif page[1] == 'glb':
449            pass
450        else:
451            raise Exception, 'Unknown constraint page '+ page[1]                   
452        if len(choices):
453            l1 = l2 = 1
454            for i1,i2,i3 in choices:
455                l1 = max(l1,len(i1))
456                l2 = max(l2,len(i2))
457            fmt = "{:"+str(l1)+"s} {:"+str(l2)+"s} {:s}"
458            atchoice = [fmt.format(*i1) for i1 in choices]
459            dlg = G2G.G2MultiChoiceDialog(
460                G2frame,legend,
461                'Constrain '+str(FrstVarb)+' with...',atchoice,
462                toggle=False,size=(625,400),monoFont=True)
463            dlg.CenterOnParent()
464            res = dlg.ShowModal()
465            Selections = dlg.GetSelections()[:]
466            dlg.Destroy()
467            if res != wx.ID_OK: return []
468            if len(Selections) == 0:
469                dlg = wx.MessageDialog(
470                    G2frame,
471                    'No variables were selected to include with '+str(FrstVarb),
472                    'No variables')
473                dlg.CenterOnParent()
474                dlg.ShowModal()
475                dlg.Destroy()
476                return []
477        else:
478            dlg = wx.MessageDialog(
479                G2frame,
480                'There are no appropriate variables to include with '+str(FrstVarb),
481                'No variables')
482            dlg.CenterOnParent()
483            dlg.ShowModal()
484            dlg.Destroy()
485            return []
486        # now process the variables provided by the user
487        varbs = [str(FrstVarb),] # list of selected variables
488        for sel in Selections:
489            var = varList[sel]
490            # phase(s) included
491            l = var.split(':')
492            if l[0] == "all":
493                phlist = [str(Phases[phase]['pId']) for phase in Phases]
494            else:
495                phlist = [l[0]]
496            # histogram(s) included
497            if l[1] == "all":
498                hstlist = [str(Histograms[hist]['hId']) for hist in Histograms]
499            elif '=' in l[1]:
500                htyp = l[1].split('=')[1]
501                hstlist = [str(Histograms[hist]['hId']) for hist in Histograms if 
502                           Histograms[hist]['Instrument Parameters'][0]['Type'][1] == htyp]
503            else:
504                hstlist = [l[1]]
505            if len(l) == 3:
506                for ph in phlist:
507                    for hst in hstlist:
508                        var = ph + ":" + hst + ":" + l[2]
509                        if var in varbs: continue
510                        varbs.append(var)
511            else: # constraints with atoms or rigid bodies
512                if len(l) == 5: # rigid body parameter
513                    var = ':'.join(l)
514                    if var in varbs: continue
515                    varbs.append(var)
516                elif l[3] == "all":
517                    for ph in phlist:
518                        key = G2obj.LookupPhaseName(l[0])[0]
519                        for hst in hstlist: # should be blank
520                            for iatm,at in enumerate(Phases[key]['Atoms']):
521                                var = ph + ":" + hst + ":" + l[2] + ":" + str(iatm)
522                                if var in varbs: continue
523                                varbs.append(var)
524                elif '=' in l[3]:
525                    for ph in phlist:
526                        key = G2obj.LookupPhaseName(l[0])[0]
527                        cx,ct,cs,cia = Phases[key]['General']['AtomPtrs']
528                        for hst in hstlist: # should be blank
529                            atyp = l[3].split('=')[1]
530                            for iatm,at in enumerate(Phases[key]['Atoms']):
531                                if at[ct] != atyp: continue
532                                var = ph + ":" + hst + ":" + l[2] + ":" + str(iatm)
533                                if var in varbs: continue
534                                varbs.append(var)
535                else:
536                    for ph in phlist:
537                        key = G2obj.LookupPhaseName(l[0])[0]
538                        for hst in hstlist: # should be blank
539                            var = ph + ":" + hst + ":" + l[2] + ":" + l[3]
540                            if var in varbs: continue
541                            varbs.append(var)
542        if len(varbs) >= 1 or 'constraint' in constType:
543            constr = [[1.0,FrstVarb]]
544            for item in varbs[1:]:
545                constr += [[1.0,G2obj.G2VarObj(item)]]
546            if 'equivalence' in constType:
547                return [constr+[None,None,'e']]
548            elif 'function' in constType:
549                return [constr+[None,False,'f']]
550            elif 'constraint' in constType:
551                return [constr+[1.0,None,'c']]
552            else:
553                raise Exception,'Unknown constraint type: '+str(constType)
554        else:
555            dlg = wx.MessageDialog(
556                G2frame,
557                'There are no selected variables to include with '+str(FrstVarb),
558                'No variables')
559            dlg.CenterOnParent()
560            dlg.ShowModal()
561            dlg.Destroy()
562        return []
563
564    def CheckAddedConstraint(newcons):
565        '''Check a new constraint that has just been input.
566        If there is an error display a message and give the user a
567        choice to keep or discard the last entry (why keep? -- they
568        may want to delete something else or edit multipliers).
569        Since the varylist is not available, no warning messages
570        should be generated.
571
572        :returns: True if constraint should be added
573        '''
574        allcons = []
575        for key in data:
576            if key.startswith('_'): continue
577            allcons += data[key]
578        allcons += newcons
579        if not len(allcons): return True
580        G2mv.InitVars()   
581        constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
582        errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
583        if errmsg:
584            res = G2frame.ErrorDialog('Constraint Error',
585                'Error with newly added constraint:\n'+errmsg+
586                '\n\nDiscard newly added constraint?',parent=G2frame,
587                wtype=wx.YES_NO)
588            return res != wx.ID_YES
589        elif warnmsg:
590            print 'Unexpected contraint warning:\n',warnmsg
591        return True
592
593    def CheckChangedConstraint():
594        '''Check all constraints after an edit has been made.
595        If there is an error display a message and give the user a
596        choice to keep or discard the last edit.
597        Since the varylist is not available, no warning messages
598        should be generated.
599       
600        :returns: True if the edit should be retained
601        '''
602        allcons = []
603        for key in data:
604            if key.startswith('_'): continue
605            allcons += data[key]
606        if not len(allcons): return True
607        G2mv.InitVars()   
608        constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
609        errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
610        if errmsg:
611            res = G2frame.ErrorDialog('Constraint Error',
612                'Error after editing constraint:\n'+errmsg+
613                '\n\nDiscard last constraint edit?',parent=G2frame,
614                wtype=wx.YES_NO)
615            return res != wx.ID_YES
616        elif warnmsg:
617            print 'Unexpected contraint warning:\n',warnmsg
618        return True
619             
620    def PageSelection(page):
621        'Decode page reference'
622        if page[1] == "phs":
623            vartype = "phase"
624            varList = phaseList
625            constrDictEnt = 'Phase'
626        elif page[1] == "hap":
627            vartype = "Histogram*Phase"
628            varList = hapList
629            constrDictEnt = 'HAP'
630        elif page[1] == "hst":
631            vartype = "Histogram"
632            varList = histList
633            constrDictEnt = 'Hist'
634        elif page[1] == "glb":
635            vartype = "Global"
636            varList = globalList
637            constrDictEnt = 'Global'
638        else:
639            raise Exception,'Should not happen!'
640        return vartype,varList,constrDictEnt
641
642    def OnAddHold(event):
643        '''Create a new Hold constraint
644
645        Hold constraints allows the user to select one variable (the list of available
646        variables depends on which tab is currently active).
647        '''
648        page = G2frame.Page
649        vartype,varList,constrDictEnt = PageSelection(page)
650        title1 = "Hold "+vartype+" variable"
651        if not varList:
652            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
653                parent=G2frame)
654            return
655        l2 = l1 = 1
656        for i in varList:
657            l1 = max(l1,len(i))
658            loc,desc = G2obj.VarDescr(i)
659            l2 = max(l2,len(loc))
660        fmt = "{:"+str(l1)+"s} {:"+str(l2)+"s} {:s}"
661        varListlbl = [fmt.format(i,*G2obj.VarDescr(i)) for i in varList]
662        #varListlbl = ["("+i+") "+G2obj.fmtVarDescr(i) for i in varList]
663        legend = "Select variables to hold (Will not be varied, even if vary flag is set)"
664        dlg = G2G.G2MultiChoiceDialog(G2frame,
665            legend,title1,varListlbl,toggle=False,size=(625,400),monoFont=True)
666        dlg.CenterOnParent()
667        if dlg.ShowModal() == wx.ID_OK:
668            for sel in dlg.GetSelections():
669                Varb = varList[sel]
670                VarObj = G2obj.G2VarObj(Varb)
671                newcons = [[[0.0,VarObj],None,None,'h']]
672                if CheckAddedConstraint(newcons):
673                    data[constrDictEnt] += newcons
674        dlg.Destroy()
675        wx.CallAfter(OnPageChanged,None)
676       
677    def OnAddEquivalence(event):
678        '''add an Equivalence constraint'''
679        page = G2frame.Page
680        vartype,varList,constrDictEnt = PageSelection(page)
681        title1 = "Setup equivalent "+vartype+" variables"
682        title2 = "Select additional "+vartype+" variable(s) to be equivalent with "
683        if not varList:
684            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
685                parent=G2frame)
686            return
687#        legend = "Select variables to make equivalent (only one of the variables will be varied when all are set to be varied)"
688        GetAddVars(page,title1,title2,varList,constrDictEnt,'equivalence')
689       
690    def OnAddAtomEquiv(event):
691        ''' Add equivalences between all parameters on atoms '''
692        page = G2frame.Page
693        vartype,varList,constrDictEnt = PageSelection(page)
694        title1 = "Setup equivalent atom variables"
695        title2 = "Select additional atoms(s) to be equivalent with "
696        if not varList:
697            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
698                parent=G2frame)
699            return
700#        legend = "Select atoms to make equivalent (only one of the atom variables will be varied when all are set to be varied)"
701        GetAddAtomVars(page,title1,title2,varList,constrDictEnt,'equivalence')
702       
703    def OnAddRiding(event):
704        ''' Add riding equivalences between all parameters on atoms '''
705        page = G2frame.Page
706        vartype,varList,constrDictEnt = PageSelection(page)
707        title1 = "Setup riding atoms "
708        title2 = "Select additional atoms(s) to ride on "
709        if not varList:
710            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
711                parent=G2frame)
712            return
713#        legend = "Select atoms to ride (only one of the atom variables will be varied when all are set to be varied)"
714        GetAddAtomVars(page,title1,title2,varList,constrDictEnt,'riding')
715   
716    def OnAddFunction(event):
717        '''add a Function (new variable) constraint'''
718        page = G2frame.Page
719        vartype,varList,constrDictEnt = PageSelection(page)
720        title1 = "Setup new variable based on "+vartype+" variables"
721        title2 = "Include additional "+vartype+" variable(s) to be included with "
722        if not varList:
723            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
724                parent=G2frame)
725            return
726#        legend = "Select variables to include in a new variable (the new variable will be varied when all included variables are varied)"
727        GetAddVars(page,title1,title2,varList,constrDictEnt,'function')
728                       
729    def OnAddConstraint(event):
730        '''add a constraint equation to the constraints list'''
731        page = G2frame.Page
732        vartype,varList,constrDictEnt = PageSelection(page)
733        title1 = "Setup constraint on "+vartype+" variables"
734        title2 = "Select additional "+vartype+" variable(s) to include in constraint with "
735        if not varList:
736            G2frame.ErrorDialog('No variables','There are no variables of type '+vartype,
737                parent=G2frame)
738            return
739#        legend = "Select variables to include in a constraint equation (the values will be constrainted to equal a specified constant)"
740        GetAddVars(page,title1,title2,varList,constrDictEnt,'constraint')
741
742    def GetAddVars(page,title1,title2,varList,constrDictEnt,constType):
743        '''Get the variables to be added for OnAddEquivalence, OnAddFunction,
744        and OnAddConstraint. Then create and check the constraint.
745        '''
746        #varListlbl = ["("+i+") "+G2obj.fmtVarDescr(i) for i in varList]
747        l2 = l1 = 1
748        for i in varList:
749            l1 = max(l1,len(i))
750            loc,desc = G2obj.VarDescr(i)
751            l2 = max(l2,len(loc))
752        fmt = "{:"+str(l1)+"s} {:"+str(l2)+"s} {:s}"
753        varListlbl = [fmt.format(i,*G2obj.VarDescr(i)) for i in varList]       
754        dlg = G2G.G2SingleChoiceDialog(G2frame,'Select 1st variable:',
755            title1,varListlbl,monoFont=True,size=(625,400))
756        dlg.CenterOnParent()
757        if dlg.ShowModal() == wx.ID_OK:
758            sel = dlg.GetSelection()
759            FrstVarb = varList[sel]
760            VarObj = G2obj.G2VarObj(FrstVarb)
761            moreVarb = FindEquivVarb(FrstVarb,varList)
762            newcons = SelectVarbs(page,VarObj,moreVarb,title2+FrstVarb,constType)
763            if len(newcons) > 0:
764                if CheckAddedConstraint(newcons):
765                    data[constrDictEnt] += newcons
766        dlg.Destroy()
767        wx.CallAfter(OnPageChanged,None)
768                       
769    def FindNeighbors(phase,FrstName,AtNames):
770        General = phase['General']
771        cx,ct,cs,cia = General['AtomPtrs']
772        Atoms = phase['Atoms']
773        atNames = [atom[ct-1] for atom in Atoms]
774        Cell = General['Cell'][1:7]
775        Amat,Bmat = G2lat.cell2AB(Cell)
776        atTypes = General['AtomTypes']
777        Radii = np.array(General['BondRadii'])
778        AtInfo = dict(zip(atTypes,Radii)) #or General['BondRadii']
779        Orig = atNames.index(FrstName.split()[1])
780        OType = Atoms[Orig][ct]
781        XYZ = G2mth.getAtomXYZ(Atoms,cx)       
782        Neigh = []
783        Dx = np.inner(Amat,XYZ-XYZ[Orig]).T
784        dist = np.sqrt(np.sum(Dx**2,axis=1))
785        sumR = AtInfo[OType]+0.5    #H-atoms only!
786        IndB = ma.nonzero(ma.masked_greater(dist-0.85*sumR,0.))
787        for j in IndB[0]:
788            if j != Orig:
789                Neigh.append(AtNames[j])
790        return Neigh
791       
792    def GetAddAtomVars(page,title1,title2,varList,constrDictEnt,constType):
793        '''Get the atom variables to be added for OnAddAtomEquiv. Then create and
794        check the constraints. Riding for H atoms only.
795        '''
796        Atoms = {G2obj.VarDescr(i)[0]:[] for i in varList if 'Atom' in G2obj.VarDescr(i)[0]}
797        for item in varList:
798            atName = G2obj.VarDescr(item)[0]
799            if atName in Atoms:
800                Atoms[atName].append(item)
801        AtNames = Atoms.keys()
802        AtNames.sort()
803        dlg = G2G.G2SingleChoiceDialog(G2frame,'Select 1st atom:',
804            title1,AtNames,monoFont=True,size=(625,400))
805        dlg.CenterOnParent()
806        FrstAtom = ''
807        if dlg.ShowModal() == wx.ID_OK:
808            sel = dlg.GetSelection()
809            FrstAtom = AtNames[sel]
810            if 'riding' in constType:
811                phaseName = (FrstAtom.split(' in ')[1]).strip()
812                phase = Phases[phaseName]
813                AtNames = FindNeighbors(phase,FrstAtom,AtNames)
814            else:
815                AtNames.remove(FrstAtom)
816        dlg.Destroy()
817        if FrstAtom == '':
818            print 'no atom selected'
819            return
820        dlg = G2G.G2MultiChoiceDialog(
821            G2frame,title2+FrstAtom,
822            'Constrain '+str(FrstAtom)+' with...',AtNames,
823            toggle=False,size=(625,400),monoFont=True)
824        if dlg.ShowModal() == wx.ID_OK:
825            Selections = dlg.GetSelections()[:]
826        else:
827            print 'no target atom selected'
828            dlg.Destroy()
829            return
830        dlg.Destroy()
831        for name in Atoms[FrstAtom]:
832            newcons = []
833            constr = []
834            if 'riding' in constType:
835                if 'AUiso' in name:
836                    constr = [[1.0,G2obj.G2VarObj(name)]]
837                elif 'AU11' in name:
838                    pass
839                elif 'AU' not in name:
840                    constr = [[1.0,G2obj.G2VarObj(name)]]
841            else:
842                constr = [[1.0,G2obj.G2VarObj(name)]]
843            pref = ':'+name.rsplit(':',1)[0].split(':',1)[1]    #get stuff between phase id & atom id
844            for sel in Selections:
845                name2 = Atoms[AtNames[sel]][0]
846                pid = name2.split(':',1)[0]                     #get phase id for 2nd atom
847                id = name2.rsplit(':',1)[-1]                    #get atom no. for 2nd atom
848                if 'riding' in constType:
849                    pref = pid+pref
850                    if 'AUiso' in pref:
851                        parts = pref.split('AUiso')
852                        constr += [[1.2,G2obj.G2VarObj('%s:%s'%(parts[0]+'AUiso',id))]]
853                    elif 'AU' not in pref:
854                        constr += [[1.0,G2obj.G2VarObj('%s:%s'%(pref,id))]]
855                else:
856                    constr += [[1.0,G2obj.G2VarObj('%s:%s'%(pid+pref,id))]]
857            if not constr:
858                continue
859            if 'frac' in pref and 'riding' not in constType:
860                newcons = [constr+[1.0,None,'c']]
861            else:
862                newcons = [constr+[None,None,'e']]
863            if len(newcons) > 0:
864                if CheckAddedConstraint(newcons):
865                    data[constrDictEnt] += newcons
866        wx.CallAfter(OnPageChanged,None)
867                       
868    def MakeConstraintsSizer(name,pageDisplay):
869        '''Creates a sizer displaying all of the constraints entered of
870        the specified type.
871
872        :param str name: the type of constraints to be displayed ('HAP',
873          'Hist', 'Phase', or 'Global')
874        :param wx.Panel pageDisplay: parent panel for sizer
875        :returns: wx.Sizer created by method
876        '''
877        #TODO: show symmetry generated constraints - no clue how to do this.
878        constSizer = wx.FlexGridSizer(0,6,0,0)
879        maxlen = 70 # characters before wrapping a constraint
880        for Id,item in enumerate(data[name]):
881            refineflag = False
882            helptext = ""
883            eqString = ['',]
884            if item[-1] == 'h': # Hold on variable
885                constSizer.Add((-1,-1),0)              # blank space for edit button
886                typeString = 'FIXED'
887                var = str(item[0][1])
888                varMean = G2obj.fmtVarDescr(var)
889                eqString[-1] =  var +'   '
890                helptext = "Prevents variable:\n"+ var + " ("+ varMean + ")\nfrom being changed"
891            elif isinstance(item[-1],str): # not true on original-style (2011?) constraints
892                constEdit = wx.Button(pageDisplay,-1,'Edit',style=wx.BU_EXACTFIT)
893                constEdit.Bind(wx.EVT_BUTTON,OnConstEdit)
894                constSizer.Add(constEdit,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER,1)            # edit button
895                Indx[constEdit.GetId()] = [Id,name]
896                if item[-1] == 'f':
897                    helptext = "A new variable"
898                    if item[-3]:
899                        helptext += " named "+str(item[-3])
900                    helptext += " is created from a linear combination of the following variables:\n"
901                    for term in item[:-3]:
902                        var = str(term[1])
903                        if len(eqString[-1]) > maxlen:
904                            eqString.append(' ')
905                        m = term[0]
906                        if eqString[-1] != '':
907                            if m >= 0:
908                                eqString[-1] += ' + '
909                            else:
910                                eqString[-1] += ' - '
911                                m = abs(m)
912                        eqString[-1] += '%.3f*%s '%(m,var)
913                        varMean = G2obj.fmtVarDescr(var)
914                        helptext += "\n" + var + " ("+ varMean + ")"
915                    if '_Explain' in data:
916                        if data['_Explain'].get(item[-3]):
917                            helptext += '\n\n'
918                            helptext += data['_Explain'][item[-3]]
919                    # typeString = 'NEWVAR'
920                    # if item[-3]:
921                    #     eqString[-1] += ' = '+item[-3]
922                    # else:
923                    #     eqString[-1] += ' = New Variable'
924                    if item[-3]:
925                        typeString = item[-3] + ' = '
926                    else:
927                        typeString = 'New Variable = '
928                    #print 'refine',item[-2]
929                    refineflag = True
930                elif item[-1] == 'c':
931                    helptext = "The following variables constrained to equal a constant:"
932                    for term in item[:-3]:
933                        var = str(term[1])
934                        if len(eqString[-1]) > maxlen:
935                            eqString.append(' ')
936                        if eqString[-1] != '':
937                            if term[0] > 0:
938                                eqString[-1] += ' + '
939                            else:
940                                eqString[-1] += ' - '
941                        eqString[-1] += '%.3f*%s '%(abs(term[0]),var)
942                        varMean = G2obj.fmtVarDescr(var)
943                        helptext += "\n" + var + " ("+ varMean + ")"
944                    typeString = 'CONST'
945                    eqString[-1] += ' = '+str(item[-3])
946                elif item[-1] == 'e':
947                    helptext = "The following variables are set to be equivalent, noting multipliers:"
948                    for term in item[:-3]:
949                        var = str(term[1])
950                        if term[0] == 0: term[0] = 1.0
951                        if len(eqString[-1]) > maxlen:
952                            eqString.append(' ')
953                        if eqString[-1] == '':
954                            eqString[-1] += var+' '
955                            first = term[0]
956                        else:
957                            eqString[-1] += ' = %.3f*%s '%(first/term[0],var)
958                        varMean = G2obj.fmtVarDescr(var)
959                        helptext += "\n" + var + " ("+ varMean + ")"
960                    typeString = 'EQUIV'
961                else:
962                    print 'Unexpected constraint',item
963               
964            else:
965                print 'Removing old-style constraints'
966                data[name] = []
967                return constSizer
968            constDel = wx.Button(pageDisplay,-1,'Delete',style=wx.BU_EXACTFIT)
969            constDel.Bind(wx.EVT_BUTTON,OnConstDel)
970            Indx[constDel.GetId()] = [Id,name]
971            constSizer.Add(constDel,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER,1)             # delete button
972            if helptext:
973                ch = G2G.HelpButton(pageDisplay,helptext)
974                constSizer.Add(ch,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER,1)
975            else:
976                constSizer.Add((-1,-1))
977            if refineflag:
978                ch = G2G.G2CheckBox(pageDisplay,'',item,-2)
979                constSizer.Add(ch,0,wx.LEFT|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER,1)
980            else:
981                constSizer.Add((-1,-1))               
982            constSizer.Add(wx.StaticText(pageDisplay,-1,typeString),
983                           0,wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_CENTER|wx.RIGHT|wx.LEFT,3)
984            if len(eqString) > 1:
985                Eq = wx.BoxSizer(wx.VERTICAL)
986                for s in eqString:
987                    Eq.Add(wx.StaticText(pageDisplay,-1,s),0,wx.ALIGN_CENTER_VERTICAL)
988            else:
989                Eq = wx.StaticText(pageDisplay,-1,eqString[0])
990            constSizer.Add(Eq,1,wx.ALIGN_CENTER_VERTICAL)
991        return constSizer
992               
993    def OnConstDel(event):
994        'Delete a constraint'
995        Obj = event.GetEventObject()
996        Id,name = Indx[Obj.GetId()]
997        del(data[name][Id])
998        wx.CallAfter(OnPageChanged,None)
999       
1000    def OnConstEdit(event):
1001        '''Called to edit an individual contraint in response to a
1002        click on its Edit button
1003        '''
1004        Obj = event.GetEventObject()
1005        Id,name = Indx[Obj.GetId()]
1006        if data[name][Id][-1] == 'f':
1007            items = data[name][Id][:-3]
1008            constType = 'New Variable'
1009            if data[name][Id][-3]:
1010                varname = data[name][Id][-3]
1011            else:
1012                varname = ""
1013            lbl = 'Enter value for each term in constraint; sum = new variable'
1014            dlg = ConstraintDialog(G2frame,constType,lbl,items,
1015                                   varname=varname,varyflag=data[name][Id][-2])
1016        elif data[name][Id][-1] == 'c':
1017            items = data[name][Id][:-3]+[
1018                [data[name][Id][-3],'fixed value =']]
1019            constType = 'Constraint'
1020            lbl = 'Edit value for each term in constant constraint sum'
1021            dlg = ConstraintDialog(G2frame,constType,lbl,items)
1022        elif data[name][Id][-1] == 'e':
1023            items = data[name][Id][:-3]
1024            constType = 'Equivalence'
1025            lbl = 'The following terms are set to be equal:'
1026            dlg = ConstraintDialog(G2frame,constType,lbl,items,'/')
1027        else:
1028            return
1029        try:
1030            prev = data[name][Id][:]
1031            if dlg.ShowModal() == wx.ID_OK:
1032                result = dlg.GetData()
1033                for i in range(len(data[name][Id][:-3])):
1034                    if type(data[name][Id][i]) is tuple: # fix non-mutable construct
1035                        data[name][Id][i] = list(data[name][Id][i])
1036                    data[name][Id][i][0] = result[i][0]
1037                if data[name][Id][-1] == 'c':
1038                    data[name][Id][-3] = str(result[-1][0])
1039                elif data[name][Id][-1] == 'f':
1040                    # process the variable name to put in global form (::var)
1041                    varname = str(dlg.newvar[0]).strip().replace(' ','_')
1042                    if varname.startswith('::'):
1043                        varname = varname[2:]
1044                    varname = varname.replace(':',';')
1045                    if varname:
1046                        data[name][Id][-3] = varname
1047                    else:
1048                        data[name][Id][-3] = ''
1049                    data[name][Id][-2] = dlg.newvar[1]
1050                if not CheckChangedConstraint():
1051                    data[name][Id] = prev
1052        except:
1053            import traceback
1054            print traceback.format_exc()
1055        finally:
1056            dlg.Destroy()
1057        wx.CallAfter(OnPageChanged,None)
1058   
1059    def UpdateConstraintPanel(panel,typ):
1060        '''Update the contents of the selected Constraint
1061        notebook tab. Called in :func:`OnPageChanged`
1062        '''
1063        if panel.GetSizer(): panel.GetSizer().Clear(True)
1064        Siz = wx.BoxSizer(wx.VERTICAL)
1065        Siz.Add((5,5),0)
1066        Siz.Add(MakeConstraintsSizer(typ,panel),1,wx.EXPAND)
1067        panel.SetSizer(Siz,True)
1068        Size = Siz.GetMinSize()
1069        Size[0] += 40
1070        Size[1] = max(Size[1],450) + 20
1071        panel.SetSize(Size)
1072        panel.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1)
1073        panel.Show()
1074
1075    def OnPageChanged(event):
1076        '''Called when a tab is pressed or when a "select tab" menu button is
1077        used (see RaisePage), or to refresh the current tab contents (event=None)
1078        '''
1079        if event:       #page change event!
1080            page = event.GetSelection()
1081        else: # called directly, get current page
1082            page = G2frame.constr.GetSelection()
1083        #G2frame.constr.SetSize(G2frame.dataWindow.GetClientSize())    #TODO -almost right
1084        G2frame.constr.ChangeSelection(page)
1085        text = G2frame.constr.GetPageText(page)
1086        G2frame.dataWindow.ConstraintEdit.Enable(G2gd.wxID_EQUIVALANCEATOMS,False)
1087#        G2frame.dataWindow.ConstraintEdit.Enable(G2gd.wxID_ADDRIDING,False)
1088        if text == 'Histogram/Phase':
1089            G2frame.Page = [page,'hap']
1090            UpdateConstraintPanel(HAPConstr,'HAP')
1091        elif text == 'Histogram':
1092            G2frame.Page = [page,'hst']
1093            UpdateConstraintPanel(HistConstr,'Hist')
1094        elif text == 'Phase':
1095            G2frame.Page = [page,'phs']
1096            G2frame.dataWindow.ConstraintEdit.Enable(G2gd.wxID_EQUIVALANCEATOMS,True)
1097#            G2frame.dataWindow.ConstraintEdit.Enable(G2gd.wxID_ADDRIDING,True)
1098            if 'DELETED' in str(PhaseConstr):   #seems to be no other way to do this (wx bug)
1099                if GSASIIpath.GetConfigValue('debug'):
1100                    print 'wx error: PhaseConstr not cleanly deleted after Refine'
1101                return
1102            UpdateConstraintPanel(PhaseConstr,'Phase')
1103        elif text == 'Global':
1104            G2frame.Page = [page,'glb']
1105            UpdateConstraintPanel(GlobalConstr,'Global')
1106
1107    def RaisePage(event):
1108        'Respond to a "select tab" menu button'
1109        try:
1110            i = (G2gd.wxID_CONSPHASE,
1111                 G2gd.wxID_CONSHAP,
1112                 G2gd.wxID_CONSHIST,
1113                 G2gd.wxID_CONSGLOBAL).index(event.GetId())
1114            G2frame.constr.SetSelection(i)
1115            wx.CallAfter(OnPageChanged,None)
1116        except ValueError:
1117            print('Unexpected event in RaisePage')
1118
1119    def SetStatusLine(text):
1120        G2frame.GetStatusBar().SetStatusText(text)                                     
1121       
1122    G2gd.SetDataMenuBar(G2frame,G2frame.dataWindow.ConstraintMenu)
1123    #G2frame.SetLabel(G2frame.GetLabel().split('||')[0]+' || '+'Constraints')
1124    G2frame.SetTitle('Constraints')
1125    SetStatusLine('')
1126   
1127    G2frame.Bind(wx.EVT_MENU, OnAddConstraint, id=G2gd.wxID_CONSTRAINTADD)
1128    G2frame.Bind(wx.EVT_MENU, OnAddFunction, id=G2gd.wxID_FUNCTADD)
1129    G2frame.Bind(wx.EVT_MENU, OnAddEquivalence, id=G2gd.wxID_EQUIVADD)
1130    G2frame.Bind(wx.EVT_MENU, OnAddHold, id=G2gd.wxID_HOLDADD)
1131    G2frame.Bind(wx.EVT_MENU, OnAddAtomEquiv, id=G2gd.wxID_EQUIVALANCEATOMS)
1132#    G2frame.Bind(wx.EVT_MENU, OnAddRiding, id=G2gd.wxID_ADDRIDING)
1133    # tab commands
1134    for id in (G2gd.wxID_CONSPHASE,
1135               G2gd.wxID_CONSHAP,
1136               G2gd.wxID_CONSHIST,
1137               G2gd.wxID_CONSGLOBAL):
1138        G2frame.Bind(wx.EVT_MENU, RaisePage,id=id)
1139
1140    #G2frame.constr = G2G.GSNoteBook(parent=G2frame.dataWindow,size=G2frame.dataWindow.GetClientSize())
1141    G2frame.constr = G2G.GSNoteBook(parent=G2frame.dataWindow)
1142    G2frame.dataWindow.GetSizer().Add(G2frame.constr,1,wx.ALL|wx.EXPAND)
1143    # note that order of pages is hard-coded in RaisePage
1144    PhaseConstr = wx.ScrolledWindow(G2frame.constr)
1145    G2frame.constr.AddPage(PhaseConstr,'Phase')
1146    HAPConstr = wx.ScrolledWindow(G2frame.constr)
1147    G2frame.constr.AddPage(HAPConstr,'Histogram/Phase')
1148    HistConstr = wx.ScrolledWindow(G2frame.constr)
1149    G2frame.constr.AddPage(HistConstr,'Histogram')
1150    GlobalConstr = wx.ScrolledWindow(G2frame.constr)
1151    G2frame.constr.AddPage(GlobalConstr,'Global')
1152    wx.CallAfter(OnPageChanged,None)
1153    G2frame.constr.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, OnPageChanged)
1154    # validate all the constrants -- should not see any errors here normally
1155    allcons = []
1156    for key in data:
1157        if key.startswith('_'): continue
1158        allcons += data[key]
1159    if not len(allcons): return
1160    G2mv.InitVars()   
1161    constDictList,fixedList,ignored = G2stIO.ProcessConstraints(allcons)
1162    errmsg, warnmsg = G2mv.CheckConstraints('',constDictList,fixedList)
1163    if errmsg:
1164        G2frame.ErrorDialog('Constraint Error','Error in constraints:\n'+errmsg,
1165            parent=G2frame)
1166    elif warnmsg:
1167        print 'Unexpected contraint warning:\n',warnmsg
1168
1169################################################################################
1170#### Make nuclear-magnetic phase constraints - called by OnTransform in G2phsGUI
1171################################################################################       
1172       
1173def MagConstraints(G2frame,oldPhase,newPhase,Trans,Vec,atCodes):
1174    '''Add constraints for new magnetic phase created via transformation of old
1175    nuclear one
1176    NB: A = [G11,G22,G33,2*G12,2*G13,2*G23]
1177    '''
1178   
1179    def SetUniqAj(pId,iA,Aname,SGLaue):
1180        if SGLaue in ['4/m','4/mmm'] and iA in [0,1]:
1181            parm = '%d::%s'%(pId,'A0')
1182        elif SGLaue in ['m3','m3m'] and iA in [0,1,2]:
1183            parm = '%d::%s'%(pId,'A0')
1184        elif SGLaue in ['6/m','6/mmm','3m1', '31m', '3'] and iA in [0,1,3]:
1185            parm = '%d::%s'%(pId,'A0')
1186        elif SGLaue in ['3R', '3mR']:
1187            if ia in [0,1,2]:
1188                parm = '%d::%s'%(pId,'A0')
1189            else:
1190                parm = '%d::%s'%(pId,'A3')
1191        else:
1192            parm = '%d::%s'%(pId,Aname)
1193        return parm
1194       
1195    Histograms,Phases = G2frame.GetUsedHistogramsAndPhasesfromTree()
1196    UseList = newPhase['Histograms']
1197    detTrans = np.abs(nl.det(Trans))
1198    invTrans = nl.inv(Trans)
1199#    print 'invTrans',invTrans
1200    nAcof = G2lat.cell2A(newPhase['General']['Cell'][1:7])
1201   
1202    opId = oldPhase['pId']
1203    npId = newPhase['pId']
1204    cx,ct,cs,cia = newPhase['General']['AtomPtrs']
1205    nAtoms = newPhase['Atoms']
1206    oSGData = oldPhase['General']['SGData']
1207    nSGData = newPhase['General']['SGData']
1208    oAcof = G2lat.cell2A(oldPhase['General']['Cell'][1:7])
1209    nAcof = G2lat.cell2A(newPhase['General']['Cell'][1:7])
1210    item = G2gd.GetGPXtreeItemId(G2frame,G2frame.root,'Constraints') 
1211    constraints = G2frame.GPXtree.GetItemPyData(item)
1212#    GSASIIpath.IPyBreak()
1213    parmDict = {}
1214    varyList = []
1215    for ia,code in enumerate(atCodes):
1216        atom = nAtoms[ia]
1217        siteSym = G2spc.SytSym(atom[cx:cx+3],nSGData)[0]
1218        CSX = G2spc.GetCSxinel(siteSym)
1219        item = code.split('+')[0]
1220        iat,opr = item.split(':')
1221        Nop = abs(int(opr))%100-1
1222        if '-' in opr:
1223            Nop *= -1
1224        Opr = oldPhase['General']['SGData']['SGOps'][abs(Nop)][0]
1225        if Nop < 0:         #inversion
1226            Opr *= -1
1227        XOpr = np.inner(Opr,Trans.T)
1228        names = ['dAx','dAy','dAz']
1229        for ix,name in enumerate(names):
1230            IndpCon = [1.0,G2obj.G2VarObj('%d::%s:%s'%(opId,name,iat))]
1231            DepCons = []
1232            for iop,opval in enumerate(XOpr[ix]):
1233                if opval and CSX[0][ix]:    #-opval from defn of dAx, etc.
1234                    DepCons.append([-opval,G2obj.G2VarObj('%d::%s:%d'%(npId,names[iop],ia))])
1235            if len(DepCons) == 1:
1236                constraints['Phase'].append([IndpCon,DepCons[0],None,None,'e'])
1237            elif len(DepCons) > 1:
1238                for Dep in DepCons:
1239                    Dep[0] *= -1
1240                constraints['Phase'].append([IndpCon]+DepCons+[0.0,None,'c'])
1241        for name in ['Afrac','AUiso']:
1242            IndpCon = [1.0,G2obj.G2VarObj('%d::%s:%s'%(opId,name,iat))]
1243            DepCons = [1.0,G2obj.G2VarObj('%d::%s:%d'%(npId,name,ia))]
1244            constraints['Phase'].append([IndpCon,DepCons,None,None,'e'])
1245        #how do I do Uij's for most Trans?
1246    Anames = [['A0','A3','A4'],['A3','A1','A5'],['A4','A5','A2']]
1247    As = ['A0','A1','A2','A3','A4','A5']
1248    Aids = [[0,0,'A0',-1],[1,1,'A1',-1],[2,2,'A2',-1],[0,1,'A3',2],[0,2,'A4',1],[1,2,'A5',0]]
1249    Axes = ['a','b','c']
1250    Holds = []
1251    for iA,Aid in enumerate(Aids):
1252        parm = SetUniqAj(opId,iA,Aid[2],oSGData['SGLaue'])
1253        parmDict[parm] = oAcof[iA]
1254        varyList.append(parm)
1255        IndpCon = [1.0,G2obj.G2VarObj(parm)]
1256        DepCons = []
1257        for iat in range(3):
1258            if nSGData['SGLaue'] in ['-1','2/m']:       #set holds
1259                if (abs(nAcof[iA]) < 1.e-8) and (abs(Trans[Aid[0],Aid[1]]) < 1.e-8):
1260                    if Axes[iat] != oSGData['SGUniq'] and oSGData['SGLaue'] != nSGData['SGLaue']:
1261                        HoldObj = G2obj.G2VarObj('%d::%s'%(npId,Aid[2]))
1262                        if not HoldObj in Holds: 
1263                            constraints['Phase'].append([[0.0,HoldObj],None,None,'h'])
1264                            Holds.append(HoldObj)
1265                            continue
1266#            print iA,Aid,iat,invTrans[iat][Aid[0]],invTrans[Aid[1]][iat],Anames[Aid[0]][Aid[1]],parm
1267            if abs(invTrans[iat,Aid[1]]) > 1.e-8 and abs(nAcof[iA]) > 1.e-8:
1268                parm = SetUniqAj(npId,iA,Anames[Aid[0]][Aid[1]],nSGData['SGLaue'])
1269                parmDict[parm] = nAcof[As.index(Aid[2])]
1270                if not parm in varyList:
1271                    varyList.append(parm)
1272                DepCons.append([Trans[Aid[0],Aid[0]]*Trans[Aid[1],Aid[1]],G2obj.G2VarObj(parm)])
1273        if len(DepCons) == 1:
1274            constraints['Phase'].append([IndpCon,DepCons[0],None,None,'e'])
1275        elif len(DepCons) > 1:       
1276            for Dep in DepCons:
1277                Dep[0] *= -1
1278            constraints['Phase'].append([IndpCon]+DepCons+[0.0,None,'c'])
1279#    constDict,fixedList,ignored = G2stIO.ProcessConstraints(constraints['Phase'])
1280#    groups,parmlist = G2mv.GroupConstraints(constDict)
1281#    G2mv.GenerateConstraints(groups,parmlist,varyList,constDict,fixedList,parmDict)
1282#    print 'old',parmDict
1283#    G2mv.Dict2Map(parmDict,varyList)
1284#    print 'new',parmDict
1285    for hId,hist in enumerate(UseList):    #HAP - seems OK
1286        ohapkey = '%d:%d:'%(opId,hId)
1287        nhapkey = '%d:%d:'%(npId,hId)
1288        IndpCon = [1.0,G2obj.G2VarObj(ohapkey+'Scale')]
1289        DepCons = [detTrans,G2obj.G2VarObj(nhapkey+'Scale')]
1290        constraints['HAP'].append([IndpCon,DepCons,None,None,'e'])
1291        for name in ['Size;i','Mustrain;i']:
1292            IndpCon = [1.0,G2obj.G2VarObj(ohapkey+name)]
1293            DepCons = [1.0,G2obj.G2VarObj(nhapkey+name)]
1294            constraints['HAP'].append([IndpCon,DepCons,None,None,'e'])
1295       
1296################################################################################
1297#### Rigid bodies
1298################################################################################
1299
1300def UpdateRigidBodies(G2frame,data):
1301    '''Called when Rigid bodies tree item is selected.
1302    Displays the rigid bodies in the data window
1303    '''
1304    if not data.get('RBIds') or not data:
1305        data.update({'Vector':{'AtInfo':{}},'Residue':{'AtInfo':{}},
1306            'RBIds':{'Vector':[],'Residue':[]}})       #empty/bad dict - fill it
1307           
1308    global resList
1309    Indx = {}
1310    resList = []
1311    plotDefaults = {'oldxy':[0.,0.],'Quaternion':[0.,0.,0.,1.],'cameraPos':30.,'viewDir':[0,0,1],}
1312   
1313    def OnPageChanged(event):
1314        global resList
1315        resList = []
1316        if event:       #page change event!
1317            page = event.GetSelection()
1318        else:
1319            page = G2frame.rbBook.GetSelection()
1320        #G2frame.rbBook.SetSize(G2frame.dataWindow.GetClientSize())    #TODO -almost right
1321        G2frame.rbBook.ChangeSelection(page)
1322        text = G2frame.rbBook.GetPageText(page)
1323        if text == 'Vector rigid bodies':
1324            G2gd.SetDataMenuBar(G2frame,G2frame.dataWindow.VectorBodyMenu)
1325            G2frame.Bind(wx.EVT_MENU, AddVectorRB, id=G2gd.wxID_VECTORBODYADD)
1326            G2frame.Page = [page,'vrb']
1327            UpdateVectorRB()
1328        elif text == 'Residue rigid bodies':
1329            G2gd.SetDataMenuBar(G2frame,G2frame.dataWindow.RigidBodyMenu)
1330            G2frame.Bind(wx.EVT_MENU, AddResidueRB, id=G2gd.wxID_RIGIDBODYADD)
1331            G2frame.Bind(wx.EVT_MENU, OnImportRigidBody, id=G2gd.wxID_RIGIDBODYIMPORT)
1332            G2frame.Bind(wx.EVT_MENU, OnDefineTorsSeq, id=G2gd.wxID_RESIDUETORSSEQ) #enable only if residue RBs exist?
1333            G2frame.Page = [page,'rrb']
1334            UpdateResidueRB()
1335           
1336    def getMacroFile(macName):
1337        defDir = os.path.join(os.path.split(__file__)[0],'GSASIImacros')
1338        dlg = wx.FileDialog(G2frame,message='Choose '+macName+' rigid body macro file',
1339            defaultDir=defDir,defaultFile="",wildcard="GSAS-II macro file (*.mac)|*.mac",
1340            style=wx.OPEN | wx.CHANGE_DIR)
1341        try:
1342            if dlg.ShowModal() == wx.ID_OK:
1343                macfile = dlg.GetPath()
1344                macro = open(macfile,'Ur')
1345                head = macro.readline()
1346                if macName not in head:
1347                    print head
1348                    print '**** ERROR - wrong restraint macro file selected, try again ****'
1349                    macro = []
1350            else: # cancel was pressed
1351                macro = []
1352        finally:
1353            dlg.Destroy()
1354        return macro        #advanced past 1st line
1355       
1356    def getTextFile():
1357        dlg = wx.FileDialog(G2frame,'Choose rigid body text file', '.', '',
1358            "GSAS-II text file (*.txt)|*.txt|XYZ file (*.xyz)|*.xyz|"
1359            "Sybyl mol2 file (*.mol2)|*.mol2|PDB file (*.pdb;*.ent)|*.pdb;*.ent",
1360            wx.OPEN | wx.CHANGE_DIR)
1361        try:
1362            if dlg.ShowModal() == wx.ID_OK:
1363                txtfile = dlg.GetPath()
1364                ext = os.path.splitext(txtfile)[1]
1365                text = open(txtfile,'Ur')
1366            else: # cancel was pressed
1367                ext = ''
1368                text = []
1369        finally:
1370            dlg.Destroy()
1371        if 'ent' in ext:
1372            ext = '.pdb'
1373        return text,ext.lower()
1374       
1375    def OnImportRigidBody(event):
1376        page = G2frame.rbBook.GetSelection()
1377        if 'Vector' in G2frame.rbBook.GetPageText(page):
1378            pass
1379        elif 'Residue' in G2frame.rbBook.GetPageText(page):
1380            ImportResidueRB()
1381           
1382    def AddVectorRB(event):
1383        AtInfo = data['Vector']['AtInfo']
1384        dlg = MultiIntegerDialog(G2frame,'New Rigid Body',['No. atoms','No. translations'],[1,1])
1385        if dlg.ShowModal() == wx.ID_OK:
1386            nAtoms,nTrans = dlg.GetValues()
1387            rbId = ran.randint(0,sys.maxint)
1388            vecMag = [1.0 for i in range(nTrans)]
1389            vecRef = [False for i in range(nTrans)]
1390            vecVal = [np.zeros((nAtoms,3)) for j in range(nTrans)]
1391            rbTypes = ['C' for i in range(nAtoms)]
1392            Info = G2elem.GetAtomInfo('C')
1393            AtInfo['C'] = [Info['Drad'],Info['Color']]
1394            data['Vector'][rbId] = {'RBname':'UNKRB','VectMag':vecMag,'rbXYZ':np.zeros((nAtoms,3)),
1395                'rbRef':[0,1,2,False],'VectRef':vecRef,'rbTypes':rbTypes,'rbVect':vecVal,'useCount':0}
1396            data['RBIds']['Vector'].append(rbId)
1397        dlg.Destroy()
1398        UpdateVectorRB()
1399       
1400    def AddResidueRB(event):
1401        AtInfo = data['Residue']['AtInfo']
1402        macro = getMacroFile('rigid body')
1403        if not macro:
1404            return
1405        macStr = macro.readline()
1406        while macStr:
1407            items = macStr.split()
1408            if 'I' == items[0]:
1409                rbId = ran.randint(0,sys.maxint)
1410                rbName = items[1]
1411                rbTypes = []
1412                rbXYZ = []
1413                rbSeq = []
1414                atNames = []
1415                nAtms,nSeq,nOrig,mRef,nRef = [int(items[i]) for i in [2,3,4,5,6]]
1416                for iAtm in range(nAtms):
1417                    macStr = macro.readline().split()
1418                    atName = macStr[0]
1419                    atType = macStr[1]
1420                    atNames.append(atName)
1421                    rbXYZ.append([float(macStr[i]) for i in [2,3,4]])
1422                    rbTypes.append(atType)
1423                    if atType not in AtInfo:
1424                        Info = G2elem.GetAtomInfo(atType)
1425                        AtInfo[atType] = [Info['Drad'],Info['Color']]
1426                rbXYZ = np.array(rbXYZ)-np.array(rbXYZ[nOrig-1])
1427                for iSeq in range(nSeq):
1428                    macStr = macro.readline().split()
1429                    mSeq = int(macStr[0])
1430                    for jSeq in range(mSeq):
1431                        macStr = macro.readline().split()
1432                        iBeg = int(macStr[0])-1
1433                        iFin = int(macStr[1])-1
1434                        angle = 0.0
1435                        nMove = int(macStr[2])
1436                        iMove = [int(macStr[i])-1 for i in range(3,nMove+3)]
1437                        rbSeq.append([iBeg,iFin,angle,iMove])
1438                data['Residue'][rbId] = {'RBname':rbName,'rbXYZ':rbXYZ,'rbTypes':rbTypes,
1439                    'atNames':atNames,'rbRef':[nOrig-1,mRef-1,nRef-1,True],'rbSeq':rbSeq,
1440                    'SelSeq':[0,0],'useCount':0}
1441                data['RBIds']['Residue'].append(rbId)
1442                print 'Rigid body '+rbName+' added'
1443            macStr = macro.readline()
1444        macro.close()
1445        UpdateResidueRB()
1446       
1447    def ImportResidueRB():
1448        AtInfo = data['Residue']['AtInfo']
1449        text,ext = getTextFile()
1450        if not text:
1451            return
1452        rbId = ran.randint(0,sys.maxint)
1453        rbTypes = []
1454        rbXYZ = []
1455        atNames = []
1456        txtStr = text.readline()
1457        if 'xyz' in ext:
1458            txtStr = text.readline()
1459            txtStr = text.readline()
1460        elif 'mol2' in ext:
1461            while 'ATOM' not in txtStr:
1462                txtStr = text.readline()
1463            txtStr = text.readline()
1464        elif 'pdb' in ext:
1465            while 'ATOM' not in txtStr[:6] and 'HETATM' not in txtStr[:6]:
1466                txtStr = text.readline()
1467                #print txtStr
1468        items = txtStr.split()
1469        while len(items):
1470            if 'txt' in ext:
1471                atName = items[0]
1472                atType = items[1]
1473                rbXYZ.append([float(items[i]) for i in [2,3,4]])
1474            elif 'xyz' in ext:
1475                atType = items[0]
1476                rbXYZ.append([float(items[i]) for i in [1,2,3]])
1477                atName = atType+str(len(rbXYZ))
1478            elif 'mol2' in ext:
1479                atType = items[1]
1480                atName = items[1]+items[0]
1481                rbXYZ.append([float(items[i]) for i in [2,3,4]])
1482            elif 'pdb' in ext:
1483                atType = items[-1]
1484                atName = items[2]
1485                xyz = txtStr[30:55].split()                   
1486                rbXYZ.append([float(x) for x in xyz])
1487            atNames.append(atName)
1488            rbTypes.append(atType)
1489            if atType not in AtInfo:
1490                Info = G2elem.GetAtomInfo(atType)
1491                AtInfo[atType] = [Info['Drad'],Info['Color']]
1492            txtStr = text.readline()
1493            if 'mol2' in ext and 'BOND' in txtStr:
1494                break
1495            if 'pdb' in ext and ('ATOM' not in txtStr[:6] and 'HETATM' not in txtStr[:6]):
1496                break
1497            items = txtStr.split()
1498        rbXYZ = np.array(rbXYZ)-np.array(rbXYZ[0])
1499        data['Residue'][rbId] = {'RBname':'UNKRB','rbXYZ':rbXYZ,'rbTypes':rbTypes,
1500            'atNames':atNames,'rbRef':[0,1,2,False],'rbSeq':[],'SelSeq':[0,0],'useCount':0}
1501        data['RBIds']['Residue'].append(rbId)
1502        print 'Rigid body UNKRB added'
1503        text.close()
1504        UpdateResidueRB()
1505       
1506    def FindNeighbors(Orig,XYZ,atTypes,atNames,AtInfo):
1507        Radii = []
1508        for Atype in atTypes:
1509            Radii.append(AtInfo[Atype][0])
1510        Radii = np.array(Radii)
1511        Neigh = []
1512        Dx = XYZ-XYZ[Orig]
1513        dist = np.sqrt(np.sum(Dx**2,axis=1))
1514        sumR = Radii[Orig]+Radii
1515        IndB = ma.nonzero(ma.masked_greater(dist-0.85*sumR,0.))
1516        for j in IndB[0]:
1517            if j != Orig:
1518                Neigh.append(atNames[j])
1519        return Neigh
1520       
1521    def FindAllNeighbors(XYZ,atTypes,atNames,AtInfo):
1522        NeighDict = {}
1523        for iat,xyz in enumerate(atNames):
1524            NeighDict[atNames[iat]] = FindNeighbors(iat,XYZ,atTypes,atNames,AtInfo)
1525        return NeighDict
1526       
1527    def FindRiding(Orig,Pivot,NeighDict):
1528        riding = [Orig,Pivot]
1529        iAdd = 1
1530        new = True
1531        while new:
1532            newAtms = NeighDict[riding[iAdd]]
1533            for At in newAtms:
1534                new = False
1535                if At not in riding:
1536                    riding.append(At)
1537                    new = True
1538            iAdd += 1
1539            if iAdd < len(riding):
1540                new = True
1541        return riding[2:]
1542                       
1543    def OnDefineTorsSeq(event):
1544        rbKeys = data['Residue'].keys()
1545        rbKeys.remove('AtInfo')
1546        rbNames = [data['Residue'][k]['RBname'] for k in rbKeys]
1547        rbIds = dict(zip(rbNames,rbKeys))
1548        rbNames.sort()
1549        rbId = 0
1550        if len(rbNames) == 0:
1551            print 'There are no rigid bodies defined'
1552            G2frame.ErrorDialog('No rigid bodies','There are no rigid bodies defined',
1553                                parent=G2frame)
1554            return
1555        elif len(rbNames) > 1:
1556            dlg = wx.SingleChoiceDialog(G2frame,'Select rigid body for torsion sequence','Torsion sequence',rbNames)
1557            if dlg.ShowModal() == wx.ID_OK:
1558                sel = dlg.GetSelection()
1559                rbId = rbIds[rbNames[sel]]
1560                rbData = data['Residue'][rbId]
1561            dlg.Destroy()
1562        else:
1563            rbId = rbIds[rbNames[0]]
1564            rbData = data['Residue'][rbId]
1565        if not len(rbData):
1566            return
1567        atNames = rbData['atNames']
1568        AtInfo = data['Residue']['AtInfo']
1569        atTypes = rbData['rbTypes']
1570        XYZ = rbData['rbXYZ']
1571        neighDict = FindAllNeighbors(XYZ,atTypes,atNames,AtInfo)
1572        TargList = []           
1573        dlg = wx.SingleChoiceDialog(G2frame,'Select origin atom for torsion sequence','Origin atom',rbData['atNames'])
1574        if dlg.ShowModal() == wx.ID_OK:
1575            Orig = dlg.GetSelection()
1576            TargList = neighDict[atNames[Orig]]
1577        dlg.Destroy()
1578        if not len(TargList):
1579            return
1580        dlg = wx.SingleChoiceDialog(G2frame,'Select pivot atom for torsion sequence','Pivot atom',TargList)
1581        if dlg.ShowModal() == wx.ID_OK:
1582            Piv = atNames.index(TargList[dlg.GetSelection()])
1583            riding = FindRiding(atNames[Orig],atNames[Piv],neighDict)
1584            Riding = []
1585            for atm in riding:
1586                Riding.append(atNames.index(atm))
1587            rbData['rbSeq'].append([Orig,Piv,0.0,Riding])           
1588        dlg.Destroy()
1589        UpdateResidueRB()
1590
1591    def UpdateVectorRB(Scroll=0):
1592        AtInfo = data['Vector']['AtInfo']
1593        refChoice = {}
1594        if 'DELETED' in str(G2frame.GetStatusBar()):   #seems to be no other way to do this (wx bug)
1595            if GSASIIpath.GetConfigValue('debug'):
1596                print 'wx error: Rigid Body/Status not cleanly deleted after Refine'
1597            return
1598        SetStatusLine(' You may use e.g. "c60" or "s60" for a vector entry')
1599        def rbNameSizer(rbId,rbData):
1600
1601            def OnRBName(event):
1602                event.Skip()
1603                Obj = event.GetEventObject()
1604                rbData['RBname'] = Obj.GetValue()
1605               
1606            def OnDelRB(event):
1607                Obj = event.GetEventObject()
1608                rbId = Indx[Obj.GetId()]
1609                if rbId in data['Vector']:
1610                    del data['Vector'][rbId]
1611                    data['RBIds']['Vector'].remove(rbId)
1612                    rbData['useCount'] -= 1
1613                wx.CallAfter(UpdateVectorRB)
1614               
1615            def OnPlotRB(event):
1616                Obj = event.GetEventObject()
1617                Obj.SetValue(False)
1618                G2plt.PlotRigidBody(G2frame,'Vector',AtInfo,rbData,plotDefaults)
1619           
1620            nameSizer = wx.BoxSizer(wx.HORIZONTAL)
1621            nameSizer.Add(wx.StaticText(VectorRBDisplay,-1,'Rigid body name: '),
1622                0,wx.ALIGN_CENTER_VERTICAL)
1623            RBname = wx.TextCtrl(VectorRBDisplay,-1,rbData['RBname'])
1624            Indx[RBname.GetId()] = rbId
1625            RBname.Bind(wx.EVT_TEXT_ENTER,OnRBName)
1626            RBname.Bind(wx.EVT_KILL_FOCUS,OnRBName)
1627            nameSizer.Add(RBname,0,wx.ALIGN_CENTER_VERTICAL)
1628            nameSizer.Add((5,0),)
1629            plotRB = wx.CheckBox(VectorRBDisplay,-1,'Plot?')
1630            Indx[plotRB.GetId()] = rbId
1631            plotRB.Bind(wx.EVT_CHECKBOX,OnPlotRB)
1632            nameSizer.Add(plotRB,0,wx.ALIGN_CENTER_VERTICAL)
1633            nameSizer.Add((5,0),)
1634            if not rbData['useCount']:
1635                delRB = wx.CheckBox(VectorRBDisplay,-1,'Delete?')
1636                Indx[delRB.GetId()] = rbId
1637                delRB.Bind(wx.EVT_CHECKBOX,OnDelRB)
1638                nameSizer.Add(delRB,0,wx.ALIGN_CENTER_VERTICAL)
1639            return nameSizer
1640           
1641        def rbRefAtmSizer(rbId,rbData):
1642           
1643            def OnRefSel(event):
1644                Obj = event.GetEventObject()
1645                iref = Indx[Obj.GetId()]
1646                sel = Obj.GetValue()
1647                rbData['rbRef'][iref] = atNames.index(sel)
1648                FillRefChoice(rbId,rbData)
1649           
1650            refAtmSizer = wx.BoxSizer(wx.HORIZONTAL)
1651            atNames = [name+str(i) for i,name in enumerate(rbData['rbTypes'])]
1652            rbRef = rbData.get('rbRef',[0,1,2,False])
1653            rbData['rbRef'] = rbRef
1654            if rbData['useCount']:
1655                refAtmSizer.Add(wx.StaticText(VectorRBDisplay,-1,
1656                    'Orientation reference atoms A-B-C: %s, %s, %s'%(atNames[rbRef[0]], \
1657                     atNames[rbRef[1]],atNames[rbRef[2]])),0)
1658            else:
1659                refAtmSizer.Add(wx.StaticText(VectorRBDisplay,-1,
1660                    'Orientation reference atoms A-B-C: '),0,wx.ALIGN_CENTER_VERTICAL)
1661                for i in range(3):
1662                    choices = [atNames[j] for j in refChoice[rbId][i]]
1663                    refSel = wx.ComboBox(VectorRBDisplay,-1,value='',
1664                        choices=choices,style=wx.CB_READONLY|wx.CB_DROPDOWN)
1665                    refSel.SetValue(atNames[rbRef[i]])
1666                    refSel.Bind(wx.EVT_COMBOBOX, OnRefSel)
1667                    Indx[refSel.GetId()] = i
1668                    refAtmSizer.Add(refSel,0,wx.ALIGN_CENTER_VERTICAL)
1669            return refAtmSizer
1670                       
1671        def rbVectMag(rbId,imag,rbData):
1672           
1673            def OnRBVectorMag(event):
1674                event.Skip()
1675                Obj = event.GetEventObject()
1676                rbId,imag = Indx[Obj.GetId()]
1677                try:
1678                    val = float(Obj.GetValue())
1679                    if val <= 0.:
1680                        raise ValueError
1681                    rbData['VectMag'][imag] = val
1682                except ValueError:
1683                    pass
1684                Obj.SetValue('%8.4f'%(val))
1685                wx.CallAfter(UpdateVectorRB,VectorRB.GetScrollPos(wx.VERTICAL))
1686                G2plt.PlotRigidBody(G2frame,'Vector',AtInfo,data['Vector'][rbId],plotDefaults)
1687               
1688            def OnRBVectorRef(event):
1689                Obj = event.GetEventObject()
1690                rbId,imag = Indx[Obj.GetId()]
1691                rbData['VectRef'][imag] = Obj.GetValue()
1692                       
1693            magSizer = wx.wx.BoxSizer(wx.HORIZONTAL)
1694            magSizer.Add(wx.StaticText(VectorRBDisplay,-1,'Translation magnitude: '),
1695                0,wx.ALIGN_CENTER_VERTICAL)
1696            magValue = wx.TextCtrl(VectorRBDisplay,-1,'%8.4f'%(rbData['VectMag'][imag]))
1697            Indx[magValue.GetId()] = [rbId,imag]
1698            magValue.Bind(wx.EVT_TEXT_ENTER,OnRBVectorMag)
1699            magValue.Bind(wx.EVT_KILL_FOCUS,OnRBVectorMag)
1700            magSizer.Add(magValue,0,wx.ALIGN_CENTER_VERTICAL)
1701            magSizer.Add((5,0),)
1702            magref = wx.CheckBox(VectorRBDisplay,-1,label=' Refine?') 
1703            magref.SetValue(rbData['VectRef'][imag])
1704            magref.Bind(wx.EVT_CHECKBOX,OnRBVectorRef)
1705            Indx[magref.GetId()] = [rbId,imag]
1706            magSizer.Add(magref,0,wx.ALIGN_CENTER_VERTICAL)
1707            return magSizer
1708           
1709        def rbVectors(rbId,imag,mag,XYZ,rbData):
1710
1711            def TypeSelect(event):
1712                AtInfo = data['Vector']['AtInfo']
1713                r,c = event.GetRow(),event.GetCol()
1714                if vecGrid.GetColLabelValue(c) == 'Type':
1715                    PE = G2elemGUI.PickElement(G2frame,oneOnly=True)
1716                    if PE.ShowModal() == wx.ID_OK:
1717                        if PE.Elem != 'None':
1718                            El = PE.Elem.strip().lower().capitalize()
1719                            if El not in AtInfo:
1720                                Info = G2elem.GetAtomInfo(El)
1721                                AtInfo[El] = [Info['Drad'],Info['Color']]
1722                            rbData['rbTypes'][r] = El
1723                            vecGrid.SetCellValue(r,c,El)
1724                    PE.Destroy()
1725                wx.CallAfter(UpdateVectorRB,VectorRB.GetScrollPos(wx.VERTICAL))
1726
1727            def ChangeCell(event):
1728                r,c =  event.GetRow(),event.GetCol()
1729                if r >= 0 and (0 <= c < 3):
1730                    try:
1731                        val = float(vecGrid.GetCellValue(r,c))
1732                        rbData['rbVect'][imag][r][c] = val
1733                    except ValueError:
1734                        pass
1735                G2plt.PlotRigidBody(G2frame,'Vector',AtInfo,data['Vector'][rbId],plotDefaults)
1736                wx.CallAfter(UpdateVectorRB,VectorRB.GetScrollPos(wx.VERTICAL))
1737
1738            vecSizer = wx.BoxSizer()
1739            Types = 3*[wg.GRID_VALUE_FLOAT+':10,5',]+[wg.GRID_VALUE_STRING,]+3*[wg.GRID_VALUE_FLOAT+':10,5',]
1740            colLabels = ['Vector x','Vector y','Vector z','Type','Cart x','Cart y','Cart z']
1741            table = []
1742            rowLabels = []
1743            for ivec,xyz in enumerate(rbData['rbVect'][imag]):
1744                table.append(list(xyz)+[rbData['rbTypes'][ivec],]+list(XYZ[ivec]))
1745                rowLabels.append(str(ivec))
1746            vecTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types)
1747            vecGrid = G2G.GSGrid(VectorRBDisplay)
1748            vecGrid.SetTable(vecTable, True)
1749            vecGrid.Bind(wg.EVT_GRID_CELL_CHANGE, ChangeCell)
1750            if not imag:
1751                vecGrid.Bind(wg.EVT_GRID_CELL_LEFT_DCLICK, TypeSelect)
1752            attr = wx.grid.GridCellAttr()
1753            attr.SetEditor(G2G.GridFractionEditor(vecGrid))
1754            for c in range(3):
1755                vecGrid.SetColAttr(c, attr)
1756            for row in range(vecTable.GetNumberRows()):
1757                if imag:
1758                    vecGrid.SetCellStyle(row,3,VERY_LIGHT_GREY,True)                   
1759                for col in [4,5,6]:
1760                    vecGrid.SetCellStyle(row,col,VERY_LIGHT_GREY,True)
1761            vecGrid.SetMargins(0,0)
1762            vecGrid.AutoSizeColumns(False)
1763            vecSizer.Add(vecGrid)
1764            return vecSizer
1765       
1766        def FillRefChoice(rbId,rbData):
1767            choiceIds = [i for i in range(len(rbData['rbTypes']))]
1768           
1769            rbRef = rbData.get('rbRef',[-1,-1,-1,False])
1770            for i in range(3):
1771                choiceIds.remove(rbRef[i])
1772            refChoice[rbId] = [choiceIds[:],choiceIds[:],choiceIds[:]]
1773            for i in range(3):
1774                refChoice[rbId][i].append(rbRef[i])
1775                refChoice[rbId][i].sort()     
1776           
1777        if VectorRB.GetSizer(): VectorRB.GetSizer().Clear(True)
1778        VectorRBDisplay = wx.Panel(VectorRB)
1779        VectorRBSizer = wx.BoxSizer(wx.VERTICAL)
1780        for rbId in data['RBIds']['Vector']:
1781            if rbId != 'AtInfo':
1782                rbData = data['Vector'][rbId]
1783                FillRefChoice(rbId,rbData)
1784                VectorRBSizer.Add(rbNameSizer(rbId,rbData),0)
1785                VectorRBSizer.Add(rbRefAtmSizer(rbId,rbData),0)
1786                XYZ = np.array([[0.,0.,0.] for Ty in rbData['rbTypes']])
1787                for imag,mag in enumerate(rbData['VectMag']):
1788                    XYZ += mag*rbData['rbVect'][imag]
1789                    VectorRBSizer.Add(rbVectMag(rbId,imag,rbData),0)
1790                    VectorRBSizer.Add(rbVectors(rbId,imag,mag,XYZ,rbData),0)
1791                VectorRBSizer.Add((5,5),0)
1792                data['Vector'][rbId]['rbXYZ'] = XYZ       
1793        VectorRBSizer.Layout()   
1794        VectorRBDisplay.SetSizer(VectorRBSizer,True)
1795        Size = VectorRBSizer.GetMinSize()
1796        Size[0] += 40
1797        Size[1] = max(Size[1],450) + 20
1798        VectorRBDisplay.SetSize(Size)
1799        VectorRB.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1)
1800        VectorRB.Scroll(0,Scroll)
1801       
1802    def UpdateResidueRB():
1803        AtInfo = data['Residue']['AtInfo']
1804        refChoice = {}
1805        RefObjs = []
1806
1807        def rbNameSizer(rbId,rbData):
1808
1809            def OnRBName(event):
1810                Obj = event.GetEventObject()
1811                rbData['RBname'] = Obj.GetValue()
1812               
1813            def OnDelRB(event):
1814                Obj = event.GetEventObject()
1815                rbId = Indx[Obj.GetId()]
1816                if rbId in data['Residue']: 
1817                    del data['Residue'][rbId]
1818                    data['RBIds']['Residue'].remove(rbId)
1819                wx.CallAfter(UpdateResidueRB)
1820               
1821            def OnPlotRB(event):
1822                Obj = event.GetEventObject()
1823                Obj.SetValue(False)
1824                G2plt.PlotRigidBody(G2frame,'Residue',AtInfo,rbData,plotDefaults)
1825           
1826            nameSizer = wx.BoxSizer(wx.HORIZONTAL)
1827            nameSizer.Add(wx.StaticText(ResidueRBDisplay,-1,'Residue name: '),
1828                0,wx.ALIGN_CENTER_VERTICAL)
1829            RBname = wx.TextCtrl(ResidueRBDisplay,-1,rbData['RBname'])
1830            Indx[RBname.GetId()] = rbId
1831            RBname.Bind(wx.EVT_TEXT_ENTER,OnRBName)
1832            RBname.Bind(wx.EVT_KILL_FOCUS,OnRBName)
1833            nameSizer.Add(RBname,0,wx.ALIGN_CENTER_VERTICAL)
1834            nameSizer.Add((5,0),)
1835            plotRB = wx.CheckBox(ResidueRBDisplay,-1,'Plot?')
1836            Indx[plotRB.GetId()] = rbId
1837            plotRB.Bind(wx.EVT_CHECKBOX,OnPlotRB)
1838            nameSizer.Add(plotRB,0,wx.ALIGN_CENTER_VERTICAL)
1839            nameSizer.Add((5,0),)
1840            if not rbData['useCount']:
1841                delRB = wx.CheckBox(ResidueRBDisplay,-1,'Delete?')
1842                Indx[delRB.GetId()] = rbId
1843                delRB.Bind(wx.EVT_CHECKBOX,OnDelRB)
1844                nameSizer.Add(delRB,0,wx.ALIGN_CENTER_VERTICAL)
1845            return nameSizer
1846           
1847        def rbResidues(rbId,rbData):
1848           
1849            def TypeSelect(event):
1850                AtInfo = data['Residue']['AtInfo']
1851                r,c = event.GetRow(),event.GetCol()
1852                if vecGrid.GetColLabelValue(c) == 'Type':
1853                    PE = G2elemGUI.PickElement(G2frame,oneOnly=True)
1854                    if PE.ShowModal() == wx.ID_OK:
1855                        if PE.Elem != 'None':
1856                            El = PE.Elem.strip().lower().capitalize()
1857                            if El not in AtInfo:
1858                                Info = G2elem.GetAtomInfo(El)
1859                                AtInfo[El] = [Info['Drad']['Color']]
1860                            rbData['rbTypes'][r] = El
1861                            vecGrid.SetCellValue(r,c,El)
1862                    PE.Destroy()
1863
1864            def ChangeCell(event):
1865                r,c =  event.GetRow(),event.GetCol()
1866                if r >= 0 and (0 <= c < 3):
1867                    try:
1868                        val = float(vecGrid.GetCellValue(r,c))
1869                        rbData['rbXYZ'][r][c] = val
1870                    except ValueError:
1871                        pass
1872                       
1873            def RowSelect(event):
1874                r,c =  event.GetRow(),event.GetCol()
1875                if c < 0:                   #only row clicks
1876                    for vecgrid in resList:
1877                        vecgrid.ClearSelection()
1878                    vecGrid.SelectRow(r,True)
1879
1880            def OnRefSel(event):
1881                Obj = event.GetEventObject()
1882                iref,res,jref = Indx[Obj.GetId()]
1883                sel = Obj.GetValue()
1884                ind = atNames.index(sel)
1885                rbData['rbRef'][iref] = ind
1886                FillRefChoice(rbId,rbData)
1887                for i,ref in enumerate(RefObjs[jref]):
1888                    ref.SetItems([atNames[j] for j in refChoice[rbId][i]])
1889                    ref.SetValue(atNames[rbData['rbRef'][i]])
1890                if not iref:     #origin change
1891                    rbXYZ = rbData['rbXYZ']
1892                    rbXYZ -= rbXYZ[ind]
1893                    res.ClearSelection()
1894                    resTable = res.GetTable()
1895                    for r in range(res.GetNumberRows()):
1896                        row = resTable.GetRowValues(r)
1897                        row[2:4] = rbXYZ[r]
1898                        resTable.SetRowValues(r,row)
1899                    res.ForceRefresh()
1900                    G2plt.PlotRigidBody(G2frame,'Residue',AtInfo,rbData,plotDefaults)
1901               
1902            Types = 2*[wg.GRID_VALUE_STRING,]+3*[wg.GRID_VALUE_FLOAT+':10,5',]
1903            colLabels = ['Name','Type','Cart x','Cart y','Cart z']
1904            table = []
1905            rowLabels = []
1906            for ivec,xyz in enumerate(rbData['rbXYZ']):
1907                table.append([rbData['atNames'][ivec],]+[rbData['rbTypes'][ivec],]+list(xyz))
1908                rowLabels.append(str(ivec))
1909            vecTable = G2G.Table(table,rowLabels=rowLabels,colLabels=colLabels,types=Types)
1910            vecGrid = G2G.GSGrid(ResidueRBDisplay)
1911            Indx[vecGrid.GetId()] = rbId
1912            resList.append(vecGrid)
1913            vecGrid.SetTable(vecTable, True)
1914            vecGrid.Bind(wg.EVT_GRID_CELL_CHANGE, ChangeCell)
1915            vecGrid.Bind(wg.EVT_GRID_CELL_LEFT_DCLICK, TypeSelect)
1916            vecGrid.Bind(wg.EVT_GRID_LABEL_LEFT_CLICK, RowSelect)
1917            attr = wx.grid.GridCellAttr()
1918            attr.SetEditor(G2G.GridFractionEditor(vecGrid))
1919            for c in range(3):
1920                vecGrid.SetColAttr(c, attr)
1921            for row in range(vecTable.GetNumberRows()):
1922                for col in range(5):
1923                    vecGrid.SetCellStyle(row,col,VERY_LIGHT_GREY,True)
1924            vecGrid.SetMargins(0,0)
1925            vecGrid.AutoSizeColumns(False)
1926            vecSizer = wx.BoxSizer()
1927            vecSizer.Add(vecGrid)
1928           
1929            refAtmSizer = wx.BoxSizer(wx.HORIZONTAL)
1930            atNames = rbData['atNames']
1931            rbRef = rbData['rbRef']
1932            if rbData['rbRef'][3] or rbData['useCount']:
1933                refAtmSizer.Add(wx.StaticText(ResidueRBDisplay,-1,
1934                    'Orientation reference atoms A-B-C: %s, %s, %s'%(atNames[rbRef[0]], \
1935                     atNames[rbRef[1]],atNames[rbRef[2]])),0)
1936            else:
1937                refAtmSizer.Add(wx.StaticText(ResidueRBDisplay,-1,
1938                    'Orientation reference atoms A-B-C: '),0,wx.ALIGN_CENTER_VERTICAL)
1939                refObj = [0,0,0]
1940                for i in range(3):
1941                    choices = [atNames[j] for j in refChoice[rbId][i]]
1942                    refSel = wx.ComboBox(ResidueRBDisplay,-1,value='',
1943                        choices=choices,style=wx.CB_READONLY|wx.CB_DROPDOWN)
1944                    refSel.SetValue(atNames[rbRef[i]])
1945                    refSel.Bind(wx.EVT_COMBOBOX, OnRefSel)
1946                    Indx[refSel.GetId()] = [i,vecGrid,len(RefObjs)]
1947                    refObj[i] = refSel
1948                    refAtmSizer.Add(refSel,0,wx.ALIGN_CENTER_VERTICAL)
1949                RefObjs.append(refObj)
1950           
1951            mainSizer = wx.BoxSizer(wx.VERTICAL)
1952            mainSizer.Add(refAtmSizer)
1953            mainSizer.Add(vecSizer)
1954            return mainSizer
1955           
1956        def SeqSizer(angSlide,rbId,iSeq,Seq,atNames):
1957           
1958            def ChangeAngle(event):
1959                event.Skip()
1960                Obj = event.GetEventObject()
1961                rbId,Seq = Indx[Obj.GetId()][:2]
1962                val = Seq[2]
1963                try:
1964                    val = float(Obj.GetValue())
1965                    Seq[2] = val
1966                except ValueError:
1967                    pass
1968                Obj.SetValue('%8.2f'%(val))
1969                G2plt.PlotRigidBody(G2frame,'Residue',AtInfo,data['Residue'][rbId],plotDefaults)
1970               
1971            def OnRadBtn(event):
1972                Obj = event.GetEventObject()
1973                Seq,iSeq,angId = Indx[Obj.GetId()]
1974                data['Residue'][rbId]['SelSeq'] = [iSeq,angId]
1975                angSlide.SetValue(int(100*Seq[2]))
1976               
1977            def OnDelBtn(event):
1978                Obj = event.GetEventObject()
1979                rbId,Seq = Indx[Obj.GetId()]
1980                data['Residue'][rbId]['rbSeq'].remove(Seq)       
1981                wx.CallAfter(UpdateResidueRB)
1982           
1983            seqSizer = wx.FlexGridSizer(0,5,2,2)
1984            seqSizer.AddGrowableCol(3,0)
1985            iBeg,iFin,angle,iMove = Seq
1986            ang = wx.TextCtrl(ResidueRBDisplay,-1,'%8.2f'%(angle),size=(50,20))
1987            if not iSeq:
1988                radBt = wx.RadioButton(ResidueRBDisplay,-1,'',style=wx.RB_GROUP)
1989                data['Residue'][rbId]['SelSeq'] = [iSeq,ang.GetId()]
1990            else:
1991                radBt = wx.RadioButton(ResidueRBDisplay,-1,'')
1992            radBt.Bind(wx.EVT_RADIOBUTTON,OnRadBtn)                   
1993            seqSizer.Add(radBt)
1994            delBt = wx.RadioButton(ResidueRBDisplay,-1,'')
1995            delBt.Bind(wx.EVT_RADIOBUTTON,OnDelBtn)
1996            seqSizer.Add(delBt)
1997            bond = wx.TextCtrl(ResidueRBDisplay,-1,'%s %s'%(atNames[iBeg],atNames[iFin]),size=(50,20))
1998            seqSizer.Add(bond,0,wx.ALIGN_CENTER_VERTICAL)
1999            Indx[radBt.GetId()] = [Seq,iSeq,ang.GetId()]
2000            Indx[delBt.GetId()] = [rbId,Seq]
2001            Indx[ang.GetId()] = [rbId,Seq,ang]
2002            ang.Bind(wx.EVT_TEXT_ENTER,ChangeAngle)
2003            ang.Bind(wx.EVT_KILL_FOCUS,ChangeAngle)
2004            seqSizer.Add(ang,0,wx.ALIGN_CENTER_VERTICAL)
2005            atms = ''
2006            for i in iMove:   
2007                atms += ' %s,'%(atNames[i])
2008            moves = wx.TextCtrl(ResidueRBDisplay,-1,atms[:-1],size=(200,20))
2009            seqSizer.Add(moves,1,wx.ALIGN_CENTER_VERTICAL|wx.EXPAND|wx.RIGHT)
2010            return seqSizer
2011           
2012        def SlideSizer():
2013           
2014            def OnSlider(event):
2015                Obj = event.GetEventObject()
2016                rbData = Indx[Obj.GetId()]
2017                iSeq,angId = rbData['SelSeq']
2018                val = float(Obj.GetValue())/100.
2019                rbData['rbSeq'][iSeq][2] = val
2020                Indx[angId][2].SetValue('%8.2f'%(val))
2021                G2plt.PlotRigidBody(G2frame,'Residue',AtInfo,rbData,plotDefaults)
2022           
2023            slideSizer = wx.BoxSizer(wx.HORIZONTAL)
2024            slideSizer.Add(wx.StaticText(ResidueRBDisplay,-1,'Selected torsion angle:'),0)
2025            iSeq,angId = rbData['SelSeq']
2026            angSlide = wx.Slider(ResidueRBDisplay,-1,
2027                int(100*rbData['rbSeq'][iSeq][2]),0,36000,size=(200,20),
2028                style=wx.SL_HORIZONTAL)
2029            angSlide.Bind(wx.EVT_SLIDER, OnSlider)
2030            Indx[angSlide.GetId()] = rbData
2031            slideSizer.Add(angSlide,0)           
2032            return slideSizer,angSlide
2033           
2034        def FillRefChoice(rbId,rbData):
2035            choiceIds = [i for i in range(len(rbData['atNames']))]
2036            for seq in rbData['rbSeq']:
2037                for i in seq[3]:
2038                    try:
2039                        choiceIds.remove(i)
2040                    except ValueError:
2041                        pass
2042            rbRef = rbData['rbRef']
2043            for i in range(3):
2044                try:
2045                    choiceIds.remove(rbRef[i])
2046                except ValueError:
2047                    pass
2048            refChoice[rbId] = [choiceIds[:],choiceIds[:],choiceIds[:]]
2049            for i in range(3):
2050                refChoice[rbId][i].append(rbRef[i])
2051                refChoice[rbId][i].sort()     
2052           
2053        if ResidueRB.GetSizer(): ResidueRB.GetSizer().Clear(True)
2054        ResidueRBDisplay = wx.Panel(ResidueRB)
2055        ResidueRBSizer = wx.BoxSizer(wx.VERTICAL)
2056        for rbId in data['RBIds']['Residue']:
2057            rbData = data['Residue'][rbId]
2058            FillRefChoice(rbId,rbData)
2059            ResidueRBSizer.Add(rbNameSizer(rbId,rbData),0)
2060            ResidueRBSizer.Add(rbResidues(rbId,rbData),0)
2061            ResidueRBSizer.Add((5,5),0)
2062            if rbData['rbSeq']:
2063                slideSizer,angSlide = SlideSizer()
2064            if len(rbData['rbSeq']):
2065                ResidueRBSizer.Add(wx.StaticText(ResidueRBDisplay,-1,
2066                    'Sel  Del  Bond             Angle      Riding atoms'),
2067                    0,wx.ALIGN_CENTER_VERTICAL)                       
2068            for iSeq,Seq in enumerate(rbData['rbSeq']):
2069                ResidueRBSizer.Add(SeqSizer(angSlide,rbId,iSeq,Seq,rbData['atNames']))
2070            if rbData['rbSeq']:
2071                ResidueRBSizer.Add(slideSizer,)
2072            ResidueRBSizer.Add(wx.StaticText(ResidueRBDisplay,-1,100*'-'))
2073
2074        ResidueRBSizer.Add((5,25),)
2075        ResidueRBSizer.Layout()   
2076        ResidueRBDisplay.SetSizer(ResidueRBSizer,True)
2077        Size = ResidueRBSizer.GetMinSize()
2078        Size[0] += 40
2079        Size[1] = max(Size[1],450) + 20
2080        ResidueRBDisplay.SetSize(Size)
2081        ResidueRB.SetScrollbars(10,10,Size[0]/10-4,Size[1]/10-1)
2082       
2083    def SetStatusLine(text):
2084        G2frame.GetStatusBar().SetStatusText(text)                                     
2085
2086    G2gd.SetDataMenuBar(G2frame,G2frame.dataWindow.RigidBodyMenu)
2087    #G2frame.SetLabel(G2frame.GetLabel().split('||')[0]+' || '+'Rigid bodies')
2088    G2frame.SetTitle('Rigid bodies')
2089    SetStatusLine('')
2090    #G2frame.rbBook = G2G.GSNoteBook(parent=G2frame.dataWindow,size=G2frame.dataWindow.GetClientSize())
2091    G2frame.rbBook = G2G.GSNoteBook(parent=G2frame.dataWindow)
2092    G2frame.dataWindow.GetSizer().Add(G2frame.rbBook,1,wx.ALL|wx.EXPAND)
2093
2094    VectorRB = wx.ScrolledWindow(G2frame.rbBook)
2095    G2frame.rbBook.AddPage(VectorRB,'Vector rigid bodies')
2096    ResidueRB = wx.ScrolledWindow(G2frame.rbBook)
2097    G2frame.rbBook.AddPage(ResidueRB,'Residue rigid bodies')
2098    UpdateVectorRB()
2099    G2frame.rbBook.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED, OnPageChanged)
2100    wx.CallAfter(OnPageChanged,None)
Note: See TracBrowser for help on using the repository browser.