Simple Fortran Questions

Numerical methods and mathematical models of Elmer
Post Reply
ScientistRenzo
Posts: 57
Joined: 14 Mar 2021, 21:01
Antispam: Yes

Simple Fortran Questions

Post by ScientistRenzo »

Hi! I hope you're having a great day!

So

I learned some fortran.

I tried to understand the user defined functions that are compiled with elmer

I have some question. I'll use this example file and code

fortranfile.f90

Code: Select all

function heatfunction(Model, n, time) result(heat)    //
    use DefUtils
    use Types
    type(Model_t) :: Model
    integer :: n
    real(kind = dp) :: time, heat
        heat = ...
end function heatfunction
0. Why is the return type unnecessary here? Unless it is and I'm wrong.

Within case.sif

Code: Select all

Heat Source = Variable time
    Procedure "HeatSourceFile" "heatfunction"
1.Do I need this function within "program programname ... end program programname", or is the function independent. In other words can fortranfile.f90 really be just a function. Or must it be within a program within a contains. I am new to fortran.
2. Can I use multiple variables: Heat Source = Variable coordinate 1, coordinate 2, time. Correspondingly: function heatfunction(Model, n, xcoor, ycoor, time), or is that incorrect.

Bonus/Extra
3. I'm interested; In general, can you give a super simplified brief explanation of why these files may be included, DefUtils and Types. All I know is that these are where Model_t may come from.
4. I wonder how the code in case.sif

Code: Select all

Heat Source = Variable time
    Procedure "HeatSourceFile" "heatfunction"
connects to function

Code: Select all

function heatfunction(Model, n, time) result(heat)
Specifically, where are the arguments to heatfunction passed from? This is not immediately obvious to me. And I'd like to know more about Model and n. Are these used to access each node in the geometry/mesh, in order to do the finite-element analysis so to speak?

Thanks for helping!
Have a great day!
kevinarden
Posts: 2301
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Simple Fortran Questions

Post by kevinarden »

See chapter 1 for more info, endfunction automatically does a return, but you can use return to end earlier in the code based on a branch.
The nodal coordinates are available in the subroutine,

https://www.nic.funet.fi/pub/sci/physic ... torial.pdf
ScientistRenzo
Posts: 57
Joined: 14 Mar 2021, 21:01
Antispam: Yes

Re: Simple Fortran Questions

Post by ScientistRenzo »

I read that, and had those questions.

Maybe someone can answer my question: Does my user defined function have to be within a program? Or is a .f90 file with ONLY a function written work?

I am new to fortran and Elmer.

And question2: Can I pass more variables to the user defined function. e.g. Heat Source = Variable Time, coordinate 1.
And Will I recieve these in fortran with function functionname(Model, n, time_, x_coor)?
I'm not sure how the consecutively ordered arguments of functionname interact with the consecutively ordered arguments of Heat Source = ...
Last edited by ScientistRenzo on 28 Mar 2021, 00:32, edited 1 time in total.
kevinarden
Posts: 2301
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Simple Fortran Questions

Post by kevinarden »

1, it is a function endsubroutine will provide a return or you can do a return
2. not sure if you can pass multiple variables from the sif, but there are variables available in the function
3. use types and use defutils ect. are elemer specific routines already compiled but can be linked in. Similar to include in C
4. Procedure is the name of the compile codes
elmerf90 Myprocedure.F90 -o Thisismyprocedure
would result in the sif being
Heat Source = Variable time
Procedure "Thisismyprocedure" "heatfunction"
where heatfunction is the name of the subroutine
this allows multiple subroutines in on procedure the Myprocedure.F90 could have dozens of subroutines such as

heatfunction
heatfunction1
heatfucntion2, etc.

Heat Source = Variable time
Procedure "Thisismyprocedure" "heatfunction1"
calls sub heatfunction1 from the compiled Thisimyprocedure

Model is the basically the mesh, n is a node in the mesh. the procedure is called for every node, at every iteration.
ScientistRenzo
Posts: 57
Joined: 14 Mar 2021, 21:01
Antispam: Yes

Re: Simple Fortran Questions

Post by ScientistRenzo »

Hey kevin thanks for helping me all the time

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Actual question 1

Is a function by itself appropriate?

examplefile.f90

Code: Select all

function
    ...
end function
Or should it be

Code: Select all

examplefile.f90
program
    ...
     contains
        function
        ...
        end function
end program
Or do I need the function within the program

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Actual question 2

I think I can use Variable Coordinate 1≡ x coordinate
With "The type Model_t is declared and defined in the source file Types.src also referenced by DefUtils.src.
It contains the mesh and all model data specified in the Solver Input File. As an example, the coordinates of
node n are obtained from Model as follows:
REAL(KIND=dp) :: x, y, z
x = Model % Nodes % x(n)
y = Model % Nodes & y(n)
z = Model % Nodes % z(n)"

So instead of Heat Source = Variable Coordinate 1 and referencing the x-coordinate by "tx(0)"
I can use real :: x and reference the x-coordinate by "x = Model % Nodes % x(n)"

Right?
raback
Site Admin
Posts: 4823
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: Simple Fortran Questions

Post by raback »

Hi

ElmerSolver is the program and it calls some dynamically linked functions - not programs.

Yes.

-Peter
ScientistRenzo
Posts: 57
Joined: 14 Mar 2021, 21:01
Antispam: Yes

Re: Simple Fortran Questions

Post by ScientistRenzo »

Great! Thanks, Peter!
Post Reply