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

proc cfd_ace_write_input {} {
    global confname file_name cfd_ace_output_path icem_powermesh
    global file_name
    
    set domains [domain_select $file_name(domains_dir) "You may either select a set of structured domains or an unstructured domain." mixed 0]    

    if {$domains == {}} return
    
    get_boco_topo
    set ace_u 0
    set check_ori 0

    set outfile $file_name(transfer_dir)/cfd_ace
    
    if {![dialog .cfd_ace "CFD-ACE" \
	  "Edit the following cfd_ace options. \
If you create a physical file, you must \
also specify a boundary condition file, and all domains \
given in the topology file must have been selected." {
	       {ace_u -opt {{"Structured (CFD-ACE)" 0} \
			    {"Unstructured (ACE-U)" 1}} -title "Output Type" }
	       {single -opt {{Single 1} {Double 0}} -title Precision \
		   -active {!$ace_u} -help "Precision"}
	       {dogrid -opt {{Yes 1} {No 0}} -title "Create grid file" \
		   -active {!$ace_u} -help "Create grid file"}
	       {dophys -opt {{Yes 1} {No 0}} -title "Create physical file" \
		   -active {!$ace_u} -help "Create physical file"}
	       {check_ori -opt {{Yes 1} {No 0}}  -active {$ace_u} \
		   -title "For surface mesh: Verify element orientation?" }
	       {topofile -type path -title "Topo file" \
		   -active {!$ace_u} -help "Topology file"}
	       {bocofile -type path -title "Boco file" \
		   -active {$dophys || $ace_u} \
		   -help "Boundary condition file"}
	       {outfile -type path -title "Output file" -help "Output file"}
	   }]} return

    if {($dophys || $ace_u) && ![file exists $bocofile]} {
	errormsg "Boundary condition file doesn't exist!"
	return
    }
    if {!$ace_u && ![file exists $topofile]} {
	errormsg "File $topofile doesn't exist"
	return
    }

    cmd_rm domain_list
#   set fp [open domain_list w]
#   foreach d $domains { puts $fp $d }
#   close $fp
    set text ""
    foreach d $domains { append text "$d\n" }
    ic_write_file domain_list $text
    
    set command "\"$cfd_ace_output_path\""
    if {$ace_u} {set command "$command -unstr -b \"$bocofile\""}
    if {!$ace_u} {
	set command "$command -t \"$topofile\" -f \"$ftopofile\""
        if {!$single} {set command "$command -d"}
        if {$dogrid} {set command "$command -grid"}
        if {$dophys} {set command "$command -phys -b \"$bocofile\""}
    }
    if {$ace_u} {
	if {!$check_ori} {set command "$command -fix"}
    }
    set command "$command -dom_list domain_list \"$outfile\""
    
    runxcom ICEM-CFD/CFD-ACE-Interface $command "" 1 0
    cmd_rm domain_list
}
