Coding and Methods Overview
In the Application Builder, custom code is represented by a Method node. To write code, you must first create a Method node. To create a Method node, click the New Method button () in a ribbon toolbar or right-click the Methods node () in the Application Builder window and select New Method.
Local Methods vs. Global Methods
The new Method node appears under the Methods node in the Application Builder tree and contains an application method. Such methods are available globally for use in all application methods and form objects. It is also possible to create local methods in some of the form objects in a form. Local methods are not accessible or visible outside of the objects where they are defined. Use local methods in forms for methods that are internal to that form. These local methods connect to events in the form objects, such as when the setting (data) changes for a checkbox. The Method node is used for referring to the method from a command sequence or a form object’s event. For use with a model in the Model Builder, you can also create Model Method nodes for model methods, which work the same as application methods but can run in the Model Builder to extend or customize some part of a COMSOL Multiphysics model.
Content of Methods
A Method node contains COMSOL Multiphysics® code and Java® code, which you can inspect and edit by double-clicking it in the Application Builder window or by right-clicking and selecting Edit. When a method is opened, it appears in a Method Editor window.
The changes made to the code in an editor tab are stored in the Application Builder model when you close the tab or after compiling the code. To compile and check the syntax of the code, click the Check Syntax button () in the ribbon toolbar (or press F9).
The code defines an ApplicationMethod Java class. This class has a method called execute that the command sequence calls when the application runs it. A typical use case is that you create a Method node, write some code in the execute method, and link this code to a command sequence that a form object in the application’s user interface can trigger. Methods that affect the state of form objects apply immediately.
The default setting is to only display the code contained inside the execute method. To display all code, enable the View all code checkbox, which is a preference setting on the Methods page under Application Builder in the Preferences window. When View all code is active, you can add import statements, for example. Import statements are helpful to make a class or a whole package (such as import java.time.*;) visible in a method. Using import statements, you do not have to write the entire class definition, and so it improves the readability of the program.
When the execute method is triggered while running the application, model is the model object of the application. From this method, you can access features of the physics and change their parameters.