User Function to Define "Timestep Sizes" Array?

General discussion about Elmer
Post Reply
andrewdnolan
Posts: 16
Joined: 26 Oct 2020, 18:33
Antispam: Yes

User Function to Define "Timestep Sizes" Array?

Post by andrewdnolan »

I'm running a transient simulation where I every M years I drop the slip-coefficient of the Stokes for N years. The timestep I'd like to use for the M years when no sliding occurs is much larger than what is needed for the N years of slip.

I'd like to use a user functions (either fortran or lua) to define the "Timestep sizes" and "Timestep intervals" arrays based on the the values of M, N, and there respective timesteps (say M_dt and N_dt, respectively). The variables M, N, M_dt and N_dt are all subject to change. I'd actually like to do some kind of grid-search across these to determine the most appropriate for my application. That's why I'd like to use a user function to define the corresponding arrays in a programmatic way.

I'm aware of the distinction between "Timestep sizes" and "Timestep size", the latter accepting a time dependent function. That doesn't really seem like what I need; apriori I know the arrays based on the parameter values I just need a function to create the array. More importantly; I later use "Exec Interval" and "Timestep Scale" keywords to execute solvers at different intervals, where the exec intervals themselves change with the different timesteps (M_dt v.s. N_dt). The "Timestep Scale" will also change based timestep, so this also will need an array or function.

I've started with a fortran function to attempt this with;

Code: Select all

Simulation
  Coordinate System           = "Cartesian 2D"
  Simulation Type             = "Transient"
  Timestepping Method         = "BDF"
  BDF order                   = 2

  Steady State Min Iterations = 1
  Steady State Max Iterations = 10

  Timestep Sizes(20)          = Variable #gravity
                                real Procedure "../../bin/SlipCoefficent" "get_timestep_sizes"
  Timestep Intervals(20)      = Variable #gravity
                                integer Procedure "../../bin/SlipCoefficent" "get_timestep_intervals"
but have run into this error message:

Code: Select all

LoadInputFile: Number of BCs: 4
LoadInputFile: Number of Body Forces: 2
LoadInputFile: Number of Initial Conditions: 2
LoadInputFile: Number of Materials: 2
LoadInputFile: Number of Equations: 2
LoadInputFile: Number of Solvers: 12
LoadInputFile: Number of Bodies: 2
At line 183 of file SlipCoefficent.f90
Fortran runtime error: Attempting to allocate already allocated variable 'timestep_intervals'

Error termination. Backtrace:
#0  0x7f6e84297d21 in ???
#1  0x7f6e84298869 in ???
#2  0x7f6e84298ee6 in ???
#3  0x7f6e8063a5df in get_timestep_intervals_
        at /home/glacier/shared_directory/Thesis/thermal-structure/src/elmer_src/SlipCoefficent.f90:183
#4  0x7f6e845b2750 in __lists_MOD_listgetintegerarray
        at /home/glacier/elmerice/fem/src/Lists.F90:3165
#5  0x7f6e84a29716 in initializeintervals
        at /home/glacier/elmerice/fem/src/ElmerSolver.F90:675
#6  0x7f6e84a35e07 in elmersolver_
        at /home/glacier/elmerice/fem/src/ElmerSolver.F90:468
#7  0x556e137a43cd in solver
        at /home/glacier/elmerice/fem/src/Solver.F90:57
#8  0x556e137a413e in main
        at /home/glacier/elmerice/fem/src/Solver.F90:34
"initializeintervals" from the "fem/src/ElmerSolver.F90" seems to be main culprit. I'm interpreting the error having to do with trying to set this keyword with a function. Note: #gravity is just a dummy variable passed to the functions; it is never actually used. I've also hardcoded the array sizes, which I'd like to avoid.

Is something along the lines of what I'd like possible? Do the arrays need to be assigned to variables somewhere before, say in the preamble or constants section? If so, is that possible with a fortran user function? I'd ideally use fortran for this, since I'd like to read the parameters of M, N, M_dt and N_dt from the constants section, which doesn't seem to be a functionality available from lua.

Hopefully this is clear. Thanks for your time. I'd be happy to share anything that'd be useful for figuring this out.
andrewdnolan
Posts: 16
Joined: 26 Oct 2020, 18:33
Antispam: Yes

Re: User Function to Define "Timestep Sizes" Array?

Post by andrewdnolan »

I've opted to do the array creation in bash script and paste the arrays as string into a template file. This seems to work fine for my purposes.

To illustrate, I have a template .sif file where the Simulation section looks something like:

Code: Select all

Simulation
  . 
  . 
  .
  Timestep Intervals(<len>)          = Integer <NT_arr>  ![a]
  Timestep Sizes(<len>)              = Real    <dt_arr>  ![a]
  Output Intervals(<len>)            = Integer <NT_arr>
  .
  .
  .
  End
  
Then in a preproccessing script I create the timestep size ($dt_arr) and timestep interval ($NT_arr) arrays based on M, N, M_dt and N_dt as descried above. With these arrays and there length ($len) I can paste them as stings into my template .sif file with:

Code: Select all

  # Update the .sif file with the timestep arrays 
  sed "s#<len>#"$len"#g;
       s#<dt_arr>#""${dt_arr[*]}""#g;
       s#<NT_arr>#""${NT_arr[*]}""#g;" "template.sif" > "periodic_timesteps.sif"

   # The updated template file can then actually be run    
   ElmerSolver periodic_timesteps.sif
This seems like a fine work around for me.
Rich_B
Posts: 421
Joined: 24 Aug 2009, 20:18

Re: User Function to Define "Timestep Sizes" Array?

Post by Rich_B »

Hello,

Looks like you have things under control, but have you had a look at the restart feature? It might be useful, such as to run a first simulation ending with saving of the restart data file, followed by modifying a second sif file and restarting with new time steps?

Rich.
andrewdnolan
Posts: 16
Joined: 26 Oct 2020, 18:33
Antispam: Yes

Re: User Function to Define "Timestep Sizes" Array?

Post by andrewdnolan »

Using a restart for each "cycle" is something I've considered, but have tried to avoid. I will be running anywhere between 50 and 200 cycles, so this quickly becomes alot of .sif files and there respective output files.

For the sake of minimizing clutter I've defaulted to one sif file for the entire simulation. But this is something I will keep in mind moving forward.

Thanks for the help,

Andrew
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: User Function to Define "Timestep Sizes" Array?

Post by raback »

Hi Andrew,

I probably didn't fully grasp what you're up to but I give two not-so-well-known-features that might help here.

1) Use "Run Control"
Historically in some steady state cases we have used timestepping as pseudotime to enable parametric studies. This simulation type is called "scanning". Works nicely except for time-dependent problems since there we need the time for the actual time. So for this reason you need an additional control structure that lets you create yet another nested loop. This is called "Run Control". Where timestepping generates variable "time" that things may depend the "Run Control" generates variable "run" \in {1,2,3,...}. For basic test case look at "HelmholtzPlaneWavesParam".

2) Use "Periodic Time"
Sometimes you have a time periodic system where you may want to refer to time always in periodic time in [0,T_period]. This is achieved by introducing "Time Period = Real" which then results to "periodic time" being available in transient simulation similarly as normal "time". There are also bunch of other functionality related to time-periodic problems such as rotating machines that helps in studying one single period and monitoring the convergence etc. A basic test case could be test case "PeriodicTime".

-Peter

PS. When looking at cases I use "grep" in Unix. Even if I wrote many of the test cases I don't remember them by heart. This may be helpful, e.g.

Code: Select all

elmeruser@elmeruser-VirtualBox:~/elmerfem/fem/tests$ grep 'run control' -ri 
ParamFourHeaters/case.sif:Run Control
RunControlStructured/case.sif:Run Control
HelmholtzPlaneWavesParam/case.sif:Run Control
OptimizeSimplexFourHeatersInt/case.sif:Run Control 
OptimizeBobyqaFourHeaters/case.sif:Run Control 
SinusFlowTunedVelo/cavity.sif:Run Control 
OptimizeNewuoaFourHeaters/case.sif:Run Control 
MinpackControlFourHeaters/case.sif:Run Control
(some lines removed)
Post Reply