Hi, Felix.

Glad to see you’ve been figuring out how to get files to close using the FAST S-Function. I thought I would post a little information regarding the structure of this function so that you and others reading this will have a better idea of what is happening.

The S-Function is just a FORTRAN subroutine compiled as a Matlab mex function using the FASTGateway.f90 source file in the FAST archive. The subroutine is called in Matlab using

[y] = FAST_SFunc(t, x, u, FLAG)where y is an output vector, t is the current simulation time, x is a vector containing the states, u is a vector containing the inputs, and FLAG is a flag that determines what the subroutine is supposed to be doing. The following FLAG values are used:

FLAG = 0: This is an intialization step. FAST and AeroDyn (and all the codes linked with them) are initialized, which means input files are read, space for arrays is allocated, and output files are opened. In this case, the return value y is a vector containing the numbers of states (zero*), outputs, and inputs, and a flag that says the function has direct feedthrough. Inputs t, x, and u are ignored. The message, “FAST_SFunc initialized with …” is written to the Matlab command window.

FLAG = 3: This step returns system outputs. The FAST/AeroDyn internal simulation time is set to the input t, and vectors x and u are used to calculate the outputs returned in vector y. Data are written to the output file(s) opened in the initialization step.

FLAG = 9: This is a termination step. It calls the termination routines in FAST and AeroDyn (and codes linked with them) to close files and deallocate memory–basically the inverse of the initialization step. Inputs t, x, and u are ignored and the return value y is empty. The message, “FAST_SFunc completed.” is written to the Matlab command window.

As noted above, memory for the internal FAST_SFunc variables is reserved and files remain opened after the initialization step. If you then clear the S-Function from the Matlab workspace before calling the termination step, the files will be locked and allocated memory will be “leaked” (i.e. unavailable to be freed or used for anything else) until you close Matlab. When we rewrite the code so that there are no implicitly saved variables or states (a big project), we should be able to close files and deallocate memory on initialization, if necessary. That would eliminate the need to manually call the termination step and remove the requirement to clear the S-Function from the Matlab workspace between simulations. However, until that happens, you must make sure the termination step (FAST_SFunc(t,[],[],9)) is called–either automatically in Simulink or manually in the Matlab command window.

*Note that although the function is set up to have zero states, AeroDyn does contain states that the S-Function does not know about. This is a severe limitation, and there will be problems if the function is called without starting at t=0 or if you ever go backwards in time. See this post for more information: [url]http://forums.nrel.gov/t/fast-simulink-interface-and-internal-states/196/1]. We plan to fix this in the future.