I need help coding a custom block on Simscape

5 views (last 30 days)
Nils
Nils on 17 Mar 2025
Answered: Yifeng Tang on 26 Mar 2025
Hi everyone, I'm struggling to code a custom block on Simscape. The purpose of this block is to convert hydraulique energy given by pressurized water to torque. This is what I have so far :
component pelton_turbine
% Ce composant calcule le couple généré par l'eau sur la turbine.
% 🔹 Déclaration des ports
nodes
H = foundation.hydraulic.hydraulic; % Port hydraulique
R = foundation.mechanical.rotational.rotational; % Port mécanique rotatif
end
% 🔹 Déclaration des paramètres
parameters
eta = {0.85, '1'}; % Rendement de la turbine
rho = {1000, 'kg/m^3'}; % Densité de l'eau
r = {0.5, 'm'}; % Rayon moyen de la roue
g = {9.81, 'm/s^2'}; % Gravité
end
% 🔹 Déclaration des variables internes
variables
Q = {0, 'm^3/s'};
T = {0, 'N*m'}; % Couple généré
H_head = {0, 'm'}; % Hauteur d'eau équivalente
end
equations
% Débit hydraulique pris directement depuis le port H
Q == H.q;
% Calcul de la hauteur d'eau (pression convertie en mètre de colonne d'eau)
H_head == H.p / (rho * g);
% Calcul du couple généré par l'eau
T == eta * rho * Q * H_head * r;
% Transmission du couple à l’axe mécanique
R.t == T;
end
end
When I'm running it I have the error : Invalid reference to node balancing variable 'H.q'. Balancing variables may only
be referenced from the 'branches' section. ChatGPT suggested to create a 'branch' section and put the Q inside but it doesn't work either. Do you have any suggestions for changes that could make that work ?
Thanks in advance for the help,
Nils

Answers (1)

Yifeng Tang
Yifeng Tang on 26 Mar 2025
This compiled without an error
component pelton_turbine
% Ce composant calcule le couple généré par l'eau sur la turbine.
% 🔹 Déclaration des ports
nodes
H = foundation.hydraulic.hydraulic; % Port hydraulique
R = foundation.mechanical.rotational.rotational; % Port mécanique rotatif
end
% 🔹 Déclaration des paramètres
parameters
eta = {0.85, '1'}; % Rendement de la turbine
rho = {1000, 'kg/m^3'}; % Densité de l'eau
r = {0.5, 'm'}; % Rayon moyen de la roue
g = {9.81, 'm/s^2'}; % Gravité
end
% 🔹 Déclaration des variables internes
variables
Q = {0, 'm^3/s'};
T = {0, 'N*m'}; % Couple généré
H_head = {0, 'm'}; % Hauteur d'eau équivalente
end
branches
% Débit hydraulique pris directement depuis le port H
Q : H.q -> *;
% Transmission du couple à l’axe mécanique
T: R.t -> *;
end
equations
% Calcul de la hauteur d'eau (pression convertie en mètre de colonne d'eau)
H_head == H.p / (rho * g);
% Calcul du couple généré par l'eau
T == eta * rho * Q * sqrt(g*H_head) * r;
end
end
Moving the equations in question to the branch section isn't hard. The documentation on the syntax and sign convention is here. You should check the equivalent equations very carefully to make sure the sign convention is consistent.
The equation for T needs some modification, too. Otherwise the units won't match. I suspect the heat term should have a sqrt, but you should verify.

Categories

Find more on Foundation and Custom Domains in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!