FAST S-Func MEXing process

Hi,
presentation first, my name is Stefano I’m from Modena,Italy. I would like to thank Marhall B. for his quick reply and account activation.

I’m trying to rebuild FAST S-Func for use within Simulink but I’m not able to figure out what is the mex-fortran gateway function layout…

Step-by-step: I’m able to succesfully compile FAST source code and get a standard .exe (using eclipseIDE+photran+g95 free compiler); I can mex the fortran sample within gnumex without error, but what I want is to mex FAST that is more complex/structured project…

First of all, I know I need to code the gateway function but I wouldn’t “guess” which variables I should pass and which not, if some source is already available out there. Does such function exist for download in some NWTC software package?

Thank you in advance for any help/suggestion you can give

@Jason Jonkman: I can send the sys sources if you are interested in.

cheers
stefano

Dear Stefano,

I answered your e-mail to the same question, but I’ll respond to this post so that other users can view the answer also.

We haven’t yet released the source code for compiling the MATLAB/Simulink version of FAST because we are not currently happy with how it has to be modified with each different release of MATLAB/Simulink. But since you asked for it, I’ve provided a link below to the source code and other files you should need to compile it yourself.

In FAST v6.01 (the version that is currently downloadable from our website as of July, 2008), I used an old version of MATLAB–v7.0.4.365 (R14) Service Pack 2–to compile the FAST_SFunc.dll. The extension of the FAST_SFunc was changed from .dll to .mexw32 in R2006a and later versions of MATLAB.

Here is a link to the archive of the yet-to-be-released (as of July 2008) FAST v6.02c-jmj with AeroDyn v12.58: [url]National Wind Technology Center's Information Portal | Wind Research | NREL. In this version, I compiled the corresponding FAST_SFunc.mexw32 with MATLAB v7.2.0.232 (R2006a), which also works with MATLAB R2006b. However, the model will not run in MATLAB R2007a or R2008a without additional changes. To get it to run on R2007a or R2008a, I had to update the FAST_SFunc source code again. (As it turns out, the FAST_SFunc source code was using an obsolete intrinsic function that had to be replaced in MATLAB R2007a.)

The updated source code for compiling the FAST S-Function in MATLAB R2007a (or R2008a) can be downloaded from [url]National Wind Technology Center's Information Portal | Wind Research | NREL. You can use the files in this archive to compile the FAST S-Function yourself. The archive also contains the updated MATLAB R2007a (or R2008a) version of the FAST_SFunc.mexw32 for FAST v6.02c-jmj with AeroDyn v12.58. The m-file “Make_FAST_SFunc.m” is the MATLAB script used to actually compile the mexw32 in R2007a (or R2008a). Additional changes may be required to get it to compile with other releases of MATLAB. Examine (and modify) this file as necessary. There are instructions at the top of this file.

We do plan on improving how we distribute the FAST_SFunc source code in our next release of FAST (available after July, 2008). In the mean time, I apologize for the confusion.

Best regards,

Dear Jason,
I’ve been busy for a while but I came back working on FAST, and I’ve been able to use your files to successfully compile .exe and .mexw32 FAST :smiley:

I’ve also uploaded a short how-to for the ones interested in Eclipse+Photran+G95 usage (actually only for S-Function), and can be found here

stefanocottafavi.com

I hope to find the time to put there also a sample FAST Fortran project and the simple .exe compiling procedure too.

Of course I’ll be happy to help other as you helped me; feel free to send over comments suggestions or bugs you find.

cheers
stefano

Dear Stefano,

Thanks for developing that site and supplying the link. I’ll be sure to forward users to your site when they ask me how to compile FAST with the G95 compiler.

Best regards,

Dear All,
I was wondering if I can follow the same steps of a 64 bit Windows 7 +MATLAB 2009b? What are the additional changes I have to make?

Thanks
Amit

Dear Amit,

I’m not sure if Stefano still views this forum. You could, perhaps, post a comment on Stefano’s website. But you may just need to try it yourself.

Best regards,

Dear Amit, Jason
sorry for the late reply but I’ve been quite busy these days.
I think the makefile should work also on WIn7 x64 and newer MATLAB but the best is try and see what happens.

good luck and let me know

cheers
ste

Dear Stefano,

I have been following your post on compiling FastSFunction for 32bit architectures. As you suggested the make file you wrote should run similarly for mexw64 compilation. So I took your instructions from your homepage and tried to compile.
Unfortunately I get an error message:

"
An internal error occured during: “Buidling Workspace”. Could not find a file to match the module name: Blade
"

Up to now I just could not figure out what mistake I am making. Maybe just the order of compilation is wrong? So I’ll post my new header. Hoping that someone might have already solved the problem.

Greetings
Ursula

Folders

ROOT = …
SRCF = ${ROOT}/src

Configuration options

NAME = FAST_SFunc.mexw64
OUT = Objects

Commands

FC = g95
DEBUG =
OPTIM = -O0
WARNING = -Wno-line-truncation
INCLUDE = -I"C:\Program Files\MATLAB\R2010b\extern\include"
LIBRARY = -L"C:\Program Files\MATLAB\R2010b\bin\win64" -lmex -lmat -lmx
MACRO = -DMATLAB_MEX_FILE
OPTIONS = -fmod=Modules -fcase-upper -fno-underscoring -r8
MACHINE =
OUTPUT = -o $(NAME)

Compiler flags (CFLAGS), Linker flags (LFLAGS)

CFLAGS = -c ${DEBUG} ${OPTIM} ${WARNING} ${INCLUDE} ${OPTIONS}
LFLAGS = -shared ${MACRO} ${LIBRARY} ${MACHINE} ${OUTPUT}

Targets: S-Function

fasts: ModG95.o SysG95.o DoublePrecGnu.o \
NWTC_IO.o NWTC_Num.o NWTC_Aero.o NWTC_Library.o
SharedInflowDefs.o HHWind.o FFWind.o FDWind.o CTWind.o UserWind.o InflowWindMod.o
SharedTypes.o AeroMods.o GenSubs.o AeroSubs.o AeroDyn.o
FAST_Mods.o NoiseMods.o fftpack.o FFTMod.o
AeroCalc.o HydroCalc.o FastIO.o FAST.o FAST_Lin.o
PitchControl_ACH.o SetVersion.o UserSubs.o UserVSCont_KP.o
FastSimulink.o FastGateway.o makefile

	${FC} ${LFLAGS} ${OUTPUT} \
	$(OUT)/ModG95.o $(OUT)/SysG95.o $(OUT)/DoublePrecGnu.o \ 
	$(OUT)/NWTC_IO.o $(OUT)/NWTC_Num.o $(OUT)/NWTC_Aero.o $(OUT)/NWTC_Library.o\
	$(OUT)/SharedInflowDefs.o $(OUT)/HHWind.o $(OUT)/FFWind.o $(OUT)/FDWind.o $(OUT)/CTWind.o $(OUT)/UserWind.o $(OUT)/InflowWindMod.o\
	$(OUT)/SharedTypes.o $(OUT)/AeroMods.o $(OUT)/GenSubs.o $(OUT)/AeroSubs.o $(OUT)/AeroDyn.o\
	$(OUT)/FAST_Mods.o $(OUT)/NoiseMods.o $(OUT)/fftpack.o $(OUT)/FFTMod.o \
	$(OUT)/AeroCalc.o $(OUT)/HydroCalc.o $(OUT)/FastIO.o $(OUT)/FAST.o $(OUT)/FAST_Lin.o \
	$(OUT)/PitchControl_ACH.o $(OUT)/SetVersion.o $(OUT)/UserSubs.o $(OUT)/UserVSCont_KP.o \
	$(OUT)/FastSimulink.o $(OUT)/FastGateway.o

Hi, Ursula.

I think you’re probably correct that the order of compilation is wrong. To compile the S_Function, you should be able to use the order of the files listed in the make_FAST_SFunc.m file, possibly replacing the SysMatlab.f90 file with one modified for Matlab on your compiler (you’ll likely have to remove the lines that say “USE IFPORT”). In the code you’ve shared, it looks like there are a couple of extra object files listed and some that are out of order.

Hi all,

I have been trying hard to mex the S-function on Matlab R2013a with intel composer XE 2011 using the “intelf12msvs2010shellopts.bat” and the “make_FAST_SFunc.m” files provided. Of course I changed the paths in the “make_FAST_SFunc” file, but, after creating all the .mod files, I get the error message:

‘link’ is not recognized as an internal or external command,
operable program or batch file.

I have been trying to change some paths, got different error messages, but was not able to mex the S-Function.

Does anybody have a solution?

Thanks in advance,
Mario

Actually I have to immediately update my previous post: I was able to correctly mex the 32bit S-Function by modifying the “intelf12msvs2010opts.dat” file according to the suggestions of Bonnie:

% (3) Change COMPFLAGS in this new .bat file:
% add these flags if they aren’t there already:
% /assume:byterecl /traceback /real_size:64 /Qzero /Qsave
% remove these flags if they exist:
% /fpp /fixed
% (4) Check variables OPTIMFLAGS and DEBUGFLAGS in the .bat file:
% remove /MD if it is there

but when I try to do the same thing for a 64bit S-Function, although I get an error free process, the Simulink Simulation crashes as soon as I try to run it. Did anybody succeed in mexing successfully a 64bit S-Function? How did it work?

Thank you,
Mario

Hi, Mario.

We are aware of an issue with the FAST_SFunc not working on 64-bit Matlab. As you said, it compiles without error, but then crashes Matlab. Unfortunately, we have not had time to debug the issue. Our immediate solution is to use 32-bit Matlab.

If you do find a solution, please let us know.

Hello,
I need to make the S_Function (mex in the matlab). But I do not know which versions of FAST , AeroDyn, Inflow, … should be used.
all the versions are changing very fast and making me confused.

would you please tel me where can I find required files for making the S_Function and which versions?

Best Regards,
Kaveh

Hi Mario and others,

I have the 64bit S-function working without any problems in Linux.
To make it work you need to change some numbers in the FASTGateway.f90 since some of it is fixed at 4 where they need to be 8 for it to work in 64 bit.
You can see all the patches and a guide for getting it up and running in Ubuntu here:http://kom.aau.dk/~anb/fast/fastinstall.html

Br
Anders

Kaveh,

If you are starting out with a new project, you should use the latest version of FAST on the web site. Currently, that is v7.02.00d-bjj. The web site also tells you which versions of the other codes you will need to compile with. If the current version of a supporting FAST module is only slightly different than the one you need, the newer version will probably work, too. If you’re still having trouble, let us know which versions you are trying to use.

Borchersen,

Thanks for posting your solution to getting the SFunc to work on 64-bit linux. I glanced at it briefly and made a quick fix to FASTGateway.f90 so that it should work on both 32 and 64-bit Windows.

To anyone else reading this,

You can download FAST_SFunc for 64-bit Windows (FAST v7.02.00d-bjj) here. I’ve also placed some new source files there so people can compile it themselves. Note that you will need to add /fpp to the compile options (for preprocessing FASTGateway.f90).

[size=70]
The quick fix involves just a few steps:

  1. In FASTGateway.f90, add a statement to use ISO_C_BINDING and then set mwPointer to C_INTPTR_T
  2. In make_FAST_SFunc.m, add the -compatibleArrayDims switch to the mex command
  3. Change your options script to use the 64-bit compiler[/size]

Hi,
I am Yunchuan Li, currently an intern student at UBC. Now I face some problem in compiling the FAST_SFunc for 64 bit Matlab with the FASTGateway.f90 and make_FAST_SFunc.m downloaded from the online folder.
My compiler is Intel Fortran Composer XE 2013 SP1. I use Microsoft Visual Studio 2013 and 64 bit version of Matlab R2013a to compile the FAST_SFunc.
I set my intelf13msvs2012opts.bat file like this:

@echo off
rem INTELF13MSVS2012OPTS.BAT
rem
rem Compile and link options used for building MEX-files using the
rem Intel? Fortran Compiler 13.0 with the Microsoft? Visual Studio?
rem 2012 Professional Edition linker.
rem
rem StorageVersion: 1.0
rem FortrankeyFileName: INTELF13MSVS2012OPTS.BAT
rem FortrankeyName: Intel Visual Fortran
rem FortrankeyManufacturer: Intel
rem FortrankeyVersion: 13.0
rem FortrankeyLanguage: Fortran
rem FortrankeyLinkerName: Microsoft Visual Studio 2012
rem FortrankeyLinkerVersion: 11.0
rem
rem $Revision: 1.1.6.1 $ $Date: 2012/09/25 18:20:21 $
rem
rem ********************************************************************
rem General parameters
rem ********************************************************************
set MATLAB=E:\Program Files\MATLAB\R2013a
set IFORT_COMPILER13=C:\Program Files (x86)\Intel\Composer XE 2013 SP1
set VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 12.0
set VCINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC
rem In this case, LINKERDIR is being used to specify the location of the SDK
set LINKERDIR=C:\Program Files (x86)\Windows Kits\8.0
set PATH=%IFORT_COMPILER13%\Bin\Intel64;%VCINSTALLDIR%\bin\amd64;%LINKERDIR%\bin\x64;%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%MATLAB_BIN%;%PATH%
set INCLUDE=%IFORT_COMPILER13%\compiler\Include;%LINKERDIR%\include\um;%LINKERDIR%\include\shared;%LINKERDIR%\include\winrt;%VCINSTALLDIR%\ATLMFC\INCLUDE;%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\VCPackages;%INCLUDE%
set LIB=%IFORT_COMPILER13%\compiler\Lib\Intel64;%LINKERDIR%\LIB\win8\um\x64;%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%MATLAB%\extern\lib\win64;%LIB%

set MW_TARGET_ARCH=win64

rem ********************************************************************
rem Compiler parameters
rem ********************************************************************
set COMPILER=ifort
set COMPFLAGS= /fpp /Qprec /assume:byterecl /traceback /real_size:64 /Qzero /Qsave “/I%MATLAB%/extern/include” -c -nologo -DMATLAB_MEX_FILE /MD /fp:source
set OPTIMFLAGS=/O2 /DNDEBUG
set DEBUGFLAGS=/Z7
set NAME_OBJECT=/Fo

rem ********************************************************************
rem Linker parameters
rem ********************************************************************
set LIBLOC=E:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft
set LINKER=link
set LINKFLAGS=/dll /export:MEXFUNCTION /LIBPATH:“%LIBLOC%” libmx.lib libmex.lib libmat.lib /implib:“%LIB_NAME%.x” /MAP:“%OUTDIR%%MEX_NAME%%MEX_EXT%.map” /NOLOGO /manifest /INCREMENTAL:NO
set LINKOPTIMFLAGS=
set LINKDEBUGFLAGS=/debug /PDB:“%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb”
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=/out:“%OUTDIR%%MEX_NAME%%MEX_EXT%”
set RSP_FILE_INDICATOR=@

rem ********************************************************************
rem Resource compiler parameters
rem ********************************************************************
set RC_COMPILER=rc /fo “%OUTDIR%mexversion.res”
set RC_LINKER=

set POSTLINK_CMDS=del “%LIB_NAME%.x” “%LIB_NAME%.exp”
set POSTLINK_CMDS1=mt -outputresource:“%OUTDIR%%MEX_NAME%%MEX_EXT%”;2 -manifest “%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest”
set POSTLINK_CMDS2=del “%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest”
set POSTLINK_CMDS3=del “%OUTDIR%%MEX_NAME%%MEX_EXT%.map”

But when I try to compile the FAST_SFunc, it results in errors:

→ link /out:“FAST_SFunc.mexw64” /dll /export:MEXFUNCTION /LIBPATH:“E:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft” libmx.lib libmex.lib libmat.lib /implib:“C:\Users\CEL\AppData\Local\Temp\mex_kBGAR3\templib.x” /MAP:“FAST_SFunc.mexw64.map” /NOLOGO /manifest /INCREMENTAL:NO @C:\Users\CEL\AppData\Local\Temp\mex_kBGAR3\mex_tmp.rsp

Creating library C:\Users\CEL\AppData\Local\Temp\mex_kBGAR3\templib.x and object C:\Users\CEL\AppData\Local\Temp\mex_kBGAR3\templib.exp
MSVCRT.lib(crtdll.obj) : error LNK2019: unresolved external symbol __imp_EncodePointer referenced in function pre_c_init
MSVCRT.lib(atonexit.obj) : error LNK2001: unresolved external symbol __imp_EncodePointer
MSVCRT.lib(crtdll.obj) : error LNK2019: unresolved external symbol __imp_DecodePointer referenced in function _CRT_INIT
MSVCRT.lib(atonexit.obj) : error LNK2001: unresolved external symbol __imp_DecodePointer
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_QueryPerformanceCounter referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentProcessId referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentThreadId referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetSystemTimeAsFileTime referenced in function __security_init_cookie
MSVCRT.lib(dllmain.obj) : error LNK2019: unresolved external symbol __imp_DisableThreadLibraryCalls referenced in function DllMain
FAST_SFunc.mexw64 : fatal error LNK1120: 7 unresolved externals

E:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Link of ‘FAST_SFunc.mexw64’ failed.

Error using mex (line 206)
Unable to complete successfully.

Error in make_FAST_SFunc (line 69)
mex(‘-v’ , … %verbose

When I did this on 32 bit Version of Matlab and try to comile FAST_SFunc for 32bit Matlab, it compiles smoothly without any error.
I don’t know what the link error is and really exhausted of ideas on how to fix the problem.
Does any body have ideas on how to solve this problem.
Thank you!
Yunchuan

I have not seen this error before, but it looks like it’s using the wrong version of the MSVCRT.lib file. That could mean that you’ve got a path issue where it’s finding the 32-bit version of the MSVCRT.lib file instead of the 64-bit version. Or perhaps you’ve got a dynamic version instead of a static version (try removing /MD from COMPFLAGS and see if that helps).

Good luck!

Hi Bonnie,
Thank you for your help!

First of all, reached the same conclusion that the linker is finding the 32bit version of the lib file. Since even though we do not set linker direction when compiling 32 bit SFunction, it compiles without any error. But when I set the linker direction to blank when compiling 64 bit SFunction, it result in the same error.

Secondly, we tried your suggestion to remove /MD from COMPFLAG, it resulted in another error:

LIBCMT.lib(wgetcwd.obj) : error LNK2001: unresolved external symbol __imp_GetFullPathNameW
LIBCMT.lib(read.obj) : error LNK2019: unresolved external symbol __imp_ReadConsoleW referenced in function _read_nolock
libirc.lib(tbk_backtrace.obj) : error LNK2019: unresolved external symbol __imp_VirtualQuery referenced in function tbk_getModuleName
FAST_SFunc.mexw64 : fatal error LNK1120: 137 unresolved externals

E:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Link of ‘FAST_SFunc.mexw64’ failed.

Error using mex (line 206)
Unable to complete successfully.

Error in make_FAST_SFunc (line 69)
mex(‘-v’

(There are actually more errors, I just cut theend of the report ).
Anyone has some ideas about this error?

Again thank you for your help!
Yunchuan Li

Could you post the first error(s)? They are more helpful than the last ones.

I’m not sure what “linker direction” is. Do you mean “LINKERDIR”? LINKERDIR is just the directory (folder) where the linker is located, though in your case, it says it’s where SDK (software development kit) is located.

Also, you should try to verify that the %PATH%, %INCLUDE% and %LIB% environment variables in your batch file are the same as the values you get when you echo them in the Intel 64 Visual Studio command prompt window. The only differences should be that the variables in the Intel 64 VS window won’t list the folders that contain %MATLAB%. For instance, the %LIB% variable in your batch file looks like it’s set like this: LIB= C:\Program Files (x86)\Intel\Composer XE 2013 SP1\compiler\Lib\Intel64; C:\Program Files (x86)\Windows Kits\8.0\LIB\win8\um\x64; C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\amd64; C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\ATLMFC\LIB\amd64; E:\Program Files\MATLAB\R2013a\extern\lib\win64;(most computers don’t have %LIB% defined outside the compiler window; if you do, there will be more folders listed under “LIB”; also, I put line breaks between the folder names, but your variable will be one long messy line).
When you type echo %LIB% in the Intel 64 Visual Studio command prompt window, does it show these folders:C:\Program Files (x86)\Intel\Composer XE 2013 SP1\compiler\Lib\Intel64; C:\Program Files (x86)\Windows Kits\8.0\LIB\win8\um\x64; C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\amd64; C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\ATLMFC\LIB\amd64; If they aren’t the same or there are more in the Intel 64 VS window than in the batch script, that will be a problem. (If you need help finding the Inte 64 Visual Studio command prompt window, see Figure 2 in this document: wind.nrel.gov/public/bjonkman/De … ctions.pdf)