## How to Automate Physics Choices and Studies Using Model Methods

##### Temesgen Kindo June 28, 2017

You solved a model under certain assumptions. When you analyze the results, you find out that those assumptions do not hold. Now, you have to amend your analysis by incorporating new physics features or changing the study type. What if you could automate such processes? Today, we will discuss how to do so easily using the Model Method feature introduced in version 5.3 of the COMSOL Multiphysics® software.

### Benefits of Customized Solution Procedures

We often make modeling decisions based on partial information. Does the flow stay laminar or does it become turbulent? Does a solid stay elastic or does it yield to become plastic under the specified loads and constraints? Are deformations large enough to require a geometrically nonlinear analysis or is small deformation theory good enough? Sometimes, a limit analysis can answer these questions. If we can answer such questions before solving the problem, we can pick the appropriate model. If not, it is economical to solve the simpler model and switch to the more complicated model only if the solution is not valid.

For example, we can do elastic analysis first and switch to elastoplastic analysis only if the maximum stress obtained by the first analysis is above the elastic limit. Similarly, we can solve assuming laminar flow and include turbulence in our model only if the Reynolds number obtained from the first analysis is high enough.

In these and other situations, where we may have to change our modeling approach based on preliminary results, it would save our efforts to automate the workflow. Information obtained from the first study can also be reused to make subsequent studies computationally efficient. Today, we will demonstrate how to do so by writing code to automate an elastoplastic analysis. The code runs an elastic analysis, checks if the maximum stress exceeds the yield stress of the material, and runs the model with plasticity if necessary.

### Creating a Model Method in COMSOL Multiphysics®

Using the COMSOL Multiphysics® software on the Windows® operating system platform, you can build an app with a customized user interface and include methods for additional functionality. Apps built using the Application Builder in Windows® can then be run on any operating system. You can share the apps with colleagues, customers, students, and more.

In version 5.3 of the COMSOL® software, we introduced a new feature, called a model method, that lets you extend the functionality of the software by writing code inside the COMSOL Multiphysics graphical user interface (GUI), even when you do not intend to make apps. The Record Code, Editor Tools, Language Elements, and Model Expression features of the Application Builder can be used to easily generate the Java® code needed to write a model method.

To add a model method, go to the Developer tab and click Model Method.

To run a model method, go to the Developer tab, click Run Model Method, and choose a method.

In previous blog posts, we discussed how model methods work and demonstrated how to use them to create randomized geometry. Today, we will extend that conversation to physics and study settings.

### Automating an Elastoplastic Analysis with Model Methods

To demonstrate how to use model methods in physics selection and study sequences, we will use a model based on an elastoplastic analysis of a holed plate from our Application Gallery. The Application Gallery example was set up with prior knowledge that the stress will exceed the elastic limit. As such, plasticity was added in the analysis. Today, we will have a model method find that out and, if plasticity is necessary, incorporate that automatically.

The procedure we want to automate here contains the following steps:

1. Run an elastic study without plasticity.
2. Check if the maximum stress exceeds the initial yield stress of the material.
3. If the maximum stress is less than the initial yield stress, stop. Otherwise, run the elastoplastic study.
4. If plasticity is necessary, reuse information from the elastic study to avoid unnecessary nonlinear analysis in the elastic range.

We add two studies and disable plasticity in the first study. In the second study, we add an Auxiliary Sweep for load ramping in the elastoplastic analysis. In the first study, the full load is applied by setting para to 1 in Global Definitions. In the second study, we use the parameters p_low and p_next to make the study efficient. These parameters are going to be set based on the results of the first study.

The second study will be computed only if the assumptions in the first study turn out to be incorrect.

In the Results section, we add a Derived Values node to evaluate the maximum stress from the first study. This could alternatively be done using the Maximum Component Coupling operator. This value, obtained in pascals (as shown in the Settings window for Surface Maximum 1) will be compared to the initial yield stress in pascals. To this end, we introduce the parameter SY_scaled.

SYield and SY_scaled are for the Materials node and the model method, respectively.

A Maximum Component Coupling operator is another alternative for this operation.

Now that we have all of the ingredients we need, let’s write the model method.

A model method that is used to automate an efficient elastoplastic analysis.

Two of the above lines warrant some discussion:

• Line 12:
• The first study is done using a load parameter of 1. If `StressRatio` in line 9 is greater than 1, its reciprocal will tell us the load parameter at the elastic limit. Note that we could do so here, as plasticity is the only possible nonlinearity in our problem. The model has no geometric nonlinearity or contact.
• Line 14:
• If plasticity is needed, we want to do the auxiliary sweep in steps of 0.05. We use the ceil (ceiling) method from the Java® Math class to obtain the lowest value of the load parameter that is a multiple of 0.05 and is after the elastic limit.

With this information, the second study (if necessary) is solved only once in the elastic region: at the elastic limit. We can see what values of the load parameter are used in the Results section.

The lowest value of the load parameter, highlighted, is estimated using the elastic study.

If you go back to Global Definitions, you will see that the model method has updated the parameters p_low and p_next from their original values of zero, shown earlier.

Parameter values changed by a model method based on results from a preliminary study.

### Closing Remarks

Today, we have demonstrated setting up efficient physics choices and study sequences using methods. Similar tasks can be accomplished by scripting. However, model methods make it easy to grab the model objects and methods needed by using the same functionality employed in the Application Builder. When needed, these methods can be augmented by regular Java® classes, such as the Math class we used in the first example, or your own classes.

We have only shown one way of performing tasks to illustrate the use of model methods in physics and solver settings, but there are alternatives and refinements. For example, in the elastoplasticity analysis, we added two studies in the Model Builder. Alternatively, you can use a single study where the plasticity and auxiliary sweep features can be enabled or disabled from a model method.

In the examples above, there are logical decisions that have to be made between studies. When there are no such decisions and you just want to refer to one study from another (say, to use one study as a precomputing step for a subsequent study), you can use the Study Reference feature. See the section Study Reference in the COMSOL Multiphysics Reference Manual for details.

If you have any questions related to today’s discussion or using COMSOL Multiphysics, contact us via the button below.

### Related Resources

Microsoft and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Oracle and Java are registered trademarks of Oracle and/or its affiliates.

#### Post Tags

Technical Content