Cantera / cantera

Chemical kinetics, thermodynamics, and transport tool suite
https://cantera.org
Other
581 stars 342 forks source link

yaml2ck handles multi-line "note" fields incorrectly #1633

Open speth opened 8 months ago

speth commented 8 months ago

Problem description

Multi-line note fields in thermo entries are not correctly converted to comment lines by yaml2ck -- only the first line has a ! character added. This causes errors round-tripping thermo files if the original CK file contained commented-out species entries, which are then re-inserted without being marked as comments.

Steps to reproduce

Use yaml2ck to convert a file containing the following species entry:

- name: CH3OCO
  composition: {H: 3, C: 2, O: 2}
  thermo:
    model: NASA7
    temperature-ranges: [200.0, 1000.0, 3000.0]
    data:
    - [6.42720143, -7.97126399e-03, 5.91518706e-05, -6.98220041e-08, 2.64882011e-11,
      -2.1166004e+04, -0.784719994]
    - [4.11346455, 0.017235061, -9.1563172e-06, 2.30732677e-09, -2.2553008e-13,
      -2.13756201e+04, 6.78109955]
    note: |-
      !DHf(0K) =     9.73 [kcal/mol], taken from SJK v0.9.
      !Q(T) from B3LYP/6-311++G(d,p) by CFG on  06Sep2013
      HCO                     H  1 C  1 O  1      G   200.0   3000.0   1000.0        1
       2.54295623E+00 5.31270712E-03-2.75965333E-06 6.86785548E-10-6.66794809E-14    2
       3.99103060E+03 1.10699925E+01 4.17956080E+00-2.85097257E-03 1.25531492E-05    3
      -1.21627661E-08 3.99714476E-12 3.73162143E+03 3.53923487E+00                   4
      DHf(0K) =   -36.23 [kcal/mol], taken from SJK v0.9.
      Q(T) from B3LYP/6-311++G(d,p) by CFG on  06Sep2013

Behavior

Resulting section of generated CK thermo file:

!!DHf(0K) =     9.73 [kcal/mol], taken from SJK v0.9.
!Q(T) from B3LYP/6-311++G(d,p) by CFG on  06Sep2013
HCO                     H  1 C  1 O  1      G   200.0   3000.0   1000.0        1
 2.54295623E+00 5.31270712E-03-2.75965333E-06 6.86785548E-10-6.66794809E-14    2
 3.99103060E+03 1.10699925E+01 4.17956080E+00-2.85097257E-03 1.25531492E-05    3
-1.21627661E-08 3.99714476E-12 3.73162143E+03 3.53923487E+00                   4
DHf(0K) =   -36.23 [kcal/mol], taken from SJK v0.9.
Q(T) from B3LYP/6-311++G(d,p) by CFG on  06Sep2013
CH3OCO                  C   2H   3O   2     G200.000   3000.000  1000.000      1
 4.11346455E+00 1.72350610E-02-9.15631720E-06 2.30732677E-09-2.25530080E-13    2
-2.13756201E+04 6.78109955E+00 6.42720143E+00-7.97126399E-03 5.91518706E-05    3
-6.98220041E-08 2.64882011E-11-2.11660040E+04-7.84719994E-01                   4

System information

Additional context

Discovered while investigating #1632.

corykinney commented 1 month ago

@speth I'm interested in helping with this. After my initial reading of yaml2ck, it looks like the snippets that might need to be modified are build_thermodynamics_text:

https://github.com/Cantera/cantera/blob/d37a76bf6101e5075bb6f82d36e360b6bac13d32/interfaces/cython/cantera/yaml2ck.py#L321

and in build_transport_text:

https://github.com/Cantera/cantera/blob/d37a76bf6101e5075bb6f82d36e360b6bac13d32/interfaces/cython/cantera/yaml2ck.py#L528

To insert !s for every newline in the note, if applicable. Is that correct or am I missing anything?

speth commented 1 month ago

Yeah, I think that would be the gist of it. If I recall correctly, there may also be handling for comments associated with each reaction which would need a similar fix.

corykinney commented 2 weeks ago

I pushed a simple fix for the multi-line thermo notes and confirmed the roundtrip produces the exact same note in the YAML file.

@speth As for the transport, it seems to me that multi-line transport notes aren't really possible in the Chemkin format, as the notes are supposed to be appended to the end of the line, is that correct? Or at least if multi-line comments are allowed, it's not roundtripping with ck2yaml, only the portion at the end of the line is retained.

Also, it seems to me that currently, yaml2ck does not even write any notes to the Chemkin file for reactions, even if they are present in the YAML file, unless I'm mistaken. So I will go ahead and implement that with multi-line notes in mind so that roundtripping the files does not lose the comments.