Skip to content

Order of extra columns in SolutionArray is not preserved #832

@ischoegl

Description

@ischoegl

System information

  • Cantera version: 2.5.0a4 (and earlier?)
  • OS: Ubuntu Xenial (16.04)
  • Python/MATLAB version: Python 3.5.2

Expected behavior

SolutionArray.write_csv writes extra columns in correct order regardless of OS / Python version.

Actual behavior

Some columns are swapped for some OS / Python versions.

To Reproduce

In [1]: import cantera as ct
  ... : gas = ct.Solution('h2o2.yaml')
  ... : arr = ct.SolutionArray(gas, 10, extra={'grid': range(10), 'velocity': range(10)})

In [2]: arr._extra
Out[2]: 
{'velocity': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 'grid': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}

In [3]: arr.write_csv('test.csv')

In [4]: !head test.csv --lines=3
velocity,grid,T,density,Y_H2,Y_H,Y_O,Y_O2,Y_OH,Y_H2O,Y_HO2,Y_H2O2,Y_AR
0.0,0.0,300.0,0.081893927638,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
1.0,1.0,300.0,0.081893927638,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0

Thoughts:

  • This is obviously linked to Python dictionaries not (necessarily) preserving order
  • An option that preserves an OrderedDict would require a tweak of internal storage, where the following should result in the expected behavior:
arr = ct.SolutionArray(gas, 10, extra=OrderedDict([('grid', range(10)), 
                                                   ('velocity', range(10))]))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions