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

Incorrect code gen for subdimension set by GET DIRECT CONSTANTS #124

Closed
ToddFincannon opened this issue Sep 28, 2021 · 1 comment · Fixed by #125 or #190
Closed

Incorrect code gen for subdimension set by GET DIRECT CONSTANTS #124

ToddFincannon opened this issue Sep 28, 2021 · 1 comment · Fixed by #125 or #190
Assignees
Labels
Milestone

Comments

@ToddFincannon
Copy link
Collaborator

Code generation is incorrect when an array is initialized by a combination of literal constants and values read from a file by GET DIRECT CONSTANTS. Two equations set the constant values. GET DIRECT CONSTANTS initializes elements given by a subdimension, with the remaining elements carved out by an EXCEPT clause.

DimA: A1, A2, A3 ~~|
SubA: A2, A3 ~~|
a[SubA] = GET DIRECT CONSTANTS('s.csv', ',', 'B2') ~~|
a[DimA] :EXCEPT: [SubA] = 0 ~~|

The first value from the CSV file is repeated for all elements in the subdimension:

// a[SubA] = GET DIRECT CONSTANTS('s.csv',',','B2')
_a[1] = 12.0;
// a[SubA] = GET DIRECT CONSTANTS('s.csv',',','B2')
_a[2] = 12.0;

The literal const goes to all elements of the subscript family, not just the elements remaining after the EXCEPT:

// a[DimA]:EXCEPT:[SubA] = 0
_a[0] = 0.0;
// a[DimA]:EXCEPT:[SubA] = 0
_a[1] = 0.0;
// a[DimA]:EXCEPT:[SubA] = 0
_a[2] = 0.0;
@ToddFincannon ToddFincannon self-assigned this Sep 28, 2021
@ToddFincannon ToddFincannon added this to the 0.6.0 milestone Sep 28, 2021
ToddFincannon added a commit that referenced this issue Oct 6, 2021
@ToddFincannon
Copy link
Collaborator Author

This turned out to be two separate problems. The GET DIRECT CONSTANTS implementation did not handle subdimensions correctly. This required a rewrite of the generateDirectConstInit function.

The dimensions are picked from the LHS in model order. They will map to the CSV data table in model order.

Then the complete set of LHS subscripts that will map to the table are generated as a list of index subscript tuples. These will end up on the LHS of C assignment statements, so they are in normal order to match C array declarations.

When one of the subscripts to be mapped to the data table is a subdimension, the variable will already be separated by the time we get to EquationGen. We test each LHS subscript to see if it's either a dimension or an index in a separated dimension. If so, we get the index from the tuple for that dimension.

To get the data table cell offset from the start cell, we start by finding the position of the LHS index in model order by comparing with the subscript families of LHS model order subscripts. To support subdimensions, we need to use the position of the index within the subdimension instead of the index value. That's because the data table has rows or columns maching the subdimension index position, not the subdimension index value.

For instance, in the equation for f in directconst.mdl, the SubA subdimension has the second and third elements of the DimA dimension. These need to map onto the first and second columns of the data table.

Finally, we add a constant variable for each cell in the table, using numeric LHS indices into the C array, and the corresponding cell offset into the table.

Whew! The second problem was solved by improving the way we match against EXCEPT subscripts. Previously, we simply tested whether the LHS subscripts were equal to the EXCEPT subscripts. When the EXCEPT subscripts involve a subdimension, we need to examine each subscript separately, and test whether an index on the LHS belongs to a subdimension in the EXCEPT subscripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment