Is it possible for a simulink block to notify the user if a fixed-step solver is being used?

104 views Asked by At

I developed a block which behaves incorrectly if the user chooses a fixed-step solver.

Is it possible for the block to check which solver is being used, and notify the user if a fixed-step solver is being used?

1

There are 1 answers

8
Matt On BEST ANSWER

Short version

This is definitely possible! You can use the callback-function StartFcn in the model properties (or in the block itself). This function is being executed each time the model is being simulated. Then add a check for the solver-type that throws an error if it is set to Fixed-step.

Here is the code to add:

if(strcmp('Fixed-step',get_param(bdroot,'SolverType')))
error('Do not use a fixed-step solver because the results are not correct!');
end

This throws the following error in the Diagnostic Viewer when your model's name is test:

error

Detailed explanation of the code

We get the name of the top-level Simulink system by executing bdroot . This system name is then used for the call to get the solver-type with get_param(bdroot,'SolverType'). Then we use strcmp to compare the returned string with 'Fixed-step'. If the current solver is fixed-step then strcmp returns 1, so we enter the if-statement and throw the error with the error-function.

Add it to a block

To add the callback-function to a block, right-click the block, then on Properties as shown in the screenshot below:

step1_block

And then go to Callbacks -> StartFcn and paste the code:

step2_block

Add it to a model

To add the callback-function to the model click on Model Properties as shown in the screenshot below:

step1_model

And then go to Callbacks -> StartFcn and paste the code:

step2_model

That's it. I hope it helps...