
# This code creates the ICEM CFD Manager.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

# Note that the file solver_table.tcl is loaded immediately before this.
# This file defines the list "solver_interfaces".

set snames ""
foreach si $solver_interfaces {
    lappend snames [lindex [lindex $si 0] 0]
}
set solver_modal_data [list solver Solver [lsort $snames] "" "" 0]

# Stuff for the output solver interface.

proc output_select_solver {} {
    global solver
    set choices [get_modal_choices solver]
    toplevel .select_one
    set one [select_one .select_one {-text "Select a \
solver to use by double-clicking on the name."} $choices $solver {}]
    if {$one == "*cancel*"} return
    if {[lsearch $choices $one] < 0} {
	errormsg "No such solver $one"
	return
    }
    set solver $one
}

proc output_write {{set_sol ""}} {
    global solver place solver_interfaces env gui_name
    
    if {$set_sol != ""} { set solver $set_sol }
    
    if {$place == ""} {
	errormsg "Can't write input -- the $gui_name(conf) or \
$gui_name(geo) is not set."
	return
    }
    
    set tdir $env(ICEM_ACN)/icemcfd/output-interfaces
    foreach si $solver_interfaces {
	foreach sn [lindex $si 0] {
	    if ![string compare $solver $sn] {
		set proc [lindex $si 1]_write_input
		if {[info procs $proc] == ""} {
		    if ![file exists $tdir/[lindex $si 1].tcl] {
			warningmsg "GUI file for $solver doesn't exist"
			return
		    }
		    uplevel #0 source $tdir/[lindex $si 1].tcl
		}
		$proc
		return
	    }
	}
    }
    
    warningmsg "Unrecognized solver name \"$solver\""
}

proc output_g2u {} {
    global place g2u_path icem_fluent icem_powermesh gui_name
    
    if {$place == ""} {
	errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
	return
    }
    set gdomain [domain_select $place/domains "Select a global domain." global 0]

    if {$gdomain == {}} return
    
    set nname [query_enter .query "Name for the output unstructured domain?" \
	       "$place/domains/uns_domain"]
    if {$nname == "*cancel*"} return
    
    set mat [query_enter .query "Material numbers?" "0"]
    if {$mat == "*cancel*"} return
    
    set tol [query_enter .query "Rounding factor?" "0.2"]
    if {$tol == "*cancel*"} return
    
    if { !$icem_fluent && !$icem_powermesh } {set boco "$place/mesh/boco"} else {set boco "$place/mesh/boco_info"}

    set command "$g2u_path $gdomain $nname $boco $mat round $tol"

    runxcom Global_to_Unstructured $command "" 1 0
}

proc output_s2u {} {
    global place s2u_path confname icem_fluent icem_powermesh gui_name
    
    if {$place == ""} {
	errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
	return
    }

    set domdir $place/domains
    
    set seldoms [domain_select $place/domains "Select structured domains \
to include in the unstructured domain." struct 1]
    if {$seldoms == {}} return

    cmd_rm domain_list
    set fp [open domain_list w]
    foreach d $seldoms { puts $fp $d }
    close $fp

    get_boco_topo

    set outfile struct_merge
    set set_tol 0
    set tol 0.00001
    set new_version 1
    set familyPerDomainFlag 1
    set familyPerSubfaceFlag 0
    set familyPerEdgeFlag 0
    set familyPerVertexFlag 0

    set family_boco $place/mesh/family_boco
    set family_topo $place/mesh/family_topo

    if {![dialog .s2u "Structured -> Unstructured" \
	  "Please edit the following options for s2u." {
	      {topofile -type path -title "Topology file" -help "Topo file"}
	      {new_version -opt {{"New (creates family_boco)" 1} {"Old (compatible with boco only)" 0}} \
		-title "Select version of converter"}
	      {bocofile -type path -active {$new_version} -title "Boundary Condition file"}
	      {familyPerDomainFlag -active {$new_version} -opt {{"Yes" 1} {"No" 0}} \
		-title "Create a family per domain?"}
	      {familyPerSubfaceFlag -active {$new_version} -opt {{"Yes" 1} {"No" 0}} \
		-title "Create a family per subface?"}
	      {familyPerEdgeFlag -active {$new_version} -opt {{"Yes" 1} {"No" 0}} \
		-title "Create a family per edge?"}
	      {familyPerVertexFlag -active {$new_version} -opt {{"Yes" 1} {"No" 0}} \
		-title "Create a family per vertex?"}
		
	      {outfile -type string -title "Output domain" -help "Output domain"}
	      {alltypes -opt {{"All types" 1} {"Triangles only" 0}} \
		  -title "Element types" -help "Element types to use"}
	      {set_tol -active {!$alltypes} -opt {{"Yes" 1} {"No" 0}} \
                        -title "set a merging tolerance"
                        -help  "set a merging tolerance"}
              {tol -type string -active {$set_tol} -title "Merging tolerance" \
                        -help "enter a merging tolerance"}
	  }]} return
    
    if {![file exists $topofile]} {
	warningmsg "Topology file doesn't exist!"
	return
    }

    if ![string compare $family_boco $bocofile] {
            if {![file exists $family_topo]} {
                errormsg "family_topo file doesn't exist!"
                return
            }
    }
    
    set command "$s2u_path -t $topofile -o $place/domains/$outfile"

    if {$new_version} {
	set command "$command -new -b $bocofile"
	if [file exists $family_topo] {set command "$command -f $family_topo"}
	if {$familyPerDomainFlag} {set command "$command -df"}
	if {$familyPerSubfaceFlag} {set command "$command -sf"}
	if {$familyPerEdgeFlag} {set command "$command -ef"}
	if {$familyPerVertexFlag} {set command "$command -vf"}
    }
    if {!$alltypes} {set command "$command -tria"}
    if $set_tol {set command "$command -tol $tol"}

    set command "$command -dom_list domain_list"

    runxcom Structured_to_Unstructured $command "" 1 0
}

proc output_merge {} {
    global place merge_unstr_path confname icem_fluent icem_powermesh gui_name
   
    if {$place == ""} {
        errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
        return
    }

    set domains [domain_select $place/domains "Select the unstructured \
        domains to be merged."  unstruct 1]
    if {$domains == {}} return

    get_boco_topo

    set combine_type 1
    set merge_type 1
    set tolerance 0.1
    set reset_pids 0
    set merge_boco 0

    set boco1 $place/mesh/boco.1
    set boco2 $place/mesh/boco.2
    set boco_merge $place/mesh/boco_merge
   
    set outfile $place/domains/merge_unstruct

    if {![dialog .merge "Merge unstruct. domains" \
          "Please edit the following options for merge_unstr." {
	      {combine_type -opt {{{Merge common nodes} 1} \
			          {{Combine without merging} 0}} }
	      {merge_type -opt {{Topological 1} {Geometric 0}} \
	 	-title "Merge type" -active $combine_type}
              {topofile -type path -title "Topology file" \
		-active {$merge_type && $combine_type}} 
	      {tolerance -type string -title "Tolerance" \
		-active {!$merge_type && $combine_type}}
	      {reset_pids -opt {{Yes 1} {No 0}} \
                   -title "Set element PIDs to their file number"}
	      {merge_boco -opt {{Yes 1} {No 0}} \
                   -title "Merge boco files"}
	      {boco1 -type string -active $merge_boco \
		   -title "First boco file"}
	      {boco2 -type string -active $merge_boco \
		   -title "Second boco file"}
	      {boco_merge -type string -active $merge_boco \
		   -title "Merged boco file"}
              {outfile -type string -title "Output domain"}
          }]} return

    if {$combine_type && $merge_type} {
	set opt "-t $topofile"
	if {![file exists $topofile]} {
            warningmsg "Topology file doesn't exist!"
            return
    	}
    } elseif {$combine_type && !$merge_type && [file exists $topofile]} {
 	set opt "-t $topofile -tol $tolerance"
	if {![file exists $topofile]} {
            warningmsg "Topology file doesn't exist!"
            return
        }
    } elseif {$combine_type && !$merge_type && ![file exists $topofile]} {
	set opt "-tol $tolerance"
    } elseif {!$combine_type} {
	set opt "-tol 0"
    }

    if {$merge_boco} {
	set opt "$opt -b1 $boco1 -b2 $boco2 -b_merge $boco_merge"
    }

    if {$reset_pids} {set opt "$opt -reset_pids"}

    cmd_rm domain_list
    set fp [open domain_list w]
    foreach d $domains { puts $fp $d }
    close $fp

    runcom "$merge_unstr_path $opt -dom_list domain_list $outfile"
}

# Note that this routine returns the name of the created superdomain.

proc output_super {all {supername ""}} {
    global confname place superdomain_path icem_fluent icem_powermesh gui_name

    if {$place == ""} {
	errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
	return ""
    }
    
    set sel [domain_select $place/domains "Select structured domains to include \
in the superdomain." struct 1]
    if {$sel == {}} return
    
    cmd_rm -f domain_list
    set fp [open domain_list w]
    foreach d $sel { puts $fp $d }
    close $fp

    set ask 0
    if {$supername == ""} {set supername $place/domains/super_domain.1; set ask 1}

    get_boco_topo
    set listname $place/transfer/sup_domain_list
    
    if $ask {
	if {![dialog .super "Create superdomain" \
	      "Please edit the following options for the superdomain program." {
		  {topofile -type path -title "Topology file" -help "Topo file"}
		  {supername -type path -title "Superdomain file" -help "Superdomain"}
		  {listname -type path -title "Index list file" -help "Index list file"}
	      }]} {return ""}
    }
    
    if {![file exists $topofile]} {
	errormsg "File $topofile doesn't exist"
	return ""
    }
    
    set command "$superdomain_path -list $listname -t $topofile \
        -o $supername -dom_list domain_list"
        
    runcom $command ""
    
    return $supername
}

proc output_qtet {} {
    global confname place qtet_path gui_name

    if {$place == ""} {
        errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
        return
    }

    set domain [domain_select $place/domains "Pick a linear tetra domain." unstruct 0]
    if {$domain == {}} return

# get name for quadratic elements domain
    set outfile [query_path .query "Quadratic tetrahedra file?" \
                 "$place/domains/quad_tet"]
    if {$outfile == "*cancel*"} return

    set ifile $domain
# check if the user wants to project edge mid nodes
    if {[query_question .query "Project new nodes?"] != "*cancel*" } {
# get tetin file
	set input [ get_tetin_file $place ]

        if {$input == ""} return
	set sfile $place/mesh/$input

        set command "$qtet_path $ifile $outfile $sfile"

    } else {
# run without tetin file
        set command "$qtet_path $ifile $outfile"

    }

    runcom $command ""
}

proc output_uncouple {} {
    global confname place uncouple_path gui_name

    if {$place == ""} {
        errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
        return
    }

    set domain [domain_select $place/domains "Pick the input domain." unstruct 0]
    if {$domain == {}} return

    set outfile [query_path .query "Output domain?" $domain.uncoupled]
    if {$outfile == "*cancel*"} return
    
    set files [glob -nocomplain "$place/mesh/tetin*"]
    set inputs {}
    foreach file $files {lappend inputs [file tail $file]}
    toplevel .select_one
    set input [select_one_lc .select_one \
		   "-text {Pick a tetin file if desired.}" \
		   $inputs {} {}]
    if {$input == "*cancel*"} return
    if {$input == ""} {
	set sfile ""
    } else {
	set sfile "-t $place/mesh/$input"
    }
    
    set command "$uncouple_path $domain $outfile $sfile"

    runcom $command ""
}

proc output_bandwidth {} {
    global confname place uncouple_path gui_name

    if {$place == ""} {
        errormsg "The $gui_name(conf) or $gui_name(geo) is not set."
        return
    }

    set domain [domain_select $place/domains "Pick the input domain." unstruct 0]
    if {$domain == {}} return

    set outfile [query_path .query "Output domain?" $domain.reduced]
    if {$outfile == "*cancel*"} return
    
    set command "$uncouple_path $domain $outfile -r"

    runcom $command ""
}

# Look up boco and topo files.  If they are not there, make the variables
# blank.

proc get_boco_topo {} {
    global place confname
    
    set boco ""; set topo ""
    if {$place != ""} {
        set tt 0
        foreach t [list $place/mesh/topo_mulcad_out \
                        $place/mesh/topo_info] {
            if [file exists $t] {
                set nt [file mtime $t]
                if {$nt > $tt} {
                    set topo $t
                    set tt $nt
                }
            }
        }
	if {$topo == ""} {
	    set t mulcad/$confname/topology/topo_mulcad_out
	    if [file exists $t] {
		set topo $t
	    }
	}
        set tt 0
        foreach b [list $place/mesh/boco \
                        $place/mesh/boco_info] {
            if [file exists $b] {
                set nt [file mtime $b]
                if {$nt > $tt} {
                    set boco $b
                    set tt $nt
                }
            }
        }
    }
    if {$boco != ""} {
        uplevel 1 set bocofile \"$boco\"
    } else {
	uplevel 1 set bocofile \"$place/mesh/boco\"
    }
    if {$topo != ""} {
        uplevel 1 set topofile \"$topo\"
    } else {
        uplevel 1 set topofile \"mulcad/$confname/topology/topo_mulcad_out\"
    }
}

