How to integrate MATLAB .m file in Python script to run it in OPAL-RT Platform?

6 visualizaciones (últimos 30 días)
I am trying to run a simulink model which has two PVs connected with grid. The simulation will tun multiple times, the PV irradiance, temperature will be fed to each simulation through running a matlab file which will randomly select the irradiance, temparature and load values. The data will be saved after eaxh simulation. As I am relatively new using Python Script in Opal RT,can anyone suggest me how can I write the MATLAB file in the script and what would be the best way to write the code?
And ,if there is any example available like this , can you share it to me?

Respuestas (1)

Yash
Yash el 27 de Ag. de 2024
Editada: Yash el 27 de Ag. de 2024
Hi Prithwi,
As I can understand, you have one simulink model, which will be simulated multiple times with varying parameters and the results will be saved using a MATLAB script. You somehow want to call this MATLAB script with python to run in OPAL-RT.
Code
Given below is an example of how you can write a MATLAB script:
% Define ranges for parameters
irradianceRange = [200, 1000]; % Example range in W/m^2
temperatureRange = [15, 40]; % Example range in Celsius
loadRange = [0.5, 1.5]; % Example range in kW
% Number of simulations to run
numSimulations = 10;
% Loop over the number of simulations
for i = 1:numSimulations
% Randomly select parameter values
irradiance = rand() * (irradianceRange(2) - irradianceRange(1)) + irradianceRange(1);
temperature = rand() * (temperatureRange(2) - temperatureRange(1)) + temperatureRange(1);
load = rand() * (loadRange(2) - loadRange(1)) + loadRange(1);
% Set the parameters in the Simulink model
set_param('your_model_name/PV_Irradiance', 'Value', num2str(irradiance));
set_param('your_model_name/PV_Temperature', 'Value', num2str(temperature));
set_param('your_model_name/Load', 'Value', num2str(load));
% Run the simulation
simOut = sim('your_model_name');
% Save the results
resultsFileName = sprintf('simulation_results_%d.mat', i);
save(resultsFileName, 'simOut');
end
Important Notes
  • Replace 'your_model_name' with the actual name of your Simulink model.
  • Ensure that the block paths (e.g., 'your_model_name/PV_Irradiance') match the names in your model.
  • Adjust the ranges for your parameters according to your specific requirements.
  • Make sure your Simulink model is set up to accept these parameters as inputs.
Call MATLAB from Python
You can further rewrite this code as a MATLAB function and call the MATLAB function from python using MATLAB Engine API for Python. Please refer to the following documentation for more edtails: https://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-python.html
Integrate Python and OPAL-RT
You should check the OPAL-RT documentation on how to integrate Python and OPAL_RT. The following resources might be helpful:
I hope this helps!

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by