SDXorg / pysd

System Dynamics Modeling in Python
http://pysd.readthedocs.org/
MIT License
349 stars 89 forks source link

Add support for Vensim TABBED ARRAYS #405

Closed rogersamso closed 1 year ago

rogersamso commented 1 year ago

Description

Add support for TABBED ARRAYS

Type of change

PR verification (to be filled by reviewers)

rogersamso commented 1 year ago

@enekomartinmartinez, please let me know if you are fine with this, and I will update the what's new and others.

enekomartinmartinez commented 1 year ago

@enekomartinmartinez, please let me know if you are fine with this, and I will update the what's new and others.

Hi @rogersamso, thank you for the changes. It is a good and clean implementation!

I just have some suggestions for the text model at https://github.com/SDXorg/test-models/pull/86

Could you please add TABBED ARRAYS of 1D and 2D there, and if possible a 3D var defined with several tabbed arrays. Also include some regular floating point numbers and with e (e.g. 0.075, 3e-05), they should work properly as the raw_number parsing works for other tests, but just in case is better to have a more general example test.

enekomartinmartinez commented 1 year ago

Hi @rogersamso

I noticed that there is a bug to be corrected. The current implementation will not accept tabbed arrays defined as a n unchangeable constants (==).

You should remove all the parsing done in the element_object and parse the tabbed_array() call at the component level (which will no be need to be visited). Please, let me know if I explained properly.

Also, please add an unchangeable constant defined tabbed array ( with two =) in the test model.

rogersamso commented 1 year ago

@enekomartinmartinez

Please check if this is better now.

However, there's still a fix to implement, as Unchangeable constants in PySD only work for scalars.

This code:

dim1:
    a, b, c
    ~   
    ~       |

unchangeable_vector[dim1]==
    1, 2, 3;
    ~   
    ~       |

translates to this:

@component.add(
    name="unchangeable_vector",
    subscripts=["dim1"],
    comp_type="Constant",
    comp_subtype="Normal",
)
def unchangeable_vector():
    return xr.DataArray([1.0, 2.0, 3.0], {"dim1": _subscript_dict["dim1"]}, ["dim1"])

Note that comp_subtype in the translated code is not Unchangeable.

This test should be added in tests/test-models/tests/unchangeable_constant/test_unchangeable_constant.mdl. If you want I can push it to the test repos too, while we prepare the fix in pysd.