Apr 10, 2021 5:59:30 PM (7 months ago)

implement constraints in scriptable, part 3; misc docs fixes

1 edited


  • trunk/GSASIIscriptable.py

    r4879 r4880  
    116116:meth:`G2Project.add_HoldConstr`                      Adds a hold constraint on one or more variables
    117117:meth:`G2Project.add_EquivConstr`                     Adds an equivalence constraint on two or more variables
    118 :meth:`G2Project.add_EqnConstr`                   Adds an equation-type constraint on two or more variables
     118:meth:`G2Project.add_EqnConstr`                       Adds an equation-type constraint on two or more variables
     119:meth:`G2Project.add_NewVarConstr`                    Adds an new variable as a constraint on two or more variables
    120120==================================================    ===============================================================================================================
    30753075        :param float total: A value that the constraint must equal
    3076         :param list varlist: A list of variables to make equivalent to the
    3077           first item in the list.
     3076        :param list varlist: A list of variables to use in the equation.
    30783077          Each value in the list may be one of the following three items:
    30793078          (A) a :class:`GSASIIobj.G2VarObj` object,
    31303129        self.add_constraint_raw(typ, constr)
     3131    def add_NewVarConstr(self,varlist,multlist=[],name=None,vary=False,reloadIdx=True):
     3132        '''Set a new-variable constraint from a list of variables to
     3133        create a new parameter from two or more predefined parameters.
     3135        Note that this will cause the project to be saved, if not
     3136        already done so. It will always save the .gpx file before
     3137        creating a constraint if reloadIdx is True.
     3139        :param list varlist: A list of variables to use in the expression.
     3140          Each value in the list may be one of the following three items:
     3141          (A) a :class:`GSASIIobj.G2VarObj` object,
     3142          (B) a variable name (str), or
     3143          (C) a list/tuple of arguments for :meth:`make_var_obj`.
     3144        :param list multlist: a list of multipliers for each variable in
     3145          varlist. If there are fewer values than supplied for varlist
     3146          then missing values will be set to 1. The default is [] which
     3147          means that all multipliers are 1.
     3148        :param str name: An optional string to be supplied as a name for this
     3149          new parameter.
     3150        :param bool vary: Determines if the new variable should be flagged to
     3151          be refined.         
     3152        :param bool reloadIdx: If True (default) the .gpx file will be
     3153          saved and indexed prior to use. This is essential if atoms, phases
     3154          or histograms have been added to the project.
     3156        Examples::
     3158            gpx.add_NewVarConstr(('0::AFrac:0','0::AFrac:1'),[0.5,0.5],'avg',True)
     3159            gpx.add_NewVarConstr(('0::AFrac:0','0::AFrac:1'),[1,-1],'diff',False,False)
     3161        The example above is a way to treat two variables that are closely correlated.
     3162        The first variable, labeled as avg, allows the two variables to refine in tandem
     3163        while the second variable (diff) tracks their difference. In the initial stages of
     3164        refinement only avg would be refined, but in the final stages, it might be possible
     3165        to refine diff. The second False value in the second example prevents the
     3166        .gpx file from being saved.
     3167        '''
     3168        if reloadIdx:
     3169            self.index_ids()
     3170        elif G2obj.TestIndexAll():
     3171            self.index_ids()
     3172        if len(varlist) < 2:
     3173            raise Exception('add_EquivEquation Error: varlist must have at least 2 variables')
     3174        constr = []
     3175        if name is not None:
     3176            name = str(name)
     3177        typ_prev = None
     3178        for i,var in enumerate(varlist):
     3179            m = 1.
     3180            try:
     3181                m = float(multlist[i])
     3182            except IndexError:
     3183                pass
     3184            # make var object
     3185            if isinstance(var, str):
     3186                var = self.make_var_obj(var,reloadIdx=False)
     3187            elif not isinstance(var, G2obj.G2VarObj):
     3188                var = self.make_var_obj(*var,reloadIdx=False)
     3189            # make constraint
     3190            constr.append([m,var])
     3191            typ = _constr_type(var)
     3192            if typ_prev is None:
     3193                typ_prev = typ
     3194                var_prev = var
     3195            if typ_prev != typ:
     3196                msg = 'Type ({}) for var {} is different from {} ({})'.format(typ,var,var_prev,typ_prev)
     3197                raise Exception('add_EquivConstr Error: '+msg)
     3198            typ_prev = typ
     3199            var_prev = var   
     3200        constr += [name, bool(vary), 'f']
     3201        self.add_constraint_raw(typ, constr)
    31333203    def add_constraint_raw(self, cons_scope, constr):
Note: See TracChangeset for help on using the changeset viewer.