
# This code makes it possible to automatically run mulcad in batch mode
# and write out domains and convert them to unstructured.
#
# Copyright (c) 1998 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
# Modification : 01/00 - do not start mesh2block      / Jerome Lordon

proc mulcad_convert {partname outfile} {
    global file_name env
    
    if {$outfile == ""} {
	set outfile unstruct_domain
    }
    if {$partname == ""} {
	set partname blah
    }
    
    if {![file exists $file_name(domains_dir)/domain.1] ||
	![file exists $file_name(mesh_dir)/tetin_pad] ||
        ![file exists $file_name(mesh_dir)/topo_mulcad_out]} {
	# Need to run padamm again.
	
	if {![file exists $file_name(mesh_dir)/index1] ||
            ![file exists $file_name(mesh_dir)/topo_mulcad_out]} {
	    # Need to run mulcad again.
	    
	    set xxx [file split $file_name(project_dir)]
	    if {([llength $xxx] != 3) || ([lindex $xxx 0] != "mulcad")} {
		error "project dir must be mulcad/foo/bar"
	    }
	    set confname [lindex $xxx 1]
	    set geoname [lindex $xxx 2]
	    
	    # First make the modals file where we turn off padamm execution.
	    set fp [open .mulcad_modals w]
	    puts $fp "ICEM MULCAD - VERSION 3.3.3 - MODALS FILE"
	    puts $fp "*"
	    puts $fp "SYSTEM/ 'PADAMM - GEN. MODALS'"
	    puts $fp "*"
	    puts $fp " 10026 'INTERACTIVE PADAMM' = 'NO'"
	    puts $fp " 10027 'GRID VISUALIZATION' = 'NO'"
	    puts $fp " 10028 'EDIT index FILE' = 'NO'"
	    puts $fp " 10041 'OVERWRITE index FILES' = 'YES'"
	    puts $fp " 10045 'WRITE IPARTA FILE' = 'NO'"
	    puts $fp " 10141 'QUIET EXECUTION' = 'YES'"
	    close $fp
	    
	    # Make a restart file for mulcad
	    set restart_file restart.[pid]
	    set fp [open $restart_file w]
	    puts $fp y
	    puts $fp F.5.13.5.6
	    puts $fp $env(ICEM_ACN)/common/gpl
	    puts $fp F
	    puts $fp F.5.13.7.MULCAD
	    puts $fp $confname
	    puts $fp $geoname
            puts $fp 9.3.5.3.3
            puts $fp F.5.13.2
            puts $fp "\["
            puts $fp "\["
            puts $fp "\["
	    puts $fp 11.2.5.3
	    puts $fp F.4.7.y
	    close $fp
	    
	    # This is a big hack.
	    if ![info exists env(LD_LIBRARY_PATH)] {
		set env(LD_LIBRARY_PATH) $env(ICEM_ACN)/ddn/shared_lib
	    } else {
		append env(LD_LIBRARY_PATH) :$env(ICEM_ACN)/ddn/shared_lib
	    }
	    
	    set cmd "[get_program bin icemddn] db=$file_name(parts_dir) \
		pn=$partname sn=1 tt=none dbms=no lf=yes \
		pi=yes bb=n i=$restart_file"
	    puts $cmd
	    catch {eval exec $cmd} res
	    puts $res
	    exec rm $restart_file
	    
	    if ![file exists $file_name(mesh_dir)/index1] {
		puts "Automatic Mulcad run failed -- please regenerate the\
			grid or index files manually using Mulcad/Padamm."
		exit 1
	    }
	}
	
	set pifile $file_name(transfer_dir)/index
	exec cp $file_name(mesh_dir)/header $pifile
	# exec sed "s~\.\./parts/iparta\..*~\.\./parts/[file tail $partfile]~" < $file_name(mesh_dir)/header > $pifile
	for {set i 1} {[file exists $file_name(mesh_dir)/index$i]} {incr i} {
	    exec cat $file_name(mesh_dir)/index$i >> $pifile
	}
	exec echo end >> $pifile
	
	set here [pwd]
	if [catch {
	    cd $file_name(transfer_dir)
	    puts "cd $file_name(transfer_dir)"
	    set cmd "[get_program icemcfd padamm] i=index"
	    puts $cmd
	    catch {eval exec $cmd} res
	    mess "$res\n"
	} res] { mess "$res\n" red }
	cd $here
    }
    set here [pwd]
    cd $file_name(mesh_dir)
    catch {exec ln -sf tetin_pad tetin1}
    cd $here
    
    if ![file exists $file_name(domains_dir)/domain.1] {
	puts "Automatic Padamm run failed -- please regenerate the\
			grid manually using Mulcad/Padamm."
	exit 1
    }
    
    set fp [open domain_list w]
    for {set i 1} {[file exists $file_name(domains_dir)/domain.$i]} {incr i} {
	puts $fp $file_name(domains_dir)/domain.$i
    }
    close $fp
    set cmd "[get_program icemcfd str_to_unstr] -dom_list domain_list \
	      -t $file_name(mesh_dir)/topo_mulcad_out \
	      -o $file_name(domains_dir)/$outfile"
    puts $cmd
    puts [eval exec $cmd]
    
    cmd_rm domain_list
    return
#   mesh2block is started from mesher_mulcad_convert now (JL)

    set cmd "[get_program icemcfd mesh2block] \
	-m $file_name(mesh_dir)/topo_mulcad_out \
	$file_name(domains_dir)/$outfile \
	$file_name(mesh_dir)/blocking.mulcad"
    puts $cmd
    puts [eval exec $cmd]
}


if ![info exists tool_name] {
    # This means we are running stand-alone ...
    
    proc mess {m args} { puts -nonewline $m }
    proc get_program {dir cmd} {
	global env
	return $env(ICEM_ACN)/$dir/$cmd
    }
    
    set_project_dir [string trimright [lindex $argv 0] /]
    if {$argc == 3} {
	set partname [lindex $argv 1]
	set outfile [lindex $argv 2]
	mulcad_convert $partname $outfile
    } elseif {$argc == 1} {
	mulcad_convert "" ""
    } else {
	puts "Usage: mulcad_convert project_dir partname output_domain"
	exit 1
    }
}

# mulcad_convert mulcad/mulcad/parts_mesh MULCAD mulcad_14x5sce uns_dom
