
Effective Tips for Modelica Simulation Debugging and Unit Testing
Discover practical strategies for successful Modelica simulation debugging and unit testing. Learn how to implement unit tests, use diagnostic tools, prepare for debugging, and troubleshoot common errors. Enhance your workflow with best practices and efficient techniques.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Modelica Training Workshop 10 Wangda Zuo, Michael Wetter Simulation Research Group Energy and Environmental Technologies Division Lawrence Berkeley National Laboratory, USA 6/18/2012
Outline Unit Test of Buildings Library Tips for Debugging Homotopy Stream Connectors 2
1.Implement at least one unit test for each class and run the unit tests. See unit test implementation. 2.Use Buildings.Utilities.Diagnostics.AssertEquality to cause a test to fail if the result is incorrect. 3.Ensure that no unit test requires a numerical Jacobian. If a numerical Jacobian is needed, improve the model. - A numerical jacobian is usually generated when Dymola is unable to automatically differentiate a function. Usage: cd ../../BuildingsPy source setenv.sh cd ../modelica/Buildings ../../bin/runUnitTests.py 3
Prepare for Debugging The most important tip: In the design phase, divide the system into different systems, components and subcomponents. Build examples to validate each component or sub-systems Name the variable using the conventions. - Code is more readable - Others can debug your code easily Add comments for each equations and variables. If possible, add the equation number of reference materials Use SI units for variable when possible - Use correct variables in equations - Enable unit check Save the working copy and commit it to SVN server regularly. - Save the clean copy - Save your work in case program crashes 4
Prepare for Debugging Use models from the Modelica standard library and Buildings library when possible - More stable than self-built models Add assert statements for model assumptions Try to avoid mixing diagram connections and coding in scripts Use SaveAs or Duplicate to move a class or package in package hierarchies Avoid algorithms in non-functions. 5
Debugging Check grammar frequently and correct grammar errors in early stage Check the message during the translation phase - Find errors with the given lines - Pay attention on warning (May be an error later on) If solver fails at time step t, run the simulation to t-dt and check the output for suspicious data Cannot get converged solution - Try different numerical solvers: Dassl, Radau etc. - Change the solver tolerance Check events Turn on the logging: Experiment setup -> Debug -> Event during simulation Numerical Jacobian To discover which functions cannot be differentiated, set the flag Hidden.PrintFailureToDifferentiate=true before translating the model 6
Common Errors Do not define value for parameters or use wrong values - Define the value of parameters at the top level - Manually calculate the nominal values Misuse indices when using array or multiple segments - Write the algorithm by hand - Add comments for the index of each equations Have too many events with if sentence - Use smooth functions Over-specify initial conditions - Use dynamic setting for initial value (not fixed value) Forget expansion vessel in fluid system - Add the missing expansion vessel Use hard-coded path for data file. E.g. / ./wzuo/ /Buildings/ . - Always use the relative path Buildings/ . 7
Homotopy Find a way to robustly and efficiently initialize the non-linear equation systems. 8
Goals of Simplified Model 1. The simplified model should approximate the accurate model around the nominal operating point. 2. The simplified model should be close enough to the actual model 3. The numerical solution of the simplified model should converge with rough initial guess values, either set by default or based on parameters specifying the nominal conditions. 9
Example Buildings.Fluid.FixedResistances.FixedResistanceDpM Calculation of pressure drop: if homotopyInitialization then if from_dp then m_flow=homotopy(actual=Buildings.Fluid.BaseClasses.Flo wModels.basicFlowFunction_dp(dp=dp, k=k,m_flow_turbulent=m_f low_turbulent), simplified=m_flow_nominal_pos*dp/dp_nominal_pos); else dp=homotopy(actual=Buildings.Fluid.BaseClasses.FlowMod els.basicFlowFunction_m_flow(m_flow=m_flow, k=k,m_flow_turbu lent=m_flow_turbulent), simplified=dp_nominal_pos*m_flow/m_flow_nominal_pos) ; 10