Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added branching for early stop #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/simulator/simulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function simulate!(sim::JutulSimulator, timesteps::AbstractVector; forces = setu
# Initialize loop
p = start_simulation_message(info_level, timesteps)
early_termination = false
stopnow = false
if initialize && first_step <= no_steps
check_forces(sim, forces, timesteps, per_step = forces_per_step)
forces_step = forces_for_timestep(sim, forces, timesteps, first_step, per_step = forces_per_step)
Expand All @@ -176,22 +177,52 @@ function simulate!(sim::JutulSimulator, timesteps::AbstractVector; forces = setu
forces_step = forces_for_timestep(sim, forces, timesteps, step_no, per_step = forces_per_step)
nextstep_global!(rec, dT)
new_simulation_control_step_message(info_level, p, rec, t_elapsed, step_no, no_steps, dT, t_tot, start_date)

t_step = @elapsed step_done, rep, dt = solve_timestep!(sim, dT, forces_step, max_its, config; dt = dt, reports = reports, step_no = step_no, rec = rec)

early_termination = !step_done
subrep = JUTUL_OUTPUT_TYPE()
subrep[:ministeps] = rep
subrep[:total_time] = t_step

if step_done
@tic "output" store_output!(states, reports, step_no, sim, config, subrep)

if begin
lastrep = rep[end]
if haskey(lastrep, :stopnow) && lastrep[:stopnow]
true
else
false
end end

subrep[:output_time] = 0.0
push!(reports, subrep)
stopnow = true

else

@tic "output" store_output!(states, reports, step_no, sim, config, subrep)

end

else

subrep[:output_time] = 0.0
push!(reports, subrep)

end

t_elapsed += t_step + subrep[:output_time]

if early_termination
n_solved = step_no-1
break
end

if stopnow
break
end

end
states, reports = retrieve_output!(sim, states, reports, config, n_solved)
final_simulation_message(sim, p, rec, t_elapsed, reports, timesteps, config, start_date, early_termination)
Expand Down Expand Up @@ -238,6 +269,9 @@ function solve_timestep!(sim, dT, forces, max_its, config; dt = dT, reports = no
# Onto the next one
done = true
break
elseif haskey(s, :stopnow) && s[:stopnow]
done = true
break
else
# Pick another for the next step...
dt = pick_timestep(sim, config, dt, dT, forces, reports, ministep_reports, step_index = step_no, new_step = false)
Expand Down
Loading