
# Stuff for diffing jobs.
#
# Copyright (c) 2001 ICEM CFD Engineering
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#

proc diff_job {jobname} {
    global model_container

    set obj [model_container]
    set ocontainer $model_container
    set model_container $obj
    model_read $jobname/model 1
    set model_container $ocontainer
}

proc diff_contents {cont1 cont2 name1 name2} {
    foreach obj [$cont1 list_contents] {
	set found([$obj getval name]) $obj
    }
    set in_both ""
    foreach obj [$cont2 list_contents] {
	set name [$obj getval name]
	if [info exists found($name)] {
	    lappend in_both $found($name) $obj
	    unset found($name)
	} else {
	    mess "[$obj getval obtype] [$obj getval name] not in $name1\n"
	}
    }
    foreach name [array names found] {
	set obj $found($name)
	mess "[$obj getval obtype] [$obj getval name] not in $name2\n"
    }
    foreach {obj1 obj2} $in_both {
	diff_object $obj1 $obj2 $name1 $name2
    }
}

proc diff_object {obj1 obj2 name1 name2} {
    global object_params
    
    set obtype [$obj1 getval obtype]
    if {$obtype != [$obj2 getval obtype]} {
	mess "$obtype [$obj1 getval name] has been changed to a [$obj2 getval obtype] object in $name2 (no further comparison possible)\n"
	return
    }
    foreach {t v} {Active active "Section list" section_list} {
	diff_object_value $obj1 $obj2 $name1 $name2 $t $v
    }
    foreach p $object_params($obtype) {
	diff_object_value $obj1 $obj2 $name1 $name2 [lindex $p 0] [lindex $p 1]
    }
    foreach shn [$obj1 getval section_list] {
	set sh1 [$obj1 getval ${shn}_shape]
	set sh2 [$obj2 getval ${shn}_shape]
	diff_shapes $sh1 $sh2 $name1 $name2
    }
}

proc diff_object_value {obj1 obj2 name1 name2 title val} {
    if [$obj1 checkval $val] {
	set val1 [$obj1 get_value_with_units $val]
    } else {
	set val1 "not given"
    }
    if [$obj2 checkval $val] {
	set val2 [$obj2 get_value_with_units $val]
    } else {
	set val2 "not given"
    }
    if ![string compare $val1 $val2] return
    
    set t $title
    if {$t == ""} { set t $val }
    mess "[$obj1 getval obtype] [$obj1 getval name] $t: $val1 -> $val2\n"
}

proc diff_shapes {sh1 sh2 name1 name2} {
    set shtype [$sh1 get -shtype]
    if {$shtype != [$sh2 get -shtype]} {
	set obj1 [$sh1 get -object]
	if {[$sh1 get -name] == "body"} {
	    set shn ""
	} else {
	    set shn " [$sh1 get -name]"
	}
	mess "[$obj1 getval obtype] [$obj1 getval name]$shn has been changed from a $shtype shape to a [$sh2 get -shtype] shape in $name2 (no further comparison possible)\n"
	return
    }
    foreach p [$sh1 list_values] {
	diff_shape_value $sh1 $sh2 $name1 $name2 [lindex $p 0] [lindex $p 0]
    }
}

proc diff_shape_value {sh1 sh2 name1 name2 title val} {
    set val1 [$sh1 get_value_with_units $val]
    set val2 [$sh2 get_value_with_units $val]
    if ![string compare $val1 $val2] return
    
    set obj1 [$sh1 get -object]
    if {[$sh1 get -name] == "body"} {
	set shn ""
    } else {
	set shn " [$sh1 get -name]"
    }
    
    set t $title
    if {$t == ""} { set t $val }
    mess "[$obj1 getval obtype] [$obj1 getval name]$shn $t: $val1 -> $val2\n"
}
