Stephen L. Campbell · Ramine Nikoukhah Modeling and Simulation with Compose and Activate Modeling and Simulation with Compose and Activate Stephen L. Campbell Ramine Nikoukhah (cid:129) Modeling and Simulation with Compose and Activate 123 StephenL. Campbell RamineNikoukhah Department ofMathematics Altair Engineering France NorthCarolina State University Antony,France Raleigh, NC,USA ISBN978-3-030-04884-6 ISBN978-3-030-04885-3 (eBook) https://doi.org/10.1007/978-3-030-04885-3 LibraryofCongressControlNumber:2018962375 Mathematics Subject Classification (2010): 34A01, 34A04, 34A38, 34H05, 68U07, 68U20, 37M05, 65Y15 ©SpringerNatureSwitzerlandAG2018 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpart of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission orinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar methodologynowknownorhereafterdeveloped. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publicationdoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfrom therelevantprotectivelawsandregulationsandthereforefreeforgeneraluse. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authorsortheeditorsgiveawarranty,expressorimplied,withrespecttothematerialcontainedhereinor for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictionalclaimsinpublishedmapsandinstitutionalaffiliations. ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSwitzerlandAG Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland This book is dedicated to our wives and children. They make the whole journey worthwhile; Homa Nikoukhah and daughters TinaandDana,GailCampbellandsonsMatt and Eric. Preface System modeling and simulation is a fundamental aspect of most areas of science and engineering. This has led to the development of software packages aimed at helping speed up the process of modeling, simulation, optimization, analysis, and assessment of systems. More advanced system modeling software provide multiple ways of creating models: Models can be programmed in specialized languages, and they can be graphically constructed as block diagrams and state machines, or expressed mathematically inequation-based languages.This book isabout two new software ™ ™ packages, called Altair Compose and Altair Activate , providing all these modeling facilities. Compose is a multi-language environment for scientific computation with spe- cial emphasis on modeling, simulation, optimization, and assessment. This book will focus on the newly developed open matrix language (OML). OML is part oftheMATLAB-likefamilyoflanguagessuchasMATLAB®,Scilab,andOctave.OML is compatible with Octave and provides bridges to Python and Tcl/Tk. An open-source version of OML has been released in 2018, available from www. openmatrix.org. Activate is an open-system graphical modeling and simulation environment providing signal-based modeling as well as physical system component-based modeling, where the latter supports Modelica and Spice libraries. OML is the underlying scripting language in Activate for defining model parameters, pro- grammatically constructing models and performing pre- and post-processing. Basic editions of Compose and Activate are available for free from Altair at https://www.altair.com/mbd2019/. Basic Compose OML includes a number of toolboxes, such as the Control Toolbox. Basic Activate includes many libraries as well.Alltheexamplesinthisbookhavebeenmodeledandsimulatedwiththebasic editions.ComposeandActivaterunundertheWindowsoperatingsystem.Thereis also a Linux version of Compose with OML. A Linux version of Activate is in development and should be available in 2019. The professional versions integrate with numerous 1D and 3D simulation tools and are available from Altair as well. vii viii Preface This book provides a tutorial in the use of Compose and Activate. Part I introduces Compose and OML and its use for modeling, simulation, and opti- mization of dynamical systems. Part II describes graphical system modeling and optimization with Activate. The support of equation-based modeling language Modelica in Activate is also presented and illustrated through a number of examples. Throughout the book, numerous examples of varying complexity are carefully developedandworkedout.Theexamplesaredrawnfromseveralapplicationareas including mechanical systems, biological systems, electrical systems, and control and signal processing systems. Anumberofpeoplehavesupportedusinmanyways.Wewouldespeciallylike to acknowledge Michael Hoffmann and James Dagg for their support and contri- butions to the development of Compose and Activate, and especially James Scapa for making it all happen. Raleigh, NC, USA Stephen L. Campbell Antony, France Ramine Nikoukhah October 2018 Contents Part I Altair Compose and OML 1 General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1 What Is OML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Compose OML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 OML in Modeling and Simulation. . . . . . . . . . . . . . . . . . . . . . 8 2 Introduction to OML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1 OML Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.1.1 Matrix Construction and Manipulation. . . . . . . . . . . . . 11 2.1.2 Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.1.3 Boolean Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.1.4 Polynomial Matrices. . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.1.5 Structures and Cells . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.1.6 Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2 OML Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.2.1 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.2.2 Iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.2.3 OML Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.2.4 User Defined C/C++ Functions . . . . . . . . . . . . . . . . . . 40 2.3 Input and Output Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.3.1 Display of Variables. . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.3.2 User Input During Computation . . . . . . . . . . . . . . . . . 44 2.3.3 Formatted Input and Output . . . . . . . . . . . . . . . . . . . . 44 2.4 OML Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.4.1 Basic Graphing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.4.2 Interpolation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.4.3 Graphic Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.4.4 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 ix x Contents 3 Modeling and Simulation in OML . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.1 Types of Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.1.1 Ordinary Differential Equations. . . . . . . . . . . . . . . . . . 62 3.1.2 Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . 63 3.1.3 Difference Equations. . . . . . . . . . . . . . . . . . . . . . . . . . 64 3.1.4 Differential Algebraic Equations . . . . . . . . . . . . . . . . . 65 3.1.5 Hybrid Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.2 OML Simulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.2.1 Implicit Differential Equations. . . . . . . . . . . . . . . . . . . 74 3.2.2 Linear Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.2.3 Root Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 3.2.4 Boundary Value Problems . . . . . . . . . . . . . . . . . . . . . 75 3.2.5 Difference Equations. . . . . . . . . . . . . . . . . . . . . . . . . . 76 3.2.6 Hybrid Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.3 Control Toolbox. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4.1 Comments on Optimization and Solving Nonlinear Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.1.1 Nonlinear Equation Solving . . . . . . . . . . . . . . . . . . . . 99 4.2 General Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 4.2.1 Genetic Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 103 4.3 Solving Nonlinear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 105 4.4 Parameter Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 4.4.1 Linear Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 5.1 Boundary Value Problems by Shooting . . . . . . . . . . . . . . . . . . 111 5.2 Parameter Fitting and Implicit Models . . . . . . . . . . . . . . . . . . . 118 5.2.1 Mathematical Model. . . . . . . . . . . . . . . . . . . . . . . . . . 119 5.2.2 OML Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 119 5.3 Open-Loop Control of a Pendulum . . . . . . . . . . . . . . . . . . . . . 126 5.3.1 Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 5.3.2 Control Problem Formulation: Tracking. . . . . . . . . . . . 127 5.3.3 Control Problem Formulation: Minimizing Time . . . . . 130 5.3.4 Optimization Problem. . . . . . . . . . . . . . . . . . . . . . . . . 132 5.3.5 Implementation in OML . . . . . . . . . . . . . . . . . . . . . . . 134 5.4 PDEs and Time-Delay Systems Using MOL . . . . . . . . . . . . . . 137 5.4.1 Solving PDEs Using MOL . . . . . . . . . . . . . . . . . . . . . 138 5.4.2 Systems with Delays . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.5 Plane Boarding Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 5.6 Introduction to Block Diagrams. . . . . . . . . . . . . . . . . . . . . . . . 151 Contents xi Part II Altair Activate 6 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 6.1 Modeling and Simulation in Activate. . . . . . . . . . . . . . . . . . . . 159 6.1.1 Running Activate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 6.1.2 Constructing a First Model . . . . . . . . . . . . . . . . . . . . . 159 6.1.3 Support of Vectors and Matrices in Activate . . . . . . . . 164 6.1.4 Time Delay Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . 170 6.1.5 Activation Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 6.2 Virtual Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.2.1 Modeling a Traffic Light as a State Machine . . . . . . . . 177 6.3 Synchronism and Special Blocks . . . . . . . . . . . . . . . . . . . . . . . 183 7 Expression Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 7.2 Activate MathExpression Block. . . . . . . . . . . . . . . . . . . . . . . . . 188 7.2.1 MathExpressions Block . . . . . . . . . . . . . . . . . . . . . . . . 189 7.2.2 Example: Bouc-Wen Model . . . . . . . . . . . . . . . . . . . . 190 7.3 Activate MatrixExpression Block. . . . . . . . . . . . . . . . . . . . . . . . 191 7.3.1 The Matrix Expression Language . . . . . . . . . . . . . . . . 192 7.3.2 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 7.4 Activate OmlExpression Block . . . . . . . . . . . . . . . . . . . . . . . . . 204 8 Beyond Single Run Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 8.1 End Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 8.1.1 Stop Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 8.1.2 Restart Option. . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 8.1.3 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 8.2 BobyqaOpt Block. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 8.2.1 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 9 Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 9.1 Predator Prey Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 9.2 Thermostat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 9.3 Generic Extended Kalman Filter Model . . . . . . . . . . . . . . . . . . 226 9.4 Signal Processing Application . . . . . . . . . . . . . . . . . . . . . . . . . 234 9.5 Neuroscience Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9.6 Active Cruise Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 9.6.1 Model Construction . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9.6.2 Implementation of the Adaptive Cruise Control . . . . . . 241 9.7 Swing-Up Pendulum Problem . . . . . . . . . . . . . . . . . . . . . . . . . 247