How to stop the simulation when an error occurs in Dymola? - modelica

In Dymola, if an error occurs during the simulation, the calculation would be jammed. How could I stop the simulation now? I tried a lot of methods, but the only way that works right now is using the task manager to terminate the Dymola process.

There are multiple ways to stop a stuck simulation in Dymola
The "Stop" button next to the "Simulation" button. The short-cut for this is F12
Create a empty file "stop" (with no extension) in Dymola's working directory.
Within the (Windows) command-window, hitting CTRL-C a different number of times
once: will give you a status of the progress of the integration
twice: will pause the simulation and will give you some online debugging possibilities
three times: stops the simulation (this will sometimes give you more of the result in the plots than using (1.)
Also you will find the sections "Diagnostics for stuck simulation" and "Debug facilities when running a simulation", which corresponds to (3.), in the Dymola Manual (I think it was Volume 2).

Related

forcing matlab not to pause on warnings

I have encountered a weird situation recently: when I am running my programs, sometimes the execution pauses when it reaches some warning statements (yet, sometimes it doesn't stop in identical situations, and just outputs the warning statement). I cant force the program not to stop. when I click on the small triangle below the "run" or "Breakpoints" in the editor "stop on warning" is sometimes checked, If I remove its tick, it becomes checked again after a while and the program stops on some warnings!
Has anybody encountered similar issue? is there a way to force the program not stop (maybe using some code)
Programmatically, you can use dbstop and dbclear to set and clear break conditions.
In your case, use dbclear if warning.
And see https://www.mathworks.com/help/matlab/ref/dbclear.html

Debugging functions in matlab

Question: Is there a preferred way to debug functions in matlab?
If I all calculations in a script, then every variable is in the workspace and I can easily query them to find out what's not working right. However, if I use a function, then only my outputs are visible and I can't see what's going wrong. I understand there are ways of getting around this, but thusfar they seem to be more trouble than just making one, long ugly, script. So how do YOU debug functions in matlab? Is there a preferred/efficient way of doing this?
I always make sure to enable "Stop If Error" in the Breakpoints menu and if I want to debug a specific function I set a breakpoint at the first line in that function (or at the point of interest). Note that "clear all", which is common in the beginning of scripts deletes all break points. Use "clear variables" instead.
See MATLAB settings - stop if errors for more info on how to make the Stop If Error persist when you restart Matlab.

MATLAB code break

I have started running a script on MATLAB that takes days to finish. Usually, if I changed my mind and I don't want wait for it to finish and I get content with the intermediate results, I highlight the command window and press Ctrl-C to break the code.
Now, I have run MATLAB. But its desktop got kinda stuck in the background. When I try to restore the desktop from the toolbar, it does not restore. But I know from the task manager that the process is running and is consuming Memory and CPU performance. So, I am kinda stuck. I don't want to kill the process because I need the intermediate values in the workspace, and I can't open the desktop to break the code using ctrl-c.
Is there any solution? For example, is there any command that can be used in the command prompt to act as ctrl-c for MATLAB?
I am using MATLAB R2012b and Windows 8.
Quick try to fix the recent Problem:
Try ty set a higher priority to matlab.exe in the Task Manager. (Right click -> Priority -> Higher than normal). Then see if you can get the window to front.
Some approaches to avoid this problem in future:
Try to optimize your code. For starters look at: http://de.mathworks.com/help/matlab/matlab_prog/vectorization.html
Use Matlab compiler for faster execution: http://de.mathworks.com/products/compiler/
Include some drawnow commands at stratetic positions in the code. This allows matlab to process the Event Queue and capture ctr-C commands.
Save intermediate results to output files. For example you could write an output file all 30 min with your intermediate results. Easyiest way would be just save(filename). Then a .matfile with all your workspace variables is generated. You can than kill the process in the task manager, without loosing too much results.

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf

Can you pause MATLAB? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 6 years ago.
In MATLAB, I'm running some code which takes a while to run. I'd like to pause the code to check on some variable values. Is there a way I can do this without having to re-run the code from the beginning? I don't want to terminate the program; just pause it.
You can halt execution and give a command prompt in two ways of which I am aware:
Putting keyboard in your code where you want to stop.
Setting a breakpoint.
You can resume and stop execution with dbcont and dbquit, respectively. To step forward, use dbstep. dbstack lets you see where you are. There are many more commands. The help page for any of these will give you other suggestions.
As Dennis Jaheruddin has pointed out, dbstop also has several useful features worth trying. In particular is the ability to set conditional and global (any line meeting a criterion) breakpoints via the dbstop if syntax. For example, dbstop if error will break to a debugging command prompt on any error. One suggestion he made, which I now do, is to put dbstop if error into startup.m so that this behavior will be default when you start MATLAB. You may need to create this file in a userpath folder; edit(fullfile(regexp(userpath,'^[^;]*','match','once'),'startup.m')).
One way to achieve what you're looking for would be to use code sections (also known as code cells), where you divide your code into sections divided by lines with two percent signs (%%).
Then, in the editor, you can press ctrl+enter to execute the current code section, and ctrl+up/down to navigate between sections.
Well there is the pause command, but then you cannot check for the variable contents in the workspace because the program is running.
What you probably want is to set a breakpoint (See the Debug menu / key F12).
At a breakpoint matlab pauses the program and enters debugging mode in which you can see and edit the variables. Once finished, you can resume the program where it was paused.
I'm not sure about Windows users but if you're running Linux you can start Matlab in a terminal using
matlab -nodesktop
then once Matlab has started, cd to your project directory and start your Matlab script. Now whenever you want to pause execution you can use ctrl-Z. Then to resume type fg. I hope this helps.