Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

How to allow a user to inputs a function from a text file into a Fortran program

Customer__Intel12
3,256 Views
My idea about upgrading my program is to allow user to input a mathematical function into a program instead of have to edit the function in Fortran file. For example,

user should be able to input 1 independent variable function like

f(x) = sin(x) + ( 1+x^2 + ln(x) )^0.25

or

f(x) = x^2.4 + 1/(sin(x) + ln(x))

to a text file. After that the program will read the equation from the text file, use this function in other calculation several times then return a result.

Currently, in my program, user has to open the source code then edit the function and recompile the source code again in order to change the function inside my program.

If anyone can help me with this issue will be very helpful?
Thank you for your time to read my question.

If you have the way to allow use input a multiple independent variable explicit fuction like

f(x,y) = x + sin(y) * ln(x) - y^2

please also tell me.
0 Kudos
1 Solution
Neels
New Contributor II
3,256 Views
Maybe you are looking for something like this:

http://sourceforge.net/projects/fparser/

View solution in original post

0 Kudos
8 Replies
mecej4
Honored Contributor III
3,256 Views
Fortran is a compiled language (as are C, Pascal, Cobol, PL/I..). Source code has to be processed by a compiler before execution. What you are attempting to do would make it necessary for your Fortran application to do either

(i) call the compiler to compile the user-defined function and run the resulting object code, and manage the interaction between these processes or

(ii) implement a restricted interpreter, which can parse and evaluate expressions. There are several such interpreters that were published in earlier years.

See also: HICEST
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,256 Views
Fortran can call C/C++ routines
Windows WIN32 API is implemented as C routines (read Fortran can useWIN32 API)
WIN32 API can probe, load and unload Windows DLL's
Therefore

Read in your function from file and write as seperate .f90 file
Compile .f90 fileand link into .DLL
load .DLL
obtain address of function (WIN32 API to probe DLL for entry point)
call (unknown) function

If your functions are relatively trivial and called once or just a few times, thenI would suggest writing a parser and interpreter in Fortran (or C/C++).

Jim Dempsey
0 Kudos
Customer__Intel12
3,256 Views
Thank you for your time to answer my question (for the first person who answer me too)

I get some idea on how to do it but it seem that I need to learn more on how to do what you've suggested.
To be honest, I just get a rough idea on how to do it but really don't know how to really do what you've suggested.

In my case, the function will run ~1000 times.
The input will be an elementary algebra - explicit function.
Sometimes it have 2 independent variable sometimes it doesn't.
The time to run program is about 10 - 30 minutes.

When I use the program myself I just edit the function in .f90 file and recompile it.
If other users do this then they will know or change my source code so I try to find the way for users to enter the formular without seeing or changing my source code.

Is there any ways to ask the compiler to compile an additional code and link to the source code that is already compiled?

If I can do this then the user just need to run my program on the computer that has a Fortran compiler.
When a user run my program, then my program will automatically ask the compiler to compile the addition code and link it to the existing program that is already compiled.

Thank you for your time to read my message
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,256 Views

There are two perspectives to consider
a) Is your program the control
b) Is the user access to your program the control

If a) then your program needs to call a user specified function (or functions) defined at run time. This situation seems to be what was outlined in your first post. For this I suggest:

1) Compile your program containing
...
call UserFunc1_AB(A, B)
...
call UserFunc2_AB(A, B)
(above in a select/case, if/then, ...)
2) Ship the fully optimized .OBJ files without Debug symbols
3) Ship empty .F90 source files containing just the subroutine, argumentsand return
4a) user modifies UserFunn_AB, ...to their needs, user compiles and links with your .obj
.or.
4b) You provide automation program that performs 4a) under program control using user supplied script file

If b), then ship your components as static or dynamic library.

Alternative is to write a simple interpreter. The interpreter code may take 1ms to execute a statement (could be less depending on your programming skill). Interpreter overhead for run of program ~1 second out of 10-30 minute run time. 1/600'th to 1/1800th the totalrun time. Writing the interpreter would be the easiest way to go as long as the user functions do not contain conditionals, function calls to other user functions. You should be able to find sample interpreter code on the internet.

A second alternative is to have your .f90 program integrate to Visual Basic or other scripting language (similar to writing a .f90/C/C++ app). Then you could implement the user defined functions as VB/VJ/Vother code. This may take a little bit of learning on how to build a multi-language app.

Jim Dempsey

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,256 Views
Note, the 1,2,3,4a/4b requires the user to have a Fortran compiler.

Using your own interpeter requires no additional capability of the user.

Using VB/VJ/... requires user to be on Windows with VB/VJ/...

Jim Dempsey
0 Kudos
Neels
New Contributor II
3,257 Views
Maybe you are looking for something like this:

http://sourceforge.net/projects/fparser/

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,256 Views
neels,

The summary looks good. The OP should give it a look.

Thanks for the link.

Code like this should have been laying around for 30+ years.

Jim
0 Kudos
Jugoslav_Dujic
Valued Contributor II
3,256 Views
I used Stuart Midgley's function parser (the older version) for XGraph sample.
0 Kudos
Reply