230
INTRODUCTION TO Application Builder

Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

INTRODUCTION TO

Application Builder

Page 2: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

C o n t a c t I n f o r m a t i o nVisit the Contact COMSOL page at www.comsol.com/contact to submit general inquiries, contact

Technical Support, or search for an address and phone number. You can also visit the Worldwide

Sales Offices page at www.comsol.com/contact/offices for address and contact information.

If you need to contact Support, an online request form is located at the COMSOL Access page at

www.comsol.com/support/case.

Other useful links include:

• Support Center: www.comsol.com/support

• Product Download: www.comsol.com/product-download

• Product Updates: www.comsol.com/support/updates

• Discussion Forum: www.comsol.com/community

• Events: www.comsol.com/events

• COMSOL Video Gallery: www.comsol.com/video

• Support Knowledge Base: www.comsol.com/support/knowledgebase

Part number: CM020011

I n t r o d u c t i o n t o A p p l i c a t i o n B u i l d e r © 1998–2014 COMSOL

Protected by U.S. Patents listed on www.comsol.com/patents, and U.S. Patents 7,519,518; 7,596,474; 7,623,991; and 8,457,932. Patents pending.

This Documentation and the Programs described herein are furnished under the COMSOL Software License Agreement (www.comsol.com/comsol-license-agreement) and may be used or copied only under the terms of the license agreement.

COMSOL, COMSOL Multiphysics, Capture the Concept, COMSOL Desktop, and LiveLink are either registered trademarks or trademarks of COMSOL AB. All other trademarks are the property of their respective owners, and COMSOL AB and its subsidiaries and products are not affiliated with, endorsed by, sponsored by, or supported by those trademark owners. For a list of such trademark owners, see www.comsol.com/trademarks.

Version: December 2014 COMSOL 5.0.1

IntroductionToApplicationBuilder.book Page 1 Tuesday, December 16, 2014 4:16 PM

Page 3: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 3 Tuesday, December 16, 2014 4:16 PM

Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Future Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

The Application Builder Desktop Environment . . . . . . . . . . . . . . 9

Running an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Running an Application in COMSOL Multiphysics . . . . . . . . 14

Running an Application in COMSOL Server . . . . . . . . . . . . . 20

Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

The Form Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

The Forms Settings Window. . . . . . . . . . . . . . . . . . . . . . . . . . 31

The Individual Form Settings Windows . . . . . . . . . . . . . . . . . 31

Form Editor Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Form Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Input Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Text Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Data Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

The Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

Menu Bar and Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Ribbon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Events at Startup and Shutdown. . . . . . . . . . . . . . . . . . . . . . . 62

Global Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

| 3

Page 4: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 4 Tuesday, December 16, 2014 4:16 PM

Form and Form Object Events . . . . . . . . . . . . . . . . . . . . . . . . 65

Using Local Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Scalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Array 1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

Array 2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Choice List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

The Method Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Converting a Command Sequence to a Method . . . . . . . . . 86

Language Elements Window. . . . . . . . . . . . . . . . . . . . . . . . . . 90

Model Code Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Model Data Access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

Recording Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Checking Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

Find and Replace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Model Expressions Window . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Extracting Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Method Editor Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Ctrl+Space and Tab for Code Completion . . . . . . . . . . . . 101

Local Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Methods with Input and Output Arguments. . . . . . . . . . . . 104

Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

The Model Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Language Element Examples . . . . . . . . . . . . . . . . . . . . . . . . . 107

Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

4 |

Page 5: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 5 Tuesday, December 16, 2014 4:16 PM

Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113

Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115

Appendix A—Form Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .116

List of All Form Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116

Check Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117

Combo Box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121

Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137

Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138

Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138

Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139

Progress Bar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140

Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141

Message Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142

Results Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143

Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144

Form Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146

Card Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148

File Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151

Array Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159

Radio Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162

Selection Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163

Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .167

List Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168

Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172

Slider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174

Toolbar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .175

Spacer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177

Appendix B—Grid Layout Mode . . . . . . . . . . . . . . . . . . . . . . .179

| 5

Page 6: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 6 Tuesday, December 16, 2014 4:16 PM

Appendix C—Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . 184

Appendix D—Built-in Method Library . . . . . . . . . . . . . . . . . . . 186

Appendix E—Guidelines for Building Applications . . . . . . . . . 218

Appendix F—The Application Library Examples. . . . . . . . . . . 221

6 |

Page 7: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 7 Tuesday, December 16, 2014 4:16 PM

Introduction

A COMSOL application is a COMSOL Multiphysics model with a user interface. This book gives a quick overview of the Application Builder desktop environment with examples that show you how to use the Form Editor and the Method Editor. Reference materials are also included in this book featuring a list of the built-in methods and functions that are available.

If you want to check out an example application before reading this book, open and explore one of the applications from the Application Libraries. Keep it open while reading this book to try things out.

The Application Builder is included in the Windows version of COMSOL Multiphysics and accessible from the COMSOL Desktop environment. COMSOL Multiphysics and its add-on products are used to create an application. A license for the same add-on products is required to run the application from COMSOL Multiphysics or COMSOL Server.Additional resources, including video tutorials, are available online at www.comsol.com.

RUNNING APPLICATIONS WITH COMSOL MULTIPHYSICS

With a license of COMSOL Multiphysics, applications can be run from the COMSOL Desktop in Windows, OS X, and Linux.

RUNNING APPLICATIONS WITH COMSOL SERVER

With a COMSOL Server license, a web implementation of an application can be run in major web browsers on platforms such as Windows, OS X, iOS, Linux, and Android™. In Windows, you can also run COMSOL applications by connecting to a COMSOL Server with an easy-to-install COMSOL client, available for download from www.comsol.com. COMSOL Server does not include the Application Builder, Physics Builder, and Model Builder tools that come with the COMSOL Desktop environment.

GUIDELINES FOR BUILDING APPLICATIONS

If you are not experienced in building a graphical user interface or in programming, you may want to read “Appendix E—Guidelines for Building Applications” on page 218.

Introduction | 7

Page 8: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 8 Tuesday, December 16, 2014 4:16 PM

Future Versions

You can look forward to exciting additions and significant improvements to the Application Builder during the upcoming year. There will be two new releases: V5.1 in March 2015, and V5.2 in the fall of 2015.Some things that will be done for V5.1:

FORM EDITOR

• Copy forms and form objects between applications• Modify multiple form objects at the same time• Background image for forms• The Results table and Table form objects will merge• Better progress GUI support

METHOD EDITOR

• Code folding (”+”)• Add Ctrl+Alt+double-click in method editor code to open clicked method

in the Method Editor

GENERAL

• Merge the.mph and .mphapp formats to .mph• The Form Editor and Method Editor will merge into one tabbed window• Extended Internet functionality such as mail and FTP• Microsoft Excel® file export/import with LiveLink™ for Excel®

8 | Future Versions

Page 9: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 9 Tuesday, December 16, 2014 4:16 PM

The Application Builder Desktop Environment

The screenshot above is what you will see when you first start working with the Application Builder. The main components of the Application Builder desktop environment are:• Application Builder window and ribbon tab• COMSOL Desktop environment• Form Editor• Method Editor

APPLICATION BUILDER WINDOW—The Application Builder window with the model tree and the application tree.

APPLICATION TAB—The Application Builder tab in the ribbon gives easy access to commands used for application design.

COMSOL DESKTOP ENVIRONMENT—An application is built on a COMSOL Multiphysics model and the COMSOL Desktop environment is available in its entirety.

SETTINGS WINDOW—Click any node in the model tree or application tree to see its associated Settings window displayed next to the Application Builder window.

The Application Builder Desktop Environment | 9

Page 10: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 10 Tuesday, December 16, 2014 4:16 PM

THE FORM EDITOR

Use the Form Editor for user interface layout by creating forms with form objects such as input fields, graphics, and buttons. The main components of the Form Editor are:• Form ribbon tab• Explorer window with the application tree• Form window• Settings window

EXPLORER WINDOW—The Explorer window displays the Application tree.

FORM TAB—The Form tab in the ribbon gives easy access to the form editor.

SETTINGS WINDOW—Click any application tree node or form object to see its associated Settings window.

FORM EDITOR WINDOW—The tabbed Form Editor window allows you to move objects around by dragging. Click an object to edit its settings.

10 | The Application Builder Desktop Environment

Page 11: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 11 Tuesday, December 16, 2014 4:16 PM

Creating a New FormTo create a new form, right-click the Forms node and select New Form. You can also click New Form in the ribbon. Creating a new form will automatically open the New Form page of the Application Wizard.Assuming your application has one form named form1 and that you would like to edit it, you open the Form Editor in either of these two ways:• In the application tree, double-click the form1 node.• In the application tree, right-click the form1 node and select Edit.

The Application Builder Desktop Environment | 11

Page 12: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 12 Tuesday, December 16, 2014 4:16 PM

THE METHOD EDITOR

Use the Method Editor to write methods for actions not covered by the standard use of the model tree nodes. A method is another name for what in other programming languages are known as a subroutine, function, or procedure.The main components of the Method Editor are:• Method and Debug ribbon tabs• Explorer window with the application tree• Method window• Model Expressions, Language Elements, and Model Code windows• Settings window

METHOD WINDOW—The tabbed Method Window allows you to switch between editing different methods.

EXPLORER WINDOW—The Explorer window displays the application tree.

METHOD and DEBUG TABS—The Method and Debug tabs in the ribbon give easy access to tools for writing and debugging code.

MODEL EXPRESSIONS, LANGUAGE ELEMENTS, and MODEL CODE WINDOWS—These windows display tools for writing code. The Model Expressions window shows all constants, parameters, variables, and functions available in the model. The Language Elements window is used to insert template code for built-in methods. The Model Code window is used to extract code for editing and running model tree nodes.

SETTINGS WINDOW—Click any application tree node to see its associated Settings window.

12 | The Application Builder Desktop Environment

Page 13: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 13 Tuesday, December 16, 2014 4:16 PM

Creating a New MethodTo create a new method, right-click the Methods node and select New Method. You can also click New Method in the ribbon. Creating a new method will automatically open the Method Editor. Methods created in this way are global methods and are accessible from all methods and form objects.

A sequence of commands associated with a button or menu item can be automatically converted to a new method by clicking Convert to New Method. Open the new method by clicking Go to Method. You can also create a method that is local to a form object by clicking Create Local Method. These options as shown in the figure below.

If a method already exists, say with the name method1, then you open the Method Editor using, for example, any of these ways:• In the application tree, double-click the method1 node.• In the application tree, right-click the method1 node and select Edit.• Below the command sequence in the Settings window of a form object or an

event, click Go to Method.

The Application Builder Desktop Environment | 13

Page 14: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 14 Tuesday, December 16, 2014 4:16 PM

Running an Application

With a COMSOL Multiphysics license, applications can be run from the COMSOL Desktop. With a COMSOL Server license, applications can be run in major web browsers on a variety of operating systems and hardware platforms. In addition, you can run applications by connecting to COMSOL Server with an easy-to-install COMSOL client for Windows.The following two sections explain how to run applications from the COMSOL Multiphysics and COMSOL Server environments.

Running an Application in COMSOL Multiphysics

In COMSOL Multiphysics, you run an application using any of these three ways:• Click Test Application in the Application tab in the ribbon.• Select Run Application from the File menu.• Double-click an MPHAPP-file.

TESTING AN APPLICATION

Test Application is available in the Application tab and is used for quick tests. It opens a separate window with the application user interface while keeping the Application Builder desktop environment running.

While testing an application, you can apply changes to forms and methods by clicking the Apply Changes button. To preview the layout of a form without running the application, click Preview Form in the ribbon of the Form Editor.When Test Application is used, all methods are automatically compiled with the built-in Java compiler. Any syntax errors will generate error messages and the process of testing the application will be stopped. To check for syntax errors before testing an application, click the Check Syntax button in the ribbon of the Method Editor.

14 | Running an Application

Page 15: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 15 Tuesday, December 16, 2014 4:16 PM

Check Syntax finds syntax errors by compiling the methods using the built-in Java compiler. Any syntax errors will, in this case, be displayed in the Errors and Warnings window in the Method Editor. For more information, see “The Method Editor” on page 86.

RUNNING AN APPLICATION

Run Application is available from the File menu and starts the application in the COMSOL Desktop environment. Select Run Application to use an application for production purposes. For example, you can run an application that was created by someone else that is password protected from editing, but not from running.

DOUBLE-CLICKING AN MPHAPP-FILE

When you double-click an MPHAPP-file, the application opens in COMSOL Multiphysics in the same way as if you started it from Run Application.

SAVING A RUNNING APPLICATION

When you run an application, it is assigned the name Untitled.mphapp and is a copy of the original MPHAPP-file. If you provide input to an application other than the default values, the application will typically change its state. Working with a copy allows you to save an application in the new state without accidentally overwriting the original file.By default, the user of an application will not be prompted to save changes when exiting the application. You control this behavior from the root node of the application. In the Application Builder section, select the check box Ask to save application when closing, as shown in the figure below.

As an alternative, you can add a button or menu item with a command to save the application. For more information, see page 60.

Running an Application | 15

Page 16: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 16 Tuesday, December 16, 2014 4:16 PM

APPLICATION LIBRARIES

From the File menu, select Application Libraries to run and explore the example applications that are included in the COMSOL installation. Many of the screenshots in this book are taken from these examples.

16 | Running an Application

Page 17: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 17 Tuesday, December 16, 2014 4:16 PM

You run an application or open it for editing by clicking the corresponding buttons below the Application Libraries tree.

Each application has an associated thumbnail image that is displayed in the Application Libraries. In the COMSOL Server web interface, the thumbnail image is displayed in the Application Library page.To set the thumbnail image, click the root node of the application tree. The Settings window has two options: Set from Graphics Window and Load from File.

Running an Application | 17

Page 18: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 18 Tuesday, December 16, 2014 4:16 PM

The Load from File option allows you to load images in the PNG- or JPG-file formats. The maximum image size is 280-by-210 pixels.

PASSWORD PROTECTION

An application can be password protected to manage permissions. You assign separate passwords for editing and running in the Settings window, accessible by

18 | Running an Application

Page 19: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 19 Tuesday, December 16, 2014 4:16 PM

clicking the root node in the Application Builder window. You must have permission to edit an application in order to create passwords for running it.

When you open a password-protected MPHAPP-file, for editing or running, a dialog box prompts you for the password:

To remove password protection, create an empty password.The password protection is used to encrypt all model and application settings, including methods. However, binary data such as the finalized geometry including embedded CAD files, mesh data, and solution data are not encrypted.

SECURITY SETTINGS

When creating an application with the Application Builder, it is important to consider the security of the computer hosting the application. Both COMSOL Multiphysics and COMSOL Server provide a very similar set of security settings for controlling whether or not an application should be allowed to perform external function calls, contain links to C libraries, run MATLAB functions, access external processes, etc.The security settings in COMSOL Multiphysics can be found in the Security page in the Preferences window accessed from the File menu. In COMSOL Server they are available in the Preferences page in the COMSOL Server web interface, if you are logged in as an administrator. If you are not sure what security settings to use, contact your systems administrator.

Running an Application | 19

Page 20: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 20 Tuesday, December 16, 2014 4:16 PM

Running an Application in COMSOL Server

COMSOL applications can be run by connecting to COMSOL Server from a web browser, or from a COMSOL Client for Windows. The COMSOL Client for Windows allows a user to run applications that require a LiveLink™ product for CAD, as described in “Running Applications in the COMSOL Client”. In addition, the COMSOL Client for Windowssupports interactive graphics in 1D, 2D, and 3D.Running applications in a web browser does not require any installation and no web browser plug-ins are needed. Running an application in a web browser supports interactive graphics in 3D using WebGL™ technology. However, selecting objects in graphics using mouse clicks in not supported when running in a web browser.

RUNNING APPLICATIONS IN A WEB BROWSER

Using a web browser you can point directly to the computer name and port number of a COMSOL Server web interface—for example, http://abc.mycorp.com:2036, assuming that port number 2036 is used by your COMSOL Server installation. You need to provide a username and password to log in using the web interface.

20 | Running an Application

Page 21: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 21 Tuesday, December 16, 2014 4:16 PM

When logged in, the Application Library page displays a list of applications to run.

Click Launch to run an application. Applications are run in separate tabs in the browser.

Limitations When Running Applications in Web BrowsersWhen you create applications for running in a web browser, make sure you use the grid layout mode in the Application Builder. This will ensure that the user

Running an Application | 21

Page 22: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 22 Tuesday, December 16, 2014 4:16 PM

interface layout adapts to the size and aspect ratio of the browser window. For low-resolution displays, make sure to test the user interface layout in the target platform to check that all form objects are visible. Applications that contain resizable graphics forms may not fit in low-resolution displays. In such cases, use graphics with fixed width and height to make sure all form objects fit in the target browser window.When running in a web browser, the interactive selection of domains, boundaries, edges, and points is not supported. The LiveLink™ products for CAD software packages are not supported when running in a web browser.When running COMSOL applications in web browsers for smartphones and certain tablets, not all functionality is supported. Typical limitations include the ability to play sounds or open documents. In addition, file upload and download may not be supported.

RUNNING APPLICATIONS IN THE COMSOL CLIENT

As an alternative to using a web browser for running applications, the COMSOL Client for Windows can be used to connect to COMSOL Server and run applications natively in the Windows operating system. This typically gives better graphics performance and supports interactive graphics in 1D, 2D, and 3D. In addition, the COMSOL Client for Windows allows running applications that require a LiveLink™ product for CAD.You can install the COMSOL Client together with COMSOL Server. You can also download the COMSOL Client from www.comsol.com/client-download.The following LiveLink™ products are available in the COMSOL Client, provided that the COMSOL Server you connect to has the required licenses:• LiveLink™ for AutoCAD

• LiveLink™ for Inventor (One Window interface not available)• LiveLink™ for PTC Creo Parametric™

• LiveLink™ for PTC Pro/ENGINEER

• LiveLink™ for Revit

• LiveLink™ for Solid Edge

22 | Running an Application

Page 23: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 23 Tuesday, December 16, 2014 4:16 PM

Before you can use the COMSOL Client to run applications, you must log in to the COMSOL Server web interface with a valid username and password.

Logging in from the COMSOL Client displays a COMSOL Server web interface identical to that seen when logging in from a web browser. Using the COMSOL Client, applications run as native Windows applications in separate windows that have a Windowslook-and-feel. For example, applications run in the COMSOL

Running an Application | 23

Page 24: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 24 Tuesday, December 16, 2014 4:16 PM

Client may have a Windows specific ribbon with tabs. When run in a web browser, ribbons are represented by a toolbar.

For more information on running applications with COMSOL Server, see the COMSOL Server Manual.

24 | Running an Application

Page 25: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 25 Tuesday, December 16, 2014 4:16 PM

Getting Started

STARTING FROM A COMSOL MULTIPHYSICS MODEL

To create an application based on a COMSOL Multiphysics model, you can use the Application Wizard. If you have a model (with file extension .mph) already loaded in the COMSOL Desktop, select File>Save As, and then choose Save As type: Application (with file extension.mphapp).

If you don’t have a model loaded, select File>New, and then click Application Wizard.

Getting Started | 25

Page 26: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 26 Tuesday, December 16, 2014 4:16 PM

Select a model (.mph) from the Model Libraries or click Browse to select from some other location, and then click Open.

A dialog box with progress bar appears with the text “Creating new application from model”.

When the progress bar has finished, the New Form window will open.

CREATING A NEW FORM

The New Form window aids in the initial user interface layout and is displayed by the Application Wizard after a model is saved or selected for use as an application. When a model is saved as an application, the New Form window is displayed as a dialog box. The New Form window assists you with adding the most common user interface components, so-called form objects, to the first form of your application. It has three tabs:• Inputs/outputs

26 | Getting Started

Page 27: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 27 Tuesday, December 16, 2014 4:16 PM

• Graphics• Buttons

Double-click a node or click the Move to Selected button to move a node from the Available area to the Selected area. The selected nodes will become form objects in the application, and a preview of the form is shown in the Preview area to the right. The size as well as other settings for form objects can be edited after exiting the Application Wizard.You can also choose to exit the New Form window at this stage by clicking Done.

The Inputs/Outputs TabThe Inputs/outputs tab displays the model tree nodes that can serve, by default, as an input field or a data display object. You can make other parts of the model available for input and output by using Model Data Access, see page 50. Input fields added by the Application Wizard will be accompanied by a text label and a unit, when applicable.

Getting Started | 27

Page 28: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 28 Tuesday, December 16, 2014 4:16 PM

In the figure below, three parameters Length, Width, and Applied voltage have been selected to serve as input fields:

In the figure below, corresponding to a different model, three Derived Values nodes have been selected to serve as data display objects:

After exiting the Application Wizard, you can edit the size and font color as well as other settings for input fields and data display objects.

28 | Getting Started

Page 29: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 29 Tuesday, December 16, 2014 4:16 PM

The Graphics TabThe Graphics tab displays the model tree nodes that can serve as graphics objects: Geometry, Selection, Mesh, and Results. In the figure below, two such nodes have been selected.

The Buttons TabThe Buttons tab displays the model tree nodes that can be run by clicking a button in the application user interface. Examples of such model tree nodes are Plot Geometry, Plot Mesh, Compute Study, and each of the different plot groups under Results. In addition, you can add buttons for GUI commands.

Getting Started | 29

Page 30: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 30 Tuesday, December 16, 2014 4:16 PM

In the figure below, three buttons have been added for plotting the Geometry, plotting the Mesh, and running a Study.

Using the Form Editor, you can add buttons that run your own custom command sequences or methods.

EXITING THE APPLICATION WIZARD

Click Done to exit the Application Wizard. This automatically opens the Application Builder desktop environment.

SAVING AN APPLICATION

To save an application, from the File Menu, select File>Save As. Browse to a folder where you have write permissions, and save the file in the MPHAPP-file format. The MPHAPP-file contains all the information about the model and the application.

30 | Getting Started

Page 31: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 31 Tuesday, December 16, 2014 4:16 PM

The Form Editor

Use the Form Editor for user interface layout to create forms with form objects such as input fields, graphics, buttons, and more.

The Forms Settings Window

The Settings window for forms is displayed when you click the Forms node in the application tree. It lets you change the overall appearance of forms with settings for text color, background color, and font.

The default is that all new forms inherit these settings.

The Individual Form Settings Windows

Each form has its own Settings window with sections for:• a Name used to reference the form in other form objects and methods• a form Title that is used in applications with several forms• when to store changes in dialog boxes (Store changes in dialogs)• the Initial size of the form when used as a dialog box or when the Main

Window is set to have its size determined by the form• whether the form should be Resizable when used as a dialog box• the Margins with respect to the upper-left corner• viewing sections in expanded or collapsed format (Section Settings)

The Form Editor | 31

Page 32: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 32 Tuesday, December 16, 2014 4:16 PM

• a table of all form objects included in the form (Grid Layout for Contained Form Objects)

• the appearance with settings for text color and background color• events that are triggered when a form is loaded or closed

Double-click a form node to open its window in the Form Editor. Right-click a form window tab to see its context menu with options for closing, floating, and tiling form windows.

32 | The Form Editor

Page 33: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 33 Tuesday, December 16, 2014 4:16 PM

SKETCH AND GRID LAYOUT MODES

The Application Builder defaults to sketch layout mode, which lets you use fixed object positions and size. The instructions in this section “The Form Editor” assume that the Form Editor is in sketch layout mode. For information on grid layout mode, see “Appendix B—Grid Layout Mode” on page 179.

Form Editor Preferences

To access Preferences for the Form Editor, choose File>Preferences and select the Form Editor section.

The Form Editor section in Preferences includes settings for changing the defaults for Layout mode, Margins, and Sketch grid.

The Form Editor | 33

Page 34: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 34 Tuesday, December 16, 2014 4:16 PM

Form Objects

POSITIONING FORM OBJECTS

You can easily change the positioning of form objects such as input fields, graphics objects, and buttons:• Click an object to select it. A selected object is highlighted with a blue

frame.• To select multiple objects, use Ctrl+click. You

can also click and drag to create a selection box in the form window to select all objects within it.

• Hold and drag to move to the new position. Blue guidelines will aid in the positioning relative to other objects.

• You can also use the keyboard arrow keys to move objects. Use Ctrl+arrow keys to fine-tune the position.

In the figures below, a Plot button has been moved from its original position to the top of the form. Blue guide lines show its alignment relative to one of the input fields and the Plot button.

34 | The Form Editor

Page 35: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 35 Tuesday, December 16, 2014 4:16 PM

RESIZING FORM OBJECTS

To resize an object:• Click an object to select it.• Hold and drag one of the handles, shown as blue dots, of the highlighted

blue frame. If there are no handles, then this type of form object cannot be resized interactively. However, the size can be controlled from its Settings window.

DELETING AN OBJECT

To delete an object, click to select it and then press Delete on your keyboard. You can also click the delete button in the quick access toolbar.

ADJUSTING SIZE AND POSITION BY THE NUMBER OF PIXELS

You can adjust the position and size of an object by typing the number of pixels in the Position and Size section of its Settings window:• Click an object to select it. Make sure its Settings window is shown. If not,

double-click the object or click the Settings button in the Form tab.• Edit the numbers in the Position and Size section. These values correspond

to the number of pixels for the width and height in addition to the x- and y-position. The position is relative to the upper-left corner of a form.

CHANGING THE APPEARANCE OF DISPLAYED TEXT

For form objects that display text, the Appearance section in the Settings window lets you change properties such as the text displayed, its font, font color, and font size. For some form objects such as a button, the size of the object will adapt to the length of the text string.

The Form Editor | 35

Page 36: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 36 Tuesday, December 16, 2014 4:16 PM

In the figure below, the Settings window for a text label object is shown where the font size and color is changed.

THE NAME OF A FORM OBJECT

A form object has a name, which is a text string without spaces. This name is used in other form objects and methods to reference the object.

36 | The Form Editor

Page 37: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 37 Tuesday, December 16, 2014 4:16 PM

INSERTING FORM OBJECTS

You can insert form objects in addition to those created by the Application Wizard. In the Form ribbon tab, select the Insert Object menu to see a listing of all available objects.

The remainder of this section “The Form Editor” describes only the types of form objects that are added by the Application Wizard. For information on the other form objects, see “Appendix A—Form Objects” on page 116. The form objects added by the Application Wizard may include: • Button• Graphics• Input Field• Text Label• Unit• Data Display

EVENTS AND ACTIONS ASSOCIATED WITH FORM OBJECTS

You can associate objects such as buttons, menu items, ribbon buttons, forms, and form objects with actions triggered by an event. An action can be a sequence of commands including global methods or local methods. Local methods are not accessible or visible outside of the object where it is defined. The events that can be associated with an object depend on the type of object and include: button

The Form Editor | 37

Page 38: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 38 Tuesday, December 16, 2014 4:16 PM

click, keyboard shortcut, load of a form (On load), and change of the value of a variable (On data change).Using Ctrl+Alt+click on the form object opens the method in the method editor. If there is no method associated with the form object, a new local method will be created, associated with the form object, and opened in the method editor.

Button

A button is triggered by a button click event. The main section of the Settings window for a button allows you to:• Edit the form object name of the button.• Edit the text displayed on the button.• Use a picture instead of the default rendering of a button.• Change the button size from normal to large.• Add a tooltip with a text that is shown when hovering over the button.• Add a keyboard shortcut by clicking the input field and enter a combination

of the modifier keys Shift, Ctrl, and Alt together with a keyboard key. Alt must be accompanied by at least one additional modifier.

38 | The Form Editor

Page 39: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 39 Tuesday, December 16, 2014 4:16 PM

CHOOSING COMMANDS TO RUN

The section Choose Commands to Run lets you control the action associated with a button click event. The figure below shows the Settings window for a button that triggers a sequence of four commands.

To define a sequence of commands, in the Choose Commands to Run section, select a node in the model tree. Then click one of the highlighted buttons under the tree or right-click and select the command. In the figure below, the Geometry node is selected and the available commands Run and Plot are highlighted. Click Run to add a geometry-building command to the command sequence. Click Plot to add a command that plots the geometry.

The Form Editor | 39

Page 40: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 40 Tuesday, December 16, 2014 4:16 PM

The command icons highlighted for selection are the ones that are applicable to the particular model tree node. This is a list of the command icons that may be available, depending upon the node:• Run• Plot• Set Value• Show• Show as Dialog• Enable• Disable

Some commands require an argument, such as the different plot commands. The argument to a plot command defines which of the different graphics objects the plot should be rendered in.The example below shows the Settings window and command sequence for a Compute button as created by the Application Wizard. This button has a command sequence with two commands: Compute Study 1 and Plot Temperature.

40 | The Form Editor

Page 41: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 41 Tuesday, December 16, 2014 4:16 PM

The Plot Temperature command has one argument graphics1. To add or edit an input argument, click the Edit Argument button below the command sequence, as shown in the figure below.

To reference graphics objects in a specific form, the following syntax is used: /form1/graphics2, /form3/graphics1, etc.To control the order and contents of the sequence of commands, use the Move Up, Move Down, and Delete buttons located below the command sequence table.

SHOWING A FORM AS A DIALOG BOX

A button can also be used to open a new form. This can be done in two ways. The first is to use the Show command, which will leave the current form and display the new form. The second is to use the Show as Dialog command. In this case, the new form will pop up as a dialog box over the current form, and will usually be requesting some input from the user.In order to use the Show as Dialog command, begin with the Choose Commands to Run section and select the form that you would like to show. The figure below

The Form Editor | 41

Page 42: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 42 Tuesday, December 16, 2014 4:16 PM

shows an example of the settings for a button with the command Show form2 as dialog.

With these settings, clicking the button in the application will launch the following dialog box corresponding to form2:

42 | The Form Editor

Page 43: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 43 Tuesday, December 16, 2014 4:16 PM

The form2 window in this example contains a text label object and an OK button, as shown in the figure below.

In the Settings window, the Dialog Actions section has two check boxes:• Close dialog• Store changes

In the example above, the Close dialog check box is selected. This ensures that the form2 window is closed when the OK button is clicked. Since form2 does not have any user inputs, there is no need to select the Store changes check box.Typical dialog box buttons and their associated Dialog Actions are:

A dialog box blocks any other user interaction with the application until it is closed. This means that in a form that is used as a dialog box you need to create at least one button with the Close dialog check box selected.In order to control when data entered in a dialog box is stored, there is a list in the top section of the Settings window of a form where you can select whether to

BUTTON DIALOG ACTIONS

OK Close dialog and Store changes

Cancel Close dialog

Apply Store changes

The Form Editor | 43

Page 44: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 44 Tuesday, December 16, 2014 4:16 PM

store data On request or Immediately when the change occurs, as shown in the figure below.

CHANGING WHICH FORM IS VISIBLE

In the section Choose Commands to Run, in addition to the Show as dialog command you can select the Show command. The figure below shows the command sequence for a button with a command Show form3.

This command will leave the form associated with the button and make the specified form visible to the user.

Graphics

Each graphics object gets a default name such as graphics1, graphics2, etc., when it is created. These names are used to reference graphics objects in command sequences for buttons, menu items, and in methods. To reference graphics objects in a specific form, use the syntax: /form1/graphics2, /form3/graphics1, etc.

44 | The Form Editor

Page 45: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 45 Tuesday, December 16, 2014 4:16 PM

SELECTING THE SOURCE FOR INITIAL GRAPHICS CONTENT

In the Settings window for a graphics object, use the section Source for Initial Graphics Content to set the plot group to be displayed as default. If a solution exists for the displayed plot group, then the corresponding solution will be visualized when the application is started. The figure below shows the Settings window for a graphics object with a Temperature plot selected as the source.

In addition to Results plot nodes, you can also use Selection, Geometry, and Mesh nodes as the Selected source.

APPEARANCE

For a graphics object, the Appearance section of the Settings window allows you to:• include the standard plot toolbar• include an icon such as a logo image in the upper-right corner

The Form Editor | 45

Page 46: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 46 Tuesday, December 16, 2014 4:16 PM

• set the background color for 2D plots• set a flat or graded background color for 3D plots

The figure below shows an application where the background Top color is set to white and the Bottom color to gray. In addition, the standard plot toolbar is not included.

AUTOMATIC RESIZING OF GRAPHICS OBJECT

In order to make the graphics object of an application resizable, follow these steps:• Change the layout mode of the form containing the graphics object from

sketch mode to grid mode. This will result in the form being divided into a number of intersecting rows and columns, with at most one form object at each intersection. For more information on using the grid layout mode, see “Appendix B—Grid Layout Mode” on page 179.

• Change the Height for any row covering the graphics object to Grow. To change this, click the leftmost column of the row you would like to access.

46 | The Form Editor

Page 47: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 47 Tuesday, December 16, 2014 4:16 PM

• Change the Width for any column covering the graphics object to Grow. To change this, click the uppermost row of the column you would like to access.

• Select the graphics object and change both the Horizontal alignment and Vertical alignment to Fill.

Following the steps above, you may find it easier to make graphics objects resizable by performing grid layout mode operations such as adding empty rows and columns as well as merging cells.

Input Field

An input field allows a user to change the value of a parameter or variable. In the Application Wizard, when a parameter or variable is selected, three form objects are created:• a text label object for the parameter or variable description• an input field object for the value• a unit object (if applicable) that carries the unit of measure

To insert an additional input field, use the Insert Object menu in the ribbon Form tab and select Input Field. In the Form Editor, you link it to a certain parameter or variable by selecting it from the model tree in the Source section. In the Source

The Form Editor | 47

Page 48: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 48 Tuesday, December 16, 2014 4:16 PM

section of the Input Field settings, you can also set an Initial value. The figure below shows the Settings window for an input field.

The default setting for the Initial value is From data source. If the Editable check box is cleared, then the Initial value will be displayed by the application and cannot be changed.

48 | The Form Editor

Page 49: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 49 Tuesday, December 16, 2014 4:16 PM

DATA VALIDATION

The Data Validation section of the Settings window for an input field allows you to validate user inputs with respect to units and values.

The Unit dimension check list has the following options:• None• Compatible with physical quantity• Compatible with unit expression• Append unit to number

A value or expression can be highlighted in orange to provide a warning when the user of an application enters an incompatible unit (a unit of measure that cannot be converted to the units specified in the Data Validation settings). Enable this feature by selecting Compatible with physical quantity or Compatible with unit expression. In addition, the user will see a tooltip explaining the unit mismatch, as shown in the figure below.

If there is a unit mismatch, and if no further error control is performed by the application, the numeric value of the entered expression will be converted to the default unit. In the above figure, 9[kg] will be converted to 9[m].The Append unit to number option allows the user to type in a number without manually appending a unit using the COMSOL square bracket [] unit syntax.The None option does not provide unit validation.

The Form Editor | 49

Page 50: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 50 Tuesday, December 16, 2014 4:16 PM

The options Append unit to number and None allow you to use a filter to validate the input. The Filter list for the option None has the following options:• None• Double• Integer• Regular expression

The Filter list for the option Append unit to number only allows for the Double and Integer options.The Double and Integer options filter the input based on a Minimum and Maximum value. If the input is outside of these values, an error message is displayed.The Regular expression option allows you to use a regular expression for matching the input string and to also generate a custom Error message. For more information on regular expressions, see the dynamic help. Click the help icon in the upper-right corner of a window and search for “regular expression”.When creating an input field using the Application Wizard, or when creating new form, the program tries to create an input field with the setting Append unit to number if possible. This setting expects a user to enter a number in the corresponding edit field. However, it can also handle a number followed by the exact unit that is appended. For example, if the unit is mm, then 1[mm] is allowed but 1[m] is not. A parameter that has the expression 1.23[mm], will therefore get the appended unit mm and the default value in the edit field will 1.23.

MODEL DATA ACCESS

There are many properties in the model tree that are not made available by default. The reason for this is that a model typically contains hundreds or even thousands of properties, and the full list would be unwieldy. However, these “hidden” properties may be made available to your application by a technique called Model Data Access.To access individual properties of a model tree node, click the Model Data Access button in the Declarations segment of the Applications ribbon tab of the Application Builder.

50 | The Form Editor

Page 51: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 51 Tuesday, December 16, 2014 4:16 PM

Then, when you click on a model tree node, check boxes appear next to the individual settings. In the figure below, the check box for the Electric potential boundary condition is selected:

The Form Editor | 51

Page 52: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 52 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window for an Input Field. The list of possible Sources for this field now contains the Electric Potential.

Model Data Access can also be used for Data Display, Combo Boxes, and other form objects.

Unit

In the Settings window for a unit object you can set the unit to a fixed string, or link it to an input field. Click the Go to Source button to the right of the Unit

52 | The Form Editor

Page 53: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 53 Tuesday, December 16, 2014 4:16 PM

Label list to show, in the form, the input field object it is linked to. The figure below shows the Settings window for a unit object.

When adding an input field using the Application Wizard a unit object is automatically added when applicable. By default, the unit is displayed using Unicode rendering. As an alternative, you can use LaTeX rendering by selecting the LaTeX markup check box. Then the display of units will not depend upon the selected font.

Text Label

A text label object simply displays text in a form. When adding an input field using the Application Wizard a text label object is automatically added for the description text of the associated parameter or variable. There is a check box

The Form Editor | 53

Page 54: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 54 Tuesday, December 16, 2014 4:16 PM

allowing for Multiline text. If selected, the Wrap text check box is enabled. The figure below shows the Settings window for a text label object.

To insert an additional text label, use the Insert Object menu in the ribbon Form tab and select Text Label.

Data Display

A data display object is used to display the output from a Derived Values node such as a Global Evaluation or a Volume Maximum. In the Application Wizard, when a Derived Values node is selected, two form objects are created based on the corresponding Derived Values node variable:• a text label object for the description of the variable• a data display object for the value of the variable

The settings for these form objects can subsequently be edited. To insert additional data display objects, use the Insert Object menu in the ribbon Form tab and select Data Display.

54 | The Form Editor

Page 55: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 55 Tuesday, December 16, 2014 4:16 PM

In the Settings window for a data display object, select a node in the model tree or an application declared variable in the Source section. Then click the Use as Source button below.The Number Format section lets you set the number of digits for the precision, notation, and exponent.The figure below shows an example with data display objects for the variables Coil Resistance and Coil Inductance. A formatted unit label is automatically displayed as part of the object if applicable.

By default, the unit of a data display object is displayed using Unicode rendering. As an alternative, you can use LaTeX rendering by selecting the LaTeX markup check box. Then the data display does not rely on the selected font.

The Form Editor | 55

Page 56: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 56 Tuesday, December 16, 2014 4:16 PM

The Main Window

In the application tree, the Main Window node represents the main window of an application and is also the top-level node for the user interface. It contains the window layout, the main menu specification, and an optional ribbon specification.

GENERAL SETTINGS

The Settings window contains a General section with settings for the:• Title• Icon• Menu type• Status bar

The Title is the text at the top of the Main Window in an application, with the Icon shown to the left of this text. By default, it is the same as the title of the model used to create the application.In the Icon field, select an image from the library or add an image (*.png) from the local file system to the library and use it as an icon. If you add a new image, it will be added to the image library and thereby embedded into the application.The Main Window node of the application tree has one child node, named the Menu Bar. Using the Menu type setting of the Main Window, you can change this child node from Menu Bar to Ribbon.

56 | The Main Window

Page 57: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 57 Tuesday, December 16, 2014 4:16 PM

The Status bar list controls what is shown in the status bar: Select Progress for displaying a progress bar when applicable (the default) or None.

FORM REFERENCE SETTINGS

The Form Reference section contains a reference to the form that the main window displays. This setting is important when using a form collection because it determines which form is displayed as the Main Window when the application is opened the first time.

INITIAL SIZE SETTINGS

The initial size setting determines the size of the main window when the application is first started. There are four options:• Automatic: adapt the main window size to the form objects used in the form

specified by the Form Reference setting• Maximized• Use main form’s size• Manual: allow for entering the pixel size for the width and height

Menu Bar and Toolbar

The Menu Bar node can have Menu child nodes that represent menus at the top level of the Main Window.

The Main Window | 57

Page 58: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 58 Tuesday, December 16, 2014 4:16 PM

For the Menu Bar option, you can also add a Toolbar. The Toolbar node and the Menu nodes have the same type of child nodes.

MENU, ITEM, AND SEPARATOR

The child nodes of the Menu and Toolbar nodes can be of type Menu, Item, or Separator, as shown in the figure below:

A Menu node has settings for Name and Title.

A Menu node can have child Menu nodes that represent submenus.

58 | The Main Window

Page 59: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 59 Tuesday, December 16, 2014 4:16 PM

A Separator displays a horizontal line between groups of menus and items and has no settings.The settings window for an Item node is similar to that of a button and contains a sequence of commands. Just like a button, an item can have an associated keyboard shortcut. For more information, see “Button” on page 38. The figure below shows the Settings window for an Item associated with a method for creating a report.

The Main Window | 59

Page 60: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 60 Tuesday, December 16, 2014 4:16 PM

The figure below shows an example of an application with a file menu.

The Settings window for the Save As item is shown in the figure below.

60 | The Main Window

Page 61: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 61 Tuesday, December 16, 2014 4:16 PM

When running an application in the COMSOL Desktop environment, a Close Application menu item is always present.

Ribbon

You can opt to add a Ribbon to the Main Window instead of a Menu Bar. The Ribbon node contains the specifications of a ribbon with toolbars placed on one or several tabs. For the Ribbon option, a File menu is made available directly under the Main Window node.

RIBBON TAB AND RIBBON SECTION

Child nodes to the Ribbon are of the type Ribbon Tab. Child nodes to a Ribbon Tab are of the type Ribbon Section. Child nodes to a Ribbon Section can be of the type Item, Menu or Separator. Item and Menu provide the same functionality as described previously for the Menu Bar and Toolbar. A Separator added as a child to a Ribbon Section is a vertical line that separates groups of Items and Menus in the running application. A Separator is displayed as a horizontal line in the application tree. The figure below shows an example.

The Main Window | 61

Page 62: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 62 Tuesday, December 16, 2014 4:16 PM

Events

An Event is any activity (a button click, a keyboard shortcut, the loading of a form, or a changing of the value of a variable) that signals a need for the application to carry out one or more actions. Each action can be a sequence of commands of the type described earlier, but may also include the execution of methods. The methods themselves may be local methods, associated with particular form objects, or global methods that can be initiated from anywhere in the application. The global methods are listed in the Method node of the application tree. The local methods are defined in the Settings windows of the forms or form objects with which they are associated. When a form object has an associated method, it may be opened for editing by performing a Ctrl+Alt+click of the icon of the object. If the Ctrl+Alt+click is performed on a form object that has no method, then a new local method will be created, associated with the object, and opened for editing.The Events that initiate these actions may also be global or local. The global events are listed in the Events node of the application tree, and include all events that are triggered by changes to the various data entities, such as global parameters or string variables. Global events can also be associated with the startup and shutdown of the application. The local events, like local objects, are defined in the Settings windows of the forms or form objects with which they are associated.

Events at Startup and Shutdown

Global or local methods can be associated with the events of startup (On startup) and shutdown (On shutdown) of an application. To access these events, click the Events node in the application tree.

62 | Events

Page 63: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 63 Tuesday, December 16, 2014 4:16 PM

A shutdown event is triggered when:• the user of an application closes the application window by clicking the

Close icon in the upper-right corner of the application window• the Exit Application command is issued by a Form Object• a method is run using the command exit()

A method run at a shutdown event can, for example, automatically save critical data or prompt the user to save data.

Global Events

Right-click the Events node and choose Event to add an event to an application. An event listens for a change in a running application. If a change occurs, it runs a sequence of commands. You can, for example, use events to control which

Events | 63

Page 64: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 64 Tuesday, December 16, 2014 4:16 PM

method to run when the value of a combo box is changed by the user. In the figure below, when the value of the string variable svar is changed, method1 is run.

Note that since this type of event has global scope and is not associated with a particular form, the full path: /form1/graphics1 needs to be used when referencing graphics objects.The following two sections describe the options available in the Event Settings window.

SOURCE FOR DATA CHANGE EVENT

This section presents a filtered view of the tree from the Application Builder window. The nodes represent some sort of data or have children that do.Typically, you will only see available parameters, variables, and data nodes underneath their respective branches in the application tree. You can find these nodes underneath the Events branch in the Application Builder.

64 | Events

Page 65: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 65 Tuesday, December 16, 2014 4:16 PM

You can extend the list of available data nodes by clicking on the Model Data Access button in the Application ribbon tab. For more information, see “Model Data Access” on page 92.

CHOOSE COMMANDS TO RUN

This section is similar to that of a button and enables you to define a sequence of commands. For more information, see “Button” on page 38.

Form and Form Object Events

Form and form object events are similar to global events but are defined for forms or individual form objects. These events have no associated list of commands but refer directly to one global or local method.

EVENTS TRIGGERED BY DATA CHANGE

For certain types of form objects, you can specify a method to run when data is changed. This setting is available in the Events section for the form object, as shown in the figure below.

The drop-down list On data change contains None (the default), any available methods under the Methods node of the application tree, and optionally a local method.The form objects supporting this type of event are:• input field• check box• combo box• file import• radio button• text• list box• slider

Buttons have associated button click events. The corresponding action is a command sequence defined in the Settings window of a button object. For more information on button command sequences, see “Button” on page 38.

Events | 65

Page 66: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 66 Tuesday, December 16, 2014 4:16 PM

EVENTS TRIGGERED BY LOADING OR CLOSING A FORM

Forms can run methods when they are loaded (On load) or closed (On close).

This type of event is available in the Settings window of a form and is typically used when a form is shown as a dialog box or to activate forms used as panes in a form collection.

Using Local Methods

Events can call local methods that are not displayed in the application tree. For more information on local methods, see “Local Methods” on page 102.

66 | Events

Page 67: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 67 Tuesday, December 16, 2014 4:16 PM

Declarations

The Declarations node in the application tree is used to declare global variables in addition to the global parameters and variables already defined in the model. Variables defined under the Declarations node are used in form objects and methods. In form objects, they store values to be used by other form objects or by methods. Variables that are not passed between form objects and methods, but that are internal to methods, do not need to be declared in the Declarations node. In methods, variables defined under the Declarations node have global scope and can be used directly with their name.There are five different types of declarations:• Scalar• Array 1D• Array 2D• Choice List• File

Right-click the Declarations node to access these.

In addition, the first three types of declarations can be of the following types:• String• Boolean• Integer• Double

Scalar

Scalar declarations are used to define variables to be used as strings, Booleans, integers, or doubles. A scalar variable in the Declarations node is similar to a global

Declarations | 67

Page 68: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 68 Tuesday, December 16, 2014 4:16 PM

parameter or variable in a model. You can use a string variable to represent a double, integer, or Boolean by using conversion functions in a method; for more information see “Conversion Methods” on page 187.

STRING

You can use a string variable as a Source in many of the form objects, such as input fields, combo boxes, and check boxes. A global parameter or variable in a model has the restriction that its value has to be a valid model expression, while a scalar string variable has no such restriction.The figure below shows the Settings window in Declaration for two string variables svar_results and svar_mat.

String declarations, as well as other declarations, can be loaded and saved from/to file by using the Load from File and Save to File buttons.

68 | Declarations

Page 69: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 69 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window for a combo box with the string variable svar_mat used as the source.

Declarations | 69

Page 70: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 70 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window for a string variable distance used as the source for an input field.

The Default value of an input field will overwrite a Default value in a declaration.

70 | Declarations

Page 71: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 71 Tuesday, December 16, 2014 4:16 PM

BOOLEAN

You can use a Boolean variable as a source in check boxes, other form objects, and methods. A Boolean variable can have two states: true or false. The default value is false.

Example CodeIn the example code below, the boolean variable bvar has its value controlled by a check box. If bvar is true then plot group 4 (pg4) is plotted in graphics1. Otherwise, plot group 1 (pg1) is plotted.

if (bvar) { useGraphics(model.result("pg4"),"graphics1");} else { useGraphics(model.result("pg1"),"graphics1");}

Declarations | 71

Page 72: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 72 Tuesday, December 16, 2014 4:16 PM

INTEGER AND DOUBLE

Integer and double variables are similar to strings, with the additional requirement that the value is an integer or double, respectively.

For an input field linked to an integer variable, an error message is displayed if the entered value is not an integer:

Array 1D

The Array 1D node declares one or more named arrays of strings, Booleans, integers, or doubles that you can access from form objects and methods. The number of elements in a 1D array is not restricted in any way, and you can, for example, use a 1D array to store a column in a table with variable number of rows. The Settings window contains a single table, where you specify one variable array

72 | Declarations

Page 73: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 73 Tuesday, December 16, 2014 4:16 PM

per row. In the figure below, two double arrays are declared, xcoords and ycoords.

The New element value is assigned to new elements of the array in certain add operations, such as when adding a row to a table. Arrays for strings, Booleans, and integers are functionally similar to arrays of doubles.

INITIAL VALUES

The initial value can be a 1D array of arbitrary length. To edit the initial values, click the Edit Default button below the List of Variables. This opens a dialog box for typing the value of each component. See the figure below for an example of a 1D array of integers.

Declarations | 73

Page 74: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 74 Tuesday, December 16, 2014 4:16 PM

ARRAY SYNTAX

An array definition must start and end with curly braces ({ and }), and you separate each element with a comma. In cases where you need special characters inside an array element (spaces and commas, for example), surround the element with single quotes ('). The table below shows a few examples of 1D arrays:

Array 2D

The Array 2D node declares one or more 2D arrays that you can access from form objects and methods. In the figure below, a 2D double array xycoords is declared.

ARRAY SYNTAX RESULTING ARRAY

{1, 2, 3} A 3-element array with the elements 1, 2, and 3

{} An empty array

{'one, two', 'three by four'} A 2-element array with elements containing special characters

{{1, 2, 3},{'one, two', 'three by four'}}

A 2-element array containing one 3-element array and one 2-element array

74 | Declarations

Page 75: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 75 Tuesday, December 16, 2014 4:16 PM

INITIAL VALUES

The default (or initial) value can be a 2D array of arbitrary size. To edit the initial values, click the Edit Default button below the List of Variables. This opens a dialog box for typing the value of each component; see the figure below.

ARRAY SYNTAX

The table below shows a few examples of 2D arrays:

For 2D arrays, rows correspond to the first index so that {{1,2,3},{4,5,6}} is equivalent to the matrix:

1 2 3

4 5 6

Choice List

The Choice List node contains lists that can be used by combo boxes, radio buttons or list boxes. The Settings window for a choice list contains a Name field and a table with a Value column and a Display name column. Enter the property value in the first column and the corresponding text to display to the user (for example, in a combo box list) in the second column. The value is always

ARRAY SYNTAX RESULTING ARRAY

{{}} An empty 3D array

{{"5","6"},{"7","8"}} A 2-by-2 matrix of strings

{{1, 2, 3}, {4, 5, 6}} A 2-by-3 matrix of doubles

Declarations | 75

Page 76: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 76 Tuesday, December 16, 2014 4:16 PM

interpreted as a string. In the example below, mat1 will become the string “mat1” when returned from the combo box.

As an alternative to creating a choice list by right-clicking the Declarations node, you can click the Add New Choice List button in the Settings window for form objects that use such a list, as shown in the figure below.

ACTIVATION CONDITION

You can right-click the Choice List node to add an Activation Condition subnode. Use an activation condition to switch between two or more choice lists contingent on the value of a variable. For an example of using choice lists with activation conditions, see “Using a Combo Box to Change Material” on page 126.

76 | Declarations

Page 77: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 77 Tuesday, December 16, 2014 4:16 PM

File

An entry under the File declaration node is typically used by a File Import object, although it can also be used in methods. The figure below shows the Settings window of a file declaration.

A file import object allows the user of an application to browse and select a file to be uploaded to the application at run time. The file chosen by the user can be referenced in a method by using the syntax upload:///file1, upload:///file2, etc. This syntax can also be used in any file browser text fields within a model. The figure below shows a file reference used in the Filename field of the Import model tree node for a model using geometry import.

For an example of using a file import object, see “File Import” on page 151.

Declarations | 77

Page 78: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 78 Tuesday, December 16, 2014 4:16 PM

FILE SCHEME SYNTAX

In addition to the file scheme syntax upload:///, there are file schemes for temporary files and shared files. The table below summarizes all available file schemes.

For more information on how to use the embedded:/// syntax, see “Libraries” on page 112.The table below summarizes the usage of the different file schemes. In the table, a check mark means that this scheme is available, and (r) means that it is the recommended scheme.

SCHEME REFERS TO DEFAULT PATH TYPICAL USAGE

embedded:/// Files embedded in the application using Libraries>Files

N/A Experimental data, CAD files, mesh files, interpolation data

upload:/// Files to be uploaded by the user at run time

Determined by the Target directory in the Settings window of the File declaration

Experimental data, CAD files, mesh files, interpolation data

temp:/// Files in a random temporary directory, which is unique for each started application instance. These files are deleted when the application is closed.

A random subdirectory to the folder for temporary files, as determined by the settings in Preferences>Files

Temporary files produced by command sequences or methods, or output to be saved on the client (for use with COMSOL Server)

user:/// Files in a directory shared by all applications for the current user

Determined by the settings in Preferences>Files

Output from methods to be saved between sessions

common:/// Files in a directory shared by all users

Determined by the settings in Preferences>Files

Files shared between many users or applications

USAGE EMBEDDED UPLOAD TEMP USER COMMON

File is used as input (r) File is output (r) Method reading a file (r) Method writing a file (r) File is client-side (r)

78 | Declarations

Page 79: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 79 Tuesday, December 16, 2014 4:16 PM

You can set the preferences for the paths to temporary, user, and common files in the Files page of File>Preferences, as shown in the figure below.

LETTING THE USER SAVE A FILE CREATED BY AN APPLICATION

Some applications may need to produce temporary files. This is required, for example, when you save a report or an output plot. In that case, operation proceeds in two steps. In the first step, the application produces the report or plot and saves it to a temporary file in the temp:/// file scheme. In the second step, the temporary file is saved to a disc file.To carry out this process, you first choose the node that is creating the report or plot and use the Output section of the settings to place the output in a temporary file. In the example below, an Export>Plot node is created in the model tree to

Declarations | 79

Page 80: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 80 Tuesday, December 16, 2014 4:16 PM

create an output plot. The Settings window for this node is shown below, with the Filename in its Output section set to temp:///lineplot.txt.

To save the plot to disc in this example, a button is created. In the Settings for the button, in the section Choose Commands to Run, the first item is to create the output graph file by choosing the Export>Plot node created above and clicking on Run. The second item is created by choosing GUI Commands>File Commands>Save File As and clicking on Run again. In the Output section of the

80 | Declarations

Page 81: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 81 Tuesday, December 16, 2014 4:16 PM

button Settings, set the filename to the name of the temporary file created by the Export Plot command, in this case temp:///lineplot.txt.

As an alternative to using the temp:/// scheme, you can select the Always ask for filename check box in the settings of the Export>Plot node of the Model Tree. This allows you to leave the Filename field of that node blank and also skip the call

Declarations | 81

Page 82: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 82 Tuesday, December 16, 2014 4:16 PM

to Save File As in the button command sequence as shown in the figure below.

The Save File As command provides a dedicated Edit Argument dialog box with easy access to all embedded files as well as shortcuts for all file schemes.

CREATING REPORTS

The example below shows an application where a report in the Microsoft Word format (.docx) can be saved by the user. The figure below shows the Parameter

82 | Declarations

Page 83: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 83 Tuesday, December 16, 2014 4:16 PM

Estimation tab in the ribbon of the application. In this tab, there is a Create button in the Report section.

The associated application tree node is shown in the figure below.

Declarations | 83

Page 84: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 84 Tuesday, December 16, 2014 4:16 PM

The following figure shows how the syntax temp://file was used in the Filename field in the Settings window of the Report node.

In this application, the check box Open finished report is selected, which means that the Word document will open after the report has been created. The user of the application can then save the report from the Word file menu.In this example, the file schemes user:/// or common:/// could have been used in the same way if the check boxes Always ask for filename or Open finished report had not been selected. The user and common file schemes are primarily useful when the same files are used repeatedly by an application.

84 | Declarations

Page 85: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 85 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window of the Create ribbon item.

The method b_report contains the following code:if(length(results_status)>0){ alert("New input data. Compute to update results first.");} else { model.result().report("rpt1").run();}

The file scheme syntax can also be used directly in methods. The code below is from a method used to export an HTML report.

String answerh = request("Enter file name","File Name", "Untitled.html");if(answerh != null){ model.result().report("rpt1").set("format","html"); model.result().report("rpt1").set("filename","user:///"+answerh); model.result().report("rpt1").run();}

Declarations | 85

Page 86: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 86 Tuesday, December 16, 2014 4:16 PM

The Method Editor

Use the Method Editor to write code for actions not included among the standard run commands of the model tree nodes. The methods may, for example, execute loops, process inputs and outputs, and send messages and alerts to the user of the application.The Java programming language is used to write COMSOL methods, which means that all Java syntax and Java libraries can be used. In addition to the Java libraries, the Application Builder has its own built-in library for building applications and modifying the model object. (The model object is the data structure that stores the state of the underlying COMSOL Multiphysics that is embedded in the application.) More information on these functions may be found in “Appendix D—Built-in Method Library” on page 186.Methods can be global or local. Global methods are displayed in the application tree and are accessible from all methods and form objects. A local method is associated with a form object or event and can be opened from that form object’s or event’s Settings window. For more information on local methods, see “Local Methods” on page 102.

A number of tools and resources are available to help you create code for methods. These are covered in the following sections and will make you more productive by allowing you to copy-paste or auto-generate blocks of code.

Converting a Command Sequence to a Method

In the Form Editor, click the Convert to New Method button below a previously created command sequence of a Settings window. The command sequence is automatically replaced by an equivalent method.

86 | The Method Editor

Page 87: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 87 Tuesday, December 16, 2014 4:16 PM

Consider a case where you have created a compute button and you want to be alerted by a sound when the computation is finished. The figure below shows the Settings window of the compute button.

The Method Editor | 87

Page 88: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 88 Tuesday, December 16, 2014 4:16 PM

• Click the Convert to New Method button below the command sequence.

The command sequence is replaced by a method, in this example method3.• Click the Go to Method button. The method editor opens with the tab for method3 active.

• In the method editor, add a second line that plays the library sound success.wav.

The newly added line is indicated with a green bar to the left.

88 | The Method Editor

Page 89: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 89 Tuesday, December 16, 2014 4:16 PM

FORM OBJECT WITH ASSOCIATED METHODS

Form objects that have associated methods are indicated with a special icon, as shown in the figure below.

Using Ctrl+Alt+click on the form object opens the method in the method editor. If there is no method associated with the form object a new local method will be created, associated with the form object, and opened in the method editor.In this example, both the check box called Find prong length and the Compute button have associated methods.

The Method Editor | 89

Page 90: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 90 Tuesday, December 16, 2014 4:16 PM

Language Elements Window

The Language Elements window in the Method Editor shows a listing of some Language Constructs. Double-click or right-click one of the items in the list to insert template code in the currently selected method:

See also “Language Element Examples” on page 107.

90 | The Method Editor

Page 91: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 91 Tuesday, December 16, 2014 4:16 PM

Model Code Window

In the Model Code window in the Method Editor, right-click a model tree node to generate code associated with that node. Depending on the node, up to eight different options are available:

- Get- Set- Set All- Create- Run- Enable- Disable- Edit Node

Selecting one of the first seven options will add the corresponding code to the currently selected method. The Edit Node option brings you to the Settings window for the model tree node.The figure below shows an example of a node with six options.

The Method Editor | 91

Page 92: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 92 Tuesday, December 16, 2014 4:16 PM

KEYBOARD SHORTCUTS

In a method with code that refers to the model object such as:model.result("pg3").feature("surf1").create("hght1", "Height");

if you position the cursor in “surf1” and press F11 on the keyboard, the corresponding Surface plot node is highlighted in the Model Code window. Click the Edit Node to open its Settings window. For more information on keyboard shortcuts, see “Appendix C—Keyboard Shortcuts” on page 184.

Model Data Access

To access individual properties of a model tree node, click the Model Data Access button in the Declarations section of the Application ribbon tab:

The reason you need to enable Model Data Access this way is that a model typically contains hundreds or even thousands of properties that could be accessed and the list would be too long to be practical.When you click a model tree node, such as the Heat Flux node in the figure below, check boxes appear next to the individual properties. This example is based on the busbar tutorial model described in the book Introduction to COMSOL

92 | The Method Editor

Page 93: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 93 Tuesday, December 16, 2014 4:16 PM

Multiphysics. In the figure below, the check boxes for Heat transfer coefficient and External temperature are selected:

The Method Editor | 93

Page 94: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 94 Tuesday, December 16, 2014 4:16 PM

If you now switch to the Model Code window, you will see additional nodes appear under the Heat Flux node. Right-click and use Get or Set to generate code in an active method window, as shown in the figure below:

In the above example, Get and Set for the Heat transfer coefficient and the External temperature properties will generate the following code:

model.physics("ht").feature("hf1").getString("h");model.physics("ht").feature("hf1").getString("Text");

model.physics("ht").feature("hf1").set("h", "htc");model.physics("ht").feature("hf1").set("Text", "293.15[K]");

Recording Code

Click the Record Code button in the Method tab of the Method Editor to record a sequence of operations that you perform using the model tree in the Application Builder window, as shown in the figure below.

The code is recorded in the active method window, if any. If no method window is opened, a new method will be created.

94 | The Method Editor

Page 95: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 95 Tuesday, December 16, 2014 4:16 PM

While recording code, the method window gets a red frame:

To stop recording code, click the Stop Recording button.

You can also record code directly from the COMSOL Desktop by clicking Record a New Method. In this case, a new method is always created.The previous section on Model Data Access shows how to set the values of the Heat transfer coefficient and the External temperature properties of the busbar tutorial model. To generate similar code by using Record Code, follow these steps:• Create a simple application based on the busbar model (MPHAPP-file).• In the Application tab of COMSOL Desktop, click Record a New Method,

or while having the Method Editor open, click Record Code.• Change the value of the Heat transfer coefficient to, say, 5.• Change the value of the External temperature to, say, 300[K].• Click Stop Recording.• If not already opened, open the method with the recorded code.

The resulting code is listed below:

The Method Editor | 95

Page 96: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 96 Tuesday, December 16, 2014 4:16 PM

with(model.physics("ht").feature("hf1")); set("h", "5"); set("Text", "300[K]");endwith();

In this case, the automatic recording contains a with() statement in order to make the code more compact. For more information on the use of with(), see “The With Statement” on page 109.Use Record Code to quickly learn how to interact with the model object. The auto-generated code shows you the name of properties, parameters and variables. Use strings and string-number conversions to insert new parameter values to model properties. By combining Record Code with Model Data Access you can, for example, extract a parameter value using get, process its value in a method, and set it back into the model object using set.

Checking Syntax

Click Check Syntax in the ribbon to see messages in the Errors and Warnings window related to syntax errors or unused variables.

In addition to messages in the Errors and Warnings window, syntax errors are indicated with a wavy underline, as shown in the figure below.

96 | The Method Editor

Page 97: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 97 Tuesday, December 16, 2014 4:16 PM

Find and Replace

Click Find in the ribbon Method tab to open a dialog box used to find and replace strings in methods, as shown in the figure below.

Model Expressions Window

The Model Expressions window in the Method Editor shows a list of Model Expressions used as input and output arguments. Double-click or right-click one of the items in the list to insert the corresponding expression:

The Method Editor | 97

Page 98: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 98 Tuesday, December 16, 2014 4:16 PM

Extracting Variables

If you look at the example below you will notice that each line of code has a repeating prefix.

Readers familiar with object-oriented programming will recognize such a prefix as being the name of an object instance. The Extract Variable button provides a utility for simplifying code by replacing these instances with a variable name.In the example above, the cursor has been positioned at the first occurrence of feature. Click the Extract Variable button to transform the source code into what is shown in the figure below.

The code starting with the prefix feature has been replaced with the variable mslc1. When you click the button, an Extract Variable dialog box opens where you can enter a suitable variable name in the Name field.

98 | The Method Editor

Page 99: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 99 Tuesday, December 16, 2014 4:16 PM

Syntax Highlighting

Different language elements in the code are displayed using different styles:

This example includes three styles:• Keywords, such as for and int, are displayed in blue boldface font• Strings are displayed in red font• The remainder of the code is displayed in black font

You can customize the syntax highlighting theme in the Preferences dialog box. See “Method Editor Preferences” below.

The Method Editor | 99

Page 100: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 100 Tuesday, December 16, 2014 4:16 PM

Method Editor Preferences

To access the Preferences for the Method Editor, choose File>Preferences and select the Method Editor section.

By default, the Method Editor only shows the most relevant code. To see all code in a method, select the Show all code check box.The check box Close brackets automatically controls whether the Method Editor should automatically add closing brackets such as curly brackets {, brackets [, and parentheses (.The check box Generate compact code using ‘with’ statements controls the utilization of with statements in automatically generated code. For more information, see “The With Statement” on page 109.Under Syntax highlighting, the Theme list contains two predefined themes, Modern (the default) and Classic. Choose User defined to define a syntax highlighting mode where the colors can be assigned to individual language elements.

100 | The Method Editor

Page 101: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 101 Tuesday, December 16, 2014 4:16 PM

Ctrl+Space and Tab for Code Completion

While typing code in the Method Editor, you can get suggestions for code completions. The list of possible completions are shown in a separate completion list that opens. In some situations, detailed information appears in a separate window when an entry is selected in the list. Code completion can always be requested with the keyboard shortcut Ctrl+Space. When accessing parts of the model object you will get a list of possible completions automatically, as shown in the figure below:

Select a completion by using the arrow keys to select an entry in the list and press the Tab or Enter key to confirm the selection.If the list is long, you can filter by typing the first few characters of the completion you are looking for.If you enter the first few characters of, for example, a variable or method name and press Ctrl+Space, the possible completions are shown:

In the example above, only variables that match the string iv are shown. This example shows that variables local to the method also appear in the completion suggestions.

The Method Editor | 101

Page 102: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 102 Tuesday, December 16, 2014 4:16 PM

Local Methods

You can add local methods to buttons, menu items, and events. Local methods do not have nodes displayed under the Methods node in the application tree. In the Method Editor window for a local method, its tab displays the path to its associated user interface component, as shown in the figure below for the case of a check box.

LOCAL METHODS FOR BUTTONS, MENU ITEMS, AND GLOBAL EVENTS

For buttons, menu items, and global events, you add a local method by clicking the Create Local Method toolbar button under the sequence of commands, as shown in the figure below.

The function of this button is similar to the Extract to New Method toolbar button, with the only difference that it creates a local method not visible in the global method list in the application tree. It also opens the new method in the Method editor after creating it. For buttons, Ctrl+Alt+click can be used as a shortcut for creating the local method. Clicking the button Go to method will

102 | The Method Editor

Page 103: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 103 Tuesday, December 16, 2014 4:16 PM

open the local method. The figure below shows a call to a local method associated with a button.

To avoid any risk of corrupting code in a local method, it is not allowed to use Extract to New Method when there is a local method present in the command sequence.

LOCAL METHODS FOR FORM AND FORM OBJECT EVENTS

To add a local method for a form or form object event, other than a button, click the Create Local Method button in the Events section of the Settings window. The selected On data change method then changes from None to Local method, as shown in the figure below, and the method editor is opened.

To open an existing local method in the Method Editor, click the Go to Source button. Click the Remove Local Method button to delete the local method.As an alternative to Ctrl+Alt+click, you can right-click a form object and select Edit Local Method from its context menu.

By using events, methods can be local to a form or form object. Such methods are not displayed under the Methods node. For more information, see “Events” on page 62.

The Method Editor | 103

Page 104: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 104 Tuesday, December 16, 2014 4:16 PM

Methods with Input and Output Arguments

A method is allowed to have several input arguments and one output argument. You define input and output arguments in the Settings window of an active method window. If the Settings window is not visible, click Settings in the Method tab. The figure below shows a method randomize with two input arguments var and coords and one output coordsout. The method adds random values to an array, coords, of doubles. The degree of randomness is controlled by the input variable var. The new values are stored in the array coordsout.

A method is allowed to call itself for the purpose of recursion.

Debugging

Click the Debug tab in the Method Editor to view the tools available for debugging methods. Click in the gray column to the left of the code line numbers to set breakpoints. When you run the application, the method will stop at the

104 | The Method Editor

Page 105: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 105 Tuesday, December 16, 2014 4:16 PM

breakpoints. The figure below shows a method stopped at the line highlighted in yellow.

In the Main group of the Debug tab, click the Step button to go to the next line in the method. Click Continue to run the method up until the next breakpoint. Click Stop to stop running the method. Click Step Into to step into the next method, if possible. Use Remove All to remove all break points. Instead of removing, you can disable all break points by clicking Disable All. Click the Debug

The Method Editor | 105

Page 106: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 106 Tuesday, December 16, 2014 4:16 PM

Log to display debugging messages in a separate Debug Log window, as shown in the figure below.

Use the debugLog command to display the value of variables in the Debug Log window. The code below illustrates using the debugLog command to display the values of strings and components of a 1D double array.

int len=xcoords.length;if (selected==0) {

106 | The Method Editor

Page 107: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 107 Tuesday, December 16, 2014 4:16 PM

for (int i = 0; i < len; i++) { double divid=double(i)/len; xcoords[i] = Math.cos(2.0*Math.PI*divid); ycoords[i] = Math.sin(2.0*Math.PI*divid); debugLog("x:"); debugLog(xcoords[i]); debugLog("y:"); debugLog(ycoords[i]); debugLog("selected is 0"); }}

For more information on commands for debugging, see “Debug Methods” on page 212.

The Model Object

The model object provides a large number of methods, including methods for setting up and running sequences of operations. The Convert to Method, Record Code, Model Code, and Language Elements utilities of the Method Editor produce statements using such model object methods. For more information on and example code related to the model object and its methods, see Appendix C—Language Elements and Reserved Names in the book Introduction to COMSOL Multiphysics, as well as the COMSOL Programming Reference Manual.

Language Element Examples

The Javaprogramming language is used to write COMSOL methods, which means that Java statements and syntax in general can be used.

UNARY AND BINARY OPERATORS IN THE MODEL OBJECT

The table below describes the unary and binary operators that can be used when accessing the model object, such as when defining material properties and boundary conditions, and in results expressions used for postprocessing and visualization.

PRECEDENCE LEVEL SYMBOL DESCRIPTION

1 () {} . grouping, lists, scope

2 ^ power

3 ! - + unary: logical not, minus, plus

The Method Editor | 107

Page 108: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 108 Tuesday, December 16, 2014 4:16 PM

UNARY AND BINARY OPERATORS IN METHODS (JAVA SYNTAX)The table below describes the most important unary and binary operators used in Java code in methods.

ACCESSING A VARIABLE IN THE DECLARATIONS NODE

Variables defined in the Declarations node are available as global variables in a method and need no further declarations.

4 [] unit

5 * / binary: multiplication, division

6 + - binary: addition, subtraction

7 < <= > >= comparisons: less-than, less-than or equal, greater-than, greater-than or equal

8 == != comparisons: equal, not equal

9 && logical and

10 || logical or

11 , element separator in lists

PRECEDENCE LEVEL SYMBOL DESCRIPTION

1 ++ -- unary: postfix addition and subtraction

2 ++ -- + - ! unary: addition, subtraction, positive sign, negative sign, logical not

3 * / % binary: multiplication, division, modulus

4 + - binary: addition, subtraction

5 < <= > >= comparisons: less-than, less-than or equal, greater-than, greater-than or equal

6 == != comparisons: equal, not equal

7 && binary: logical and

8 || binary: logical or

9 ?: conditional ternary

10 = += -= *= /= %= >>= <<= &= ^= |=

assignments

11 , element separator in lists

PRECEDENCE LEVEL SYMBOL DESCRIPTION

108 | The Method Editor

Page 109: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 109 Tuesday, December 16, 2014 4:16 PM

BUILT-IN ELEMENTARY MATH FUNCTIONS

Elementary math functions used in methods rely on the Java math library. Some examples:

Math.sin(double)Math.cos(double)Math.random()Math.PI

THE IF STATEMENT

if(a<b) {alert(toString(a));} else {alert(toString(b));}

THE FOR STATEMENT

// Iterate i from 1 to N:int N=10;for (int i = 1; i <= N; i++) { // Do something}

THE WHILE STATEMENT

double t=0,h=0.1,tend=10;while(t<tend) { //do something with t t=t+h;}

THE WITH STATEMENT

// Set the global parameter L to a fixed valuewith(model.param()); set("L", "10[cm]");endwith();

The code above is equivalent to:model.param().set("L", "10[cm]");

ACCESSING A GLOBAL PARAMETER

You would typically use the Model Code window to access global parameters.Get the global parameter L, defined in the model, and store it in the double variable Length:

double Length=model.param().evaluate("L");

To return the unit of the parameter L, if any, use:

The Method Editor | 109

Page 110: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 110 Tuesday, December 16, 2014 4:16 PM

String Lunit=model.param().evaluateUnit(“L”);

COMPARING STRINGS

Comparing string values in Java has to be done with .equals() and not with the == operator. This is due to the fact that the == operator compares whether the strings are the same objects and does not consider their values. The below code demonstrates string comparisons:

boolean streq=false;String a="string A";String b="string B";streq=a.equals(b);// In this case streq==falseb="string A";streq=a.equals(b);// In this case streq==true

ALERTS AND MESSAGES

The methods alert, confirm, and request display a dialog box with a text string and optional user input. The following example uses confirm to ask the user if a direct or an iterative solver should be used in an application. Based on the answer, the alert function is then used to show the estimated memory requirement for the selected solver type in a message dialog box:

String answer = confirm("Which solver do you want to use?", "Solver Selection","Direct", "Iterative");if(answer.equals("Direct")) { alert("Using the direct solver will require about 4GB of memory when solving.");} else { alert("Using the iterative solver will require about 2GB of memory when solving.");}

CREATING AND REMOVING MODEL TREE NODES

Remove a square object sq1 and a circle object c1:model.geom("geom1").feature().remove("sq1");model.geom("geom1").feature().remove("c1");

Remove a series of geometry objects (circles) with tags c1, c2, ..., c10:for(int n=1;n<=10;n=n+1) { model.geom("geom1").feature().remove("c"+n);}

The syntax "c"+n automatically converts the integer n to a string before concatenating it to the string "c".To remove all geometry objects:

for(String tag : model.geom("geom1").feature().tags()) {

110 | The Method Editor

Page 111: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 111 Tuesday, December 16, 2014 4:16 PM

model.geom("geom1").feature().remove(tag); }

Create a circle with tag c1, build the circle object (run the node), and create a rectangle with tag r1:

model.geom("geom1").create("c1", "Circle");model.geom("geom1").run("c1");model.geom("geom1").create("r1", "Rectangle");

ExampleThis is a larger block of code that removes, creates, and accesses physics interface feature nodes:

String[] flowrate = column1;String[] Mw = column2;

for (PhysicsFeature feat : model.physics("pfl").feature()) { if (feat.getType().equals("Inlet")) model.physics("pfl").feature().remove(feat.tag());}

if (flowrate != null) { for (int i=0; i<flowrate.length; i++) { if (flowrate[i] != "") { if (Mw[i] != "") { int d = 1+i; model.physics("pfl").create("inl" +d, "Inlet"); model.physics("pfl").feature("inl" +d).setIndex("spec", "3", 0); model.physics("pfl").feature("inl" +d).set("qsccm0", flowrate[i]); model.physics("pfl").feature("inl" +d).set("Mn", Mw[i]); model.physics("pfl").feature("inl" +d).selection().set(new int[]{d}); } } }}

The need to remove and create model tree nodes is fundamental when writing methods because the state of the model object is changing each time a model tree node is run. In the method above, the number of physics feature nodes are dynamically changing depending on user inputs. Each time the simulation is run, old nodes are removed first and then new nodes are added.

The Method Editor | 111

Page 112: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 112 Tuesday, December 16, 2014 4:16 PM

Libraries

In the application tree, the Libraries node contains images, sounds, and files to be embedded in an MPHAPP-file so that you do not have to distribute them along with the application. In addition, the Libraries node may contain Java utility class nodes and nodes for external Java and C libraries.

To manage files loaded by the user of an application at run time, rather than using the Libraries node you need to create a file declaration and a file browser form object. For more information on files to be loaded at run time, see “File” on page 77 and “File Import” on page 151.

The embedded files can, for example, be referenced in form objects or in methods by using the syntax embedded:///file1, embedded:///file2, and so on. For example, to reference the image file compute.png, use the syntax embedded:///compute.png.Note that you are not required to have the file extension as part of the file name; instead, arbitrary names can be used. To minimize the size of your MPHAPP-file, delete unused images, sounds, or other files.

Images

The Images library contains a number of preloaded sample images in the PNG-file format. If you wish to embed other image files, click the Add File to Library icon. A larger selection of images is available in the COMSOL installation folder at the

112 | Libraries

Page 113: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 113 Tuesday, December 16, 2014 4:16 PM

location data/images. Images are referenced in image form objects or in methods.

To preview an image, click the name of the image and then click the Preview button below the List of Images. This opens a dialog box displaying the image, as shown in the figure below.

Sounds

The Sounds library contains a few preloaded sounds in the WAV-file format. If you wish to embed other sound files, click the Add File to Library icon. A larger

Libraries | 113

Page 114: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 114 Tuesday, December 16, 2014 4:16 PM

selection of sounds is available in the COMSOL installation folder at the location data/sounds.

To play a sound, click the name of the sound and then click the Preview button below the List of Sounds.To play a sound in an application, write a method containing a call to the function playSound, such as:

playSound("success.wav");

114 | Libraries

Page 115: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 115 Tuesday, December 16, 2014 4:16 PM

Files

The Files library is empty by default. Click the Add File to Library icon to embed files of any type in your application.

The embedded files can be referenced in a method by using the syntax embedded:///data1.txt, embedded:///data2.txt, and so on. This syntax can also be used in any file browser text fields in a model. For more information, see “File” on page 77 and “File Methods” on page 194.

Libraries | 115

Page 116: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 116 Tuesday, December 16, 2014 4:16 PM

Appendix A—Form Objects

This appendix provides information about form objects and expands upon the section “The Form Editor” on page 31. The items followed by a * in the following list have already been described in detail in that section. The remaining items are discussed in this appendix.

List of All Form Objects

• Input- Input Field*- Button*- Check Box- Combo Box

• Labels- Text Label*- Unit*- Equation- Line

• Display- Data Display*- Graphics*- Web Page- Image- Progress Bar- Log- Message Log- Results Table

• Subforms- Form- Form Collection

116 | Appendix A—Form Objects

Page 117: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 117 Tuesday, December 16, 2014 4:16 PM

• Composite- Card Stack- File Import- Array Input- Radio Button- Selection Input

• Miscellaneous- Text- List Box- Table- Slider- Toolbar- Spacer

Check Box

A check box has two values: on for selected and off for cleared. The state of a check box is typically stored in a Boolean variable in the Declarations node.

Appendix A—Form Objects | 117

Page 118: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 118 Tuesday, December 16, 2014 4:16 PM

USING A CHECK BOX TO CONTROL VISUALIZATION

In the example below, the state of a check box is stored in a variable bvar, whose Settings window is shown in the figure below.

In this example, the variable controls whether the title and color legend are included in a plot (on) or not included (off).

118 | Appendix A—Form Objects

Page 119: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 119 Tuesday, December 16, 2014 4:16 PM

The example below shows the Settings window for the check box associated with bvar.

You associate a check box with a declared variable by selecting it from the tree in the Source section.The text label for a check box gets its name, by default, from the Description field of the Boolean variable that it is associated with. The name in this case is Include title and color legend.The Default value of the variable bvar is overwritten by the Value for selected (on) or the Value for cleared (off) and does not need to be edited. When used in methods, the values on and off are aliases for true and false, respectively. These values can be used as Booleans in if statements, for example.

Appendix A—Form Objects | 119

Page 120: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 120 Tuesday, December 16, 2014 4:16 PM

The application in the figure shown below provides an example where the title and color legend of a temperature plot in the plot group pg2 is included only if the check box is selected.

This block of code shows the corresponding method:if (bvar) { with(model.result("pg2")); set("titletype", "auto"); endwith(); with(model.result("pg2").feature("surf1")); set("colorlegend", "on"); endwith(); model.result("pg2").run();} else { with(model.result("pg2").feature("surf1")); set("colorlegend", "off"); endwith(); with(model.result("pg2")); set("titletype", "none"); endwith(); model.result("pg2").run();}useGraphics(model.result("pg2"),"graphics1");

120 | Appendix A—Form Objects

Page 121: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 121 Tuesday, December 16, 2014 4:16 PM

Combo Box

A combo box can serve as either a combination of a drop-down list box and an editable text field or as a drop-down list box without the capability of editing.

USING A COMBO BOX TO CHANGE PARAMETERS IN RESULTS

To illustrate the use of a combo box, consider an application where the user selects one of six different mode shapes to be visualized in a structural vibration analysis. These six mode shapes correspond to six different eigenfrequencies that the user selects from a combo box:

The figure below shows the Settings window for this combo box.

Appendix A—Form Objects | 121

Page 122: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 122 Tuesday, December 16, 2014 4:16 PM

Selecting the SourceIn the Source section, you select the variable that should have its value controlled by the combo box. In this example, a string variable svar is selected. The figure below shows the Settings window for this variable.

In the Initial value list, choose a method to define a default value for the combo box. The options are First allowed value (the default) and Custom default. For the Custom default option, enter a default value in the associated field. The default value that you enter must exist among the allowed values.

122 | Appendix A—Form Objects

Page 123: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 123 Tuesday, December 16, 2014 4:16 PM

Allowed ValuesIn the section for Allowed values, you can add Choice List nodes that contribute allowed values to the combo box. The choice list declaration associated with this example is shown in the figure below.

The string variable svar is allowed to have one of these six values: 7, 8, 9, 10, 11, or 12. The text strings in the Display name column are shown in the combo box.You can select the Allow other values check box to get a combo box where you can type arbitrary values. Such combo boxes can accept any value and are not restricted to the values defined by the choice lists. In this example, however, only six predefined values are allowed.

Appendix A—Form Objects | 123

Page 124: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 124 Tuesday, December 16, 2014 4:16 PM

EventsUse events to control which method to run when the value of a combo box is changed by the user. In the figure below, when the value of svar is changed, method1 is run.

124 | Appendix A—Form Objects

Page 125: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 125 Tuesday, December 16, 2014 4:16 PM

The code for method1 is listed below.with(model.result("pg1")); set("looplevel", new String[]{svar});endwith();model.result("pg1").run();

This code links the value of the string svar to the Eigenfrequency setting in the Plot Group pg1. In this case, the string svar takes the values "7", "8", "9", "10", "11", or "12".To create the above code, you can use Record Code:• Click Record Code to record a new method.• Change the Eigenfrequency from mode 7 to mode 8. In the figure below,

this corresponds to changing from 451Hz to 692 Hz in the Settings window for a plot group used to visualize the Mode Shape.

• Click Stop Recording.

The resulting code is shown below.with(model.result("pg1")); set("looplevel", new String[]{"8"});endwith();model.result("pg1").run();

Now replace the string”8” with svar to end up with the code listing for method1 above.

Appendix A—Form Objects | 125

Page 126: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 126 Tuesday, December 16, 2014 4:16 PM

USING A COMBO BOX TO CHANGE MATERIAL

As an extension of the previous example, consider an application where combo boxes are used to select the material. In this case, an activation condition can also be used for more flexibility in the user interface design.The figure below shows screenshots from an application where the user can choose between two materials, Aluminum or Steel, using a combo box named Material. A second combo called Alloy shows a list of Aluminum alloys or Steel alloys according to the choice made in the Material list.

126 | Appendix A—Form Objects

Page 127: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 127 Tuesday, December 16, 2014 4:16 PM

The material choice is implemented in the embedded model using global materials and a material link, as shown in the figure below.

Each material is indexed with a string: mat1, mat2, ..., mat5. An event listens for changes to the value of the global variable alloy, where the value is controlled by a combo box. When the value is changed, the method listed below is run.

with(model.material("matlnk1")); set("link", alloy);endwith();

Appendix A—Form Objects | 127

Page 128: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 128 Tuesday, December 16, 2014 4:16 PM

The figure below shows the declaration of two string variables, material and alloy, which are controlled from the Material and Alloy combo boxes, respectively.

The application utilizes three choice lists: Aluminum Alloys, Steel Alloys, and Material. An activation condition is used for the Aluminum Alloys and Steel Alloys choice lists, as shown in the figure below.

128 | Appendix A—Form Objects

Page 129: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 129 Tuesday, December 16, 2014 4:16 PM

The Settings window for the Material combo box is shown below.

Note that the Material combo box uses the material string variable as its source. The Material choice list is used to define a discrete set of allowed values for the

Appendix A—Form Objects | 129

Page 130: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 130 Tuesday, December 16, 2014 4:16 PM

material string variable. The Settings window for the Material choice list is shown below.

130 | Appendix A—Form Objects

Page 131: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 131 Tuesday, December 16, 2014 4:16 PM

The Settings window for the Alloy combo box is shown in the figure below.

Appendix A—Form Objects | 131

Page 132: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 132 Tuesday, December 16, 2014 4:16 PM

Note that the Alloy combo box uses both the Aluminum Alloys and the Steel Alloys choice lists. The choice list for Aluminum Alloys is shown in the figure below.

132 | Appendix A—Form Objects

Page 133: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 133 Tuesday, December 16, 2014 4:16 PM

The activation condition for the Aluminum Alloys choice list is shown in the figure below.

Appendix A—Form Objects | 133

Page 134: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 134 Tuesday, December 16, 2014 4:16 PM

USING A COMBO BOX TO CHANGE ELEMENT SIZE

You can use the Model Data Access functionality to give the user of an application direct access to a property in the embedded model. Consider an example where a combo box is used to change the element size in a mesh, as in the figure below.

Clicking Model Data Access in the ribbon gives access to the combo box for predefined element size, as shown in the figure below.

Select the green check box to the left of the list to make it available as a source to the combo box.

134 | Appendix A—Form Objects

Page 135: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 135 Tuesday, December 16, 2014 4:16 PM

The corresponding Settings window for the combo box is shown in the figure below.

Changing the Initial value to From data source ensures that the element size setting of the model, in this case Normal, is used as the default element size in the application. A choice list, Predefined size (hauto), is automatically created, corresponding to the list in the model. To use a more limited set of choices, delete

Appendix A—Form Objects | 135

Page 136: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 136 Tuesday, December 16, 2014 4:16 PM

the virtual choice list and create a new choice list with fewer options. In this case, a choice list with three entries could be created, as shown in the figure below.

To learn which values are used by the Element size list in the model, use Record Code and change the value from Normal to Fine and then to Coarse. Stop the recording and read the values in the auto-generated code. The values for Fine, Normal, and Coarse are 4, 5, and 6, respectively.

136 | Appendix A—Form Objects

Page 137: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 137 Tuesday, December 16, 2014 4:16 PM

Equation

An equation object can display a LaTeX equation by entering the expression in the Enter equation in LaTeX syntax field.

A preview is shown of the rendered LaTeX syntax after leaving the text field.

Appendix A—Form Objects | 137

Page 138: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 138 Tuesday, December 16, 2014 4:16 PM

Line

Use the line form object to add a horizontal or vertical line to a form, which can be used, for example, to separate groups of form objects. For the horizontal line option, you can also add text that appears within the line.

Web Page

A web page object can display the contents of a web page as part of the user interface. You can specify the page source in three different ways from the Source list:• Use the default option Page to enter HTML code in a text area below the

list, enclosed by the <html> and </html> start and end tags.

138 | Appendix A—Form Objects

Page 139: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 139 Tuesday, December 16, 2014 4:16 PM

• Use the URL option to link to a web page on the Internet.• Use the File option to point to a local file resource containing HTML code.

Type the name of the file in the File field or click Browse to locate the file on the local file system.

Image

Use an image form object to add an image to a form. An image object is different from a graphics object in that an image object is not interactive. Choose an image file from one of the library images, accessible from a drop-down list, or by clicking the Add Image to Library icon to select a file from the local file system. The figure

Appendix A—Form Objects | 139

Page 140: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 140 Tuesday, December 16, 2014 4:16 PM

below shows the Settings window for an image object referencing the image cube_large.png defined in the Libraries node.

If you select an image file from your file system, this file will be embedded in the application and added to the list of Images under the Libraries node.While you can change the x- and y-position of the image, the width and height settings are determined by the image file.

You can paste images from the Windows clipboard to a form window by using Ctrl+V. Such images will be added automatically to the Images library and embedded in the application. The names for pasted images are automatically set to: pasted_image_1.png, pasted_image_2.png, etc.

Progress Bar

A progress bar object displays progress based on a value that is updated by a method. Use a progress bar to provide feedback on remaining run time for

140 | Appendix A—Form Objects

Page 141: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 141 Tuesday, December 16, 2014 4:16 PM

methods. For more information, see “Progress Methods” on page 214. The figure below shows the Settings window of a progress bar object.

Note that the built-in progress bar that is visible in the status bar of an application is controlled by the Settings window of the Main Window node. The built-in progress bar shows the progress of the built-in COMSOL Multiphysics core algorithms such as geometry operations, meshing, and solving.

Log

The log form object adds a log window that displays messages from the built-in COMSOL Multiphysics core algorithms such as geometry operations, meshing, and solving.

Appendix A—Form Objects | 141

Page 142: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 142 Tuesday, December 16, 2014 4:16 PM

The Include standard log toolbar check box is selected by default. When selected, the toolbar in the Log window that you see in the COMSOL Desktop is included in the application.

Message Log

The message log object adds a window where you can display messages to inform the user about operations that the application carries out. Implement this feature using the built-in message method with syntax: message(String message). See also “GUI-Related Methods” on page 200.

The Include standard message log toolbar check box is selected by default. When selected, the toolbar in the Messages window that you see in the COMSOL

142 | Appendix A—Form Objects

Page 143: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 143 Tuesday, December 16, 2014 4:16 PM

Desktop is included in the application. The Show COMSOL messages check box is selected by default to enable messages from the built-in COMSOL Multiphysics core algorithms such as geometry operations, meshing, and solving. Clear the check box to only allow messages from the application itself.

Results Table

The results table object is used to display numerical results in a table.

Appendix A—Form Objects | 143

Page 144: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 144 Tuesday, December 16, 2014 4:16 PM

The source of the results table data is one of the child nodes to the Derived Values node under Results. In the figure below, a Point Evaluation node is used as the source.

The Include standard results table toolbar check box is selected by default. When selected, the toolbar is included in the application as it would appear in a Table window in the COMSOL Desktop.

Form

Within a form, you can embed a subform by creating a link to it. You do this by selecting the form you would like to link to from the Form Reference section of the Settings window for the subform. The figure below shows an example where one of the cells of the form Main has a link to the form Input. The example uses the grid layout mode as explained in “Appendix B—Grid Layout Mode” on page

144 | Appendix A—Form Objects

Page 145: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 145 Tuesday, December 16, 2014 4:16 PM

179.

Appendix A—Form Objects | 145

Page 146: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 146 Tuesday, December 16, 2014 4:16 PM

The figure below shows the referenced form Input as it appears using grid layout mode.

Form Collection

A form collection object consists of several forms presented in a main form. In this example there are four forms that appear as tabs in a single Main Window.

There are three different layout options. From the Type list, choose between:• Tabs, which is the default setting, displays the forms using tabbed panes.

146 | Appendix A—Form Objects

Page 147: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 147 Tuesday, December 16, 2014 4:16 PM

• List displays a list to the left of the form panes, where you can select the form to display.

• Sections displays each form in a separate section.

In the Panes section, in the Use selected forms as panes list, each form represents a pane. These will be displayed in the application in the order they appear in the list.You can control which tab (or list entry) is active by linking to a string variable in the section Active Pane Selector, as shown in the figure below.

When an Active Pane Selector is selected, the Default pane setting will initialize the chosen string variable, overriding the initial value set in the string variable declaration. The string variable needs to be equal to one of the form names in the form collection such as temperature or conversion in the example above. Otherwise, it will be ignored.

Appendix A—Form Objects | 147

Page 148: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 148 Tuesday, December 16, 2014 4:16 PM

If you change the value of the pane selector pane in the above example, in a method that will be run at some point (a button method, for example), then the pane with the new value will be activated. Example:

pane=”conversion”; /* Activate the conversion pane on completion of this method */

For a form collection with the Type set to Sections, the Active Pane Selector has no effect. Using an Active Pane Selector is optional and is only needed if you wish to control which tab is active by some method other than clicking its tab. To remove a string variable used as an Active Pane Selector, click the Clear source toolbar button under the tree.

Card Stack

A card stack is a form object that contains cards. A Card is another type of form object, one that is only used in the context of a card stack. Flip between cards in a card stack to show one at a time. You associate a card stack with a data source that controls which card to show. Each card specifies a value that is compared against the data source of the card stack. The card stack shows the first card with the matching value. If no cards match, nothing is shown.

USING A CARD STACK TO FLIP BETWEEN TWO IMAGES

Consider an application where the image shown to the user depends on the computed value, for example, the value of a Global Evaluation node. If the value is below a certain threshold, one image is displayed to indicate that the design meets the specifications. Otherwise, a different image is displayed. The figure

148 | Appendix A—Form Objects

Page 149: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 149 Tuesday, December 16, 2014 4:16 PM

below shows a card stack with two cards and a string variable image as its Active Card Selector.

The table in the Cards section contains the cards in the Card column and their associated activating values in the Activating value column. The stack decides which card to display based on their activating values. In this example, the Activating values are the strings card1 and card2 (not to be confused with the identical names of the card objects).The figure below shows the declaration of the string variable image and its default value card1.

Appendix A—Form Objects | 149

Page 150: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 150 Tuesday, December 16, 2014 4:16 PM

Right-click a Card in a form window to select between the different cards in a card stack, as shown in the figure below.

The figure below shows card1 with its image form object.

150 | Appendix A—Form Objects

Page 151: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 151 Tuesday, December 16, 2014 4:16 PM

The figure below shows card2 with its image form object.

The method below flips between the two cards by changing the value of the string variable image based on the value of a Global Evaluation node gev1. The value of the string is set to either card1 or card2, as shown in the example below.

double[][] parr = model.result().numerical("gev1").getReal();double p = parr[0][0];

if (p < 760) image="card1";else image=”card2”;

File Import

A file import object is used to display a file browser with an associated input field for browsing to a file or entering its path and name. It is used to enable file import by the user of an application at run time, when the file is not available in the application beforehand.

Appendix A—Form Objects | 151

Page 152: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 152 Tuesday, December 16, 2014 4:16 PM

IMPORTING EXPERIMENTAL DATA

Consider an application where the user is providing a file with experimental data at run time. The figure below shows the file import object of such an application as it appears in grid layout mode.

The figure below shows the Settings window of the corresponding file import object and its link to a file declaration. For more information on file declarations,

152 | Appendix A—Form Objects

Page 153: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 153 Tuesday, December 16, 2014 4:16 PM

see “File” on page 77.

In this application, the File types table specifies that only CSV files are allowed.

Appendix A—Form Objects | 153

Page 154: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 154 Tuesday, December 16, 2014 4:16 PM

The Settings window for the File declaration is shown in the figure below.

Note that the file extension .csv used in the declaration is optional.In order to make it possible to run the application without having to first provide experimental data, a file containing default experimental data is embedded in the application. This default data file is used by the application by accessing it with the embedded:/// file scheme syntax, as shown in the figure below. In this example, the application performs a least-squares fit to the experimental data.

The following method handles the logic to determine if user-provided experimental data files exist, or if the default data set should be used.

if (exists("upload:///experimental.csv")) { with(model.physics("opt").feature("glsobj1")); set("fileName", "upload:///experimental.csv"); endwith();}

154 | Appendix A—Form Objects

Page 155: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 155 Tuesday, December 16, 2014 4:16 PM

else{ String s_data = confirm("No experimental data file was uploaded. Do you want to use the embedded data?", "Experimental Data", "Yes", "Cancel Parameter Estimation"); if(s_data.equals("Cancel Parameter Estimation")){ return; }}

In the case a user-provided file exists, the code replaces embedded:///experimental_default.csv with upload:///experimental.csv, in the physics interface glsobj1.

USER-DEFINED CAD IMPORT

Consider an application where a CAD file can be selected and imported at run time, as shown by the figure below.

The figure below shows the Settings window of the corresponding file import object and its link to a file declaration. For more information on file declarations,

Appendix A—Form Objects | 155

Page 156: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 156 Tuesday, December 16, 2014 4:16 PM

see “File” on page 77.

156 | Appendix A—Form Objects

Page 157: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 157 Tuesday, December 16, 2014 4:16 PM

The model tree Import node with its Settings window is shown below.

Note the syntax upload:///file1 referring to the file declaration file1.

Appendix A—Form Objects | 157

Page 158: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 158 Tuesday, December 16, 2014 4:16 PM

In this example application, a button called Update Geometry runs a method that imports and plots the resulting geometry object, then runs a Zoom Extents command. The Settings window for this button is shown in the figure below.

The code for the method updategeometry is listed below.model.geom("geom1").feature("imp1").importData(); model.geom("geom1").run();useGraphics(model.geom("geom1"), "/form1/graphics1");zoomExtents("/form1/graphics1");

Note that the geometry import method importData() is not automatically run by the method model.geom("geom1").run(). This means that in order to allow a user to change the CAD file referenced in the application, a method needs to be used that includes a call to this geometry import method (if not, then the previously imported geometry will be used.)

158 | Appendix A—Form Objects

Page 159: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 159 Tuesday, December 16, 2014 4:16 PM

Array Input

An array input object has an input table used to enter array or vector-valued input data. An array input object supports string arrays as data sources. You can add an optional label, symbol, and unit.

USING AN ARRAY INPUT OBJECT FOR 3D POINT COORDINATE INPUT

Consider an application where the user enters 3D coordinates for a point where the stress is evaluated. The figure below shows a screenshot from an application with an array input, a button, a text label, and a data display object.

Appendix A—Form Objects | 159

Page 160: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 160 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window of the array input object.

160 | Appendix A—Form Objects

Page 161: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 161 Tuesday, December 16, 2014 4:16 PM

The figure below shows the Settings window for the declaration of the Array 1D Double samplecoords.

In the Settings window of the array input object:• In the Length field, enter the length of the array as a positive integer. The

default is 3.• From the Show vector as list, choose Table (the default) to show the array

components as a table, or choose Components to show each array component as a separate input field with a label.

• In the Initial values table, enter the default values for the components in the array.

• The Layout Options section provides settings for adding optional labels and units to the array input.

In this example, when the user clicks the button labeled Evaluate stress at point, the following method is run.

with(model.result().dataset("cpt1")); set("pointx", samplecoords[0]); set("pointy", samplecoords[1]); set("pointz", samplecoords[2]);endwith();

Appendix A—Form Objects | 161

Page 162: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 162 Tuesday, December 16, 2014 4:16 PM

Radio Button

A radio button object has a fixed number of options from which you can choose one. It is most useful when you have just a handful of options.

USING RADIO BUTTONS TO SELECT A LOAD

Consider an application where the user can select one of three predefined loads as shown in the following figure.

The corresponding Settings window is shown below.

162 | Appendix A—Form Objects

Page 163: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 163 Tuesday, December 16, 2014 4:16 PM

In the Initial value list, choose the manner in which the initial selection of the radio button should be made. The options are From data source, First allowed value (the default), and Custom default. For the Custom default option, enter a default value in the associated field. The default value that you enter must exist among the allowed values.In the Choice list section, you can add choice lists that contribute allowed values to the radio button object, where each valid value represents one radio button.The radio button names are taken from the Display name column of their associated choice list. The figure below shows the choice list used in this example.

Selection Input

Sometimes it is convenient to group geometric entities (domains, boundaries, edges, or points) together into an explicit selection. In the example below, explicit selections are used to define which boundaries that a boundary condition should be applied to. A selection input object can be used in an application to let the user interactively select which geometric entities should belong to an explicit selection.

Appendix A—Form Objects | 163

Page 164: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 164 Tuesday, December 16, 2014 4:16 PM

The figure below shows an application where the user can select boundaries to be excited by an incoming wave.

Selecting geometric entities, such as boundaries, can be done in two ways: by clicking directly on the graphics object or by adding geometric entity numbers in a selection input object.To make it possible to directly select a graphics object by clicking on it, you can link a graphics object to an explicit selection as shown in the figure below. In the

164 | Appendix A—Form Objects

Page 165: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 165 Tuesday, December 16, 2014 4:16 PM

figure, there are two explicit selections sel1 and sel2, and the graphics object graphics2 is linked to the selection sel1.

When a graphics object is linked directly to an explicit selection in this way, the graphics object displays the geometry and the user can interact with it by clicking on the boundaries. The boundaries will then be added (or removed) to the corresponding explicit selection. Use Ctrl+click to remove boundaries from a selection.

Appendix A—Form Objects | 165

Page 166: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 166 Tuesday, December 16, 2014 4:16 PM

To make it possible to select by number, you can link a selection input object to an explicit selection as shown in the figure below.

In a selection input object, you can copy, paste, remove, clear, and zoom in to selections.

You can choose to use a graphics object as the source of a selection without having any selection input object. You can also link both a graphics object and a selection input object to the same explicit selection.

166 | Appendix A—Form Objects

Page 167: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 167 Tuesday, December 16, 2014 4:16 PM

Text

A text object is a text field with default text that is taken from a string variable. The Settings window for a text object is shown below.

In the Value field, enter the initial text. By default, the initial text is set to the initial value of the string variable used as a source.The check box Editable is cleared by default. If selected, the text object can be used, for example, to type comments in a running application. If the text is changed by the user, it is stored in the string variable that is used as the data source.The check box Wrap text is selected by default. Clear this check box to disable wrapping of the text; a scroll bar appears if the text does not fit.

Appendix A—Form Objects | 167

Page 168: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 168 Tuesday, December 16, 2014 4:16 PM

List Box

A list box object is similar to a radio button object except that it allows for the simultaneous selection of multiple options.

USING A LIST BOX TO SUPERIMPOSE VIBRATIONAL MODES

Consider an application where the first six vibrational modes of a mechanical part can be superimposed and visualized by selecting them from a list box, as shown in the figure below.

As an alternative, the following figure shows that a list can be displayed as a dialog box.

168 | Appendix A—Form Objects

Page 169: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 169 Tuesday, December 16, 2014 4:16 PM

The Settings window for the list box of this example is shown in the figure below.

The Select values in list allows you to choose between two alternatives, List box or Dialog, for displaying the list.If you use a string array as the source, you can, in the running application, select more than one item in the list using Shift+click or Ctrl+click. For other sources,

Appendix A—Form Objects | 169

Page 170: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 170 Tuesday, December 16, 2014 4:16 PM

you can only select one value from the list. This example uses a 1D string array svar1D. Its Settings window is shown below.

In the Choice List section, you can add choice lists that contribute allowed values to the list box. The figure below shows the choice list used in this example.

The vibrational modes 1–6 correspond to trivial rigid body modes and are not of interest in this application, hence the list of values starts at 7. The choice list allows you to hide the actual mode values in the model from the user by only displaying the strings in the Display name column; the first non-rigid body modes are named Mode 1, the second Mode 2, etc.

170 | Appendix A—Form Objects

Page 171: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 171 Tuesday, December 16, 2014 4:16 PM

The method below uses the COMSOL Multiphysics operator with() to visualize the superimposed modes. This example is somewhat simplified since it ignores the effects of amplitude and phase for the modes.

String withstru="0";String withstrv="0";String withstrw="0";for(int i=0;i<svar1D.length;i++){ withstru=withstru + "+" + "with(" + svar1D[i] + ",u)"; withstrv=withstrv + "+" + "with(" + svar1D[i] + ",v)"; withstrw=withstrw + "+" + "with(" + svar1D[i] + ",w)";}

with(model.result("pg7").feature("surf1").feature("def")); setIndex("expr", withstru, 0); setIndex("expr", withstrv, 1); setIndex("expr", withstrw, 2);endwith();model.result("pg7").run();useGraphics(model.result("pg7"),"/form1/graphics8");zoomExtents("/form1/graphics8");

Assuming the user selected the modes 1, 3, and 5 by using the list box, the method creates an expression with(1,u)+with(3,u)+with(5,u). This expression is then used for the x-displacement (dependent variable u) in a displacement plot. In a similar way, the method automatically creates expressions for the variables v and w associated with the y- and z-displacement, respectively. Note that the command with(), used in results as in the example above, is different from the built-in with() command used to shorten syntax that is described in “With, Get, and Set Methods” on page 206.

Appendix A—Form Objects | 171

Page 172: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 172 Tuesday, December 16, 2014 4:16 PM

Table

A table object represents a table with rows and columns that can be used to define input or output. The figure below shows an example of a running application with a table object used to accept input:

The Sources section of a Table Settings window contains a table with four columns:• Header• Width• Editable• Data source

Each row in this table defines a column in the table object.

172 | Appendix A—Form Objects

Page 173: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 173 Tuesday, December 16, 2014 4:16 PM

The figure below shows an example of a table with three columns where the data source references three 1D string arrays.

The string arrays define the initial values for the rows corresponding to the three columns as shown in the figure below:

Appendix A—Form Objects | 173

Page 174: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 174 Tuesday, December 16, 2014 4:16 PM

TABLE OPTIONS

In this section, you can activate the following table options:• Automatically add new rows• Rows can be added• Rows can be deleted• Table can be cleared• Rows can be moved up and down• Table data can be saved to a file and loaded

Slider

A slider is a form object for choosing numerical input using a slider control.

USING A SLIDER TO CHANGE THE MAGNITUDE OF A STRUCTURAL LOAD

Consider an application where the magnitude of a load can be changed by a slider control, such as in the figure below.

In this example, the slider is accompanied by an input field that is used to display the selected value.

174 | Appendix A—Form Objects

Page 175: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 175 Tuesday, December 16, 2014 4:16 PM

The Settings window of the slider is shown in the figure below.

In this example, the slider uses a global parameter F as its source.You determine the range of values for the data source by defining the maximum and minimum value and the number of steps for the slider.From the Value type list, choose Integer (the default) or Real depending on the type of data in the data source for the slider. In the Value field, enter the initial value for the slider.

Toolbar

A toolbar object contains the specifications of a toolbar with toolbar buttons. The figure below shows a toolbar with buttons for Save, Save As, and Exit.

Appendix A—Form Objects | 175

Page 176: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 176 Tuesday, December 16, 2014 4:16 PM

The Settings window for this toolbar is shown in the figure below.

Each row in the Toolbar Items table contains either an item, corresponding to a toolbar button, or a separator. Use the buttons below the table to add items or separators, change row order, or to delete a row. Click the Edit button to display

176 | Appendix A—Form Objects

Page 177: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 177 Tuesday, December 16, 2014 4:16 PM

the Settings window associated with each row. The figure below shows the Settings window of the Save As item.

The text in the Text field will be shown as a tooltip when hovering over the toolbar button. The Icon list and the Keyboard shortcut field represent the same functionality as for a button object. For more information, see “Button” on page 38.

Spacer

A spacer object is invisible in the user interface and is only used when working in grid layout mode. It defines a space of fixed size that you can use to ensure that neighboring form objects have enough space to show their contents. Typically, you would use a spacer next to a table or graphics object to ensure that they are

Appendix A—Form Objects | 177

Page 178: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 178 Tuesday, December 16, 2014 4:16 PM

rendered properly. If the user resizes the window so that it becomes smaller than the size of the spacer, the effective size of the window is maintained by displaying scroll bars. The figure below shows the Settings window of a spacer object.

178 | Appendix A—Form Objects

Page 179: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 179 Tuesday, December 16, 2014 4:16 PM

Appendix B—Grid Layout Mode

The Application Builder defaults to sketch layout mode, which lets you use fixed positions and sizes of objects. Use grid layout mode to position and size objects based on a background grid. This layout mode is more suitable for designing a resizable user interface.Switch to grid layout mode by clicking Grid in the Layout group in the ribbon. The buttons and menus in the Grid group give you easy access to commands for:• Changing the row and column growth rules between Fit, Grow, and Fixed,

which determine the layout when the user interface is resized• Inserting or removing rows and columns• Aligning form objects within grid cells• Merging and splitting cells

THE FORM SETTINGS WINDOW AND THE GRID

After switching to grid layout mode, the form window shows blue grid lines. The section Grid Layout for Contained Form Objects, in the Settings window, shows column widths and row heights.

Appendix B—Grid Layout Mode | 179

Page 180: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 180 Tuesday, December 16, 2014 4:16 PM

To change the overall size of a form, as displayed in the Form Editor, click the top left corner of the form.

A blue frame is now shown with handles for resizing in the horizontal and vertical direction, respectively.

ADJUSTING ROWS AND COLUMNS

Click the leftmost cell of a row to select it. The leftmost cells are only used for selecting rows, form objects cannot be inserted there. When a row is selected, the Row Settings menu and the Insert and Remove commands are enabled in the ribbon tab. The figure below shows the fourth row highlighted.

180 | Appendix B—Grid Layout Mode

Page 181: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 181 Tuesday, December 16, 2014 4:16 PM

Similarly for selecting a column, click the cell at the top. This cell cannot contain any form objects. The figure below shows the third column highlighted. In this case, the Column Settings menu is enabled in the ribbon tab.

The Row Settings and the Column Settings have the same three options:• Fit sets the row height or column width to the smallest possible value given

the size of the form objects in that row or column• Grow sets the row height or column width to grow proportionally to the

overall size of the form• Fixed sets a fixed value for the number of pixels for the row height or column

width

You can interactively change the row height and column width by dragging the grid lines.

In this case, the number of pixels will be displayed and the Row Settings or Column Settings growth policy will be changed automatically to Fixed.

Appendix B—Grid Layout Mode | 181

Page 182: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 182 Tuesday, December 16, 2014 4:16 PM

SELECTING, SPLITTING, AND MERGING CELLS

Click an individual cell to select it. A selected cell is shown with deeper blue grid lines.

You can select Merge Cells and Split Cells to adjust the cell size and layout to your form objects.

ALIGNING FORM OBJECTS

The Align menu gives you options for aligning form objects within the cell. You can also let a form object dynamically fill the cell horizontally or vertically.

As an alternative, you can right-click a form object and select from a context menu.

182 | Appendix B—Grid Layout Mode

Page 183: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 183 Tuesday, December 16, 2014 4:16 PM

DRAG AND DROP FORM OBJECTS

You can move form objects by drag and drop. Click a form object to select it, and then drag it to another cell that is not already occupied with another form object.

Appendix B—Grid Layout Mode | 183

Page 184: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 184 Tuesday, December 16, 2014 4:16 PM

Appendix C—Keyboard Shortcuts

The table below lists the keyboard shortcuts available in the Application Builder.

SHORTCUT ACTION APPLICATION BUILDER

FORM EDITOR

METHOD EDITOR

Ctrl+A Select all Ctrl+D Deselect all Ctrl+C Copy Ctrl+V Paste Del Delete Ctrl+N Create a new application Ctrl+S Save an application Ctrl+F8 Test an application Ctrl+Shift+F8 Apply changes Ctrl+R Record code F11 Go to node F12 Extract variable F1 Display help F2 Rename applicable nodes F3 Disable applicable nodes F4 Enable applicable nodes Ctrl+up arrow Move applicable nodes up Ctrl+down arrow Move applicable nodes down Ctrl+Z Undo Ctrl+Y Redo (Ctrl+Shift+Z on Mac) F5 Continue (in debugger) F6 Step (in debugger) F7 Step into (in debugger) F8 Check syntax Ctrl+F Find and replace text in methods Ctrl+space Autocomplete method code Ctrl+U Make selected code lowercase Ctrl+Shift+U Make selected code uppercase

184 | Appendix C—Keyboard Shortcuts

Page 185: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 185 Tuesday, December 16, 2014 4:16 PM

Ctrl+B Toggle breakpoint on selected line Ctrl+scroll wheel up Zoom in, in method code window Ctrl+scroll wheel down

Zoom out, in method code window

Ctrl+all arrow keys Fine-tune position of selected form objects

All arrow keys Fine-tune position of selected form objects

Ctrl+Alt+A Go to Application Builder window Ctrl+Alt+M Go to Method Editor Ctrl+Alt+F Go to Form Editor Ctrl+Alt+left-click Create a local method Alt+F4 Close window Ctrl+F4 Close document Ctrl+Shift+F4 Close all documents

SHORTCUT ACTION APPLICATION BUILDER

FORM EDITOR

METHOD EDITOR

Appendix C—Keyboard Shortcuts | 185

Page 186: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 186 Tuesday, December 16, 2014 4:16 PM

Appendix D—Built-in Method Library

This appendix contains listings of all built-in functions and methods for use in the Method Editor, except for methods that operate on the model object. For more information on the model object and its methods, see the COMSOL Programming Reference Manual.The syntax rules are those of the Java® programming language. Note that each line of code needs to end with a semicolon (;), but the semicolon is omitted in the listings below.

186 | Appendix D—Built-in Method Library

Page 187: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

getC d

m

getC d

getC

getS

w

getS

w

getS

w

toBoll

toBo

toBo"

toDo

IntroductionToApplicationBuilder.book Page 187 Tuesday, December 16, 2014 4:16 PM

Conversion Methods

SYNTAX DESCRIPTION

olumn String[] getColumn(String[][] matrix, int column)

Returns a String[] for a specifiecolumn in the matrix. Useful when values have been read froa file and only certain columns should be shown in a table.

olumn double[] getColumn(double[][] matrix, int column)

Returns a double[] for a specifiecolumn in the matrix.

olumn int[] getColumn(int[][] matrix, int column)

Returns an int[] for a specified column in the matrix.

ubMatrix String[][] getSubMatrix(String[][] matrix, int startCol, int endCol, int startRow, int endRow)

Returns a rectangular submatrixof the input matrix spanning columns from startCol to endCol, and rows from startRoto endRow.

ubMatrix double[][] getSubMatrix(double[][] matrix, int startCol,int endCol, int startRow, int endRow)

Returns a rectangular submatrixof the input matrix spanning columns from startCol to endCol, and rows from startRoto endRow.

ubMatrix int[][] getSubMatrix(int[][] matrix, int startCol, int endCol, int startRow, int endRow)

Returns a rectangular submatrixof the input matrix spanning columns from startCol to endCol, and rows from startRoto endRow.

olean boolean toBoolean(String str) Converts the given string to a Boolean. ("true" returns true, aother strings return false)

olean boolean [] toBoolean(String[]... strs) Converts all the strings in the given array to Booleans ("true" returns true, all other strings return false) and returns a Boolean array.

olean boolean [][] toBoolean(String[][]... strs)

Converts all the strings in the given matrix to Booleans ("truereturns true, all other strings return false) and returns a Boolean matrix.

uble double toDouble(String str) Converts the given string to a double.

Appendix D—Built-in Method Library | 187

Page 188: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

toDo

toDo

toIn

toIn

toIn

toSt

toSt

toSt a

toSt f

toSt

toSt ns

toSt

toSt ns

NAM

IntroductionToApplicationBuilder.book Page 188 Tuesday, December 16, 2014 4:16 PM

uble double[] toDouble(String... strs) Converts all the strings in the given arrray to doubles and returns a double array.

uble double[][] toDouble(String[]... strs) Converts all the strings in the given matrix to doubles and returns a double matrix.

t int toInt(String str) Converts the given string to aninteger.

t int[] toInt(String... strs) Converts all the strings in the given arrray to integers and returns an integer array.

t int[][] toInt(String[]... strs) Converts all the strings in the given matrix to integers and returns an integer matrix.

ring String toString(int value) Converts the given integer to astring.

ring String toString(double value) Converts the given double to astring.

ring String toString(boolean value) Converts the given Boolean tostring.

ring String toString(double value, int decimals)

Converts the given double to astring with the given number odecimals.

ring String toString(double value, String format)

Converts the given double to astring using the given format specifier, which is the same as java.util.Formatter. See the corresponding Java format string documentation for moreinformation.

ring String[] toString(double[] darray) Converts all the doubles in thegiven array to strings and retura string array.

ring String[][] toString(double[][] dmatrix) Converts all the doubles in thegiven matrix to strings and returns a string matrix.

ring String[] toString(int[] iarray) Converts all the integers in thegiven array to strings and retura string array.

E SYNTAX DESCRIPTION

188 | Appendix D—Built-in Method Library

Page 189: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

toSt

toSt e ns

toSt e

NAME

IntroductionToApplicationBuilder.book Page 189 Tuesday, December 16, 2014 4:16 PM

ring String[][] toString(int[][] imatrix) Converts all the integers in thegiven matrix to strings and returns a string matrix.

ring String[] toString(boolean[] barray) Converts all the Booleans in thgiven array to strings and retura string array.

ring String[][] toString(boolean[][] bmatrix)

Converts all the Booleans in thgiven matrix to strings and returns a string matrix.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 189

Page 190: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

conc

concn le n d

cont

find es

find1

find

leng

repl as t.

splies.

spli

subs en

uniq

IntroductionToApplicationBuilder.book Page 190 Tuesday, December 16, 2014 4:16 PM

String Methods

E SYNTAX DESCRIPTION

at String concat(String separator, String ... strs)

Concatenates the given varargs-array of strings into a single string using the given separator.

at String[] concat(String colSepar, String rowSepar, String[]... matr)

Concatenates the given string-matrix (which can be giveas a varargs of rows) into a singstring. Puts colSepar betweevalues of columns on a row, anrowSepar between rows.

ains boolean contains(String[] strs, String str)

Returns true if the given string array contains the given string.

int[] find(String[] strs, String str) Returns an array with the indicto all occurrences of str in strs.

In int findIn(String[] strs, String str) Returns the index to the first occurrence of str in strs or -if no match.

In int findIn(String str, String toFind) Returns the first index of str that is the start of the substringtoFind. If there is no substringmatching toFind in str, -1 isreturned.

th int length(String str) Returns the length of the stringstr.

ace String replace(String str, String orig, String replacement)

Returns a string where orig hbeen replaced by replacemen

t String[] split(String str) Returns an array of strings by splitting the given string at spac

t String[] split(String str, String separator)

Returns an array of strings by splitting the given string at the given separator.

tring String substring(String str, int start, int length)

Returns a substring with the givlength starting at the given position.

ue String[] unique(String[] strs) Returns an array of strings withthe unique values in the given array of strings.

190 | Appendix D—Built-in Method Library

Page 191: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

copy ay

copy g

copy ay

s.

copye

copy ay

s.

copye

equa

equa

equa e

equa e

IntroductionToApplicationBuilder.book Page 191 Tuesday, December 16, 2014 4:16 PM

Collection Methods

SYNTAX DESCRIPTION

String[] copy(String... toCopy) Returns a copy of the given arrof strings, which can also be specified as a varargs of strings.

String[][] copy(String[]... toCopy) Returns a copy of the given strinmatrix, which can also be specified as a vararags of rows (string arrays).

double[] copy(double... toCopy) Returns a copy of the given arrof doubles, which can also be specified as a varargs of double

double[][] copy(double[]... toCopy) Returns a copy of the given double matrix, which can also bspecified as a vararags of rows (double arrays).

int[] copy(int... toCopy) Returns a copy of the given arrof integers, which can also be specified as a varargs of integer

int[][] copy(int[]... toCopy) Returns a copy of the given integer matrix, which can also bspecified as a vararags of rows (integer arrays).

ls boolean equals(String[] str1, String[] str2)

Returns true if all strings in the given array are equal and they have the same number of elements.

ls boolean equals(String[][] matr1, String[][] matr2)

Returns true if all strings in the given matrix are equal and theyhave the same number of elements.

ls boolean equals(int[] ints1, int[] ints2)

Returns true if all integers in thgiven array are equal and they have the same number of elements.

ls boolean equals(int[][] ints1, int[][] ints2)

Returns true if all integers in thgiven matrix are equal and theyhave the same number of elements.

Appendix D—Built-in Method Library | 191

Page 192: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

equa

- f

).

equa lt

equa ~

equa lt

equa ~ in ns

e

equa lt

sort

sort s.

sort s.

merg all en

NAM

IntroductionToApplicationBuilder.book Page 192 Tuesday, December 16, 2014 4:16 PM

ls boolean equals(double dl1, double dl2, double relErrorTolerance)

Compares whether the relativeerror of two doubles is within allowed tolerance using abs( ( ab ) / b ), where b is the larger othe doubles (by absolute value

ls boolean equals(double dl1, double dl2) Same as above, but uses a defaurelErrorTolerance of 0.0001.

ls boolean equals(double[] dbls1, double[] dbls2, double relErrorTolerance)

Compares the relative errors (abs( ( a - b) / b ) of elements inthe arrays pairwise and returnstrue if all the relative errors arebelow relErrorTolerance (and the arrays have the same number of elements).

ls boolean equals(double[] dbls1, double[] dbls2)

Same as above, but uses a defaurelErrorTolerance of 0.0001.

ls boolean equals(double[][] dbls1, double[][] dbls2, double relErrorTolerance)

Compares the relative errors (abs( ( a - b ) / b ) of elements the matrices pairwise and returtrue if all the relative errors arebelow relErrorTolerance (and the matrices have the samnumber of elements).

ls boolean equals(double[][] dbls1, double[][] dbls2)

Same as above, but uses a defaurelErrorTolerance of 0.0001.

sort(String[] strs) Sorts the given array of strings.NOTE: The array is sorted in place.

sort(int[] ints) Sorts the given array of integerNOTE: The array is sorted in place.

sort(double[] doubles) Sorts the given array of doubleNOTE: The array is sorted in place.

e merge(String[]... toMerge) Returns an array of strings with the strings merged from the givarrays.

E SYNTAX DESCRIPTION

192 | Appendix D—Built-in Method Library

Page 193: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

merg th

merg th

NAME

IntroductionToApplicationBuilder.book Page 193 Tuesday, December 16, 2014 4:16 PM

e merge(int[]... toMerge) Returns an array of integers wiall the integers from the two given arrays.

e merge(double[]... toMerge) Returns an array of doubles wiall the doubles from the two given arrays.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 193

Page 194: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

readF iven

openF be

the

openB the

readM en file e

del

readS en ile e

del

readC

. 180

write

nly.

write the eans

nly.

IntroductionToApplicationBuilder.book Page 194 Tuesday, December 16, 2014 4:16 PM

File Methods

SYNTAX DESCRIPTION

ile String readFile(String name)

Returns the contents in the gfile as a string.

ileStreamReader CsReader openFileStreamReader(String name)

Returns a CsReader that canused to read line by line or character by character from given file.

inaryFileStreamReader CsBinaryReader openBinaryFileStreamReader(String name)

Returns a CsBinaryReaderthat can be used to read fromgiven file byte by byte.

atrixFromFile double[][] readMatrixFromFile(String name)

Reads the contents of the givfile into a double matrix. Thehas the same spreadsheet typformat as available in the motree Export node.

tringMatrixFromFile String[][] readStringMatrixFromFile(String name)

Reads the contents of the givfile into a string matrix. The fhas the same spreadsheet typformat as available in the motree Export node.

SVFile String[][] readCSVFile(String name)

Reads a file with comma-separated values (CSV-file) into a string matrixExpects file to use the RFC 4format for CSV.

File writeFile(String name, String contents)

Writes the given string to thegiven file. Embedded files (embedded:///) are read-o

File writeFile(String name, double[][] data)

Writes the given data to the given file. The format used isspreadsheet format, which mit can be read by readMatrixFromFile. Embedded files (embedded:///) are read-o

194 | Appendix D—Built-in Method Library

Page 195: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

writethe ans

le.

nly.

openF ded

openB the ded

write o a

nly. for

write to a

nly. for

exist n t a ind ile ates ), ists

NAME

IntroductionToApplicationBuilder.book Page 195 Tuesday, December 16, 2014 4:16 PM

Example CodeThe code below reads user provided experimental data in a .csv file. A file import object is used to allow the user of an application to upload the file. The file is then input to the physics interface for least-squares fitting. If the file doesn’t exist, the user is asked to confirm that default data will be used.

File writeFile(String name, String[][] data)

Writes the given data to the given file. The format used is spreadsheet format, which meit can be read by readStringMatrixFromFiEmbedded files (embedded:///) are read-o

ileStreamWriter CsWriter openFileStreamWriter(String name)

Returns a CsWriter that canwrite to the given file. Embedfiles (embedded:///) are read-only.

inaryFileStreamWriter CsBinaryWriter openBinaryFileStreamWriter(String name)

Returns a CsBinaryWriterthat can be used to write to given file byte by byte. Embedfiles (embedded:///) are read-only.

CSVFile writeCSVFile(String name, String[][] data)

Writes the given string data tCSV file. Embedded files (embedded:///) are read-oThe RFC 4180 format is usedthe CSV.

CSVFile writeCSVFile(String name, double[][] data)

Writes the given double data CSV file. Embedded files (embedded:///) are read-oThe RFC 4180 format is usedthe CSV.

s boolean exists(String name) Tests whether a file with givename exists. If the name is nopath name, exists tries to fout if there is an embedded fwith that name (e.g. concatenembedded:/// to the nameand only then tests if a file exwith the given path.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 195

Page 196: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 196 Tuesday, December 16, 2014 4:16 PM

if (exists("upload:///experimental.csv")) { with(model.physics("opt").feature("glsobj1")); set("fileName", "upload:///experimental.csv"); endwith();}else{ String s_data = confirm("No experimental data file was uploaded. Do you want to use the embedded data?", "Experimental Data", "Yes", "Cancel Parameter Estimation"); if(s_data.equals("Cancel Parameter Estimation")){ return; }}

196 | Appendix D—Built-in Method Library

Page 197: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

file

exec h

s .

exec h

to

er

y,

play e re

play

IntroductionToApplicationBuilder.book Page 197 Tuesday, December 16, 2014 4:16 PM

System Methods

Example CodeThe line of code below plays one of the sounds available in the data/sounds folder of the COMSOL installation and that has been embedded in the application and stored in the Sounds library.

playSound("embedded:///success_1.wav");

SYNTAX DESCRIPTION

Open fileOpen(String name) Opens the file represented by name with the associated program on the client.

uteOSCommand String executeOSCommand(String command, String... params)

Executes the OS command witthe given command (full path) and parameters. Execution timeout after a default 180 secondsReturns everything the processprinted to its out-stream as a string. When applicable, the command is run server-side.

uteOSCommand String executeOSCommand(String command, int timeoutSec, String params...)

Executes the OS command witthe given command (full path) and parameters. Returns everything the process printed its out-stream as a string. The execution is forcibly stopped afttimeoutSec seconds if the command has not finished. To disable the timeout functionalittimeoutSec value 0 can be used. When applicable, the command is run server-side.

Sound playSound(String name) Plays the sounds in the given filon the client. Only .wav files asupported; no external librariesare required.

Sound playSound(double hz, int millis) Plays a signal at given frequencyhz and with given duration millis in milliseconds on theclient.

Appendix D—Built-in Method Library | 197

Page 198: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 198 Tuesday, December 16, 2014 4:16 PM

The line of code below opens a PDF file embedded in the application and stored in the File library.

fileOpen("embedded:///tubular_reactor.pdf");

As an alternative, a method with an input argument can be written and called from a command sequence of a menu item or button, as shown in the example below. The figure below shows a method b_open_pdf that opens a file with filename as input argument.

198 | Appendix D—Built-in Method Library

Page 199: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 199 Tuesday, December 16, 2014 4:16 PM

The figure below shows the corresponding command sequence for a ribbon menu item.

Appendix D—Built-in Method Library | 199

Page 200: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

Call ds

call

useG

cs or

useF

close.

dial t

aler xt.

aler xt

confe

IntroductionToApplicationBuilder.book Page 200 Tuesday, December 16, 2014 4:16 PM

GUI-Related Methods

E SYNTAX DESCRIPTION

a method directly <methodName>() Call a method from the Metholist by using its name, for example, method1(), method2().

Method callMethod(String name) Alternate way to call a methodfrom the Methods list; used internally and in cases of namecollisions.

raphics useGraphics(ModelEntity entity, String name)

Plots the given entity (Plot Group, Geometry, Mesh or Explicit Selection) in the graphiform object given by the name name path in the second argument.

orm useForm(String name) Shows the form with the givenname in the current main window. Equivalent to the usemethod of a Form object; see below.

eDialog closeDialog(String name) Closes the form, shown as a dialog box, with the given nam

og dialog(String name) Shows the form with the givenname as a dialog box. Equivalento the dialog method of a Form object; see below.

t alert(String text) Stops execution and displays analert message with the given te

t alert(String text, String title)

Stops execution and displays analert message with the given teand title.

irm String confirm(String text) Stops execution and displays a confirmation dialog box with thgiven text. It also displays two buttons, “Yes” and “No”. The method returns “Yes” or “No”depending on what the user clicks.

200 | Appendix D—Built-in Method Library

Page 201: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

confe ys

confe ys

confe ys

erro

requ

e

requ

e is xt

NAME

IntroductionToApplicationBuilder.book Page 201 Tuesday, December 16, 2014 4:16 PM

irm String confirm(String text, String title)

Stops execution and displays a confirmation dialog box with thgiven text and title. It also displatwo buttons, “Yes” and “No”. The method returns “Yes” or “No” depending on what the user clicks.

irm String confirm(String text, String title, String yes, String no)

Stops execution and displays a confirmation dialog box with thgiven text and title. It also displatwo buttons with the given strings as labels. The method returns the label of the button that the user clicks.

irm String confirm(String text, String title, String yes, String no, String cancel)

Stops execution and displays a confirmation dialog box with thgiven text and title. It also displathree buttons with the given strings as labels. The method returns the label of the button that the user clicks.

r error(String message) Stops execution and opens an error dialog box with the givenmessage.

est String request(String text) Stops execution and displays a dialog box with a text field, requesting input from the user. The given text is the label of thtext field. The method returns the entered text or null if thecancel button is clicked.

est String request(String text, String defaultString)

Stops execution and displays a dialog box with a text field, requesting input from the user. The given text is the label of thtext field and the default string the text initially shown in the tefield. The method returns the entered text or null if the cancel button is clicked.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 201

Page 202: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

requ

e e

l

mess e .

evalct th or

getC

t

d

setF m r

setF m r

NAM

IntroductionToApplicationBuilder.book Page 202 Tuesday, December 16, 2014 4:16 PM

est String request(String text, String title, String defaultString)

Stops execution and displays a dialog box with a text field, requesting input from the user.The given text is the label of thtext field, the default string is thtext initially shown in the text field, and the title is the title of the dialog box. The method returns the entered text or nulif the cancel button is clicked.

age message(String message) Sends a message to the messaglog if available in the application

uateToTable evaluateToTable(NumericalFeature entity, String name, boolean clear)

Evaluates the given entity, a Derived Value, in the table objegiven by the name or name pain the second argument, whichwill then be the default target fthe evaluations of the Derived Value. If the third argument is true, the table is cleared beforeadding the new data; otherwisethe data is appended.

hoiceList ChoiceList getChoiceList(String name)

Returns an object of the type ChoiceList, inherited from DataSource, representing a choice list node under the declarations branch. The type ChoiceList has methods thamake it easier to change the matrix value with respect to changing and accessing values andisplay names individually.

ormObjectEnabled setFormObjectEnabled(String name, boolean enabled)

Sets the enable state for the forobject specified by the name oname path.

ormObjectVisible setFormObjectVisible(String name, boolean visible)

Sets the visible state for the forobject specified by the name oname path.

E SYNTAX DESCRIPTION

202 | Appendix D—Built-in Method Library

Page 203: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

setF ct

it e

setF

NAME

IntroductionToApplicationBuilder.book Page 203 Tuesday, December 16, 2014 4:16 PM

Example CodeThis line of code displays the visualization of plot group 5 (pg5) in the graphics object graphics1 in the form with the name Temperature.

useGraphics(model.result("pg5"), "/Temperature/graphics1");

The code below displays the mesh in the model tree node mesh1 in the graphics object graphics1 contained in the card of a card stack. The second line runs a zoom extents command to ensure proper visualization of the mesh.

useGraphics(model.mesh("mesh1"), "/mesh/cardstack1/card1/graphics1");zoomExtents("/mesh/cardstack1/card1/graphics1");

The code below displays a request dialog box that lets the user type in a file name used for an HTML report. If the user has typed a file name, then a report is generated.

String answerh = request("Enter file name","File Name", "Untitled.html");if(answerh != null){ model.result().report("rpt1").set("format","html"); model.result().report("rpt1").set("filename","appuser:///"+answerh); model.result().report("rpt1").run();}

The code below is similar to the code above, but in this case the report is saved in Microsoft Word format (.docx).

String answerw = request("Enter file name","File Name", "Untitled.docx");if(answerw != null){ model.result().report("rpt2").set("format","docx"); model.result().report("rpt2").set("filename","appuser:///"+answerw); model.result().report("rpt2").run();}

ormObjectText setFormObjectText(String name, String text)

Sets the text for the form objespecified by the name or namepath in the second argument. This method throws an error ifis impossible to set a text for thspecified form object.

ormObjectEditable setFormObjectEditable(String name, boolean editable)

Sets the editable state for the form object specified by the name or name path. This functionality is only available fortext field objects.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 203

Page 204: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

setL he e

setLf

setV o is

setD is

ue.

getV

getD e

getV

getD

IntroductionToApplicationBuilder.book Page 204 Tuesday, December 16, 2014 4:16 PM

ChoiceList Methods

Example CodeThe code below adds the string Aluminum 3004 to the choice list from the example in the section “Using a Combo Box to Change Material” on page 126. Note that the choice list index starts at 0 whereas the material tags start at 1 (mat1, mat2, mat3, and mat4).

ChoiceList choiceList = getChoiceList("choicelist1");choiceList.setListRow("mat4", "Aluminum 3004", 3);

E SYNTAX DESCRIPTION

istRow setListRow(String value, String displayName, int row)

Sets the value and display namefor the given row (0-based). If trow is equals to the length of thlist, a new row is added.

ist setList(String[] values, String[] displayNames)

Sets all the values and display names, replacing the contents othe choice list.

alue setValue(String value, int row) Sets the value for the given row(0-based). If the row is equals tthe length of the list a new rowadded with the value and an empty display name.

isplayName setDisplayName(String displayName, int row)

Sets the display name for the given row (0-based). If the rowequals to the length of the list anew row is added with the display name and an empty val

alue String getValue(int row) Returns the value for the givenrow (0-based).

isplayName String getDisplayName(int row) Returns the display name for thgiven row (0-based).

alues String[] getValues() Returns all values as an array.

isplayNames String[] getDisplayNames() Returns all display names as anarray.

204 | Appendix D—Built-in Method Library

Page 205: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

save

save

exit

file

zoom

tran n

scen

IntroductionToApplicationBuilder.book Page 205 Tuesday, December 16, 2014 4:16 PM

Built-In GUI Methods

Example CodeThe line of code below saves a document to the user folder (as specified in the Preferences).

fileSaveAs("user:///mixer.docx");

SYNTAX DESCRIPTION

Application saveApplication() Saves the application.

ApplicationAs saveApplicationAs() Saves the application under a different name. (Or as an MPH-file.)

exit() Exits the application.

SaveAs fileSaveAs(String file) Downloads a file to the client.

Extents zoomExtents(String graphicsName) Zoom extents in the given graphics object.

sparency transparency(String graphicsName) Toggle transparency in the givegraphics object.

elight sceneLight(String graphicsName) Toggle scene light in the given graphics object.

Appendix D—Built-in Method Library | 205

Page 206: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

with

he

endw t

e an

set e ll

set ty et

set ty et

setet e

ts

IntroductionToApplicationBuilder.book Page 206 Tuesday, December 16, 2014 4:16 PM

With, Get, and Set Methods

E SYNTAX DESCRIPTION

with(target) Sets a target to use with subsequent calls to set(), setIndex(), getString() etc.

Unset the target afterward withendwith(). The calls to with()/endwith() can be nested.

Supported target types (from tCOMSOL Model Object) are:PropFeature ParameterEntity PhysicsPropElemFeature ExpressionBase GeomObjectSelectionMaterialModel ShapeFeatureFeatureInfo

ith endwith() Unsets the target set by the lascall to with(), restoring the target that was set before it. Thcalls to with()/endwith() cbe nested.

set(String name, boolean value) Sets the Boolean property valufor the target set by the last cato with().

set(String name, boolean[] value) Sets the boolean vector properor parameter value for the targset by the last call to with().

set(String name, boolean[][] value)

Sets the boolean matrix properor parameter value for the targset by the last call to with().

set(String name, double value) Sets a double property or parameter value for the target sby the last call to with(). If thtarget set was of the ExpressionBase type, this sethe variable name and expression.

206 | Appendix D—Built-in Method Library

Page 207: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

set r et

set et

setet e

, n

set et f

, t n

set or et

setet

ts

set or et

e e

set or

NAME

IntroductionToApplicationBuilder.book Page 207 Tuesday, December 16, 2014 4:16 PM

set(String name, double[] value) Sets a double vector property oparameter value for the target sby the last call to with().

set(String name, double[][] value)

Sets double matrix property orparameter value for the target sby the last call to with().

set(String name, int value) Sets an integer property or parameter value for the target sby the last call to with(). If thtarget is of the GeomObjectSelection typethis sets the selection of the giveobject to be an entity.

set(String name, int[] value) Sets an integer vector propertyor parameter value for the targset by the last call to with(). Ithe target is of GeomObjectSelection typethis sets the selection to be thegiven entities of the given objecand does not affect the selectioof other objects.

set(String name, int[][] value) Sets a integer matrix property parameter value for the target sby the last call to with().

set(String name, String value) Sets a string property or parameter value for the target sby the last call to with().

If the target set was of the ExpressionBase type, this sethe variable name and expression.

set(String name, String[] value) Sets the string vector property parameter value for the target sby the last call to with().

If the target is of the FeatureInfo type, this sets thuser-defined expressions for thfirst definition.

set(String name, String[][] value)

Sets the string matrix property parameter value.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 207

Page 208: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

set n, et

set n, et

set n

set n

setI r e

setI r e

setI r e

setI r e

setI r e

setI or e

NAM

IntroductionToApplicationBuilder.book Page 208 Tuesday, December 16, 2014 4:16 PM

set(String var, String expr, String descr)

Sets a variable name, expressioand description for the target sby the last call to with().

set(String var, double expr, String descr)

Sets a variable name, expressioand description for the target sby the last call to with().

set(String... onames) Sets the selection to be the giveobjects for the target set by thelast call to with().

set(String[] onames, int[][] entities)

Sets the selection to be the giveentities of the given objects forthe target set by the last call towith(). Does not affect the selection of other objects.

ndex setIndex(String name, String value, int index)

Sets a string vector property oparameter value at index for thtarget set by the last call to with(). Index in vector is 0-based.

ndex setIndex(String name, String value, int firstIndex, int secondIndex)

Sets a string matrix property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, String[] value, int index)

Sets a string matrix property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, double value, int index)

Sets a string vector property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, double value, int firstIndex, int secondIndex)

Sets a string matrix property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, double[] value, int index)

Sets a double matrix property parameter value at index for thtarget set by the last call to with().

E SYNTAX DESCRIPTION

208 | Appendix D—Built-in Method Library

Page 209: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

setI r e

setI r e

setI

getI y ll

getI y ll

getB e

getB or

o d n.

getB or

getD

getS

getS g et

NAME

IntroductionToApplicationBuilder.book Page 209 Tuesday, December 16, 2014 4:16 PM

ndex setIndex(String name, int value, int index)

Sets a string vector property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, int value, int firstIndex, int secondIndex)

Sets a string matrix property oparameter value at index for thtarget set by the last call to with().

ndex setIndex(String name, int[] value, int index)

Sets an integer matrix propertyor parameter value at index forthe target set by the last call towith().

ntArray int[] getIntArray(String name) Gets an integer vector propertfor the target set by the last cato with().

ntMatrix int[][] getIntMatrix(String name) Gets an integer matrix propertfor the target set by the last cato with().

oolean boolean getBoolean(String name) Gets a Boolean property for thtarget set by the last call to with().

ooleanArray boolean[] getBooleanArray(String name)

Gets boolean vector property parameter for the target set bythe last call to with().

If the target is of FeatureInftype, this returns the user-defineexpressions for the first definitio

ooleanMatrix boolean[][] getBooleanMatrix(String name)

Gets boolean matrix property parameter for the target set bythe last call to with()

ouble double getDouble(String name) Gets a double property for thetarget set by the last call to with().

tring String getString(String name) Gets a string property or parameter for the target set bythe last call to with().

tring String getString(String name, int index)

Gets a string value from a strinvector property for the target sby the last call to with().

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 209

Page 210: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

getS g et

getD or

getD or

getS r

e

getS

he

e

or

getS r

getD f

getI e

NAM

IntroductionToApplicationBuilder.book Page 210 Tuesday, December 16, 2014 4:16 PM

tring String getString(String name, int firstIndex, int secondIndex)

Gets a string value from a strinmatrix property for the target sby the last call to with().

oubleArray double[] getDoubleArray(String name)

Gets a double vector property parameter for the target set bythe last call to with().

oubleMatrix double[][] getDoubleMatrix(String name)

Gets a double matrix property parameter for the target set bythe last call to with().

tringArray String[] getStringArray(String name)

Gets a string vector property oparameter for the target set bythe last call to with(). If the target is of the type FeatureInfo, this returns theuser-defined expressions for thfirst definition

tringArray String[] getStringArray(String name, int sdim)

Returns the user-defined expressions for the given definition for the target set by tlast call to with(). The argument name is the identifiername, for example, the variablename. The argument sdim is thoccurrence of the definition, which can be larger than zero fvariables that have more than one definition on different geometric entities.

tringMatrix String[][] getStringMatrix(String name)

Gets a string matrix property oparameter for the target set bythe last call to with().

blStringArray String[][] getDblStringArray(String name)

Returns the value as a matrix ostrings for the target set by thelast call to with().

nt int getInt(String name) Gets an integer property for thtarget set by the last call to with().

E SYNTAX DESCRIPTION

210 | Appendix D—Built-in Method Library

Page 211: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

get r

desc or

NAME

IntroductionToApplicationBuilder.book Page 211 Tuesday, December 16, 2014 4:16 PM

Example CodeThe code below sets the global parameter L to a fixed value.

with(model.param()); set("L", "10[cm]");endwith();

The code below sets the title and color legend of a plot group pg2.with(model.result("pg2")); set("titletype", "auto");endwith();with(model.result("pg2").feature("surf1")); set("colorlegend", "on");endwith();model.result("pg2").run();

The code below changes the visualization of a plot group pg2 by setting the property looplevel that controls the solution parameter, to the string variable svar, as defined under the Declarations node.

with(model.result("pg1")); set("looplevel", new String[]{svar});endwith();model.result("pg1").run();

The code below sets the material link index to the string variable alloy, defined under the Declarations node.

with(model.material("matlnk1")); set("link", alloy);endwith();

The code below sets the coordinates of a cut point data set cpt1 to the values of the 1D array samplecoords[].

with(model.result().dataset("cpt1")); set("pointx", samplecoords[0]); set("pointy", samplecoords[1]); set("pointz", samplecoords[2]);endwith();

The code below sets the components of a deformation plot.with(model.result("pg7").feature("surf1").feature("def")); setIndex("expr", withstru, 0);

String get(String var) Returns a variable expression fothe target set by the last call towith().

r String descr(String var) Returns a variable description fthe target set by the last call towith().

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 211

Page 212: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

debu

IntroductionToApplicationBuilder.book Page 212 Tuesday, December 16, 2014 4:16 PM

setIndex("expr", withstrv, 1); setIndex("expr", withstrw, 2);endwith();

Debug Methods

Example CodeThe code below prints strings and doubles to the Debug Log window.

xcoords[i] = Math.cos(2.0*Math.PI*divid);ycoords[i] = Math.sin(2.0*Math.PI*divid);debugLog("These are component values for case 1:");debugLog("x:");debugLog(xcoords[i]);debugLog("y:");debugLog(ycoords[i]);

E SYNTAX DESCRIPTION

gLog debugLog(arg) Prints the value of arg to the Debug Log window. The input argument arg can be a scalar, 1D array, or 2D array of the types string, double, int or boolean.

212 | Appendix D—Built-in Method Library

Page 213: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

exte

NAME

invo

e

invo

s

closs

ry

IntroductionToApplicationBuilder.book Page 213 Tuesday, December 16, 2014 4:16 PM

Methods for External C Libraries

External Method

Methods Returned by the External MethodThe external method returns an object of type External with the following methods:

SYNTAX DESCRIPTION

rnal External external(String name) Returns an interface to an external C (native) library givenby the name of the library feature.

SYNTAX DESCRIPTION

ke long invoke(String method, Object... arguments)

Invokes the named native method in the library with the supplied arguments. Strings areconverted to char *. Returns thvalue returned by the method. (Only 32 bits are returned on a32-bit platform.)

keWideString long invokeWideString(String method, Object... arguments)

Invokes the named native method in the library with the supplied arguments. Strings areconverted to wchar_t *. Returnthe value returned by the method. (Only 32 bits are returned on a 32-bit platform.)

e void close() Releases the library and frees resources. If you do not call thimethod, it is automatically invoked when the external librais no longer needed.

Appendix D—Built-in Method Library | 213

Page 214: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAM

star e).

star ,

star , n

setP ss e e

clos .

IntroductionToApplicationBuilder.book Page 214 Tuesday, December 16, 2014 4:16 PM

Progress Methods

E SYNTAX DESCRIPTION

tProgress startProgress() Opens the Progress dialog box(only one is ever open at a tim

tProgress startProgress(boolean indeterminate)

Opens the Progress dialog boxpossibly showing indeterminateprogress.

tProgress startProgress(boolean indeterminate, String message)

Opens the Progress dialog boxpossibly showing indeterminateprogress and using the user-givemessage in the dialog box.

rogress setProgress(int workDone) Sets the progress in the Progredialog box to a given value in thrange 0-100 (values out of rangare converted to 0 or 100).

eProgress closeProgress() Closes the Progress dialog box

214 | Appendix D—Built-in Method Library

Page 215: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

star

e or

or

n

g

e

setP

e or

or

n

g

e

NAME

IntroductionToApplicationBuilder.book Page 215 Tuesday, December 16, 2014 4:16 PM

Example CodeThis example will open a dialog containing a progress bar that is updated after one second to 30% and a little later to 80%. Then finally the dialog is closed:

try { startProgress(); Thread.sleep(1000); setProgress(30);

tProgress startProgress(String name) Resets the value of the given ProgressBar to 0. The ProgressBar to control can bspecified by the absolute path, fexample, form1/progressbar1. It canalso be specified by a name related to the context from which the method was called, fexample, progressbar2. Thisworks, for example, if the method was called from a buttothat is in the same form as the progress bar in question. Nothinis done if no ProgressBar corresponding to the given namis found.

rogress setProgress(String name, int workDone)

Sets the value of the given ProgressBar in the range 0-100 (values out of range are converted to 0 or 100). The ProgressBar to control can bspecified by the absolute path, fexample, form1/progressbar1. It canalso be specified by a name related to the context from which the method was called, fexample, progressbar2. Thisworks, for example, if the method was called from a buttothat is in the same form as the progress bar in question. Nothinis done if no ProgressBar corresponding to the given namis found.

SYNTAX DESCRIPTION

Appendix D—Built-in Method Library | 215

Page 216: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 216 Tuesday, December 16, 2014 4:16 PM

Thread.sleep(2000); setProgress(80); Thread.sleep(1000); closeProgress();} catch ( InterruptedException e) { // this is required because of the Thread.sleep used in the example to wait a little // between updates to Progress Dialog}

The example below will open a dialog containing a progress bar in an indeterminate mode. The progress bar will not be set to a certain percentage, although it will spin to show that something is being done in the background.

try { startProgress(true); Thread.sleep(5000); closeProgress();} catch ( InterruptedException e) { // this is required because of the Thread.sleep used in the example to wait a little // between updates to Progress Dialog}

Using the technique shown below, it is possible to show a custom message in the Progress dialog box:

try { startProgress(true, "Doing heavy calculations"); Thread.sleep(5000); closeProgress();} catch ( InterruptedException e) { // this is required because of the Thread.sleep used in the example to wait a little // between updates to Progress Dialog}

216 | Appendix D—Built-in Method Library

Page 217: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

NAME

time

curr

curr

IntroductionToApplicationBuilder.book Page 217 Tuesday, December 16, 2014 4:16 PM

Date and Time Methods

SYNTAX DESCRIPTION

Stamp long timeStamp() Current time as milliseconds since midnight, January 1, 1970UTC.

entDate String currentDate() Returns the current date as a string (formatted according to the server's defaults) for the current date.

entTime String currentTime() Returns the current time as a string (not including date, and formatted according to the server's defaults).

Appendix D—Built-in Method Library | 217

Page 218: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 218 Tuesday, December 16, 2014 4:16 PM

Appendix E—Guidelines for Building Applications

General• Include reports to files with input data and corresponding output data.• Make it intuitive. Provide help, hints, and documentation as necessary.• Make it foolproof: “Safe I/O”, “Reset to default data”,…• Save a thumbnail image with the model.• Include a description text (it will be visible in the COMSOL Server library).• Test the application on the computer platforms it is intended for.• Be minimalistic. From the developer’s point of view it makes it much easier

to make sure logic works, to organize, to debug, to maintain, and to develop further. From a user’s point of view it makes it easier to use the application. The minimalistic approach requires more care while developing but much less maintenance later on and much higher adoption among users.

• Embed libraries in the model if they are of manageable size.• Password protect as needed. (Remember: No one can help you if you forget

the password.)

Methods• Don’t create more methods than necessary.

Fewer methods gives you a shorter list of methods to browse through when looking for something. Fewer methods usually means fewer lines of code to worry about.- If several methods you wrote do essentially the same thing, consider merging

them into one method and deal with the different cases by input arguments. - Don’t create a method if it is only called from one place. Insert the code right

into that place instead.

• Create a local method if it is only used in a form, triggered by a form event or a form object event.

• Give methods descriptive names, and name them so that similar methods are grouped together when sorted alphabetically. You will need to remember

218 | Appendix E—Guidelines for Building Applications

Page 219: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 219 Tuesday, December 16, 2014 4:16 PM

less and you will find what you are looking for easier then. Long names are better than hard to understand short names.Method naming examples:- Start all methods that don’t deliver any output by “p”. (p for procedure)- Start all methods that deliver output with “f”. (f for function)- Start all menu item methods with “m”. (m for menu)- Start a method that you will visit frequently with “a” to make it appear first

in the list.- Start all your plot methods with “Plot”. mPlotMesh, mPlotResults, for

example, for menu item methods.• The points above apply to method code as well. Minimalistic, as few lines of

code as possible, as few variables as possible. Give variables descriptive names. Long names are better than hard-to-understand short names. Optimize the code to run efficiently.

• The above points apply to declarations as well. Good names, not more than necessary. Declare variables where they are used (in forms and methods or in the model).

Forms• Don’t create more forms than necessary.• Don’t create a subform unless it is used in more than one form. If it is used

in only one form, include the pieces in that form instead. • Give forms descriptive names. Same reasoning as for methods.• Make good use of the many different types of form objects. Some are good

for some things, some are good for others.• Don’t insert more form objects than necessary. Too many options for input

data may make the application hard to use. Too much output data makes it hard to find important information.

• Insert a text field for the user to leave comments to save with the user’s set of input and output data when saving the application.

• Consider inserting a button with a method to reset to default data.• Apply “Safe I/O”:

- For input fields, alert the user about input data that are “out of bounds”. You can do that either by an alert triggered by an “On Data Change” event set on an input field, or by setting limits in the form objects settings window (which

Appendix E—Guidelines for Building Applications | 219

Page 220: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 220 Tuesday, December 16, 2014 4:16 PM

then sets hard limits). In a method generating the alert you may just warn the user and then allow the input data if the user chooses to go ahead anyways.

- On output fields give the precision that makes sense. If current results are not based on current input data, show it. If the computation failed, show it.

• Include tooltips, help, documentation, hints, and comprehensive reports.• Provide the user information about how long it takes to run the simulation

with default input data on a typical computer. It could be seconds, it could be hours, or even days, depending on the application, so that’s something the user would like to know before hitting the compute button. Consider playing a sound to alert the user when computation has finished. The user may be doing something else while waiting for results. (Sending an email message with a report to the user or some other place when the computation is done may be a better alternative if the computation is really long.)

• Spend some time on the layout of a form. A good looking form makes it easier and more fun to use the application.

• Consider setting keyboard shortcuts for buttons and menu items.

220 | Appendix E—Guidelines for Building Applications

Page 221: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 221 Tuesday, December 16, 2014 4:16 PM

Appendix F—The Application Library Examples

In this first version we have included tutorial applications to demonstrate some of the capabilities of the Application Builder.

Tubular ReactorThis application is designed for students to investigate how the activation energy, heat of reaction, and thermal conductivity in a non-ideal tubular reactor influence the axial and radial conversion and temperature profiles in the reactor.The model embedded in the application is defined using the Chemical Species Transport interface included in COMSOL Multiphysics and does not require any add-on products.The model definition is included in an exercise from Scott Fogler’s text book “Elements of Chemical Reaction Engineering”, which is used in most western universities in undergraduate and graduate courses in the subject of chemical reaction engineering. It describes a non-ideal tubular reactor for the exothermic reaction of propylene oxide with water to form propylene glycol. The reactor has a fully developed laminar flow profile and it is equipped with a cooling jacket around the outer walls of the reactor. These effects are clearly visualized in the output of the application.

Appendix F—The Application Library Examples | 221

Page 222: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 222 Tuesday, December 16, 2014 4:16 PM

Tuning ForkThis application computes the resonant frequency of a tuning fork with a user-defined prong length. Optionally, you can instead give a user-defined target frequency and the application will find the corresponding prong length. The prong and handle radii are taken from a commercially available tuning fork.The model embedded in the application is defined using the Solid Mechanics interface included in COMSOL Multiphysics and does not require any add-on products.The prong length search-algorithm is a secant method with a starting interval based on an analytical expression from beam theory.For more information on the background theory, see the COMSOL Multiphysics Model Library documentation for the model tuning_fork.mph.At the end of the computation, the playSound command is used to produce a sine-wave sound at the computed frequency.

Gas Box DesignA gas box, used in semiconductor processing industry, consists of a bank of (usually 12) mass flow controllers (MFC) which regulate the flow of chemicals in to a process chamber. In order for the MFCs to function correctly, the pressure immediately downstream needs to be below a certain threshold, typically atmospheric pressure. This is needed to maintain a chocked flow condition across the flow controller. There is typically a network of piping which connects the flow controllers to the process chamber. If the flow rate through the controllers is high,

222 | Appendix F—The Application Library Examples

Page 223: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 223 Tuesday, December 16, 2014 4:16 PM

the pipe thickness is very small or the process chamber is high, the back pressure in the pipe network exceeds atmospheric. The application computes the pressure distribution inside a network of pipes, using the Pipe Flow Module, and indicates whether the back pressure will exceed the target threshold.

Appendix F—The Application Library Examples | 223

Page 224: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 224 Tuesday, December 16, 2014 4:16 PM

224 | Appendix F—The Application Library Examples

Page 225: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 225 Tuesday, December 16, 2014 4:16 PM

Index

1D array 72, 161

2D array 74

3D coordinates 159

A action 37, 39, 62

activation condition 126

add-on products 7

alert 110

aligning form objects 179

appearance 35

Application Builder

desktop environment 7, 9

window and tab 9

Application Libraries 7, 16

Application Library 21, 221

Application Library example

gas box design 222

tubular reactor 221

tuning fork 222

application tree 10, 12

Application Wizard 25, 37, 53

applications

launching 21

apply changes 14

array 72

array 1D 72, 161

array 2D 74

array input 159

array syntax 74

B Boolean variable 72, 73, 117

breakpoint 105

built-in methods 186

button 38, 44, 102

command sequence 39

keyboard shortcut 38

size 38

tooltip 38

button click event 39

buttons tab 29

C CAD import 158

card stack 148

cards 148

check box 117

settings window 119

check syntax 96

choice list 75

code completion 101

color legend 118

column settings 181

combo box 69, 121, 126

command sequence 41, 44, 88

common, file scheme 84

COMSOL Client 22

running applications in 22

COMSOL client 7, 14

COMSOL Desktop environment 9

COMSOL Multiphysics 7, 14

COMSOL Server 7, 14, 17

confirm 110

convert to new method 86

creating

applications from models 26

forms 26

methods 13

curly brackets 100

D data change 65

data display 54

data validation 49

debug log window 106

debug tab 12, 104

debugging 104

| 225

Page 226: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 226 Tuesday, December 16, 2014 4:16 PM

Declarations node 67, 117

delete button 41

deleting an object 35

derived values 54

description field, for Boolean variable

119

description text, for derived value 53

displayed text 35

double variable 72, 73

double-click, MPHAPP-file 14

drag and drop, form objects 34, 183

E Edit Local Method 103

embedded, file scheme 78, 112, 154

equation 137

error message 72

errors and warnings window 96

event 37, 62, 65, 102

button click 39

form 65

form object 65

global 62

keyboard shortcut 38

local 62

On close 66

On data change 65

On load 66

On shutdown 62

On startup 62

Event node 63

exit button 175

explicit selection 163

explorer window 10, 12

F file declaration 77

file download 22

file import 77, 151

file reference syntax 112

file scheme

common 84

embedded 78, 112, 154

temp 84

upload 77, 155, 157

user 84

file upload 22

files library 115

fit, row and column setting 179

fixed, row and column setting 179

for statement 109

form 31, 144

form collection 57, 146

Form Editor

desktop location 9

overview 10

preferences 33

using 31

form event 65

form object 10, 37, 67, 116

event 65

with associated methods 89

form reference 57

form tab, in ribbon 10

form window 10

Forms node 31

function 12

G geometry 29

geometry import 158

geometry operations 141, 143

global evaluation 54

global event 62

global method 13

go to method 88

graphics object 27, 29, 44

initial 45

graphics tab 29

graphics, interactive 20

grid layout mode 21, 33, 179

grow, row and column setting 179

226 |

Page 227: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 227 Tuesday, December 16, 2014 4:16 PM

H HTML

code 138

report 85, 203

I icon 45, 56, 112, 177

if statement 109

image 112, 139

images library 112

images, as thumbnails 17

initial size, of main window 57

initial values, of array 73

input argument, to method 104

input field 70

adding 47

text object 53

inputs/outputs

forms 26

tab 27

integer 73

integer variable 72

interactive graphics 20

item, menu 58, 102

item, toolbar 176

J Java® programming language 86, 107,

186

JPG-file format 18

K keyboard shortcut 92, 101, 184

keyboard shortcut event 38, 59, 177

L language elements window 12, 90, 107

LaTeX 53, 55, 137

launching applications 21

layout mode 33, 179

layout options, form collection 146

Libraries node 112, 140

line 138

list box 168

LiveLink™ products 22

local event 62

local method 13, 38, 62, 65, 66, 89, 102,

103

logo image 45

low-resolution displays 22

M main window 57, 141

Main Window node 56

material 126

math functions 109

menu 58, 61

menu bar 56, 57

menu item 44, 102

merging cells 179

mesh 29

meshing 141, 143

method 12, 36, 44, 67, 86, 186

event 63, 64, 65

form object 89

global 13

local 13, 38, 62, 66, 89, 102

Method Editor 186

desktop location 9

overview 12

preferences 100

using 86

method tab, in ribbon 12

method window 12

Microsoft® Word® format 82

model code window 12, 91

model data access 50, 65

model expressions window 12, 97

Model Library 26

model object 107, 186

move down 41

move up 41

MPHAPP-file

format 14, 30

running 14

MPH-file format 25

multiline text 54

| 227

Page 228: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 228 Tuesday, December 16, 2014 4:16 PM

N name, of form object 36

new form 11

new method 13

number format 55

number of digits 55

O On close event 66

On data change event 65

On load event 66

On shutdown event 62

On startup event 62

open documents 22

operators 107, 108

output argument, to method 104

P panes 147

parameter, input fields and 47

parameter, text objects and 53

parentheses 100

password protected application 18

pixels 35

play sounds 22

playSound 114

PNG-file format 18, 112

position and size 35

positioning form objects 34

precedence, of operators 108

precision 55

preferences 33, 79, 100

security 19

preview form 14

procedure 12

progress bar 140

built-in 141

R radio button 162

recording code 94

recursion 104

regular expression 50

report, creating 82

request 110

resizable graphics 22

resizing form objects 35

results plot 29

ribbon 56, 61

ribbon section 61

ribbon tab 61

row settings 180

run application 14

running applications

in the COMSOL Client 22

S save 175

application 30

model as application 25

running application 15

save as 60, 175

scalar variable 67

security settings 19

selection

explicit 163

selection input 163

separator

menu 58

ribbon 61

toolbar 176

settings window

button 36

form editor 10

method editor 12

sketch layout mode 33, 179

slider 174

smartphones 22

solving 141, 143

sounds library 113

spacer 177

splitting cells 179

status bar 141

string variable 72

subroutine 12

syntax errors 96

228 |

Page 229: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 229 Tuesday, December 16, 2014 4:16 PM

syntax highlighting 99

T table 72, 172

tablets 22

temp, file scheme 84

test application 14

text object 47, 53, 54, 167

thumbnail image 17

title 118

toolbar 58, 175

item 176

separator 176

tooltip

for button 38

for toolbar button 177

tubular reactor, Application Library ex-

ample 221

tuning fork, Application Library exam-

ple 222

U Unicode 53, 55

unit object 47

Untitled.mphapp 15

upload, file scheme 77, 155, 157

URL 139

use as source 55

user interface layout 10

user role 22

user, file scheme 84

V variable, input fields and 47

variable, text objects and 53

volume maximum 54

W WAV-file format 113

web browser 7

web implementation of an application 7

web page 138

WebGL 20

while statement 109

with statement 100, 109

wrap text 54

| 229

Page 230: Introduction to Application Builderweb.uaemex.mx/fciencias/Educacion/EC/Taller/T2/...† Method and Debug ribbon tabs † Explorer window with the application tree † Method window

IntroductionToApplicationBuilder.book Page 230 Tuesday, December 16, 2014 4:16 PM

230 |