# ICEM CFD solver interface routine for the manager.
#
# Copyright (c) 1993-1995 ICEM Systems.

array set adhns_params {
    operation_on              	1
    operation_ns	     	1
    operation_memory            1
    operation_transport         0
    operation_time           	1.0
    operation_block            	1
    operation_conditioner       0

    iteration_on		1
    iteration_nli		0
    iteration_nlt		0.0
    iteration_mli		1
    iteration_forced_nlin	0
    iteration_forced_lin	0 

    material_on			1
    material_mu			0.0
    material_g		 	32.4
    material_rho		1.94 
    material_muref		1.0
    material_uref		1.0
    material_lref		1.0
    material_rhoref		1.94 
    material_cont		0.0
    material_moment		0.0

    time_on			1
    time_t0			0.0
    time_tf			0.0
    time_idt1			0.0
    time_idt2			0.0
    time_gst		        0.0
    time_get		      200.0
    time_adp		          0
    time_ndp		          0
    time_tstep1			0.0
    time_tstep2			0.0
    time_tstep3			0.0
    time_tstep4			0.0
    time_tstep5			0.0
    time_tstep6			0.0
    time_tstep7			0.0
    time_tstep8			0.0
    time_tstep9			0.0
    time_tstep10		0.0
}

proc adhns_write_input {} {
    global confname file_name adhns_output_path adhns_params
    global file_name
    global file_name

    set outfile $file_name(transfer_dir)/adhns
    get_boco_topo
    set paramfile $file_name(transfer_dir)/adhns.params

    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0

    set domain [domain_select $file_name(domains_dir) "Select an unstructured domain." \
	unstruct 0]
    if {$domain == {}} return

    if {![dialog .adhns "ADH_NS" \
      "Please edit the following ADH_NS options." {
	{bocofile -type path -title "Boundary condition file"}
        {outfile -type path -title "Prefix of ADH_NS input files"}
        {scale -opt {{"Yes" 1} {"No" 0}} -title "Scaling of x, y, z" }
        {x -type string -active {$scale} -title "x scaling factor" }
        {y -type string -active {$scale} -title "y scaling factor" }
        {z -type string -active {$scale} -title "z scaling factor" }
    }]} return

    if [file exists $paramfile] {
	set fp [open $paramfile r]
	while {![eof $fp]} {
	    set line [string trim [gets $fp]]
	    if {[string match \#* $line] || ([llength $line] < 2)} continue
	    set adhns_params([lindex $line 0]) [lrange $line 1 end]
	}
	close $fp
    }

    # Create a form.  If this returns "" then the form is already created and
    # will be mapped.
    set d [form_init .adhns_params "ADH_NS parameters" params]
    if {$d != ""} {
	form_checkbutton $d.oper "Operation Parameters" \
	    adhns_params(operation_on) {0 0}
	form_button $d.oper_edit Edit adhns_edit_operation {0 1} \
	    adhns_params(operation_on)

	form_checkbutton $d.iter "Iteration Parameters" \
	    adhns_params(iteration_on) {1 0}
	form_button $d.iter_edit Edit adhns_edit_iteration {1 1} \
	    adhns_params(iteration_on)

	form_checkbutton $d.mat "Material Property" \
	    adhns_params(material_on) {2 0}
	form_button $d.mat_edit Edit adhns_edit_material {2 1} \
	    adhns_params(material_on)

	form_checkbutton $d.tim "Time Control" \
	    adhns_params(time_on) {3 0}
	form_button $d.tim_edit Edit adhns_edit_time {3 1} \
	    adhns_params(time_on)

        form_finish +300+300
    }
    if ![form_wait .adhns_params] return

    set fp [open $paramfile w]
    foreach i [lsort [array names adhns_params]] {
        puts $fp "$i $adhns_params($i)"
    }

    close $fp


    set command "\"$adhns_output_path\" -dom \"$domain\" -b \"$bocofile\" \"$outfile\""
    if $scale {set command "$command -scale $x,$y,$z"}
    set command "$command -par \"$paramfile\""
    runxcom ICEM-CFD/EM-Interface $command "" 1 0
}


# All the subforms are created by these procs.

proc adhns_edit_operation {} {
    set d [form_init .adhns_params_operation "Operation Parameters" \
		"params sub"]
    if {$d != ""} {

        form_checkbutton $d.nsi "Navier-Stokes Problem" \
             adhns_params(operation_ns) {0 0 -cols 2}

        form_label $d.inc0 "Incremental memory" {1 0}
        form_label $d.trn0 "Transport quantities" {2 0}
        form_label $d.tht0 "Time step weighting" {3 0}
        form_label $d.blk0 "Blocks per processor" {4 0}
        form_label $d.pre0 "Pre-conditioner" {5 0}

        form_entry $d.inc adhns_params(operation_memory) int {1 1}
        form_entry $d.trn adhns_params(operation_transport) int {2 1}
        form_entry $d.tht adhns_params(operation_time) float {3 1}
        form_entry $d.blk adhns_params(operation_block) int {4 1}
	form_radiobutton $d.cond1 "No pre-conditioning" adhns_params(operation_conditioner) 0 {5 1}
        form_radiobutton $d.cond2 "One level additive Schwartz" adhns_params(operation_conditioner) 1 {6 1}
        form_radiobutton $d.cond3 "Two level additive Schwartz" adhns_params(operation_conditioner) 2 {7 1}
        form_radiobutton $d.cond4 "Two level hybrid" adhns_params(operation_conditioner) 3 {8 1}

	form_finish +300+300
    }
}

proc adhns_edit_iteration {} {
    set d [form_init .adhns_params_iteration "Iteration Parameters" \
		"params sub"]
    if {$d != ""} {

        form_label $d.nli0 "Non-linear iteration" {0 0}
        form_label $d.nlt0 "Non-linear tolerance" {1 0}
        form_label $d.mli0 "Maximum linear iterations" {2 0}
        form_checkbutton $d.opt1 "Forced non-linear iterations (Option 2)" \
             adhns_params(iteration_forced_nlin) {3 0}
       form_checkbutton $d.opt2 "Forced linear iterations (Option 2)" \
             adhns_params(iteration_forced_lin) {4 0}

        form_entry $d.nli adhns_params(iteration_nli) int {0 1}
        form_entry $d.nlt adhns_params(iteration_nlt) float {1 1}
        form_entry $d.mli adhns_params(iteration_mli) int {2 1}

	form_finish +300+300
    }
}

proc adhns_edit_material {} {
    set d [form_init .adhns_params_material "Material Property" \
		"params sub"]
    if {$d != ""} {

	form_label $d.dummy0 "Flow param.:" {0 0}
	form_label $d.mu0 "Viscosity" {1 0}
        form_label $d.rho0 "Density" {2 0}
        form_label $d.g0 "Gravitational acc." {3 0}

	form_label $d.dummy1 "Reference:" {0 2}
        form_label $d.muref0 "Ref. Viscosity" {1 2}
        form_label $d.rhoref0 "Ref. Density" {2 2}
        form_label $d.velref0 "Ref. Velocity" {3 2}
        form_label $d.lref0 "Ref. Length" {4 2}

	form_label $d.dummy2 "Stabilization coefficients:" {6 0}
        form_label $d.moment0 "Momentum coeff." {7 0}
        form_label $d.cont0 "Continuity coeff." {8 0}

# Flow Parameters
        form_entry $d.mu adhns_params(material_mu) float {1 1}
        form_entry $d.rho adhns_params(material_rho) float {2 1}
        form_entry $d.g adhns_params(material_g) float {3 1}
# Reference Parameters
	form_entry $d.muref adhns_params(material_muref) float {1 3}
        form_entry $d.rhoref adhns_params(material_rhoref) float {2 3}
        form_entry $d.uref adhns_params(material_uref) float {3 3}
        form_entry $d.lref adhns_params(material_lref) float {4 3}
# Stabilization Coefficients
	form_entry $d.moment adhns_params(material_moment) float {7 1}
        form_entry $d.cont adhns_params(material_cont) float {8 1}

	form_finish +300+300
    }
}

proc adhns_edit_time {} {
    set d [form_init .adhns_params_time "Time Control" \
		"params sub"]
    if {$d != ""} {

	form_label $d.t00 "Starting Time (T0)" {1 0}
        form_label $d.tf0 "Ending Time (TF)" {2 0}
        form_label $d.idt0 "time-step size" {0 2}

	form_label $d.dummy " " {3 0}
	form_label $d.gst0 "Global starting time" {4 0}
	form_label $d.get0 "Global ending time" {5 0}

	form_label $d.dummy1 " " {6 0}
        form_checkbutton $d.adp0 "Adaptive time control" \
             adhns_params(time_adp) {7 0}
        form_checkbutton $d.ndp0 "Non Adaptive time steps" \
             adhns_params(time_ndp) {8 0}

	form_label $d.dummy2 " " {9 0}
	form_label $d.dummy3 "Output  solution @:" {10 0}

        form_label $d.ts1o "Time 1" {10 1}
        form_label $d.ts2o "Time 2" {10 3}
        form_label $d.ts3o "Time 3" {11 1}
        form_label $d.ts4o "Time 4" {11 3}
        form_label $d.ts5o "Time 5" {12 1}
        form_label $d.ts6o "Time 6" {12 3}
        form_label $d.ts7o "Time 7" {13 1}
        form_label $d.ts8o "Time 8" {13 3}
        form_label $d.ts9o "Time 9" {14 1}
        form_label $d.ts10o "Time 10" {14 3}

        form_entry $d.t0 adhns_params(time_t0) float {1 1}
        form_entry $d.tf adhns_params(time_tf) float {2 1}
        form_entry $d.idt1 adhns_params(time_idt1) float {1 2}
        form_entry $d.idt2 adhns_params(time_idt2) float {2 2}
        form_entry $d.gst adhns_params(time_gst) float {4 1}
        form_entry $d.get adhns_params(time_get) float {5 1}
	form_entry $d.ts1 adhns_params(time_tstep1) float {10 2}
	form_entry $d.ts2 adhns_params(time_tstep2) float {10 4}
	form_entry $d.ts3 adhns_params(time_tstep3) float {11 2}
	form_entry $d.ts4 adhns_params(time_tstep4) float {11 4}
	form_entry $d.ts5 adhns_params(time_tstep5) float {12 2}
	form_entry $d.ts6 adhns_params(time_tstep6) float {12 4}
	form_entry $d.ts7 adhns_params(time_tstep7) float {13 2}
	form_entry $d.ts8 adhns_params(time_tstep8) float {13 4}
	form_entry $d.ts9 adhns_params(time_tstep9) float {14 2}
	form_entry $d.ts10 adhns_params(time_tstep10) float {14 4}

	form_finish +300+300
    }
}
