Message Boards Message Boards

0
|
10022 Views
|
10 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Calling Fortran from Mathematica

Posted 5 years ago

Dear Community,

Is there an easy way to call / execute a Fortran code from Mathematica?

Tx in advance, best regards

Andras

POSTED BY: agilicz
10 Replies
Posted 5 years ago

It depends on exactly what you mean by "Fortran code". Is it an executable program? Is it a DLL/shared library? The various options available in WL are documented here.

POSTED BY: Rohit Namjoshi

Hi, Agilicz, I call fortran in one of my codes... However, the output of my program is to big and somehow Mathematica does not get all of it. Nevertheless, I will insert here and maybe you can use it

First, I start a shell process and I write a line to change the shell folder to my work folder

process = StartProcess[$SystemShell];
WriteLine[process, "cd /home/path/to/folder/"]

Now, I run the program

WriteLine[process, "./prog"];

If i want to insert some input, I use the same logic

WriteLine[process, "String with input"];

Finally, I kill the process

KillProcess@process;

In my case, my Fortran code does the output of the results. So, i dont need to work with it on Mathematica.

I hope that it can help you.

Best.

POSTED BY: Estevao Teixeira

Hi Neil, actually I run the code and it output a file with the results. The result does not take too long, it finishes in about 2min... and I've used the Pause function to test about timing.

I think that it is not a Mathematica limitation.. maybe the limitation is mine. I didnt spend much time trying to improve it. Once I have time, I will try fix it. I will also check about WSTP.

Thanks for your reply.

POSTED BY: Estevao Teixeira
Posted 5 years ago

Hi Rohit,

Good point, I mean a compiled, executable code. ( .exe ) . Do you possibly have a simple example?

Tx, best regards

Andras

POSTED BY: agilicz
Posted 5 years ago

Hi Estevao,

This is cool and is exactly what I need, tx a lot. My Fortan in- and output is small, so I can process it in Mathematica. I use Fortran, because I do some heavy number crunching, which is faster in Fortran.

Many tx once again, best regards

Andras

POSTED BY: agilicz

Andras,

You should also consider RunProcess[]. It will start your program and wait for, and then return the output (if this is what you want).

Estevao -- Are you certain that your program's output is too large for MMA to read? It is possible that you are reading the output before the print is complete and the print buffer is flushed. It may be a timing issue. Another fix for this (if it really is a limitation - or the timing is problematic) is to put a C code wrapper around your FORTRAN and use WSTP to communicate -- then you can send data back and forth to your FORTRAN code.

Regards,

Neil

POSTED BY: Neil Singer
Posted 5 years ago

Tx Neil, I'll test it. I saw it in the help, but was not sure what arguments to provide.

May I have another simple silly question? I have a list, and I would like to replace negative elements in the list with 0-s. What would be a simple and elegant, Mathematica like command? I could do it in an ugly old fashion way, but I'm sure there is a much simpler solution.

Tx in advance, best regards Andras

POSTED BY: agilicz

Andras,

Is this what you want? I would use Replace[] (Shortcut: "/."):

{1, 2, -3, -4, 2, 5} /. x_ /; x < 0 -> 0

output:

{1, 2, 0, 0, 2, 5}

if x is less then zero return 0, otherwise return the value.

Regards

Neil

POSTED BY: Neil Singer

You can do

myList  = {-1, 1, 23, 4, -5, 6, 3, 4, 4, 5, -3, 6}
ReplaceAll[myList, _?Negative ->  0]
POSTED BY: Estevao Teixeira
Posted 5 years ago

@Neil, @Estevao,

Thank you very much for both of you, you saved me hours.

many happy greetings

Andras

POSTED BY: agilicz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract