Page Detailing How the Code Generation Works for Different Model Types
- This page will detail the steps taken from turning an authored model from sympy into the final C generated code.
Biomechanics: Consititutive Model
Model form, where everything starts: pcty.client.forms.MatModelsForm
Code generation starts at: MatModelsForm.generateCode
generateCode calls src/csrc/strain_energy/sympy_to_c.parse_table_file,
parse_table_file retrieves all the scalar, vector, tensor, dynamic and rpar variables and also all the defining equations, which contain everything other than the independent variables.
The resultant variables and equations from parse_table_file are then passed onto sympy_to_c.generate_c, which then passes on that information to the main method: gen_c_helper
- This will continue the generation of the C file (which generate_c started), all output is redirected to a variable, which is eventually written to the server where it will be compiled and run.
- Starts writing the declarations for the C file (i.e., double t)
Then for the vector and tensor variables it declares the PythonArrayObjects
- Then for all variable types, it creates sympy Symbols in global scope for each variable
- All of the above is done with variables that we essentially already know about, as these are the variables that are predefined with each model and which the user cannot modify.
- Now all the equations are processed.
- These equations are the equations as written on the model form, they do not contain all the information that is available from the metadata file (e.g., description, type, etc).
- If we are working with a Dynamic model, we do some extra stuff here: TODO: fill in more info
- Otherwise, we just execute the equation/line into the global space and then evaluate it:
- exec(equationLine, globals(), globals() (ex: exec('I1 = C.trace()', globals(), globals()) )
- variable = eval(varname, globals(), globals()) (ex: eval('Il', globals(), globals()) )
- If this is a symbolic variable, continue to the next variable
- Find out what the variable's type is (e.g., Matrix, eig3, eig2, or other)
- And make the C declarations for it
- And finally exec the variable into globals