Command that calls functions without the need of arguments

Hey there.

I wrote a longer code for a simulation and now I have a simple problem, but don’t know the best way to solve it.

While working on my simulation loop part, I need to “reset” my input variables. That means, I want to read INI-files again, initialize some basic properties again and reset arrays which have been filled within the last run of my simulation. Of course, I can do this by running the whole code or the initialization part again.

But I would like to have something like “reset()”, that I can type in the console and that’s it. Maybe in such a way:

command SimReset()
 a = dosomething1(b,c,d)
 e,f = dosomething2(g,h)
 i,j = importINI(k)
 ...
end

Is there any possibility to accomplish something like this?

Thanks.

Please

  1. quote code,
  2. provide a MCVE.

Generally, you would have your problem contained in a structure, bound to a variable, and you can simply implement a reset(problem::MyProblemType) method.

3 Likes

@Tamas_Papp: Thank you, I have edited my first post and will provide a better example. You are absolutely right, it is indispensable.

Here is an example. But because I have not really a problem in terms of error messages, I just give the structure of my code and use [...] to abbreviate some content here.

### initialization
function read_INI_files()
  IN_configINI = Inifile()
  IN_calcINI = Inifile()
  IN_envINI = Inifile()
  read(IN_configINI, joinpath(dirname(@__FILE__),"config.ini"))
  read(IN_calcINI, joinpath(dirname(@__FILE__),"calc.ini"))
  read(IN_envINI, joinpath(dirname(@__FILE__),"env.ini"))
  return(IN_configINI, IN_calcINI, IN_envINI)
end
function evaluate_INI_files(IN_configINI, IN_calcINI, IN_envINI)
  [...]
end
IN_configINI, IN_calcINI, IN_envINI = read_INI_files()
IN_CalculationProperties, IN_N_StoragesToday, IN_N_RatesToday, IN_SupportValues, IN_SimStart, IN_SimEnd, IN_Δt = evaluate_INI_files(IN_configINI, IN_calcINI, IN_envINI)

### general properties
function initialize_TimeStep(IN_SimStart, IN_SimEnd, IN_Δt)
  [...]
end

function initialize_Storages(IN_N_StoragesToday)
  [...]
end

SM_N_Storages = initialize_Storages(IN_N_StoragesToday)
SM_SimStart, SM_SimEnd, SM_Δt, SM_SimSteps, SM_TimeStep = initialize_TimeStep(IN_SimStart, IN_SimEnd, IN_Δt)

# ------------------------------------------------------------------------------
### simulation

include("processes/Atm_N-1.1.jl")
include("processes/Lith_N-1.1.jl")
for SM_t in range(1, Int(SM_SimSteps) - 1)
  [...]

At the moment I work at the simulation part (below the dashed line), but sometimes I need to change input variables. For this, I don’t want to call all functions individually. Instead of typing in the console…

IN_configINI, IN_calcINI, IN_envINI = read_INI_files()
IN_CalculationProperties, IN_N_StoragesToday, IN_N_RatesToday, IN_SupportValues, IN_SimStart, IN_SimEnd, IN_Δt = evaluate_INI_files(IN_configINI, IN_calcINI, IN_envINI)
SM_N_Storages = initialize_Storages(IN_N_StoragesToday)
SM_SimStart, SM_SimEnd, SM_Δt, SM_SimSteps, SM_TimeStep = initialize_TimeStep(IN_SimStart, IN_SimEnd, IN_Δt)

…I want to write something like…

reset()

Is there any easy possibility to bundle this?

To use something like reset(), you would need to use globals. This is possible, but encourages bad style.

Instead, I would just pack everything to a structure, eg

struct Model
    config
    calc
    env
    calculation_properties
    ...
end

break the initialization of this into steps/stages (depending on what you can reuse, package a group of variables to structs), and then have a single reset!(m::Model) function that works on this.

1 Like

Wonderful! I am just reading the documentation of struct in Julia, due to your hint in your other post.

I will try this. Thanks a lot, sounds feasible and useful.