Relationship between Fortran and Matlab

I am relatively new to having different programming languages ​​communicate with each other and would appreciate some help. Basically I have Fortran code and Matlab code. Both codes are first initialized and then run sequentially. Each code requires input from the other. When this process is repeated frequently enough, some convergence criteria are met and the iteration stops. To complicate matters, Fortran code requires not only input from Matlab, but its own previous iteration as well. The same is true for Matlab. Therefore, as far as I can see, it is best to open both programs throughout the entire iteration process, since I have a lot of variables and therefore cannot just write them to a text file to pass them to another program and save them for the next iteration.

So essentially what I'm trying to do is something like this:

Initializing Variable Sets A, B, C, and D

Fortran:

Input: A and B
Calculations …
Output: A (variables have now new values) and D

      

Matlab:

Input: C and D
Calculations …
Output: C (variables have now new values) and B

      

Repeat Fortran and Matlab until convergence criteria are met.

So my questions are: How do I get Matlab and Fortran to communicate with each other and pass variables to the same thing? And how does each code run the other, and then wait for the other code to complete its calculation first, before continuing?

+3


source to share


1 answer


The keywords for your favorite search engine are "fortran mex". MATLAB has very good documentation / tutorial, you can start here :

MEX file allows you to call a Fortran subroutine from MATLAB



But I believe it only works if you call Fortran routines from Matlab. You cannot easily call the Matlab.m function from Fortran code. So your "main" program should be a Matlab .m

script that calls the Fortran routines defined in the MEX file (which is actually a dynamic link library).

0


source







All Articles