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

proc modify_coarsen {} {
    global config_dir coarse_bin last_line
    global coarse_dev coarse_asp coarse_max types_loaded
    
    foreach t $types_loaded {
	if [string match TETRA* $t] {
	    mess "Warning: if you run the coarsener, your $t elements will\
		    be eliminated, leaving you with only a surface mesh.\n" red
	}
    }
    
    command "inquire_shortest_size"
    set coarse_dev(def) [expr {$last_line / 10}]
    set coarse_asp(def) 0.1
    
    set pf $config_dir(mesh)/coarse_params
    if ![make_coarse_params $pf] return
    
    set fine $config_dir(domains)/__temp_fine
    set coarse $config_dir(domains)/__temp_coarse
    
    mess "Running coarsener ...\n"
    set cmd "$coarse_bin $fine $coarse -p $pf"
    if {$coarse_dev(def) != ""} { append cmd " -t $coarse_dev(def)" }
    if {$coarse_asp(def) != ""} { append cmd " -a $coarse_asp(def)" }
    if {$coarse_max(def) != ""} { append cmd " -m $coarse_max(def)" }
    
    run_mesh_filter $cmd $fine $coarse
}

proc make_coarse_params {pf} {
    global config_dir coarse_bin families 
    global coarse_dev coarse_asp coarse_max botform_geo form_done_flag
    
    form_kill .coarsen
    set d [form_init .coarsen "Coarsen mesh" modify]
    set fr [form_scroller $d.fr 400 {0 0}]
    
    form_label $fr.faml "Family name" {0 0}
    form_label $fr.devl "Max deviation" {0 2}
    form_label $fr.aspl "Aspect ratio" {0 4}
    form_label $fr.maxl "Max size" {0 6}
    
    form_label $fr.famdef Default {2 0}
    form_entry $fr.devdef coarse_dev(def) string {2 2}
    form_entry $fr.aspdef coarse_asp(def) string {2 4}
    form_entry $fr.maxdef coarse_max(def) string {2 6}
    
    set i 5
    foreach f $families {
	form_label $fr.fam$i $f "$i 0"
	form_entry $fr.dev$i coarse_dev($f) string "$i 2"
	form_entry $fr.asp$i coarse_asp($f) string "$i 4"
	form_entry $fr.max$i coarse_max($f) string "$i 6"
	incr i
    }
    
    form_finish $botform_geo
    
    # Hell if I know ...
    $fr configure -height [expr {[winfo reqheight $fr] + 1}]
    
    tkwait variable form_done_flag(.coarsen)
    if !$form_done_flag(.coarsen) { return 0 }
    
    exec /bin/rm -f $pf
    set fp [open $pf w]
    foreach i $families {
	set line "define_family $i"
	if {($coarse_dev($i) != "") && $coarse_dev($i)} {
	    append line " max_deviation $coarse_dev($i)"
	}
	if {($coarse_asp($i) != "") && $coarse_asp($i)} {
	    append line " min_aspect $coarse_asp($i)"
	}
	if {($coarse_max($i) != "") && $coarse_max($i)} {
	    append line " max_size $coarse_max($i)"
	}
	puts $fp $line
    }
    puts $fp return
    close $fp

    return 1
}
