Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Update create_FAST_SFunc.m for Windows, update docs on Simulink MEX building #2621

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions docs/source/install/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,26 @@ The CMake options specific to OpenFAST and their default settings are:
BUILD_DOCUMENTATION - Build documentation (Default: OFF)
BUILD_FASTFARM - Enable FAST.Farm capabilities (Default: OFF)
BUILD_OPENFAST_CPP_API - Enable building OpenFAST - C++ API (Default: OFF)
BUILD_OPENFAST_CPP_DRIVER - Enable building OpenFAST C++ driver using C++ CFD API (Default: OFF)
BUILD_OPENFAST_LIB_DRIVER - Enable building OpenFAST driver using C++ Library API (Default: OFF)
BUILD_OPENFAST_SIMULINK_API - Enable building OpenFAST for use with Simulink (Default: OFF)
BUILD_SHARED_LIBS - Enable building shared libraries (Default: OFF)
BUILD_TESTING - Build the testing tree (Default: OFF)
BUILD_UNIT_TESTING - Enable unit testing (Default: ON)
CMAKE_BUILD_TYPE - Choose the build type: Debug Release (Default: Release)
CMAKE_Fortran_MODULE_DIRECTORY - Set the Fortran Modules directory
CMAKE_INSTALL_PREFIX - Install path prefix, prepended onto install directories.
CODECOV - Enable infrastructure for measuring code coverage (Default: OFF)
CMAKE_MACOSX_RPATH - Use RPATH runtime linking (Default: ON)
CODECOVERAGE - Enable infrastructure for measuring code coverage (Default: OFF)
DOUBLE_PRECISION - Treat REAL as double precision (Default: ON)
FPE_TRAP_ENABLED - Enable Floating Point Exception (FPE) trap in compiler options (Default: OFF)
GENERATE_TYPES - Use the openfast-registry to autogenerate types modules (Default: OFF)
OPENMP - Enable OpenMP support (Default: OFF)
ORCA_DLL_LOAD - Enable OrcaFlex library load (Default: OFF)
ORCA_DLL_LOAD - Enable OrcaFlex library load (Default: ON)
USE_DLL_INTERFACE - Enable runtime loading of dynamic libraries (Default: ON)
USE_LOCAL_STATIC_LAPACK - Enable downloading and building static LAPACK and BLAS libs (Default: OFF)
VARIABLE_TRACKING - Enables variable tracking for better runtime debugging output. May increase compile time. Valid only for GNU. (Defualt: ON)


Additional system-specific options may exist for a given system, but those
should not impact the OpenFAST configuration. As mentioned above, the
Expand Down Expand Up @@ -754,6 +761,27 @@ through the Intel oneAPI distributions.

.. _installation_appendix:

Simulink
~~~~~~~~
To build the MEX function for coupling OpenFAST into Simulink, there are two
options depending on platform.

Windows with Visual Studio
--------------------------
For Windows, build with the `Release_Matlab` option from the Visual Studio
project in `vs-build/FAST/FAST.sln`. Then run
`glue-codes/simulink/src/create_FAST_SFunc.m` from MATLAB (instructions at the
top of this file).

CMake
-----
For CMake builds on all platforms, enable the `-DBUILD_OPENFAST_SIMULINK_API=On`
option in CMake and build the `FAST_SFunc` target. This will place the
resulting `FAST_SFunc.mexXXXX` in
`<build-dir>/glue-codes/simulink/FAST_SFunc.mexXXXX` (and in the install
directory at `install/bin/FAST_SFunc.mexXXXX` if `make install` was called).


Appendix
~~~~~~~~
The following are additional methods for installation which may not be fully
Expand Down
2 changes: 1 addition & 1 deletion glue-codes/simulink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ endif()
install(TARGETS FAST_SFunc
EXPORT "${CMAKE_PROJECT_NAME}Libraries"
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
LIBRARY DESTINATION bin
ARCHIVE DESTINATION lib
)
68 changes: 24 additions & 44 deletions glue-codes/simulink/src/create_FAST_SFunc.m
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
%% INSTRUCTIONS
% This script is used to manually build a Simulink mex file which uses the openfastlib shared library (.dll, .so, .dylib).
% If you are building OpenFAST with CMake on linux or macOS or windows, you can automatically generate the mex file
% by specifying -DBUILD_OPENFAST_SIMULINK_API=ON when running cmake, you do not need to use this script.
% This script is used to manually build a Simulink mex file on Windows with Visual Studio. It uses the openfastlib shared
% library (.dll).
%
% Before running this script, you must have compiled OpenFAST for Simulink to create a DLL (i.e., a shared library like .so, .dylib, .lib, etc.).
% - If cmake was used, make sure the install directory is specified properly in the `installDir` variable below,
% and if using Windows, set `built_with_visualStudio` to false.
% If you are using Windows and building with CMake, do not use this script. Instead use cmake to build the FAST_SFunc.mexXXXX directly.
%
% If you are not using Windows, do not use this script. Instead use cmake to build the FAST_SFunc.mexXXXX directly.
%
% Alternative building with CMAKE:
% specify -DBUILD_OPENFAST_SIMULINK_API=ON when running cmake
% - "make FAST_SFunc" will place the resulting mex file at <build-dir>/glue-codes/simulink/FAST_SFunc.mexXXXX
% - "make install" will place the resulting mex file at install/bin/FAST_SFunc.mexXXXX
%
%
% Before running this script, you must have compiled OpenFAST for Simulink to create a DLL (i.e., a shared library .lib).
% - If the Visual Studio Solution file contained in the vs-build directory was used to create the DLL on Windows,
% make sure `built_with_visualStudio` is set to true.
% - The name of the library that was generated must match the `libname` variable below
% and should be located in the directory specified by `libDir`.
% - The `includeDir` variable must specify the directory that contains the following header files:
% "FAST_Library.h", "OpenFOAM_Types.h", and "SuperController_Types.h"
% - The `outDir` variable indicates where the resulting mex file will reside.
% "FAST_Library.h", "OpenFOAM_Types.h", "SuperController_Types.h", and "ExtLoadsDX_Types.h"
%
% Run `mex -setup` in Matlab to configure a C compiler if you have not already done so.

mexname = 'FAST_SFunc'; % base name of the resulting mex file

built_with_visualStudio = false; %if the libraries were built with cmake, set to false
built_with_visualStudio = true; %if the libraries were built with cmake, set to false


if (ispc && built_with_visualStudio)
Expand All @@ -42,28 +48,19 @@
else
%% defaults for cmake builds:

if ( ismac ) % Apple MacOS
installDir = '../../../install';
outDir = fullfile(installDir, 'lib');
elseif ( ispc ) % Windows PC
installDir = '../../../install';
outDir = fullfile(installDir, 'lib');
% If there are shared libraries does it work for outDir to be the local directory?
else
installDir = '../../../install';
outDir = '.';
end
fprintf( '\n----------------------------\n' );
fprintf( 'Do not use this script with Mac/Linux. Follow the CMake instructions at the top of the script instead.' );
fprintf( '\n----------------------------\n' );

libDir = fullfile(installDir, 'lib');
includeDir = fullfile(installDir, 'include');
libName = 'openfastlib_mex';
end

%% BUILD COMMAND
fprintf( '\n----------------------------\n' );
fprintf( 'Creating %s\n\n', [outDir filesep mexname '.' mexext] );


if ispc () % Windows PC
%% BUILD COMMAND
fprintf( '\n----------------------------\n' );
fprintf( 'Creating %s\n\n', [outDir filesep mexname '.' mexext] );


mex('-largeArrayDims', ...
... % '-v', ... %add this line for "verbose" output (for debugging)
Expand All @@ -72,27 +69,10 @@
['-I' includeDir], ...
'-I../../../modules/supercontroller/src', ... % needed for visual studio builds to find "SuperController_Types.h"
'-I../../../modules/externalinflow/src', ... % needed for visual studio builds to find "ExternalInflow_Types.h"
'-I../../../modules/extloads/src', ... % needed for visual studio builds to find "ExtLoadsDX_Types.h"
'-outdir', outDir, ...
['COMPFLAGS=$COMPFLAGS -MT -DS_FUNCTION_NAME=' mexname], ...
'-output', mexname, ...
'FAST_SFunc.c');

else % mac/unix

mex('-largeArrayDims', ...
... '-v', ... %add this line for "verbose" output (for debugging)
['-L', libDir], ...
['-l', libName], ...
'-lgfortran', ...
'-lquadmath', ...
'-llapack', ...
'-lblas', ...
'-ldl', ...
['-I', includeDir], ...
'-outdir', outDir, ...
['CFLAGS=$CFLAGS -DS_FUNCTION_NAME=' mexname], ...
... ['CXXFLAGS=$CXXFLAGS -DS_FUNCTION_NAME=' mexname], ...
'-output', mexname, ...
'FAST_SFunc.c');

end