LiveLink For MATLAB User’s Guide - COMSOL Multiphysics

Transcription

LiveLink for MATLAB User’s Guide

LiveLink for MATLAB User’s Guide 2009–2018 COMSOLProtected by patents listed on www.comsol.com/patents, and U.S. Patents 7,519,518; 7,596,474;7,623,991; 8,457,932; 8,954,302; 9,098,106; 9,146,652; 9,323,503; 9,372,673; and 9,454,625. Patentspending.This Documentation and the Programs described herein are furnished under the COMSOL Software LicenseAgreement (www.comsol.com/comsol-license-agreement) and may be used or copied only under the termsof the license agreement.COMSOL, the COMSOL logo, COMSOL Multiphysics, COMSOL Desktop, COMSOL Server, andLiveLink are either registered trademarks or trademarks of COMSOL AB. MATLAB is a registeredtrademark of The MathWorks, Inc. All other trademarks are the property of their respective owners, andCOMSOL AB and its subsidiaries and products are not affiliated with, endorsed by, sponsored by, orsupported by those or the above non-COMSOL trademark owners. For a list of such trademark owners, seewww.comsol.com/trademarks.Version:COMSOL 5.4Contact InformationVisit the Contact COMSOL page at www.comsol.com/contact to submit generalinquiries, contact Technical Support, or search for an address and phone number. You canalso visit the Worldwide Sales Offices page at www.comsol.com/contact/offices foraddress and contact information.If you need to contact Support, an online request form is located at the COMSOL Accesspage 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 COMSOL Blog: www.comsol.com/blogs 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/knowledgebasePart number: CM020008

C o n t e n t sChapter 1: IntroductionAbout this Product12Help and Documentation14Getting Help . . . . . . . . . . . . . . . . . . . . . . . . 14Where Do I Access the Documentation and the ApplicationLibraries? . . . . . . . . . . . . . . . . . . . . . . . . 18Chapter 2: Getting StartedThe Client-Server Architecture24Running COMSOL Models at the Command Line26 Starting COMSOL with MATLAB on Windows / Mac OSX / . . . . . . . . . . . . . . . . . . . . . . . . 26Linux Connecting a COMSOL server and MATLAB Manually. . . . . . . . 28Connecting to COMSOL Server . . . . . . . . . . . . . . . . 30 Changing the MATLAB Version . . . . . . . . . . . . . . . . 32The COMSOL Apps34Installing Apps in the MATLAB Apps Ribbon . . . . . . . . . . . . 34Removing Apps in the MATLAB Apps Ribbon. . . . . . . . . . . . 35The COMSOL Apps . . . . . . . . . . . . . . . . . . . . . 35Calling External Function within the Model36Chapter 3: Building ModelsThe Model Object38Important Notes About the Model Object . . . . . . . . . . . . . 38CONTENTS 3

The Model Object Methods. . . . . . . . . . . . . . . . . . . 39The General Utility Functionality . . . . . . . . . . . . . . . . . 39The Model History . . . . . . . . . . . . . . . . . . . . . . 40Loading and Saving a Model . . . . . . . . . . . . . . . . . . . 41Sharing the Model Between the COMSOL Desktop and theMATLAB Prompt. . . . . . . . . . . . . . . . . . . . 45Working with Geometries49The Geometry Sequence Syntax . . . . . . . . . . . . . . . . . 49Displaying the Geometry . . . . . . . . . . . . . . . . . . . . 50Working with Geometry Sequences . . . . . . . . . . . . . . . 51Retrieving Geometry Information . . . . . . . . . . . . . . . . 60Modeling with a Parameterized Geometry . . . . . . . . . . . . . 62Images and Interpolation Data . . . . . . . . . . . . . . . . . . 66Working with Meshes74The Meshing Sequence Syntax . . . . . . . . . . . . . . . . . . 74Displaying the Mesh . . . . . . . . . . . . . . . . . . . . . . 75Mesh Creation Functions . . . . . . . . . . . . . . . . . . . . 764 CONTENTSImporting External Meshes and Mesh Objects . . . . . . . . . . .101Measuring Mesh Quality . . . . . . . . . . . . . . . . . . .103Getting Mesh Statistics Information . . . . . . . . . . . . . . .104Getting and Setting Mesh Data. . . . . . . . . . . . . . . . .107Modeling Physics112The Physics Interface Syntax . . . . . . . . . . . . . . . . .112The Material Syntax . . . . . . . . . . . . . . . . . . . . .115Modifying the Equations . . . . . . . . . . . . . . . . . . .116Adding Global Equations . . . . . . . . . . . . . . . . . . .118Defining Model Settings Using External Data File . . . . . . . . . .120Access the User-Defined Physics Interface . . . . . . . . . . . .121Creating Selections122The Selection Node. . . . . . . . . . . . . . . . . . . . .122Coordinate-Based Selections . . . . . . . . . . . . . . . . .123Selection Using Adjacent Geometry. . . . . . . . . . . . . . .127Displaying Selections . . . . . . . . . . . . . . . . . . . .128

Computing the Solution131The Study Node . . . . . . . . . . . . . . . . . . . . . .131The Solver Sequence Syntax . . . . . . . . . . . . . . . . .132Run the Solver Sequence . . . . . . . . . . . . . . . . . . .133Adding a Parametric Sweep . . . . . . . . . . . . . . . . . .134Adding a Job Sequence. . . . . . . . . . . . . . . . . . . .134Plot While Solving . . . . . . . . . . . . . . . . . . . . .135Analyzing the Results137The Plot Group Syntax . . . . . . . . . . . . . . . . . . .137Displaying The Results . . . . . . . . . . . . . . . . . . . .138The Data Set Syntax . . . . . . . . . . . . . . . . . . . .143The Numerical Node Syntax . . . . . . . . . . . . . . . . .144Exporting Data145. . . . . . . . . . . . . . . . . . . . . .Chapter 4: Working With ModelsUsing Workspace Variables in Model Settings150The Set and SetIndex Methods . . . . . . . . . . . . . . . . Using a MATLAB Function to Define Model Properties . . . . . .151Extracting Results153150Extracting Data at Arbitrary Points . . . . . . . . . . . . . . .153Evaluating a Minimum of Expression. . . . . . . . . . . . . . .157Evaluating a Maximum of Expression . . . . . . . . . . . . . .160Evaluating an Integral . . . . . . . . . . . . . . . . . . . .162Evaluating an Expression Average164. . . . . . . . . . . . . . .Extracting Data at Node Points . . . . . . . . . . . . . . . .167Evaluating an Expression at Geometry Vertices . . . . . . . . . .170Evaluating Expressions on Particle/Ray Trajectories . . . . . . . . .173Evaluating a Global Expression . . . . . . . . . . . . . . . . .174Evaluating a Matrix Expression at Points . . . . . . . . . . . . .176Evaluating a Global Matrix . . . . . . . . . . . . . . . . . .178Extracting Data From Tables . . . . . . . . . . . . . . . . .180CONTENTS 5

Running Models in a Loop181The Parametric Sweep Node . . . . . . . . . . . . . . . . .Running Model in a Loop Using the MATLAB Tools . . . . . . . .181Running Models in Batch Mode184181The Batch Node . . . . . . . . . . . . . . . . . . . . . .184Running an M-file in Batch Mode . . . . . . . . . . . . . . . .184Running an M-file in Batch Mode Without Display . . . . . . . . .185Working with Matrices186Extracting System Matrices . . . . . . . . . . . . . . . . . .186Set System Matrices in the Model . . . . . . . . . . . . . . .192Extracting State-Space Matrices . . . . . . . . . . . . . . . .198Extracting Reduced Order State-Space Matrices . . . . . . . . . .203Extracting Solution Information and Solution Vectors210Obtaining Solution Information . . . . . . . . . . . . . . . .210Retrieving Solution Information and Solution Data Sets Based on6 CONTENTSParameter Values . . . . . . . . . . . . . . . . . . . .212Extracting Solution Vector . . . . . . . . . . . . . . . . . .215Retrieving Xmesh Information218The Extended Mesh (Xmesh) . . . . . . . . . . . . . . . . .218Extracting Xmesh Information . . . . . . . . . . . . . . . . .218Navigating the Model221Navigating the Model Object Using a GUI . . . . . . . . . . . .221Navigating The Model Object At The Command Line . . . . . . . .226Finding Model Expressions . . . . . . . . . . . . . . . . . .227Evaluating the Model Parameters . . . . . . . . . . . . . . . .228Getting Feature Model Properties . . . . . . . . . . . . . . .229Getting Parameter and Variable Definitions . . . . . . . . . . . .230Getting Selection Information . . . . . . . . . . . . . . . . .230Handling Errors and Warnings231Errors and Warnings . . . . . . . . . . . . . . . . . . . . Using MATLAB Tools to Handle COMSOL Exceptions . . . . . .231Displaying Warnings and Errors in the Model . . . . . . . . . . .231231

Improving Performance for Large Models233Allocating Memory . . . . . . . . . . . . . . . . . . . . .233Disabling Model Feature Update . . . . . . . . . . . . . . . .234Disabling The Model History . . . . . . . . . . . . . . . . .235Creating a Custom GUI236Chapter 5: Calling External FunctionsRunning External Function238Allowing External MATLAB functions . . . . . . . . . . . . . . Disabling MATLAB Splash Screen at Start-up . . . . . . . . . . Running a MATLAB Function in Applications . . . . . . . . . .238238239The MATLAB Function Feature Node Defining a MATLAB Function in the COMSOL Model . . . . . . . . . . . . . .Setting the Function Directory Path in MATLAB Adding a MATLAB Function with the COMSOL API Syntax . . . .240Function Input/Output Considerations246. . . . . . . . . . . . .240245246Updating Functions . . . . . . . . . . . . . . . . . . . . .247Defining Function Derivatives . . . . . . . . . . . . . . . . .247Chapter 6: Command ReferenceSummary of Commands250Commands Grouped by Function251colortable . . . . . . . . . . . . . . . . . . . . . . . .254mphapplicationlibraries . . . . . . . . . . . . . . . . . . .255mphcd . . . . . . . . . . . . . . . . . . . . . . . . . .256mphdoc . . . . . . . . . . . . . . . . . . . . . . . . .257mpheval . . . . . . . . . . . . . . . . . . . . . . . . .257mphevalglobalmatrix . . . . . . . . . . . . . . . . . . . .261mphevalpoint . . . . . . . . . . . . . . . . . . . . . . .263mphevalpointmatrix . . . . . . . . . . . . . . . . . . . . .266CONTENTS 7

mphevaluate8 CONTENTS. . . . . . . . . . . . . . . . . . . . . . .267mphgeom. . . . . . . . . . . . . . . . . . . . . . . . .268mphgetadj . . . . . . . . . . . . . . . . . . . . . . . .270mphgetcoords . . . . . . . . . . . . . . . . . . . . . . .271mphgetexpressions . . . . . . . . . . . . . . . . . . . . .272mphgetproperties . . . . . . . . . . . . . . . . . . . . .272mphgetselection . . . . . . . . . . . . . . . . . . . . . .273mphgetu . . . . . . . . . . . . . . . . . . . . . . . . .273mphglobal . . . . . . . . . . . . . . . . . . . . . . . .275mphimage2geom . . . . . . . . . . . . . . . . . . . . . .278mphinputmatrix . . . . . . . . . . . . . . . . . . . . . .279mphint2 . . . . . . . . . . . . . . . . . . . . . . . . .282mphinterp . . . . . . . . . . . . . . . . . . . . . . . .284mphinterpolationfile. . . . . . . . . . . . . . . . . . . . .289mphlaunch . . . . . . . . . . . . . . . . . . . . . . . .290mphload . . . . . . . . . . . . . . . . . . . . . . . . .291mphmatrix . . . . . . . . . . . . . . . . . . . . . . . .293mphmax . . . . . . . . . . . . . . . . . . . . . . . . .296mphmean. . . . . . . . . . . . . . . . . . . . . . . . .299mphmesh . . . . . . . . . . . . . . . . . . . . . . . . .301mphmeshstats . . . . . . . . . . . . . . . . . . . . . . .303mphmin . . . . . . . . . . . . . . . . . . . . . . . . .305mphmodel . . . . . . . . . . . . . . . . . . . . . . . .308mphnavigator . . . . . . . . . . . . . . . . . . . . . . .309mphopen . . . . . . . . . . . . . . . . . . . . . . . . .310mphparticle . . . . . . . . . . . . . . . . . . . . . . . .312mphplot . . . . . . . . . . . . . . . . . . . . . . . . .314mphquad2tri . . . . . . . . . . . . . . . . . . . . . . .316mphray . . . . . . . . . . . . . . . . . . . . . . . . .317mphreadstl . . . . . . . . . . . . . . . . . . . . . . . .318mphreduction . . . . . . . . . . . . . . . . . . . . . . .319mphsave . . . . . . . . . . . . . . . . . . . . . . . . .321mphsearch . . . . . . . . . . . . . . . . . . . . . . . .322mphselectbox . . . . . . . . . . . . . . . . . . . . . . .323mphselectcoords . . . . . . . . . . . . . . . . . . . . . .324mphshowerrors . . . . . . . . . . . . . . . . . . . . . .326mphsolinfo . . . . . . . . . . . . . . . . . . . . . . . .327mphsolutioninfo . . . . . . . . . . . . . . . . . . . . . .329

mphstart . . . . . . . . . . . . . . . . . . . . . . . . .331mphstate . . . . . . . . . . . . . . . . . . . . . . . . .333mphsurf . . . . . . . . . . . . . . . . . . . . . . . . .335mphtable . . . . . . . . . . . . . . . . . . . . . . . . .336mphtags . . . . . . . . . . . . . . . . . . . . . . . . .337mphthumbnail . . . . . . . . . . . . . . . . . . . . . . .338mphversion . . . . . . . . . . . . . . . . . . . . . . . .339mphviewselection . . . . . . . . . . . . . . . . . . . . .340mphwritestl . . . . . . . . . . . . . . . . . . . . . . . .342mphxmeshinfo . . . . . . . . . . . . . . . . . . . . . . .343CONTENTS 9

10 C O N T E N T S

1IntroductionThis guide introduces you to LiveLink for MATLAB , which extends yourCOMSOL modeling environment with an interface between COMSOLMultiphysics and MATLAB . The COMSOL Multiphysics ProgrammingReference Manual provides additional documentation of the COMSOL API.In this chapter: About this Product Help and Documentation11

About this ProductLiveLink for MATLAB connects COMSOL Multiphysics to the MATLAB scriptingenvironment. Using this functionality you can do the following:Set Up Models from a ScriptLiveLink for MATLAB includes the COMSOL API, which has all the necessaryfunctions and methods to implement models from scratch. For each operation done inthe COMSOL Desktop there is a corresponding command that is entered at theMATLAB prompt. It is a simplified syntax based on Java and does not require anyJava knowledge. The easiest way to learn this syntax is to save the model as an M-filedirectly from the COMSOL Desktop. Read more about building a model using thecommand line in the section Building Models.Use MATLAB Functions in Model SettingsUse LiveLink for MATLAB to set model properties with a MATLAB function. Forexample, define material properties or boundary conditions as a MATLAB routine thatis evaluated while the model is solved. Read more in Calling External Functions.Leverage MATLAB Functionality for Program FlowUse the API syntax together with MATLAB functionality to control the flow of yourprograms. For example, implement nested loops using for or while commands,implement conditional model settings with if or switch statements, or handleexceptions using try and catch. Some of these operations are described in RunningModels in a Loop and Handling Errors and Warnings.Analyze Results in MATLABThe API wrapper functions included make it easy to extract data at the command line.Functions are available to access results at node points or arbitrary locations. You canalso get low level information about the extended mesh, such as finite element meshcoordinates, and connection information between the elements and nodes. Extracteddata are available as MATLAB variables ready to be used with any MATLAB function.See Extracting Results and Retrieving Xmesh Information.Create Custom Interfaces for ModelsUse the MATLAB Guide functionality to create a user-defined graphical interface thatis combined with a COMSOL Multiphysics model. Make your models available forothers by creating graphical user interfaces tailored to expose settings and parametersof your choice.12 CHAPTER 1: INTRODUCTION

Connect to COMSOL Server LiveLink for MATLAB has the ability to connect to COMSOL Server as well asCOMSOL Multiphysics Server. This means that MATLAB scripts and GUIs thatutilize COMSOL functionality can be distributed to and used by any user that haveaccess to COMSOL Server .ABOUT THIS PRODUCT 13

Help and DocumentationIn this section: Getting Help Where Do I Access the Documentation and the Application Libraries?Getting HelpCOMSOL Multiphysics and LiveLink for MATLAB have several sources of help andinformation.T H E I N T R O D U C T I O N T O L I V E L I N K for M A T L A B To get started with LiveLink , it is recommended that you read the Introduction toLiveLink for MATLAB . It contains detailed examples about how to get you startedwith the product.ONLINE DOCUMENTATION AND OTHER RESOURCES Read this user’s guide to get detailed information about the different parts of themodel object and how these are accessed from MATLAB. In the section CommandReference the function available for use with LiveLink for MATLAB aredescribed. The COMSOL Multiphysics Programming Reference Manual contains referencedocumentation that describes the methods in the model object.You can access the online documentation directly at the MATLAB prompt using thefunction mphdoc.To open the COMSOL Documentation Help Desk enter:mphdocTo open the help window for a specific entry node enter:mphdoc(node)where node is the model object node (for instance, model.geom).To view the help of a specific feature of a node enter:mphdoc(node, fname )14 CHAPTER 1: INTRODUCTION

where fname is a string defining the feature name in the COMSOL API, e.g.mphdoc(model,'Rectangle').It is of course also possible to use MATLAB’s own help function such ashelp mphinterpanddoc mphinterpM-FILESYou can save COMSOL Multiphysics models as M-files. Use the COMSOL Desktopto get your first model implemented using the COMSOL API.Set up the model using the graphical user interface, then save the model as an M-file.Next go to the File menu and select Save, in the save window locate Save as type listand select Model File for MATLAB (*.m). This generates an M-function that can be runusing COMSOL with MATLAB.Since version 5.3 a new syntax closer to the structure in the Model Builder is available.This new syntax includes the component node as in the example below:model.component( cTag ).geom( geomTag )In the previous syntax to access the geometry node you need to enter:model.geom( geomTag )Both versions are fully supported, and the new syntax is used by default when saving amodel in the M-file format. To save a model M-file using the old syntax, you need tochange the preferences settings as described in the steps below:1 In the COMSOL Desktop, go to the File menu and click Preferences.2 In the Preferences window, click Methods. In the Code generation settings clear theoption Use component syntax.3 Click OK.At the MATLAB prompt use mphsave to save the model object model in the *.mformat as in the command below:mphsave(model, filename.m )HELP AND DOCUMENTATION 15

where filename.m is the name of the file with the *.m extension.The component syntax is not used by default.To save the model object in the *.m format using the component syntax enter:mphsave(model, filename.m ,'component','on');Models created with older versions than COMSOL 5.3 cannot be savedusing the component syntax unless File Compact History is used.16 CHAPTER 1: INTRODUCTION

THE APPLICATION LIBRARIES WINDOWStudy the LiveLink for MATLAB Application LibraryLiveLink for MATLAB includes an Application Library with detailed examplemodels. Use the function mphapplicationlibraries at the command line to start aGUI for viewing the Application Libraries.If you have installed the COMSOL apps in the MATLAB Apps ribbon,click the COMSOL Application Libraries icon ( ).The following are some models that can help you get started.Model Examples Learn how to activate and deactivate domains alternatively during a transientanalysis. See the model Domain Activation and Deactivation (model namedomain activation llmatlab). Homogenization in a Chemical Reactor (model namehomogenization llmatlab) shows how to simulate a periodic homogenizationHELP AND DOCUMENTATION 17

process in a space-dependent chemical reactor model. This homogenizationremoves concentration gradients in the reactor at a set time interval. Convective Heat Transfer with Pseudo-Periodicity (model namepseudoperiodicity llmatlab) simulates convective heat transfer in a channelfilled with water. To reduce memory requirements, the model is solved repeatedlyon a pseudo-periodic section of the channel. Each solution corresponds to adifferent section, and before each solution step the temperature at the outletboundary from the previous solution is mapped to the inlet boundary. Temperature Distribution in a Vacuum Flask (model namevacuum flask llmatlab) shows how to use the MATLAB function callback. Thisexample solves for the temperature distribution inside a vacuum flask with hotcoffee. Electrical Heating of a Busbar Solved with LiveLink for SOLIDWORKS andLiveLink for MATLAB (model name busbar llsw llmatlab) performsgeometry optimization using COMSOL Multiphysics, MATLAB, andSOLIDWORKS .Where Do I Access the Documentation and the Application Libraries?A number of internet resources have more information about COMSOL, includinglicensing and technical information. The electronic documentation, topic-based (orcontext-based) help, and the application libraries are all accessed through theCOMSOL Desktop.If you are reading the documentation as a PDF file on your computer, theblue links do not work to open an application or content referenced in adifferent guide. However, if you are using the Help system in COMSOLMultiphysics, these links work to other modules (as long as you have alicense), application examples, and documentation sets.THE DOCUMENTATION AND ONLINE HELPThe COMSOL Multiphysics Reference Manual describes all core physics interfacesand functionality included with the COMSOL Multiphysics license. This book also hasinstructions about how to use COMSOL Multiphysics and how to access theelectronic Documentation and Help content.18 CHAPTER 1: INTRODUCTION

Opening Topic-Based HelpThe Help window is useful as it is connected to many of the features on the GUI. Tolearn more about a node in the Model Builder, or a window on the Desktop, click tohighlight a node or window, then press F1 to open the Help window, which thendisplays information about that feature (or click a node in the Model Builder followed). This is called topic-based (or context) help.by the Help button (To open the Help window: In the Model Builder, Application Builder, or Physics Builder click a node orwindow and then press F1. On any toolbar (for example, Home, Definitions, or Geometry), hover themouse over a button (for example, Add Physics or Build All) and thenpress F1. From the File menu, click Help (). In the upper-right corner of the COMSOL Desktop, click the Help(button.)To open the Help window: In the Model Builder or Physics Builder click a node or window and thenpress F1. On the main toolbar, click the Help () button. From the main menu, select Help Help.Opening the Documentation WindowTo open the Documentation window: Press Ctrl F1. From the File menu select Help Documentation ().HELP AND DOCUMENTATION 19

To open the Documentation window: Press Ctrl F1. On the main toolbar, click the Documentation () button. From the main menu, select Help Documentation.THE APPLICATION LIBRARIES WINDOWEach application includes documentation with the theoretical background andstep-by-step instructions to create a model application. The applications are availablein COMSOL as MPH-files that you can open for further investigation. You can use thestep-by-step instructions and the actual applications as a template for your ownmodeling and applications. In most models, SI units are used to describe the relevantproperties, parameters, and dimensions in most examples, but other unit systems areavailable.Once the Application Libraries window is opened, you can search by name or browseunder a module folder name. Click to view a summary of the application and itsproperties, including options to open it or a PDF document.The Application Libraries Window in the COMSOL MultiphysicsReference Manual.Opening the Application Libraries WindowTo open the Application Libraries window (): From the Home toolbar, Windows menu, click (Libraries.) Applications From the File menu select Application Libraries.To include the latest versions of model examples, from the File Helpmenu, select () Update COMSOL Application Library.Select Application Libraries from the main File or Windows menus.To include the latest versions of model examples, from the Help menuselect () Update COMSOL Application Library.20 CHAPTER 1: INTRODUCTION

CONTACTING COMSOL BY EMAILFor general product information, contact COMSOL at info@comsol.com.To receive technical support from COMSOL for the COMSOL products, pleasecontact your local COMSOL representative or send your questions tosupport@comsol.com. An automatic notification and case number is sent to you byemail.COMSOL WEBSITESCOMSOL websitewww.comsol.comContact COMSOLwww.comsol.com/contactSupport Centerwww.comsol.com/supportProduct Downloadwww.comsol.com/support/downloadProduct Updateswww.comsol.com/support/updatesDiscussion eventsCOMSOL Video Gallerywww.comsol.com/videoSupport Knowledge Basewww.comsol.com/support/knowledgebaseHELP AND DOCUMENTATION 21

22 CHAPTER 1: INTRODUCTION

2Getting StartedIn this chapter: The Client-Server Architecture Running COMSOL Models at the Command Line The COMSOL Apps Calling External Function within the Model23

The Client-Server ArchitectureLiveLink for MATLAB uses the client-server mode to connect a COMSOL serverand MATLAB.The term COMSOL server designates either the COMSOL Multiphysicsserver or the COMSOL Server .When starting COMSOL with MATLAB, two processes are started — a COMSOLMultiphysics server and the MATLAB desktop. The COMSOL Desktop does not haveto be started, but it possible to have both MATLAB and COMSOL Desktopconnected to the same COMSOL Multiphysics server at the same time.COMSOL Desktop cannot connect to COMSOL Server .The communication between the two processes is based on a TCP /IP communicationprotocol. You provide login information the first time COMSOL is started withMATLAB. The login information is not related to the system’s username andpassword. This information is stored in the user preferences file and is not requiredagain when using COMSOL with MATLAB. The same login information can be usedwhen exchanging the model object between the COMSOL server and the COMSOLDesktop.The communication between the COMSOL server and MATLAB is established bydefault using port number 2036. If this port is in use, port number 2037 is usedinstead, and so on.You can manually specify the port number. See the COMSOLMultiphysics Installation Guide for more information on the COMSOLserver start-up properties.24 CHAPTER 2: GETTING STARTED

You can manually specify the port number. See the COMSOLMultiphysics Installation Guide for more information on the COMSOLserver start-up properties.A connection can be local (on the same computer), which is the common case, orremote to a COMSOL server located on a different computer, in the later case you toconnect manually MATLAB to the COMSOL server as described in the sectionConnecting a COMSOL server and MATLAB Manually.THE CLIENT-SERVER ARCHITECTURE 25

Running COMSOL Models at theCommand LineThe command to run COMSOL with MATLAB automatically connects a COMSOLprocess with MATLAB. You can also connect the process manually. This sectiondescribes this process as well as how to change the MATLAB path in the COMSOLsettings.The System Requirements section in the COMSOL MultiphysicsInstallation Guide lists the versions of MATLAB supported byLiveLink for MATLAB .In this section: Starting COMSOL with MATLAB on Windows / Mac OSX / Linux Connecting a COMSOL server and MATLAB Manually Changing the MATLAB VersionStarting COMSOL with MATLAB on Windows / Mac OSX /Linux To run a COMSOL Multiphysics model at the MATLAB prompt, start COMSOLwith MATLAB: On Windows use the COMSOL Multiphysics with MATLAB shortcut icon that iscreated on the desktop after the automatic installation. A link is also available in theWindows start menu under All Programs COMSOL Multiphysics 5.3a COMSOLMultiphysics 5.3a with MATLAB. On Mac OS X, use the COMSOL with MATLAB application available in the Applicationfolder. On Linux , enter the command comsol mphserver matlab at a terminal window.See the COMSOL Multiphysics Installation Guide for a completedescription about how to start COMSOL with MATLAB on thesesupported platforms.26 CHAPTER 2: GETTING STARTED

The first time COMSOL Multiphysics with MATLAB is started, login andpassword information is requested to establish the client/serverconnection. The information is saved in the user preference file and is notrequired again.To reset the login information, add the flag -login force to the icontarget path.To reset the login information, enter the command comsol mphservermatlab -login force at a system command prompt.RUNNING A MODEL M-FILE FROM TERMINAL PROMPTIf you want to run an M-file directly at a terminal prompt immediately after havingstarted COMSOL Multiphysics with MATLAB enter the startup command as describedbelow:comsolmphserver.exe matlab filenamecomsol mphserver matlab filenamewhere filename.m is the file containing both MATLAB and COMSOL APIcommand to be executed using COMSOL with MATLAB.RUNNING COMSOL MODELS AT THE COMMAND LINE 27

RUNNING WITHOUT DISPLAYIf you need to run COMSOL with MATLAB on a machine without support forgraphics display, add the flags nodesktop and mlnosplash to the startup command asdescribed below:comsolmphserver.exe matlab -nodesktop -mlnosplashcomsol mphserver matlab -nodesktop -mlnosplashThe above command starts MATLAB without splash screen and without the MATLABdesktop.To avoid the splash screen on the COMSOL Server you need to create theenvironment variable COMSOL MATLAB INIT and set it to matlab -nosplash.COMSOL MATLAB INIT ’matlab -nosplash’C

data are available as MATLAB variables ready to be used with any MATLAB function. See Extracting Results and Retrieving Xmesh Information. Create Custom Interfaces for Models Use the MATLAB Guide functi