Generate runnable code from a modelΒΆ

Code generation is the process of representing the CellML model in another language format. At the time of writing, two profiles are available: C (default) and Python. There are four steps to code generation:

  1. Create a Generator item and select the profile language. (The default profile is C).

  2. Pass a model to the generator for processing.

  3. Retrieve the generated implementation code. This is the contents of the *.c file (if C is the profile) or *.py if Python is selected.

  4. (optional) Retrieve the generated interface code. This is the contents of the *.h file, and is not required for the Python profile.

Generate code from a model

    // Generate runnable code in other language formats for this model.

    // Create a Generator instance.  Note that by default this uses the C language profile.
    auto generator = libcellml::Generator::create();

    // Pass the generator the model for processing.
    generator->setModel(analyser->model());
    printIssues(generator);
    // Retrieve and write the interface code (*.h) and implementation code (*.c) to files.
    std::ofstream outFile("sineComparisonExample.h");
    outFile << generator->interfaceCode();
    outFile.close();

    outFile.open("sineComparisonExample.c");
    outFile << generator->implementationCode();
    outFile.close();

    // If required, change the generator profile to Python.
    auto profile = libcellml::GeneratorProfile::create(libcellml::GeneratorProfile::Profile::PYTHON);
    generator->setProfile(profile);

    // Retrieve and write the implementation code (*.py) to a file.
    outFile.open("sineComparisonExample.py");
    outFile << generator->implementationCode();
    outFile.close();

Full context: example_simulationToolDev.cpp