# Library functions for the scripting library (scrib)
#
# File: scrib/lib/output.tcl
#
# $Revision: 408419 $ on $Date: 2005-06-17 13:23:32 -0400 (Fri, 17 Jun 2005) $
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#--------------------------------------------------------------------
#
# --> if the name of the first proc already exists in interp, return
#
if {"" != [info commands scrib_lib_output_nastran]} { return }
#
# --> procs in this file depend on the following scrib/lib/ files--source them
#     I guess we could do this by proc, too, not sure waht's best
#
if {"" != [info commands scrib_lib_setup_source_files]} {
    set required_files "utils"
    scrib_lib_setup_source_files $required_files
}

## write the unstructured domain file <em>mesh</em> with
## boundary conditions <em>boco</em> to nastran file <em>output</em>

proc scrib_lib_output_nastran {mesh boco output {nasform 0}} {
    global env

    set param_file [scrib_lib_output_nastran_params_local ""]
    set cmd {[file join $env(ICEM_ACN) icemcfd output-interfaces nasn]}
    set args [list -dom $mesh -vol 1 -shell 1 -bar 1 -thickness 0 -b $boco -param $param_file ]
## Check for Long or Short Format  
    if $nasform {
	lappend args -small_field
    }  
    return [eval ic_exec $cmd $args $output]
}

## write the unstructured domain file <em>mesh</em> with
## boundary conditions <em>boco</em> to nastran file <em>output</em>

proc scrib_lib_output_starcd {mesh boco output} {
    global env
    
    set args "-dom $mesh -b $boco $output"
    set res [ic_run_application_exec . {} \
		 [file join $env(ICEM_ACN) icemcfd output-interfaces starcd] $args]
    return $res
}

## for the family <em>familyname</em>, set a thickness of <em>thickness</em>
## on the nastran group id <em>naspid</em>. if a boundary conditions
## file <em>boco</em> is specified, then it will be loaded for the setting,
## otherwise, an new boco file will be created. if the argurment <em>overwrt</em>
## is specified, then a new boco file will be created and written over <em>boco</em>.

proc scrib_lib_output_thickness_set_nastran {familyname naspid thickness \
	{boco ""} {overwrt 0}}  {
    #
    set solver_name Nastran
    #
    #
    if {($boco != "") && [file exists $boco] && (0==$overwrt)} {
	ic_boco_load $boco
	set this_sol [ic_boco_solver]
	if {$solver_name != $this_sol} {
	    mess "trying to add $solver_name to $this_sol boco!\n"
	}
    } else {
	ic_empty_boco
	ic_boco_solver $solver_name
    }
    
    scrib_lib_utils_putdebug "nastran shell pid is $naspid\n"
    
    global proc_scrib_data
    if [info exists proc_scrib_data(matid)] {
	set matno $proc_scrib_data(matid)
    } else {
	set matno 1
    }
    
    if ![ic_uns_check_family $familyname] {
	mess "did not find family $familyname in mesh while setting thickness...\n" red
	#return
    }
    if ![ic_geo_check_family $familyname] {
	mess "could not find family $familyname in geo while setting thickness...creating\n" red
	ic_geo_new_family $familyname
    }
    
    # anything with ISHE should also get:
    #      "FAM_54325" "ELEMENT_PROP_NAME" "Surfaces/FAM_54325"
    #      "FAM_54325" "PART_NAME" "FAM_54325"
    # guessing at black magic... --jka
    
    set bc [list [list 1 ISHE $naspid $matno $matno $matno 0.0 \
		      $matno $thickness 1.0 0.833333] \
		[list 1 ELEMENT_PROP_NAME "Surfaces/${familyname}"] \
		[list 1 PART_NAME $familyname] ]
    
    set stat [catch {ic_boco_add $familyname $bc} res]
    if $stat { mess "$res\n" red }
    
    if {$boco != ""} { ic_boco_save_atr $boco }
    
    return $stat
}

## needs doc.

proc scrib_lib_output_save_edited_mesh_nastran {} {
    global proc_scrib_data
    
    # --> save changes to the mesh, and re-output nastran
    #     watch out for hard-coded paths! fixme --jka
    
    if [ic_uns_is_loaded] {
	set out_mesh  [file join $proc_scrib_data(wdir) [file tail ${proc_scrib_data(name)}]].uns
	set out_boco   [file join $proc_scrib_data(wdir) [file tail ${proc_scrib_data(name)}]].fbc
	set out_file [file join $proc_scrib_data(export_dir) [file tail ${proc_scrib_data(name)}]].nas
	
	ic_save_unstruct $out_mesh
	
	set res [scrib_lib_output_nastran $out_mesh $out_boco $out_file]	
	if {"" != $res} { mess "$res\n" }
    }
    
    return 0
}

## Needs doc.

proc scrib_lib_output_nastran_params_local {working_directory {force_ovwrt 0}} {
    global env
    
    if {"" == $working_directory} {
	set working_directory [scrib_lib_glue_get_working_directory]
    }
    
    set local_params_file_tail "nastran.par"
    set local_params_file [file join $working_directory $local_params_file_tail]
    
    # we may have already created the local version
    if {(0 == $force_ovwrt) && [file exists $local_params_file]} {
	return $local_params_file
    }
    
    # looks like we need to copy the canonical version to this
    # local working directory
    
    set canonical_params_file [file join $env(ICEM_SCRIB) params nastran.params]
    if ![file exists $canonical_params_file] {
	mess "Error: could not find the canonical Nastran params file: $canonical_params_file\n" red
	return ""
    }
    
    cmd_cp $canonical_params_file $local_params_file
    
    if ![file exists $local_params_file] {
	mess "Error: could not create the local Nastran params file: $local_params_file\n" red
	return ""
    }
    
    return $local_params_file
}






