Files
CoolProp/Web/coolprop/wrappers/MATLAB/index.rst
luzpaz 93f4a986e2 Misc. typos (#1726)
Found via `codespell -i 3 -w -I ../coolprop-word-whitelist.txt` whereby whitelist consists of:
```
cas
formate
hel
nd
te
tim
ue
uint
```
2018-08-14 23:06:49 -06:00

181 lines
8.6 KiB
ReStructuredText

.. _MATLAB:
**************
MATLAB Wrapper
**************
.. contents:: :depth: 2
Pre-compiled Binaries
=====================
Release versions of the MATLAB wrapper can be downloaded from :sfdownloads:`MATLAB` for your architecture, or from the development snapshots at :sfnightly:`MATLAB`. Whichever one you pick, make sure all the files come from the same place.
.. warning::
For users on Mac OSX 10.10+, you should use the release binaries from :sfdownloads:`MATLAB/10.10+/MATLAB` for your architecture, or from the development snapshots at :sfnightly:`MATLAB/10.10+/MATLAB`.
Download the +CoolProp.7z file and extract it using the `7-zip <http://www.7-zip.org/download.html>`_ program. Make sure that when you expand it, there are no +CoolProp intermediate folders generated. Place the mex file that is appropriate to the version of MATLAB in the directory that contains the directory +CoolProp. You can determine what version of matlab you have by running the command ``computer('arch')`` at the MATLAB command prompt. Depending on the output, you will need to download one of the following mex files:
* ``computer('arch')`` yields ``win32``: CoolPropMATLAB_wrap.mexw32
* ``computer('arch')`` yields ``win64``: CoolPropMATLAB_wrap.mexw64
* ``computer('arch')`` yields ``glnxa64``: CoolPropMATLAB_wrap.mexa64
* ``computer('arch')`` yields ``maci64``: CoolPropMATLAB_wrap.mexmaci64
Also, download the ``SwigRef.m`` and ``CoolPropsetup.m`` files.
When you are finished, you should have a folder layout like::
main
|- SwigRef.m
|- CoolPropsetup.m
|- CoolPropMATLAB_wrap.mexw64
|- +CoolProp
|- AbstractState.m
|- DmassHmass_INPUTS.m
|- ...
Then you need to add the main that contains the +CoolProp folder to the path in MATLAB. This command adds the folder ``main`` that contains CoolProp files to the MATLAB path::
addpath('/home/USERNAME/Some_folder/main')
Usage
-----
To calculate the normal boiling temperature of water at one atmosphere::
>> CoolProp.PropsSI('T','P',101325,'Q',0,'Water')
Faster Properties
=================
The interface for CoolProp for MATLAB is quite slow due to the internal structure of MATLAB that is not amenable for easy wrapping of C++ code. For that reason it can be advantageous to call the low-level interface through the DLL (shared library on linux and OSX) and obtain a few properties at a time. There is an example :ref:`below <low_level_high_level_matlab>` that demonstrates how to do this. It mirrors the code that is used to call these functions from C++ as seen in the :ref:`low-level interface <low_level_high_level>`. This method of retrieving properties is almost as fast as C++, and much faster than using the SWIG-based wrappers.
User-Compiled Binaries
======================
Pre-generated wrapper code
--------------------------
For windows users, you can almost certainly use the pre-compiled binaries described above. For some other platforms (OSX and linux primarily), there are various problems that make the use of the precompiled binaries impossible. So you would need to build it yourself on your computer to make sure that everything works properly.
Until support for MATLAB makes it into the master branch of SWIG, using swig for users (though documented below), will not be very straightforward. For that reason, a zip file has been generated with the MATLAB interface code already packaged. You will simply need to download the 7z archive, extract it, use CMake to generate a makefile, and build.
The pre-generated versions of the MATLAB wrapper source code can be downloaded from :sfdownloads:`MATLAB` for your architecture, or from the development snapshots at :sfnightly:`MATLAB`. You want the file ``swigged_MATLAB_src.7z``. Once you have downloaded this file, you should run these steps at the command line::
# Change this path to be the root directory for MATLAB if needed
export MATLAB_ROOT=/package/matlab/R2014a
7z x swigged_MATLAB_src.7z
cd coolprop/build
cmake .. -DCOOLPROP_MATLAB_SWIGAUTOGENERATED=ON
cmake --build .
You should end up with a folder layout like described above within the coolprop/build directory.
.. _swig_matlab:
Swig+Matlab
-----------
As of version 5 of CoolProp, the MATLAB wrapper has been greatly improved in capabilities thanks to the work of the SWIG team. The MATLAB wrapper will now automatically include complete wrappers of the high-level and low-level code, bringing it in line with the other SWIG-based wrappers (scilab, C#, octave, etc).
The bad part of this is that swig+matlab support has not been integrated into the main codebase of swig (as of October 2014). Thus it is necessary to compile (or obtain) swig+matlab.
The code below assumes that the swig-matlab-bin folder sits in the build directory.
Precompiled
^^^^^^^^^^^
You can download the pre-compiled nightly versions from :sfnightly:`MATLAB`.
Do-it-yourself (masochistic)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
On linux and OSX, you can simply run the script dev/scripts/build_swig_matlab.py which will check out the code for swig, obtain PCRE, and build the correct architecture's binary. The resulting code will reside in the folder dev/scripts/swig-matlab/swig-matlab-bin.
For windows, its pretty much the same story, except that swig has to be cross-compiled from a linux (debian-based OS are good) host. Install all the mingw packages in Synaptic, and then run the build script like::
python build_swig_matlab.py --windows
which should hopefully yield the binaries in dev/scripts/swig-matlab/swig-matlab-bin.
Common Requirements
-------------------
Compilation of the MATLAB wrapper requires a few :ref:`common wrapper pre-requisites <wrapper_common_prereqs>`
Linux
-----
Install MATLAB using installer downloaded from www.mathworks.com. As of version R2014a, only 64-bit MATLAB is available
OSX
---
Install MATLAB using installer downloaded from www.mathworks.com. As of version R2014a, only 64-bit MATLAB is available
Windows
-------
Install MATLAB using installer downloaded from www.mathworks.com. As of version R2014a, both of 32-bit and 64-bit MATLAB are available
Build
-----
Linux and OSX
^^^^^^^^^^^^^
Once the dependencies are installed, you can run the builder and tests using::
# Check out the sources for CoolProp
git clone https://github.com/CoolProp/CoolProp --recursive
# Move into the folder you just created
cd CoolProp
# Make a build folder
mkdir build && cd build
# Copy the swig-matlab-bin folder here (fix path as necessary) (see above for Swig discussion)
cp ../../dev/scripts/swig-matlab/swig-matlab-bin .
# Set an environmental variable that points to your MATLAB installation for use in CMake (adjust if needed)
export MATLAB_ROOT=/usr/local/MATLAB/R2014a # or /Applications/MATLAB_R2014a.app
# Build the makefile using CMake with the path hacked to use our swig
PATH=swig-matlab-bin/bin:%{PATH} cmake .. -DCOOLPROP_MATLAB_MODULE=ON -DSWIG_DIR=swig-matlab-bin/bin
# Make the MEX files (by default files will be generated in folder install_root/MATLAB relative to CMakeLists.txt file)
# Setting the SWIG_LIB explicitly is dangerous, but for now it doesn't seem there is a better solution
SWIG_LIB=swig-matlab-bin/share/swig/3.0.3 make install
Windows (32-bit and 64-bit)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
You need to just slightly modify the building procedure::
# Check out the sources for CoolProp
git clone https://github.com/CoolProp/CoolProp --recursive
# Move into the folder you just created
cd CoolProp
# Make a build folder
mkdir build && cd build
# Copy the swig-matlab-bin folder here (fix path as necessary) (see above for Swig discussion)
cp ../../dev/scripts/swig-matlab/swig-matlab-bin .
# Set an environmental variable that points to your MATLAB installation for use in CMake (adjust if needed)
set "MATLAB_ROOT=c:\Program Files\MATLAB\R2014a"
# Build the makefile using CMake with the path hacked to use our swig
set "PATH=swig-matlab-bin\bin:%{PATH}" && cmake .. -DCOOLPROP_MATLAB_MODULE=ON -DSWIG_DIR=swig-matlab-bin\bin
# Make the MEX files (by default files will be generated in folder install_root/MATLAB relative to CMakeLists.txt file)
# Setting the SWIG_LIB explicitly is dangerous, but for now it doesn't seem there is a better solution
set "SWIG_LIB=swig-matlab-bin\share\swig\3.0.3" && make install
Example Code
============
.. literalinclude:: Example.m
:language: matlab
Example Code Output
===================
.. literalinclude:: Example.out
.. _low_level_high_level_matlab:
Calling Low-Level interface through DLL
=======================================
.. literalinclude:: LowLevelHighLevelMATLAB.m
:language: matlab