export variable in .sif or .f90 ?

General discussion about Elmer
Post Reply
spacedout
Posts: 177
Joined: 30 Mar 2020, 23:27
Antispam: Yes

export variable in .sif or .f90 ?

Post by spacedout »

Good afternoon

Running the following .sif
....

Solver 1
Equation = "mu"
Variable = "none"
Variable DOFs = 1

Exported Variable 1 = mu

Procedure = "flnm" "GetMu"

......

where GetMu is given by

SUBROUTINE GetMu( Model,Solver,dt,TransientSimulation )
USE DefUtils

.....

TYPE(Variable_t), POINTER :: muVar

muVar => VariableGet( Solver % Mesh % Variables,'mu')

mu => muVar % Values

......

is not problematic


On the other hand, running

....

Solver 1
Equation = "mu"
Variable = "none"
Variable DOFs = 1

Procedure = "flnm" "GetMu"

......

where GetMu is now given by

.....

TYPE(Variable_t), POINTER :: muVar

Params => GetSolverParams()

CALL ListAddString( Params,NextFreeKeyword('Exported Variable ',Params), 'mu' )

muVar => VariableGet( Solver % Mesh % Variables,'mu')

......

yields a segmentation fault - illegal memory reference

at the last line shown above. I could stick with the first approach only but I am curious about why the second approach does not work.


Marc
spacedout
Posts: 177
Joined: 30 Mar 2020, 23:27
Antispam: Yes

Re: export variable in .sif or .f90 ?

Post by spacedout »

BTW, I have the same problem when executing a trivial code with only global variables:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0 0x2b7d260890fd in ???
#1 0x2b7d2608835d in ???
#2 0x2b7d2a64927f in ???
#3 0x2b7d319a3b5c in testing_
at /home/belm/elmer/mystuff/trial.F90:34
#4 0x2b7d2572fce6 in __mainutils_MOD_singlesolver
at /home/belm/elmer/elmerfem/fem/src/MainUtils.F90:5165
#5 0x2b7d2574523c in __mainutils_MOD_solveractivate
at /home/belm/elmer/elmerfem/fem/src/MainUtils.F90:5407
#6 0x2b7d25958ff0 in execsimulation
at /home/belm/elmer/elmerfem/fem/src/ElmerSolver.F90:2002
#7 0x2b7d25958ff0 in elmersolver_
at /home/belm/elmer/elmerfem/fem/src/ElmerSolver.F90:477
#8 0x400ef5 in solver
at /home/belm/elmer/elmerfem/fem/src/Solver.F90:69
#9 0x400c5e in main
at /home/belm/elmer/elmerfem/fem/src/Solver.F90:34
Segmentation fault


The corresponding fortran file excerpt of trial.F90 is


SUBROUTINE Testing( Model,Solver,dt,TransientSimulation )
USE DefUtils

TYPE(Solver_t) :: Solver
TYPE(Model_t) :: Model
REAL(KIND=dp) :: dt
LOGICAL :: TransientSimulation
TYPE(ValueList_t), POINTER :: Params
TYPE(Variable_t), POINTER :: trythisVar

Params => GetSolverParams()

CALL ListAddString( Params,NextFreeKeyword('Exported Variable ',Params), 'trythis' )

trythisVar => VariableGet( Solver % Mesh % Variables, 'trythis' )

Print *, "do we get here? "

trythisVar % Values(1) = 777.0

Print *, "do we get there? "


where the relevant part of the .sif is

Solver 1
Equation = "exporttest"
Variable = -global whatever
Variable DOFs = 1
Exec Solver = before all
Procedure = "trial" "Testing"
End


Now, since I get the same error with

CALL ListAddString( Params,NextFreeKeyword('Exported Variable ',Params), 'trythis' )

trythisVar => VariableGet( Solver % Mesh % Variables, 'nonexisting' )

I conclude that ListAddString did not create a global variable with name trythis. The question is why ?
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: export variable in .sif or .f90 ?

Post by raback »

Hi

The standard way to create variable in Elmer is to say for a solver

Code: Select all

Variable = myvar
It was later realized that you often need other variables too. These may be used to write postprocessing info etc. A way to allocate them is to say

Code: Select all

Exported Variable i = othervar
When these appear in the sif file the library knows to allocate them and they can the be used in the module. In fact they can be used by other modules as well because they are create prior to starting module. This is often needed in multiphysics simulation.

Now, often we know that we really need some variable. Then we may use a rather dirty trick where we introduce the keyword to the list structure within the code. Every module tries to perform an _init section where these may be set.

So to answer you question. This needs to be in the Testing_init subroutine so that library will use the keyword to really make the variable. The function call ListAddString really only adds a keyword to the list, it does not create a variable. The _init has exactly the same API and sits in the same file.

There are other routines that create variables, for example DefaultVariableAdd. The downside of these is that they are not created before you visit the module. Hence it could happen that in your multiphysics setup you need the variable before you have created it.

-Peter
spacedout
Posts: 177
Joined: 30 Mar 2020, 23:27
Antispam: Yes

Re: export variable in .sif or .f90 ?

Post by spacedout »

Kills two birds with one stone. I had been wondering for the last two months about this solver_init that was never called by solver although I could see that ElmerSolver would load solver_init for each respective solver in the .sif file.
Greatest thanks !
Marc
Post Reply