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

Allow for overriding data variables and lookups at runtime #472

Closed
chrispcampbell opened this issue Apr 30, 2024 · 1 comment · Fixed by #490 or #476
Closed

Allow for overriding data variables and lookups at runtime #472

chrispcampbell opened this issue Apr 30, 2024 · 1 comment · Fixed by #490 or #476

Comments

@chrispcampbell
Copy link
Contributor

This feature was originally filed together with adding support for overriding constant variables (see #470) but I now think it would be easier to manage as a separate issue, so I'm filing this as a standalone feature.

To enable rapid prototyping and calibration of our models, we would like to have a way to override data or lookup variables at runtime.

I prototyped this in my work-in-progress "generate JS" branch because I needed it for a small model we were working on. I believe that @ToddFincannonEI already has a related branch, so we should put our heads together on this.

@chrispcampbell chrispcampbell self-assigned this Apr 30, 2024
@chrispcampbell chrispcampbell changed the title Allow for data variables and lookups at runtime Allow for overriding data variables and lookups at runtime Apr 30, 2024
@chrispcampbell
Copy link
Contributor Author

TL;DR: This includes the following changes:

  • Updated code gen in the compile package so that it includes a setLookup function in generated C and JS models.
  • Updated the runtime packages to allow for supplying lookup overrides via a new options parameter for ModelRunner.runModel.

More details on all the changes are listed below. Most of the changes are in the form of new functionality, and none of these should cause incompatibilities for existing projects.

Changes in cli and compile packages

  • Updated C and JS code gen to generate a setLookup function that allows for setting a new lookup instance for any variable of type data or lookup. This uses the same approach as what I did long ago for storeOutput, where there's a big switch statement that has a case for the varIndex associated with each variable.

  • Updated C code gen to have more consistent whitespace, like what I did earlier for the JS code gen.

  • Added a more complete test of generated C code, similar to the test I added for JS code gen, so that we now verify the content and formatting of the generated C code.

  • Updated the __lookup and __get_data_between_times functions to return _NA_ in the case where the lookup is NULL or empty. (This was already true for __lookup in the case of a NULL lookup, but I made the two functions consistent in the handling of NULL / empty, just like what was done in the JsModelFunctions implementation of these functions.)

  • Fixed the Model.jsonList function so that it no longer omits variables of type data or lookup. These are now included in the list, and the code gen looks at the type when deciding which variables to include in the switch statement for setLookup or storeOutput.

  • Fixed the Model.jsonList function to include levelVars at the end. (Previously I had used initVars but this was incorrect and could lead to duplicates in the list.)

  • Added a test for Model.jsonList since we didn't have one previously.

Changes in the runtime and runtime-async packages

  • Added a new options argument to ModelRunner.runModel (see RunModelOptions type, which currently only includes one optional property, lookups).

  • Added LookupDef type and createLookupDef function, which are used to pass lookup data to the new lookups array in the runModel options (or setLookup function in the generated C or JS code).

  • Updated the RunModelParams implementations and the GeneratedModel implementations (including mocks) to handle the new lookups option that is included in ModelRunner.runModel, and added extensive tests.

  • Added a new override-lookups integration test that verifies the new behavior for the 4 cases: JS/synchronous, JS/asynchronous, Wasm/synchronous, Wasm/asynchronous.

Changes in the plugin-wasm package

  • Updated the docs to say that _setLookup is now one of the functions that must be included in the EXPORTED_FUNCTIONS list passed to Emscripten.

Other changes

  • Updated the top-level README in a few places to make it clear that SDEverywhere can generate JavaScript code in addition to C/WebAssembly.

  • Updated the top-level README to include a row for the parse package in the table of packages (forgot to do that when I added the parse package a while back).

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