PhasesResearchLab / ESPEI

Fitting thermodynamic models with pycalphad - https://doi.org/10.1557/mrc.2019.59
http://espei.org
MIT License
65 stars 32 forks source link

formatted_parameter broken by SymEngine #238

Open bocklund opened 2 years ago

bocklund commented 2 years ago

Switching the symbolic backend to SymEngine broke espei.utils.formatted_parameter. Here's a test to validate (run from the tests directory for the testing_data module to be importable).

# espei/tests/test_utils.py

from pycalphad import Database
from espei.utils import formatted_parameter, database_symbols_to_fit
from .testing_data import CU_MG_TDB
def test_cu_mg_parameters_can_be_formatted_to_strings():
    """Formating parameters should work for common variables parameters"""
    dbf = Database(CU_MG_TDB)
    for sym in database_symbols_to_fit(dbf):
        assert isinstance(formatted_parameter(dbf, sym), str), f"Formatted parameter for symbol {sym} (value = {dbf.symbols[sym]}) in database not a string"

Running this gives an error:

Traceback (most recent call last):
  File "/Users/bocklund1/src/calphad/espei/tests/dummy.py", line 11, in <module>
    test_cu_mg_parameters_can_be_formatted_to_strings()
  File "/Users/bocklund1/src/calphad/espei/tests/dummy.py", line 9, in test_cu_mg_parameters_can_be_formatted_to_strings
    assert isinstance(formatted_parameter(dbf, sym), str), f"Formatted parameter for symbol {sym} (value = {dbf.symbols[sym]}) in database not a string"
  File "/Users/bocklund1/src/calphad/espei/espei/utils.py", line 295, in formatted_parameter
    term = parameter_term(result['parameter'], symbol)
  File "/Users/bocklund1/src/calphad/espei/espei/utils.py", line 218, in parameter_term
    coeff, root = term_coeff.as_coeff_mul(symbol)
AttributeError: 'symengine.lib.symengine_wrapper.Symbol' object has no attribute 'as_coeff_mul'

I think the breakage might be because espei.utils.parameter_term isn't correctly picking up the first condition, since for the case of symbol being a symengine.lib.symengine_wrapper.Symbol, I think expression == symbol should evaluate to true, but evidently (via the traceback) it is evaluating to false.