Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom elements when initializing Solution/Interface object from parts #216

Open
sevyharris opened this issue Dec 2, 2024 · 2 comments
Labels
feature-request New feature request

Comments

@sevyharris
Copy link

Problem description

When I try initializing an Interface using the surface reactions and species I have in memory, it complains about not being able to find the weight of element 'X' the stand-in for a generic metal.

For context, the code for defining the Interface from a yaml works, but I'm working on a converter between pieces of RMG and Cantera and need to keep all these operations using memory and not disk reads/writes.

Steps to reproduce

Here's my Python code for reproducing the error, along with my example mechanism:
my_surface.txt (Apologies, you'll have to change the extension back to .yaml to try it out)

import cantera as ct

# read from yaml works
mech_yaml = 'my_surface.yaml'
gas = ct.Solution(mech_yaml, 'gas')
surf = ct.Interface(mech_yaml, 'surface1', [gas])

# instantiating the gas-phase only Solution from memory works
gas_model = ct.Solution(
    thermo='IdealGas',
    kinetics='GasKinetics',
    species=gas.species(),
    reactions=gas.reactions(),
)

# but the surface-phase gets stuck because it doesn't know what 'X' is
surf_model = ct.Interface(
    thermo='Surface',
    kinetics='Surface',
    species=surf.species(),
    reactions=surf.reactions(),
    adjacent=gas_model
)

Behavior

It produces the following error:

---------------------------------------------------------------------------
CanteraError                              Traceback (most recent call last)
/tmp/ipykernel_18387/760073976.py in <module>
      4     species=surf.species(),
      5     reactions=surf.reactions(),
----> 6     adjacent=gas_model
      7 )

build/python/cantera/base.pyx in cantera._cantera._SolutionBase.__cinit__()

build/python/cantera/base.pyx in cantera._cantera._SolutionBase._cinit()

build/python/cantera/base.pyx in cantera._cantera._SolutionBase._init_parts()

CanteraError: 
*******************************************************************************
CanteraError thrown by getElementWeight:
element not found: X
*******************************************************************************

I would expect there to be a way to pass in the element 'X' info (maybe through keyword arguments?), or at least define it using the Python module before creating the Interface.
I played around with the Python module for a while and did not find a way to rewrite Cantera's internal list/map of elements, but please let me know if I just missed the way to do it.

System information

  • Cantera version: 3.0.1
  • OS: Ubuntu 20.04 LTS (run from Windows Subsystem Linux)
  • Python: 3.12.7
@speth
Copy link
Member

speth commented Dec 4, 2024

No, there's currently no way to add elements via the Python interface. For custom elements, you need to start with a YAML file defining a phase that contains all the non-standard elements of interest. Here's what I posted to a similar Users' Group post previously:

To include your custom element definitions, you’ll need to specify a YAML phase definition that includes them, which can be done inline. A few small caveats:

  • The YAML phase definition does need to include at least one species to make a valid phase
  • You will need to write a loop to add the remaining species and the reactions

For example:

gas1 = ct.Solution(
    yaml="""phases:
        name: gas
        thermo: ideal-gas
        elements: [{input.yaml/elements: [AA]}]
        species: [{input.yaml/species: [O2]}]
        kinetics: bulk
        reactions: none
        state:
            T: 300.0
            P: 1.01325e+05
    """)

for S in custom_species:
    gas1.add_species(S)

for R in custom_reactions:
    gas1.add_reaction(R)

I'm going to transfer this to the Enhancements repo, as I think it would be good to add support for both providing an elements list to the Solution constructor and an add_element function to the Python Solution class.

@speth speth transferred this issue from Cantera/cantera Dec 4, 2024
@speth speth added the feature-request New feature request label Dec 4, 2024
@speth speth changed the title Error with element 'X' when initializing Interface object from parts Allow custom elements when initializing Solution/Interface object from parts Dec 4, 2024
@sevyharris
Copy link
Author

Thanks @speth, I will definitely use those features if implemented, but this workaround is quite helpful for now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature request
Projects
None yet
Development

No branches or pull requests

2 participants