
# Write a fluent/uns file.
#
# Copyright (c) 1995 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California
#

proc write_fluent_uns {fp} {
    global whoami full_nodeset full_group grid_mapping full_unstruct
    global grid_side_names boco_data full_element_set full_element_map
    
    foreach i {
	{fan  fan                14}
	{int  interior           2}
	{per  periodic           12}
	{pjmp porous-jump        14}
	{pff  pressure-far-field 9}
	{pin  pressure-inlet     4}
	{pout pressure-outlet    5}
	{rad  radiator           14}
	{sym  symmetry           7}
	{vin  velocity-inlet     10}
	{vff  velocity-far-field 13}
	{wall wall               3}
	
	{flu  fluid              ?}
	{mfin mass-flow-inlet    ?}
	{por  porous             ?}
	{sol  solid              ?}
    } {
	set fluent_boco([lindex $i 0]) [lindex $i 2]
	set full_name([lindex $i 0]) [lindex $i 1]
    }
    
    set conn [grid_connectivity]
    
    set id "[cmd_whoami]@[cmd_uname_n]"
    puts $fp "(0 \"Written by $id on [cmd_date]\")"
    puts $fp "(1 \"$whoami\")"
    puts $fp "(2 3)"
    puts $fp ""
    
    set nn [format %x [$full_nodeset get -numnodes]]
    puts $fp "(10 (0 1 $nn 1))"
    puts $fp "(10 (1 1 $nn 1 1) ("
    $full_nodeset fluent_write $fp
    puts $fp "))"
    puts $fp ""
    
    set zones ""
    set cells ""
    set faces ""

    set idnum 2
    set fnum 1
    set numfaces 0
    set unknown_count 0
    
    $conn init_face_hash
    
    # First renumber everything.
    set fluid_map [element_map -mesh $full_unstruct]
    set hset $full_element_set(hexa)
    $fluid_map add_list_range $hset 0 [expr {[$hset get -num] - 1}]
    
    set cell_number 0
    foreach dom [array names full_group] {
	set map $full_group($dom)
	if {![lindex $dom 1] && [$map count]} {
	    set cell_number [$map renumber_elements $cell_number]
	    $fluid_map subtract_other_contents $map
	}
    }
    $fluid_map renumber_elements $cell_number
    
    set cell_number 0
    
    # First make up all the face sets for the quad domains.
    
    foreach dom [array names full_group] {
	set obj [lindex $grid_mapping([lindex $dom 0]) 0]
	if {$obj == ""} continue
	set side [lindex $dom 1]
	if !$side continue
	
	set map $full_group($dom)
	if ![$map count] continue
	set sname [lindex $grid_side_names([$obj get -obtype]) $side]
	if {$sname == ""} { error "Unknown side $side of [$obj get -obtype]" }
	set fam [$obj get -fam_$sname]
	
	# ztype depends on the bocos
	set ztype wall
	set stype 3
	if {$fam != ""} {
	    if [info exists boco_data($fam)] {
		foreach b $boco_data($fam) {
		    set n [string trim [lindex $b 0]]
		    if [info exists fluent_boco($n)] {
			set ztype $full_name($n)
			set stype $fluent_boco($n)
		    }
		}
	    }
	}
	
	foreach cellcount {1 2} {
	    set cfaces [$map fluent_write $conn $cellcount]
	    set nf [llength $cfaces]
	    if !$nf continue
	    
	    set n1 [format %x $idnum]; set znum $idnum; incr idnum
	    set n2 [format %x $fnum]
	    set n3 [format %x [expr {$fnum + $nf - 1}]]
	    append faces "(13 ($n1 $n2 $n3 [format %x $stype] 4) (\n"
	    incr fnum $nf
	    foreach ff $cfaces {
		append faces "$ff\n"
		incr numfaces
	    }
	    append faces "))\n"
	    if {$fam == ""} { set fam $ztype-[incr unknown_count] }
	    append zones "(39 ($znum $ztype $fam) ())\n"
	}
    }
    
    # Now make up the face sets for the interiors of the solid objects.
    
    foreach dom [array names full_group] {
	set obj [lindex $grid_mapping([lindex $dom 0]) 0]
	if {$obj == ""} continue
	set side [lindex $dom 1]
	if $side continue
	
	set map $full_group($dom)
	if ![$map count] continue
	set sname [lindex $grid_side_names([$obj get -obtype]) $side]
	if {$sname == ""} { error "Unknown side $side of [$obj get -obtype]" }
	set fam [$obj get -fam_$sname]
	
	# stype = 1 fluid or 17 solid
	# etype = 3 quad or 4 hexa
	# ztype depends on the bocos
	
	set r1 [format %x [expr {$cell_number + 1}]]
	incr cell_number [$map count]
	set r2 [format %x $cell_number]
	
	append cells "(12 ($idnum $r1 $r2 11 4))\n"
	incr idnum
	
	set ztype interior
	
	set stype $fluent_boco(sol)
	if {$fam != ""} {
	    if [info exists boco_data($fam)] {
		foreach b $boco_data($fam) {
		    set n [string trim [lindex $b 0]]
		    if [info exists fluent_boco($n)] {
			set ztype $full_name($n)
		    }
		}
	    }
	}
	
	foreach cellcount {1 2} {
	    set cfaces [$map fluent_write $conn $cellcount]
	    set nf [llength $cfaces]
	    if !$nf continue
	    
	    set n1 [format %x $idnum]; set znum $idnum; incr idnum
	    set n2 [format %x $fnum]
	    set n3 [format %x [expr {$fnum + $nf - 1}]]

	    append faces "(13 ($n1 $n2 $n3 2 4) (\n"
	    incr fnum $nf
	    foreach ff $cfaces {
		append faces "$ff\n"
		incr numfaces
	    }
	    append faces "))\n"
	    if {$fam == ""} { set fam $ztype-[incr unknown_count] }
	    append zones "(39 ($znum $ztype $fam) ())\n"
	}
    }
    
    # Now add the fluid group.
    set r1 [format %x [expr {$cell_number + 1}]]
    incr cell_number [$fluid_map count]
    set r2 [format %x $cell_number]
    
    append cells "(12 ($idnum $r1 $r2 1 4))\n"
    incr idnum
    
    set cfaces [$fluid_map fluent_write $conn 2]
    set nf [llength $cfaces]
    if $nf {
	set n1 [format %x $idnum]; set znum $idnum; incr idnum
	set n2 [format %x $fnum]
	set n3 [format %x [expr {$fnum + $nf - 1}]]
	append faces "(13 ($n1 $n2 $n3 2 4) (\n"
	incr fnum $nf
	foreach ff $cfaces {
	    append faces "$ff\n"
	    incr numfaces
	}
	append faces "))\n"
    }
    
    $conn finish_face_hash
    $fluid_map delete
    
    set li [expr {[$full_element_set(hexa) get -num] + 1}]
    puts $fp "(12 (0 1 [format %x $li] 0))"
    puts $fp $cells
    
    puts $fp "(13 (0 1 [format %x $numfaces] 1))"
    puts $fp $faces
    
    puts $fp $zones
    close $fp
}
