
# Generic stuff.

# Copyright (c) 1993, 1994 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California

proc auto_save_all {{ask 0}} {
    global full_unstruct grid_type grid_subgrids grid_gtype
    
    if ![file_save $ask] { return 0 }
    auto_save_tetin 0
    
    if [info exists full_unstruct] {
	busy 1
	if {($grid_type == "global") && ($grid_gtype == "unif") && \
		$grid_subgrids} {
	    # We have to do the setpids once for each subgrid mesh.
	    
	    set fixfiles ""
	    
	    set submap [directory]/subgrid_map
	    set submap2 [directory]/subgrid_map.fixed
	    set fp [open $submap r]
	    set fp2 [open $submap2 w]
	    
	    while {![eof $fp]} {
		set line [gets $fp]
		if {$line == ""} continue
		set file [lindex $line 1]
		if {[lindex $line 0] == "main"} {
		    puts $fp2 "main $file.fixed [lrange $line 2 end]"
		} else {
		    puts $fp2 "sub $file.fixed [lindex $line 2].fixed\
			[lrange $line 3 end]"
		}
		lappend fixfiles $file
	    }
	    
	    close $fp
	    close $fp2
	    
	    foreach file $fixfiles {
		set mf1 [directory]/grid_output.matrix
		set mf2 [directory]/$file.matrix
		if [file exists $mf1] {
		    cmd_cp $mf1 $mf2
		} else {
		    cmd_rm $mf2
		
		}
		
		mess "Saving unstructured domain $file.fixed ...\n"
		grid_load_domain [directory]/$file temp 0
		auto_bocos_setpids temp temp
		save_domain [directory]/$file.fixed temp temp
	    }
	    mess " done\n"
	    
	} else {

	    # not used grid_conformalize modify
	    auto_bocos_setpids
	    order_bars_according_to_wires
	    set dom [directory domain]/auto_domain.1
	    mess "Saving unstructured domain $dom ..."
	    save_domain $dom full reduced
	    mess " done\n"
	}
	busy 0
	return 1
    } else {
	mess "Not saving unstructured domain -- none currently loaded.\n"
	return 0
    }
}

proc auto_run_tetra {} {
    global autohex_root env
    
    auto_save_tetin
    
    set env(ICEM_ACN) $autohex_root
    set subpro [directory subproj]
    regsub -all \\\\ $subpro / subpro
    exec $autohex_root\\bin\\cutter.exe \
	-f $env(ICEM_ACN)/lib/guibase/guibase.tcl \
	-tool $env(ICEM_ACN)/lib/tetra/tetra.tcl $subpro
    
    convert_starcd [directory domain]/cut_domain.1 \
	[directory tetin]/boco [directory output]/starcd
}

proc app_after_load {} {
    set famboco [directory tetin]/family_boco
    set boco [directory tetin]/boco
    if [file exists $famboco] {
	famboco_load $famboco
    } elseif [file exists $boco] {
	famboco_load_old $boco
    }
    if {[info commands app2_after_load] != ""} { app2_after_load }
}

# This function takes the place of the one in the icemcfd manager.

proc file_write_input {} {
    global autohex_root solvers_available current_solver
    global confname place icem_powermesh job_name
    
    if ![auto_save_all] {
	mess "Can't write input -- no grid is available\n"
	return
    }
    
    set sdir $autohex_root/icemcfd/output-interfaces
    
    if ![info exists solvers_available] {
	foreach file [glob -nocomplain $sdir/*.tcl] {
	    uplevel \#0 source $file
	}
	foreach fn [info commands *_write_input] {
	    regexp {(.*)_write_input} $fn dummy sn
	    if {[lsearch {grid file} $sn] >= 0} continue
	    lappend solvers_available $sn
	}
    }
    if ![info exists solvers_available] {
	mess "Error: no solver interfaces could be found.\n" red
	return
    }
    set sol [choose_one "Select the solver." [lsort $solvers_available] \
		 $current_solver]
    if {$sol == ""} return
    set current_solver $sol
    
    global ${sol}_output_path
    set ${sol}_output_path $sdir/$sol
    set icem_powermesh 0
    set place [directory subproj]
    regsub -all \\\\ $place / place
    set confname $job_name
    cmd_mkdir [directory transfer]
    
    ${sol}_write_input
}

# ---------------------------------------------------------------------

# These are fake procs to make the solver interfaces work correctly.

proc domain_select {args} {
    set domain [directory domain]
    regsub -all \\\\ $domain / domain
    return $domain/auto_domain.1
}

proc get_boco_topo {} {
    # Don't set the topo file since this won't work anyway with
    # those codes.
    set tetin [directory tetin]
    regsub -all \\\\ $tetin / tetin
    uplevel 1 set bocofile \"$tetin/boco\"
}

proc errormsg {m} { mess "$m\n" red }

proc runxcom {title cmd catfile hitreturn back} {
    mess "\n$title -------------------\n"
    if [catch "exec $cmd" ret] {
	mess "$ret\n" red
    } else {
	mess "$ret\n" blue
    }
    mess "Solver input file written.\n"
}
