Problems with custom blocks with python and oml
Hi,
I have tried to write a custom function, first in python and later in oml. The desired output is a time-domain simulation of the surface wave elevation using the functions in the blocks. When testing the program in python, the code gives a constant giving a constant time t, which in the block is the time signal. Why is it not able to publish the result as a signal to for example a scope block?
I also attempted to do this with oml custom function block, but it also didn't succeed.
Error python: index 1 is out of bounds for axis 0 with size 1. Failed in updating the model at the initial point. Error oml: Unable to find the initial output values of blocks. Output of these blocks do not converge.
What does the error actually mean?
Attatched is the activate model.
Thanks in advance,
Linn
Best Answer
-
Hi Linn,
I had a look at the code in your custom block and saw that you deleted the if statement for the different flags. Blocks have to perform different types of actions during an Activate simulation. e.g. initialization, updating internal states, updating outputs, etc.
These different actions have to be performed under the different flags you see in the if statement when you generate the skeleton code.
Have a look at the short guide and examples I've put together regarding custom blocks in Activate. There are examples for Compose, Python and C custom blocks:
After going through this guide, you'll have a much better understanding.
Rafael
3
Answers
-
Hi Linn,
I had a look at the code in your custom block and saw that you deleted the if statement for the different flags. Blocks have to perform different types of actions during an Activate simulation. e.g. initialization, updating internal states, updating outputs, etc.
These different actions have to be performed under the different flags you see in the if statement when you generate the skeleton code.
Have a look at the short guide and examples I've put together regarding custom blocks in Activate. There are examples for Compose, Python and C custom blocks:
After going through this guide, you'll have a much better understanding.
Rafael
3 -
Hi Linn,
I had a look at the code in your custom block and saw that you deleted the if statement for the different flags. Blocks have to perform different types of actions during an Activate simulation. e.g. initialization, updating internal states, updating outputs, etc.
These different actions have to be performed under the different flags you see in the if statement when you generate the skeleton code.
Have a look at the short guide and examples I've put together regarding custom blocks in Activate. There are examples for Compose, Python and C custom blocks:
After going through this guide, you'll have a much better understanding.
Rafael
Hi Rafael,
I don't think deleting of the if statements for the different flags were the main issue here. I tried to generate the code skeleton and pasted over the other part of the code, and the issue still remains. The output is tested and it should be 1D. What else could be the problem?Thanks,
Linn
0 -
Hi Linn,
I had a look at the code in your custom block and saw that you deleted the if statement for the different flags. Blocks have to perform different types of actions during an Activate simulation. e.g. initialization, updating internal states, updating outputs, etc.
These different actions have to be performed under the different flags you see in the if statement when you generate the skeleton code.
Have a look at the short guide and examples I've put together regarding custom blocks in Activate. There are examples for Compose, Python and C custom blocks:
After going through this guide, you'll have a much better understanding.
Rafael
I don't know how I should update my block to make it work. Could you please take a look at the python block?
0 -
Linn Liao said:
I don't know how I should update my block to make it work. Could you please take a look at the python block?
Hi Linn, I see some errors when running because you haven't declared any initial state value in the states tab of the block, yet you are trying to get the first element of the states array during the output update phase.
Initial states and parameters must be declared in their tabs if you want to use them in the custom block's code.
Rafael
0 -
Hi Linn, I see some errors when running because you haven't declared any initial state value in the states tab of the block, yet you are trying to get the first element of the states array during the output update phase.
Initial states and parameters must be declared in their tabs if you want to use them in the custom block's code.
Rafael
I see, that is indeed part of the problem. And I believe it was also a mistake to try to use the state space representation to solve my problem? I wanted to calculate the spectrum density function, and then sum up the frequencies to get a time history of wave elevation. Time is involved in this calculation through cos(omega(i)*t + phi(i)), where omega is the frequency component and phi is the phase angle.
What is the difference between updating internal states and updating outputs?
In the start I wanted to do calculations in the updating output statements, but it seems that it was the wrong move? How can I structure this code instead?
0 -
Linn Liao said:
I see, that is indeed part of the problem. And I believe it was also a mistake to try to use the state space representation to solve my problem? I wanted to calculate the spectrum density function, and then sum up the frequencies to get a time history of wave elevation. Time is involved in this calculation through cos(omega(i)*t + phi(i)), where omega is the frequency component and phi is the phase angle.
What is the difference between updating internal states and updating outputs?
In the start I wanted to do calculations in the updating output statements, but it seems that it was the wrong move? How can I structure this code instead?
Hi Linn,
From what I see, you have a single state, wave_elevation. In the block's setup menu, states tab, you should declare an initial value for it.
Also, the core of your code calculates the new wave elevation itself, not it's derivative. Therefore, you should move the main part of your code to a state update flag (created by the skeleton code).
Other than that, my only advice is to be careful that you are feeding any input to your functions in the correct form (e.g., inputs are read as numpy arrays so you should get the value contained in them before using).
Rafael
0