Learning Center

Course:

Getting Started with Building Simulation Apps

Using and Recording Methods to Create Custom Code for Simulation Apps


To create custom code for use in your simulation apps developed with the Application Builder in COMSOL Multiphysics, you can use methods. Writing, creating (via converting command sequences), and recording methods provides virtually unlimited levels of customization for your simulation apps. Activating the record code functionality in COMSOL Multiphysics creates Java code in the Application Builder that corresponds to the sequence of changes made in the Model Builder. Here, you will learn how to use this functionality. You will also see how to leverage the built-in code recording functionality to help learn how to write methods.

Tutorial: Simulation App Customization Using Methods and Recording Code

The simplified thermal microactuator tutorial model is used for the model example, which a simulation app was developed for previously. Here, the simulation app is extended to enable users of the app to choose the mesh density level from a limited number of choices. A custom form object, Combo Box, is added to do this. Declarations are defined to enable proper operation of the newly added app functionality. Custom code used by the form object is generated with the help of the Record Method programming tool. Using Record Method to learn how to write Java code for customization is shown. Making changes to the app is done using the Settings window, Editor Tools, and the Method Editor. Topics covered in the tutorial video include the following:

  • Create custom form object
    • Add the form object – Combo Box
    • Define an integer variable under Declarations to use as the source
    • Create Choice List in the settings
  • Record code
    • Activate Record Method option in the Developer ribbon tab of the Model Builder
    • Perform operations in the Model Builder
    • Stop recording
  • Return to Application Builder
    • Open Method Editor window to review the Java method code generated
    • Update code to incorporate use of integer variable defined previously
    • Rename method to reflect the action triggered
  • Update declarations
    • Add display names for options in the Choice List that are user-friendly and easily recognizable
  • Update associated form objects (such as Text Label form objects) to reflect changes
  • Use Events to trigger an action when the value of the combo box is changed
    • Update setting for Events section
    • Choose custom method created previously from the On data change menu

Further Learning

The COMSOL Documentation contains several examples of further customizations that can be made to simulation apps by creating user-defined methods. These can be found in the Application Programming Guide in the chapter on Programming Examples. Additionally, there are several keyboard shortcuts for the Application Builder that are useful to know to more quickly develop and check your user-defined methods as well as general tips to improve simulation app design and structure.


Submit feedback about this page or contact support here.