# below this depth the flow is considered "dry"
Define DRY 1e-4

# use the GfsRiver Saint-Venant solver
# shift the origin of the reference box to (0,0)
2 1 GfsRiver GfsBox GfsGEdge { x = 0.5 y = 0.5 } {
    # the domain is 3.402 m X 6.804 m
    # units for time are seconds
    PhysicalParams { L = 3.402 g = 9.81 }
    Refine 6

    # maintain the Zb1 variable using the GTS surface
    VariableFunction Zb1 bathy.gts

    # the initial water level is at z = 0, so the depth P is...
    Init {} {
        P = MAX (0., -Zb1)
    }

    # use a Sweby limiter rather than the default minmod which is too
    # dissipative
    AdvectionParams { gradient = gfs_center_sweby_gradient }

    # adapt down to 9 levels based on the slope of the (wet)
    # free-surface and with a tolerance of 1 mm
    AdaptGradient { istep = 1 } {
        cmax = 1e-3
        cfactor = 2
        maxlevel = 9
        minlevel = 6
    } (P < DRY ? 0. : P + Zb)

    # at each timestep
    Init { istep = 1 } {
	# Add a "shelf" to simulate the wall on the right-hand-side boundary
	Zb = (x > 5.448 ? 0.13535 : Zb1)
        # read in the experimental timeseries in variable 'input'
	input = input.cgd
        # implicit quadratic bottom friction with coefficient 1e-3
	U = (P > DRY ? U/(1. + dt*1e-3*Velocity/P) : 0.)
	V = (P > DRY ? V/(1. + dt*1e-3*Velocity/P) : 0.)
        P = (P > DRY ? P : 0.)
    }

    Time { end = 22.5 }
    OutputTime { istep = 10 } stderr
    OutputTiming { start = end } stderr
    OutputSimulation { step = 1 } stdout
    OutputSimulation { step = 1 } sim-%g.gfs
    EventScript { step = 1 } { gzip -f sim-*.gfs }

    # output data at probe locations
    OutputLocation { istep = 1 } input 1e-3 1.7 0
    OutputLocation { istep = 1 } p5 4.521 1.196 0
    OutputLocation { istep = 1 } p7 4.521 1.696 0
    OutputLocation { istep = 1 } p9 4.521 2.196 0

    # kinetic energy
    OutputScalarSum { istep = 1 } ke { v = (P > 0. ? Velocity2*P : 0.) }
    # free-surface elevation
    OutputScalarStats { istep = 1 } p { v = (Zb > 0. ? P : P + Zb) }
    OutputScalarNorm { istep = 1 } u { v = Velocity }
    OutputTime { istep = 1 } balance
    OutputBalance { istep = 1 } balance

    # generate movies
    GModule gfsview
    OutputView { start = 9 step = 0.0416 } { ppm2mpeg -s 640x480 > monai.mpg } { 
	width = 1280 height = 960
    } 3D.gfv
    OutputView { start = 14.63 end = 19.5 step = 0.033333333 } { 
	ppm2mpeg -s 400x600 > overhead.mpg 
    } { 
	width = 800 height = 1200
    } overhead.gfv
} {
    dry = DRY
}
GfsBox {
    # use 'subcritical' boundary condition to impose the experimental
    # water level on the left boundary
    left = Boundary { BcSubcritical U (input - Zb) }
    top = Boundary
    bottom = Boundary
}
GfsBox {
    top = Boundary
    bottom = Boundary
}
1 2 right