include hardware, fort77.

linearization isa switch.

start := proc(start("Code")).

(start("Scalar Values") :=
proc(( banner("Retrieving scalar values of dependent variables")
; scalar_values := topsort(retrieve(dependent_var, value) with {cond})
; store(scalar_values, scalar_value)
; return scalar_values
)))
.
(start("Dimensional Analysis") :=
proc(( iff not is_defined(scalar_values) then start("Scalar Values")
; banner("Dimensional analysis")
; dimensioned_values := convert(unit, scalar_values)
; store(dimensioned_values, dimensioned_value)
; return dimensioned_values
)))
.
(start("Discretization") :=
proc(( iff not is_defined(dimensioned_values) then start("Dimensional Analysis")
; banner("Discretization and interpolation")
; discrete_values := convert(grid, expand({( _G2549
~ _G2550
)}, value, dimensioned_values)) with {linear}
; store(discrete_values, discrete_value)
; return discrete_values
)))
.
(start("Linearization") :=
proc(( iff not is_defined(discrete_values) then start("Discretization")
; (iff linearization ` switch == (on) then ( banner(
"Linearized model creation")
; linear_model
; (all_discrete_values :=
retrieve(linearized_var, discrete_value) union discrete_values
)
) else
all_discrete_values := discrete_values
)
; return all_discrete_values
)))
.
(start("Indexing") :=
proc(( iff not is_defined(discrete_values) then start("Discretization")
; banner("Deriving schemes, ranges, and boundary information")
; indexed_values := domains_and_ranges(topsort(expand(declared_op(_G2550), value, discrete_values) with {point; index}))
; store(indexed_values, indexed_value)
; return indexed_values
)))
.
(start("Optimization") :=
proc(( iff not is_defined(indexed_values) then start("Indexing")
; banner("Optimizing boundary conditions")
; foreach (i in 1 to length(indexed_values) repeat
( (indexed_values_opt : i :=
indexed_values : i with {reduce}
)
; message("~p: ~p", [i, indexed_values_opt : i])
)
)
; return indexed_values_opt
)))
.
(start("CSE") :=
proc(( iff not is_defined(indexed_values_opt) then start("Optimization")
; banner("Common Subexpression Elimination")
; (cse_values :=
(domains(dice(indexed_values_opt) : 1) with
{reduce(scan); power(log)}
)
)
; store(cse_values, cse_value)
; return cse_values
)))
.
(start("Code") :=
proc(( iff not is_defined(cse_values) then start("CSE")
; banner("Code Generation")
; foreach (i in 1 to length(cse_values) do
( (code_values : i :=
domain_loops(cse_values : i) with {code}
)
; message("~p: ~p", [i, code_values : i])
)
)
; serial_code := reduce((;), code_values)
; datapar_code := serial_code with {datapar}
; return code_values
)))
.
(start("Opt Code") :=
proc(( iff not is_defined(code_values) then start("Code")
; banner("Code Optimization")
; datapar_code := serial_code with {restruct(simp); datapar}
)))
.
(start(_G2509) :=
proc((iff confirm("Retrieve the scalar values") then start("Scalar Values") else
iff confirm("Apply dimensional analysis") then start(
"Dimensional Analysis") else
iff confirm("Apply discretization") then start("Discretization") else
iff confirm("Apply stencils and schemes") then start("Indexing") else
iff confirm("Apply optimization of schemes") then start("Optimization") else
iff confirm("Apply common subexpression elimination") then start("CSE") else
iff confirm("Generate the code") then start("Code")
)))
.
(linear_model :=
proc(( local VD
; VD := derived_var ` get_instances
; foreach V in VD repeat D(V)
)))
.