NREL / SolarPILOT

SolarPILOT power tower layout and optimization tool
https://www.nrel.gov/csp/solarpilot.html
Other
47 stars 17 forks source link

Using SolarPILOT in Matlab through CoPylot #80

Open adlcalle opened 2 years ago

adlcalle commented 2 years ago

CoPylot can be used directly in MATLAB through the MATLAB API for Python which is embedded in the software since version R2014b. If you are accessing from Windows, you will need to acquire a Python interpreter which has to be compatible with your version of MATLAB. The easiest way to do that is to download the installer from python.org. The Mathworks website is full of information about that. If you are on a Linux® or Mac platform, you already have Python installed but you should be careful to have the MATLAB compatible version. Once Python is installed, you can check which version of Python MATLAB has selected to use. In your MATLAB shell type:

>> pe=pyenv

If you already have several copies of Python on your computer, then you can tell MATLAB which one you want it to use by passing the absolute path to the python executable or the version you want to pyenv. For example:

>> pe=pyenv('Version','3.8') 

Depending on your Python installation, you may need to install some additional Python libraries from the PYPI package index such as for example Pandas. The easiest way to do it is through the Python pip program. Type in your MATLAB shell:

>> pypath=char(pe.Executable);
>> system([pypath,' -m pip install pandas']);

Now, you are ready to use CoPylot. In MATLAB shell, the only thing you need is call it through py. operator:

>> cp=py.copylot.CoPylot();

This instruction works when the copylot.py and solarpilot.dll are in the current working folder. If this is not the case, you have to tell Python and CoPylot where these files are located. For example:

>> CoPylot_path='C:\Users\adelacal\source\solarpilot_dev\solarpilot\deploy\api';
>> if count(py.sys.path,CoPylot_path) == 0
      insert(py.sys.path,int32(0),CoPylot_path);
   end
>> cp = py.copylot.CoPylot(path=CoPylot_path);

After this, we can use CoPylot exactly in the same way as we would in Python. Note that the minimum example requires the TMY file to be passed.

>> r = cp.data_create();
>> TMY_file="C:\Users\adelacal\source\solarpilot_dev\solarpilot\deploy\climate_files\USA CA Daggett (TMY2).csv";
>> cp.data_set_string(r,"ambient.0.weather_file",TMY_file);
>> cp.generate_layout(r);
>> field = cp.get_layout_info(r);
>> cp.simulate(r);
>> flux = cp.get_fluxmap(r);
>> res= cp.summary_results(r);
>> eff=res.get('Solar field optical efficiency')
>> cp.data_free(r);

You must remember that you are running Python code in MATLAB so the inputs and outputs are Python variables. Floats and strings are the same variables on both platforms, but everything else must be converted if you want to use it. There are numerous examples of how to do this in test_script.m

freddyUC commented 2 years ago

I want to use CoPylot for parametric simulations, same as SolarPILOT GUI I've tried to call this module like that:

cp.data_set_matrix(r,"parametric.0.user_par_values",set_matrix)

where set_matrix gets the input for parametric values, for instance (flux_solar_az_in, flux_solar_el_in) (azimuth, elevation) I was wondering if you could get me some advice about the input structure I need.

Best regards,