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

proc cgns_write_input {} {
    global confname file_name cgns_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]
    set basename [file rootname [file tail [lindex $domains 0]]]
    if [string match "*domain" $basename] {
        set basename [file rootname $basename]
        if {$basename == "domain"} {set basename output}
        set unstr_mesh 0
        set use [tk_dialog .domselectchoose "Structured domain selection" "Choose all domains or select domains?" "" -1 All Select]
        switch $use {
            -1 {
                if [winfo exists .domselectchoose] { top_destroy .domselectchoose }
                return
            }
            1 {
                set domains [domain_select_some_struct "load for CGNS"]
            }
        }
        
    } else {
        set unstr_mesh 1
    }

    if {$domains == "{}"} return
    
    set prefix [file rootname [file tail $file_name(settings)]]
    if {$prefix == ""} {set prefix $basename}
    set outfile $file_name(transfer_dir)/$prefix

    get_boco_topo

    set elist_flag 0
    set auto_bc_flag 0
    set output_version 0
    set create_2D_1D_flag 0
    set scale 1.0
    set per_info [ic_geo_get_periodic_data]

    set periodic_flag 0
    if {[lsearch $per_info none] ==-1 } {
        set periodic_flag 1
    }
    
    if $unstr_mesh {
        if {![dialog .cgns "CGNS" \
            "Please edit the following CGNS options." {
	        {bocofile -type path -title "Boco file" -help "Boundary condition file"}
	        {outfile -type path -title "Output file" -help "Output file"}
	        {auto_bc_flag -opt {{"Yes" 1} {"No" 0}} -title "Create default BC patches?"}
	        {elist_flag -active {$unstr_mesh} -opt {{"Face elements" 1} {"Nodes" 0}} \
	        -title "Entities to use for BC patch"}
	        {create_2D_1D_flag -active {$unstr_mesh} -opt {{"Yes" 1} {"No" 0}} \
	        -title "Create Bar and Node elements"}
            {scale -type string -title "Scale factor" -help "Enter scale factor"}
            {output_version -opt {{"3.1 ADF" 0} {"3.1 HDF" 1} {"3.0 ADF" 2} {"3.0 HDF" 3} {"2.4" 4} {"2.3" 5} {"2.2" 6} {"2.1" 7}} \
            -title "CGNS file output version"}
        }]} return    
    } else {
        if {![dialog .cgns "CGNS" \
            "Please edit the following CGNS options." {
	        {topofile -active {!$unstr_mesh} -type path -title "Topo file" -help "Topology file"}
	        {bocofile -type path -title "Boco file" -help "Boundary condition file"}
	        {outfile -type path -title "Output file" -help "Output file"}
	        {auto_bc_flag -opt {{"Yes" 1} {"No" 0}} -title "Create default BC patches?"}
	        {elist_flag -active {$unstr_mesh} -opt {{"Face elements" 1} {"Nodes" 0}} \
	        -title "Entities to use for BC patch"}
	        {create_2D_1D_flag -active {$unstr_mesh} -opt {{"Yes" 1} {"No" 0}} \
	        -title "Create Bar and Node elements"}
            {scale -type string -title "Scale factor" -help "Enter scale factor"}
            {output_version -opt {{"3.1 ADF" 0} {"3.1 HDF" 1} {"3.0 ADF" 2} {"3.0 HDF" 3} {"2.4" 4} {"2.3" 5} {"2.2" 6} {"2.1" 7}} \
            -title "CGNS file output version"}
        }]} return
    }
    
    if {!$unstr_mesh && ![file exists $topofile]} {
	    errormsg "Topology file $topofile doesn't exist!"
	    return
    }
    if {![file exists $bocofile]} {
	    errormsg "Boundary condition file doesn't exist!"
	    return
    }

    cmd_rm domain_list
    set text ""
    foreach d $domains { append text "$d\n" }
    ic_write_file domain_list $text

    set command "\"$cgns_output_path\" -b \"$bocofile\" -dom_list domain_list"
    if {$unstr_mesh} {set command "$command -unstr"}
    if {!$unstr_mesh} {set command "$command -t \"$topofile\" -f \"$ftopofile\""}
    if {$unstr_mesh && $elist_flag} {set command "$command -elist"}
    if {$unstr_mesh && $create_2D_1D_flag} {set command "$command -create_2D_1D_elem"}
    if {$auto_bc_flag} {set command "$command -auto_bc"}
    set command "$command -scale $scale"
    if {$output_version ==1 || $output_version==3} {
    	set command "$command -hdf5"
    	set outfile "$outfile.hdf"
    } else {
    	set outfile "$outfile.cgns"
    }    
    if {$periodic_flag} {set command "$command -per \"$per_info\""}
    set command "$command \"$outfile\""
    if {$output_version} {
        set cgvers [lindex {3.1 3.1 3.0 3.0 2.4 2.3 2.2 2.1} $output_version]
        set command "$command -version $cgvers"
    }

    runcom $command ""
    cmd_rm domain_list
}
