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

proc precise_imperial_write_input {} {
    global confname file_name precise_imperial_output_path icem_fluent icem_powermesh
    global file_name tetin_file
    global Version

    set paramfile $file_name(solver_params)
    if {![file exists $paramfile]} {
        errormsg "Parameter file $paramfile doesn't exist"
        errormsg "Please open 'Edit Parameters' menu,"
	errormsg "set solver parameters for Precise"
	errormsg "and press <Accept> button to create parameter file."
            return
    }

	switch $Version {
	"5.1" {
		set mult 1
	}
	"10.0" {
		set mult 1
	}
	default {
		set mult 0
	}
	}
	
    set domains [domain_select $file_name(domains_dir) "Select structured domains." struct $mult]    
    if {$domains == {}} return

    get_boco_topo

	
    set outdir $file_name(project_dir)

    set tetin $file_name(tetin) 
    if { $tetin == "" } {
	errormsg "Error exit: No tetin file found!"
	return
    }
    set famtopo $ftopofile

    set binary 0
    
    set msg "Please edit the following precise options."
    if {![dialog .precise "PRECISE" $msg {
	{binary -opt {{ASCII 0} {Binary 1}} \
		-title "Output format"}
	{topofile -type path -title "Topo file"  \
		-help "Topology file"}
	{bocofile -type path -title "Boco file" \
		-help "Boundary condition file"}
	{famtopo -type path -title "Family topo file" \
		-help ".fto file"}
	{paramfile -type path -title "parameter file" \
		-help "parameter file"}
	{outdir -type path -title "Precise problem directory"
		-help "Directory where problem files are saved"}
    }]} return

    if {![file exists $topofile]} {
	errormsg "Topology file doesn't exist!"
	return
    }
    if {![file exists $bocofile]} {
	errormsg "Boundary condition file doesn't exist!"
	return
    }

    if [file exists $outdir] {
	if ![file isdirectory $outdir] {
	  errormsg "Error: $outdir is not a directory" 
	  return
	}
    } else {
	cmd_mkdir $outdir
    }

    set command "\"$precise_imperial_output_path\""

    cmd_rm domain_list
    set fp [open domain_list w]
    foreach d $domains { puts $fp $d }
    close $fp
    
    set command "$command -t \"$topofile\" -f \"$famtopo\" -b \"$bocofile\""
    if $binary {set command "$command -bin"}
    set command "$command -tin \"$tetin\" -par \"$paramfile\"" 
    set command "$command -dom_list domain_list -outdir \"$outdir\""

    runcom $command ""
#    cmd_rm domain_list
}

proc precise_default_temperature {fam bctype argtitle} {
        global param_values
        ##if {$param_values(:Solution_type,Units)== "SI"} {
        ##   set T $param_values(:Solution_type,Reference_temperature)
        ##} elseif {$param_values(:Solution_type,Units) == "Imperial"} {
           set T $param_values(:Solution_type,Reference_temperature_imp)
        ##}
        return $T
}

proc precise_default_density {fam bctype argtitle} {
        global param_values

# density = p/(RT)
# p = param_values(:Solution_type,Reference_pressure)
# T = param_values(:Solution_type,Reference_temperature)
# R = 287.05

        ##if {$param_values(:Solution_type,Units)== "SI"} {
	##   set p $param_values(:Solution_type,Reference_pressure)
	##   set T $param_values(:Solution_type,Reference_temperature)
	##   set R 287.05
	##   set density [expr {$p / ($R * $T + 0.0)}]
	##} elseif {$param_values(:Solution_type,Units) == "Imperial"} {
# convert P & T to SI, compute density, then convert density back to Imperial
#	   set p [expr {$param_values(:Solution_type,Reference_pressure) * 101325 / 14.6959}]
#          set T [expr {$param_values(:Solution_type,Reference_temperature)/1.8}]
#          set R 287.05
#          set dens_si [expr {$p / ($R * $T + 0.0)}]   
#	   set density [expr {$dens_si * 2.204624 * 0.3048 * 0.3048 * 0.3048}]
	   set R 53.35
	   set p $param_values(:Solution_type,Reference_pressure_imp)
	   set T $param_values(:Solution_type,Reference_temperature_imp)
	   set density [expr {144. * $p / ($R * $T + 0.0)}]
 	##}
	return [format %.5g $density]
}

proc precise_default_massflow {fam bctype argtitle} {
    global param_values
    puts "fam $fam bctype $bctype argtitle $argtitle"
    if {$param_values(:Solution_type,Density_model) == "isothermal"} {
	return 5
    } else {
	return 17
    }
}
