feat: add support for Vensim's ALLOCATE BY PRIORITY function#809
feat: add support for Vensim's ALLOCATE BY PRIORITY function#809NiharikaHari wants to merge 7 commits intoclimateinteractive:mainfrom
Conversation
…here into feat/allocate-by-priority
|
@NiharikaHari: Thank you for your contribution! I have some high priority tasks to attend to for the next couple days, but I will try my best to review your work by Thursday. |
There was a problem hiding this comment.
Thank you @NiharikaHari for taking the time to contribute your work on this new function!
Most of my comments below are nitpicky things about code style. I haven't read the logic too closely for the _ALLOCATE_BY_PRIORITY C runtime function, but as long as it produces equivalent results to Vensim (and I can see that it does since the tests are passing), then I will trust that the logic works.
The test_js job is failing; that job does more than just run JS-related tests, it also runs the JS-level integration tests that exercise both C/Wasm and JS code gen, and there are Emscripten warnings flagged here (see build log) that will need to be resolved.
Also, please add at least one new test in gen-equation-c-from-vensim.spec.ts to verify that code gen works as expected. You can follow the patterns used in the existing should work for ALLOCATE AVAILABLE function (1D LHS, ...) test. Add your new test just after the last ALLOCATE AVAILABLE test in that file.
|
|
||
| static double out_return[ALLOCATE_BY_PRIORITY_BUFSIZE]; | ||
|
|
||
| fprintf(stderr, request_quantities); |
There was a problem hiding this comment.
We generally don't leave printf's in production code. I know that Todd included some printf's in the _ALLOCATE_AVAILABLE implementation above, which was fine because they are gated by #ifdef PRINT_ALLOCATIONS_DEBUG_INFO. So it's OK if you want to leave these fprintf calls here, but just make sure to wrap them in #ifdef PRINT_ALLOCATIONS_DEBUG_INFO / #endif blocks.
| out_return[idx[i]] = out[i]; | ||
| } | ||
|
|
||
| fprintf(stderr, out_return); |
There was a problem hiding this comment.
Same comment as above about removing this or putting it in an #ifdef block.
|
|
||
| fprintf(stderr, out_return); | ||
| return out_return; | ||
|
|
There was a problem hiding this comment.
Remove these two blank lines.
|
|
||
| // Reduce remaining supply | ||
| supply -= dx; | ||
|
|
| } | ||
|
|
||
| // Create the outputs array | ||
| for (size_t i = 0; i < num_requesters; i++) out_return[i] = 0.0; |
There was a problem hiding this comment.
I'm not a fan of single-line loops or conditionals. Make this:
for (size_t i = 0; i < num_requesters; i++) {
out_return[i] = 0.0;
}
|
|
||
| // Continue allocating until supply is exhausted | ||
| while (supply > _epsilon) { | ||
|
|
| break; | ||
| } | ||
| } | ||
| if (!any_active) break; |
There was a problem hiding this comment.
Convert single-line conditional to multiple lines with braces.
| // smallest of (next completion, next activation, remaining supply) | ||
| double dx = dx_next_top; | ||
|
|
||
| if (!isnan(dx_next_start) && dx_next_start < dx) dx = dx_next_start; |
There was a problem hiding this comment.
Convert single-line conditional to multiple lines with braces.
| double dx = dx_next_top; | ||
|
|
||
| if (!isnan(dx_next_start) && dx_next_start < dx) dx = dx_next_start; | ||
| if (supply < dx) dx = supply; |
There was a problem hiding this comment.
Convert single-line conditional to multiple lines with braces.
| } | ||
| } | ||
| continue | ||
| } else if ((callExpr.fnId === '_ALLOCATE_BY_PRIORITY') & (index < 2)) { |
There was a problem hiding this comment.
There is a typo here: & should be &&.
Once you fix that, you should be able to remove the extra parentheses as they aren't needed.
Actually, I think you could probably just remove the index < 2 part here. Ideally we would have validation code here for each argument (similar to what the TODO comments say in the code that handles ALLOCATE AVAILABLE args above). But if we're not going to add that validation code now, I think you can drop the index < 2 part, otherwise it gives the impression that only those args need to be considered when really all args should get special treatment eventually.
|
Thank you @chrispcampbell for taking the time to review this. I'll be able to work on these changes next week. The logic in |
|
@NiharikaHari This is a useful addition to SDE. Thank you for contributing! Does you model run now? |
|
@travisfranck Yes, it does! Outside of this I'm doing some janky changes around |
Fixes #792
Feature: Allocate by priority #792
allocate_availableimplementation for consistency.