Skip to content

Commit

Permalink
FIX: equilibrium/Model: Equilibrium property calculation with output …
Browse files Browse the repository at this point in the history
…kwarg works again. The parameters kwarg dict is properly passed through to CompiledModel, Model, and the property calculator.
  • Loading branch information
richardotis committed May 2, 2017
1 parent 72a2e3e commit ba45cc2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pycalphad/core/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def calculate(dbf, comps, phases, mode=None, output='GM', fake_points=False, bro
continue
if (not isinstance(mod, CompiledModel)) or (output != 'GM'):
if isinstance(mod, CompiledModel):
mod = Model(dbf, comps, phase_name)
mod = Model(dbf, comps, phase_name, parameters=parameters)
# Construct an ordered list of the variables
variables, sublattice_dof = generate_dof(phase_obj, mod.components)
# Build the "fast" representation of that model
Expand Down
13 changes: 10 additions & 3 deletions pycalphad/core/compiled_model.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ cdef public class CompiledModel(object)[type CompiledModelType, object CompiledM
for comp in sublattice:
self.variables.append(v.Y(phase_name, idx, comp))
parameters = parameters if parameters is not None else {}
renamed_params = []
for param, val in parameters.items():
if not isinstance(param, Symbol):
parameters[Symbol(param)] = val
renamed_params.append(param)
for param in renamed_params:
parameters.pop(param)
if isinstance(parameters, dict):
parameters = OrderedDict(sorted(parameters.items(), key=str))
param_symbols = tuple(parameters.keys())
self._debug = _debug
if _debug:
debugmodel = Model(dbe, comps, phase_name, parameters)
out = debugmodel.energy
if isinstance(parameters, dict):
parameters = OrderedDict(sorted(parameters.items(), key=str))
param_symbols = tuple(parameters.keys())
undefs = list(out.atoms(Symbol) - out.atoms(v.StateVariable) - set(param_symbols))
for undef in undefs:
out = out.xreplace({undef: float(0)})
Expand Down
17 changes: 7 additions & 10 deletions pycalphad/core/equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def equilibrium(dbf, comps, phases, conditions, output=None, model=None,
for key, val in str_conds.items() if key in indep_vars)
components = [x for x in sorted(comps) if not x.startswith('VA')]
# Construct models for each phase; prioritize user models
models = unpack_kwarg(model, default_arg=Model)
models = unpack_kwarg(model, default_arg=FallbackModel)
if verbose:
print('Components:', ' '.join(comps))
print('Phases:', end=' ')
Expand All @@ -250,7 +250,7 @@ def equilibrium(dbf, comps, phases, conditions, output=None, model=None,
for name in active_phases:
mod = models[name]
if isinstance(mod, type):
models[name] = mod = mod(dbf, comps, name)
models[name] = mod = mod(dbf, comps, name, parameters=parameters)
if isinstance(mod, CompiledModel):
phase_records[name.upper()] = PhaseRecord_from_compiledmodel(mod, param_values)
maximum_internal_dof = max(maximum_internal_dof, sum(mod.sublattice_dof))
Expand Down Expand Up @@ -361,16 +361,13 @@ def equilibrium(dbf, comps, phases, conditions, output=None, model=None,
per_phase = False
for phase_name, mod in models.items():
if isinstance(mod, CompiledModel) or isinstance(mod, FallbackModel):
models[phase_name] = Model(dbf, comps, phase_name)
delayed(properties.merge, pure=False)(delayed(_eqcalculate, pure=False)(dbf, comps, active_phases,
conditions, out,
data=properties,
per_phase=per_phase,
model=models, **calc_opts),
inplace=True, compat='equals')
delayed(properties.attrs.__setitem__, pure=False)('created', datetime.utcnow())
models[phase_name] = Model(dbf, comps, phase_name, parameters=parameters)
eqcal = delayed(_eqcalculate, pure=False)(dbf, comps, active_phases, conditions, out,
data=properties, per_phase=per_phase, model=models, **calc_opts)
properties = delayed(properties.merge, pure=False)(eqcal, inplace=True, compat='equals')
if scheduler is not None:
properties = dask.compute(properties, get=scheduler)[0]
properties.attrs['created'] = datetime.utcnow()
if len(kwargs) > 0:
warnings.warn('The following equilibrium keyword arguments were passed, but unused:\n{}'.format(kwargs))
return properties
4 changes: 2 additions & 2 deletions pycalphad/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def __init__(self, dbe, comps, phase_name, parameters=None):
# This makes xreplace work with the symbols dict
symbols = {Symbol(s): val for s, val in dbe.symbols.items()}

if parameters is not None:
symbols.update([(Symbol(s), val) for s, val in parameters.items()])
def wrap_symbol(obj):
if isinstance(obj, Symbol):
return obj
else:
return Symbol(obj)
if parameters is not None:
symbols.update([(wrap_symbol(s), val) for s, val in parameters.items()])
self._symbols = {wrap_symbol(key): value for key, value in symbols.items()}

self.models = OrderedDict()
Expand Down
3 changes: 2 additions & 1 deletion pycalphad/tests/test_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,6 @@ def test_eq_unary_issue78():
"Unary equilibrium calculations work with property calculations."
eq = equilibrium(ALFE_DBF, ['AL', 'VA'], 'FCC_A1', {v.T: 1200, v.P: 101325}, output='SM')
np.testing.assert_allclose(eq.SM, 68.143273)
eq = equilibrium(ALFE_DBF, ['AL', 'VA'], 'FCC_A1', {v.T: 1200, v.P: 101325}, parameters={'GHSERAL': 1000})
eq = equilibrium(ALFE_DBF, ['AL', 'VA'], 'FCC_A1', {v.T: 1200, v.P: 101325}, output='SM', parameters={'GHSERAL': 1000})
np.testing.assert_allclose(eq.GM, 1000)
np.testing.assert_allclose(eq.SM, 0)

0 comments on commit ba45cc2

Please sign in to comment.