proc rmi_CreateLogo { w } {
global rmi
 
  set font -*-courier-bold-r-normal--14*
  label $w.1 -image $rmi(Logo)
  label $w.2 -text "$rmi(Product)"
  pack append $w $w.1 { top padx 15 } $w.2 { top pady 5} 
  
  define_strings $w.2 "" {
    Rapid Meshing methodology for
    internal combustion engines
  } "" ""
}

proc rmi_IconifyGui {on} {
global rmi tdv_default
  
  if $on {
    if { $rmi(waitWindow) != "" } {
      destroy $rmi(waitWindow)
      set rmi(waitWindow) ""
      wm deiconify .v
      update
    }
  } else {
    $tdv_default configure -active 1
    $tdv_default redraw
    wm iconify .v
    update
    set rmi(waitWindow) [ application_wait ]
    wm deiconify $rmi(mainWindow)
    update
  }
}

proc rmi_CreateMessageMask {} {
global rmi
    
  if [winfo exists .rmimess] { destroy .rmimess }
  top_window .rmimess 1 "$rmi(Product) messages" "" .v 0
  top_geometry .rmimess +200-0
    
  scrollbar .rmimess.yscroll -relief sunken -command ".rmimess.text yview" -orient v \
	    -background #cdb79e
  text .rmimess.text -yscroll ".rmimess.yscroll set" -width 90 -height 5 -exportselection 1 \
    -wrap word -padx 5 -pady 7 -relief sunken -borderwidth 3 -background white
    
  pack .rmimess.yscroll -side right -fill both
  pack .rmimess.text -side top -expand 1 -fill both
    
  set rmi(MaskMessage) .rmimess.text
  set rmi(messWindow) .rmimess
}

proc rmi_CreateFormFile { w } {
global rmi
  
  label $w.1 -image $rmi(Image)
  pack $w.1 $w.1 -side top -expand 1
  set rmi(MaskFormFile) $w
  lappend rmi(MaskForm) $w
}

proc rmi_CreateFormEngineGeometry { w } {
global rmi
   
  set sw [form_scroller $w.stuff 550 "1 0"]
  form_frame $sw.0 flat 3 {0 0}
  form_label $sw.0.1 "Project name:" {0 0}
  form_label $sw.0.2 "" {0 1}; $sw.0.2 configure -textvariable rmi(configuration) -fg blue

  form_frame $sw.1 flat 3 {1 0}
  form_label $sw.1.1 "Geometry filename:    " {0 0}
  form_entry $sw.1.1e rmi(tetin_fn) default {0 1}; $sw.1.1e configure -width 20
  set rmi(CheckOut,TetinSelected) "$sw.1.1e ==1"
  set rmi(TetinFnButton) $sw.1.1e
  set mess "Select the geometry (tetin) file to load."
  form_button $sw.1.1b "Browse..." "rmi_File \"$mess\" tetin_fn $sw.1.1e" {0 2} ""
  $sw.1.1b configure -image $rmi(Browse) -padx 2 -pady 2

  form_frame $sw.2 flat 3 {2 0}
  form_label $sw.2.1 "Scale factor to convert to mm" {0 0}; $sw.2.1 configure -width 28
  form_entry $sw.2.1e rmi(scale_factor) float {0 1}

  form_frame $sw.24 flat 3 {3 0}
  form_label $sw.24.1 "Engine RPM" {0 0}; $sw.24.1 configure -width 28
  form_entry $sw.24.1e rmi(engine_RPM) float {0 1}
  form_button $sw.24.1b "Adjust time step" "rmi_CalculateTimestep time_step" {0 2} ""
  set rmi(CheckOut,engine_RPM) "$sw.24.1e >0.0"

  form_frame $sw.25 flat 3 {4 0}
  form_label $sw.25.1 "Bore diameter in mm" {0 0}; $sw.25.1 configure -width 28
  form_entry $sw.25.1e rmi(bore_diameter) float {0 1}
  set rmi(CheckOut,bore_diameter) "$sw.25.1e >0.0"
  
  form_frame $sw.26 flat 3 {5 0}
  form_label $sw.26.1 "Stroke length in mm " {0 0}; $sw.26.1 configure -width 28 
  form_entry $sw.26.1e rmi(stroke_length) float {0 1}
  set rmi(CheckOut,stroke_length) "$sw.26.1e >0.0"
  
  form_frame $sw.3 flat 3 {6 0}
  form_label $sw.3.1 "Connecting rod length in mm " {0 0}; $sw.3.1 configure -width 28
  form_entry $sw.3.1e rmi(connecting_rod_length) float {0 1}
  set rmi(CheckOut,connecting_rod_length) "$sw.3.1e >0.0"
  
  form_frame $sw.4 flat 3 {7 0}
  form_label $sw.4.1 "Piston Distance to TDC in mm" {0 0}; $sw.4.1 configure -width 28
  form_entry $sw.4.1e rmi(piston_TDC_position) float {0 1}
  set rmi(CheckOut,piston_TDC_position) "$sw.4.1e <0.0"

  form_frame $sw.45 flat 3 {8 0}
  form_label $sw.45.1 "" {0 0}; $sw.45.1 configure -width 30
  form_label $sw.45.2 "Intake valves" {0 1}; $sw.45.2 configure -fg blue -width 14
  form_label $sw.45.3 "" {0 2}; $sw.45.3 configure -width 12
  form_label $sw.45.4 "Exhaust valves" {0 3}; $sw.45.4 configure -fg blue -width 14
  
  form_frame $sw.5 ridge 3 {9 0 -pady 3}
  form_frame $sw.5.1 flat 3 "0 0 -pady 1"
  form_frame $sw.5.2 flat 3 "0 1 -pady 1"
  form_frame $sw.5.3 flat 3 "0 2 -pady 1"
  
  set row 0
      
  form_button $sw.5.1.6 "Valve motion/angle" "rmi_CreateWinValveMotionAngle" "$row 0 -pady 1" ""
  set rmi(ValveMotionButton) $sw.5.1.6
  $rmi(ValveMotionButton) configure -width 23
  form_label $sw.5.2.fill "" "0 1 -pady 1" 
  form_label $sw.5.3.fill "" "0 2 -pady 1"
  incr row

  if !$rmi(chadice) {
    form_label $sw.5.1.1a "Current valve" "$row 0 -pady 2"
    form_menubutton $sw.5.2.1a $rmi(current_intake_valve) $sw.5.2.1a.m "$row 0 -pady 2"
    $sw.5.2.1a configure -relief raised -bd 2 -width 5 -indicatoron 1
    menu $sw.5.2.1a.m -tearoff 0
    $sw.5.2.1a.m add command -label " 1 " -command "set rmi(current_intake_valve) 1; \
    $sw.5.2.1a configure -text 1; rmi_SetCurrentLiftValve Intake intake"
    $sw.5.2.1a.m add command -label " 2 " -command "set rmi(current_intake_valve) 2; \
    $sw.5.2.1a configure -text 2; rmi_SetCurrentLiftValve  Intake intake"
    $sw.5.2.1a.m add command -label " 3 " -command "set rmi(current_intake_valve) 3; \
    $sw.5.2.1a configure -text 3; rmi_SetCurrentLiftValve Intake intake"
    set rmi(IntakeCurrentValveMenuButton) $sw.5.2.1a
    set rmi(IntakeCurrentValveButton) $sw.5.2.1a.m
    form_checkbutton $sw.5.2.1b "All valves" rmi(all_intake_valves) "$row 1 -pady 2" "" \
    	"rmi_SetMenuCurrentValve Intake intake"
    set rmi(IntakeAllValveButton) $sw.5.2.1b
    form_menubutton $sw.5.3.1a $rmi(current_exhaust_valve) $sw.5.3.1a.m "$row 0 -pady 2"
    $sw.5.3.1a configure -relief raised -bd 2 -width 5 -indicatoron 1
    menu $sw.5.3.1a.m -tearoff 0
    $sw.5.3.1a.m add command -label " 1 " -command "set rmi(current_exhaust_valve) 1; \
    $sw.5.3.1a configure -text 1; rmi_SetCurrentLiftValve Exhaust exhaust"
    $sw.5.3.1a.m add command -label " 2 " -command "set rmi(current_exhaust_valve) 2; \
    $sw.5.3.1a configure -text 2; rmi_SetCurrentLiftValve Exhaust exhaust"
    set rmi(ExhaustCurrentValveMenuButton) $sw.5.3.1a
    set rmi(ExhaustCurrentValveButton) $sw.5.3.1a.m
    form_checkbutton $sw.5.3.1b "All valves" rmi(all_exhaust_valves) "$row 1 -pady 2" "" \
    	"rmi_SetMenuCurrentValve Exhaust exhaust"
    set rmi(ExhaustAllValveButton) $sw.5.3.1b
    incr row
  }
  form_label $sw.5.1.2 "Valve lift profile file" "$row 0 -pady 2"
  form_entry $sw.5.2.2 rmi(intake_lift_fn) default "$row 0 -pady 2"
  set rmi(IntakeLiftFnButton) $sw.5.2.2
  if !$rmi(chadice) {
    $rmi(IntakeLiftFnButton) configure -highlightthickness 1 -highlightbackground white
  }
  form_entry  $sw.5.3.2 rmi(exhaust_lift_fn) default "$row 0 -pady 2"
  set rmi(ExhaustLiftFnButton) $sw.5.3.2
  if !$rmi(chadice) {
    $rmi(ExhaustLiftFnButton) configure -highlightthickness 1 -highlightbackground white
  }
  form_button $sw.5.2.3 "Browse..." "rmi_File \"Intake valve lift profile\" \
  	intake_lift_fn $sw.5.2.2" "$row 1 -pady 2" ""
  $sw.5.2.3 configure -image $rmi(Browse)
  form_button $sw.5.3.3 "Browse..." "rmi_File \"Exhaust valve lift profile\" \
  	exhaust_lift_fn $sw.5.3.2" "$row 1 -pady 2" ""
  $sw.5.3.3 configure -image $rmi(Browse)
  incr row

  form_label $sw.5.1.4 "Max lift location (DCA)" "$row 0 -pady 2"
  form_entry  $sw.5.2.5 rmi(intake_max_lift) default "$row 0 -pady 2"
  form_button $sw.5.2.6 "Calculate" "rmi_CalculateMaxLift intake" "$row 1" ""
  set rmi(IntakeMaxLiftButton) $sw.5.2.5
  form_entry  $sw.5.3.5 rmi(exhaust_max_lift) default "$row 0 -pady 2"
  form_button $sw.5.3.6 "Calculate" "rmi_CalculateMaxLift exhaust" "$row 1" "" 
  set rmi(ExhaustMaxLiftButton) $sw.5.3.5
  incr row  
  
  form_label $sw.5.1.5 "Min. valve lift (mm)" "$row 0 -pady 2"
  form_entry  $sw.5.2.7 rmi(intake_min_lift) default "$row 0 -pady 2"
  set rmi(CheckOut,intake_min_lift) "$sw.5.2.7 >0.0"   
  if $rmi(chadice) {
    form_button $sw.5.2.8 "Calculate" "rmi_CalculateMinLift intake" "$row 1 -pady 2" ""
  }
  form_entry  $sw.5.3.7 rmi(exhaust_min_lift) default "$row 0 -pady 2"
  set rmi(CheckOut,exhaust_min_lift) "$sw.5.3.7 >0.0"
  if $rmi(chadice) {
    form_button $sw.5.3.8 "Calculate" "rmi_CalculateMinLift exhaust" "$row 1 -pady 2" ""
  }

  if $rmi(chadice) {
    set pady 2
    set padx 8
    form_frame $sw.8 flat 3 "10 0 -pady 3"
    form_button $sw.8.1 "Edit geometry parameters" "rmi_CreateWinEditGeometryChadice" "0 0 -pady $pady" ""
    $sw.8.1 configure -width 25 -height 1
    form_label $sw.8.2 "Simulation start" "0 1 -pady $pady"
    form_entry $sw.8.2e rmi(degrees_simulated_start) float "0 2 -pady $pady"
    form_label $sw.8.3 "Simulation stop" "0 3 -pady $pady"
    form_entry $sw.8.3e rmi(degrees_simulated_stop) float "0 4 -pady $pady"
    form_button $sw.8.4 "Calculate" "rmi_CalculateMotion" "0 5 -pady $pady" ""
    form_button $sw.11 "Compute stations" "rmi_ComputeStationsGui" "11 0 -pady $pady -padx $padx" ""
    $sw.11 configure -width 25 
  } else {
    set pady 3
    set padx 5
    form_button $sw.8 "Edit geometry parameters" "rmi_CreateWinEditGeometry" "10 0 -pady $pady -padx 5" ""
    $sw.8 configure -width 25
  }
  
  if $rmi(chadice) {
    set row 12
  } else {
    set row 11
  }
  
  if $rmi(chadice) {
    set pady 1
    form_button $sw.15 "Compute static geometry" "rmi_ComputeBaseStations 0" "$row 0 -pady $pady -padx $padx" ""
    incr row; set rmi(StaticGeoButton) $sw.15
    form_button $sw.16 "Compute static mesh" "rmi_ComputeBaseStations 1" "$row 0 -pady $pady -padx $padx" ""
    incr row; set rmi(StaticMeshButton) $sw.16
    form_button $sw.17 "Merge static mesh" "rmi_ComputeBaseStations 2" "$row 0 -pady $pady -padx $padx" ""
    incr row; set rmi(StaticMergeButton) $sw.17
    form_button $sw.9 "Compute geometry" "rmi_ComputeGeometryChadiceGui" "$row 0 -pady $pady -padx $padx" ""
    $sw.15 configure -width 25; $sw.16 configure -width 25; $sw.17 configure -width 25
  } else {
    form_button $sw.9 "Compute geometry" "rmi_ComputeGeometryGui" "$row 0 -pady $pady -padx $padx" ""
  }
  $sw.9 configure -width 25
  define_strings $sw.9 "" {
    Output sub_tetin files for mesh creation
  } "" ""
  
  incr row
  form_button $sw.10 "Geometry preview" "rmi_GeometryPreviewGui" "$row 0 -pady $pady -padx $padx" ""
  $sw.10 configure -width 25
  
  if $rmi(chadice) {
    incr row
    form_button $sw.12 "Compute mesh" "rmi_ComputeMeshStationsGui" "$row 0 -pady $pady -padx $padx" ""
    $sw.12 configure -width 25
    define_strings $sw.12 "" {
      Compute mesh for the given geometry
    } "" ""
    incr row
    form_button $sw.14 "Merge mesh" "rmi_MergeMeshChadiceGui" "$row 0 -pady $pady -padx $padx" ""
    $sw.14 configure -width 25; set rmi(MergeMeshButton) $sw.14
    incr row
    form_button $sw.13 "Preview mesh motion" "rmi_MeshPreviewGui" "$row 0 -pady $pady -padx $padx" ""
    $sw.13 configure -width 25
  }
    
  set rmi(GrayOutEngineGeometryIntakeButtons) "$sw.5.2.2 $sw.5.2.3 $sw.5.2.5 $sw.5.2.6 $sw.5.2.7"
  set rmi(GrayOutEngineGeometryExhaustButtons) "$sw.5.3.2 $sw.5.3.3 $sw.5.3.5 $sw.5.3.6 $sw.5.3.7"
  if $rmi(chadice) {
    append rmi(GrayOutEngineGeometryIntakeButtons) " $sw.5.2.8"
    append rmi(GrayOutEngineGeometryExhaustButtons) " $sw.5.3.8"
    rmi_SetStaticButtons
  } else {
    append rmi(GrayOutEngineGeometryIntakeButtons) " $sw.5.2.1a $sw.5.2.1b"
    append rmi(GrayOutEngineGeometryExhaustButtons) " $sw.5.3.1a $sw.5.3.1b"
  }
  set rmi(GrayOutEngineGeometryButtons) "$sw.5.1.2 $sw.5.1.4 $sw.5.1.5 \
    $rmi(GrayOutEngineGeometryIntakeButtons) $rmi(GrayOutEngineGeometryExhaustButtons)"
  if !$rmi(chadice) {
    append rmi(GrayOutEngineGeometryButtons) " $sw.5.1.1a" 
  }
  set rmi(MaskFormEngineGeometry) $w
  lappend rmi(MaskForm) $w
  
  bind $sw.5.2.2 <KeyRelease> { rmi_SetIntakeArray intake_lift_fn %W }
  bind $sw.5.3.2 <KeyRelease> { rmi_SetExhaustArray exhaust_lift_fn %W }
  bind $sw.5.2.5 <KeyRelease> { rmi_SetIntakeArray intake_max_lift %W }
  bind $sw.5.3.5 <KeyRelease> { rmi_SetExhaustArray exhaust_max_lift %W }
}

proc rmi_CreateWinEditGeometry {} {
global rmi
  
  if [winfo exists .rmieditgeometry] { destroy .rmieditgeometry } 
  
  set buttons {
    {" Done " "destroy .rmieditgeometry"}
  }  
  
  set d [ form_init .rmieditgeometry "Edit geometry parameters" "" $buttons ]    
  if { $d != "" } {
    form_frame $d.1 ridge 3 ""
    form_frame $d.1.0 flat 3 ""
    form_button $d.1.0.0 " Intake  " "rmi_EditGeometryGui intake" "" ""
    pack append $d.1.0 $d.1.0.0 { left padx 15 } 
               
    set rmi(MaskFormIntakeGeometry) $d.1.0.0
    set rmi(MaskFormIntakeGeometryNumber) ""
    for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
  
      form_radiobutton $d.1.0.$i "$i" rmi(current_intake_valve) $i "" "" \
    			         "rmi_ResetLayers intake Intake"
      pack append $d.1.0 $d.1.0.$i { left padx 25 }
      lappend rmi(MaskFormIntakeGeometryNumber) $d.1.0.$i
    }
  
    form_button $d.1.0.copy "  Copy  " "rmi_CopyStart geo" "" ""
    pack append $d.1.0 $d.1.0.copy { right padx 10 }
    
    form_frame $d.1.2 flat 3 ""
    form_button $d.1.2.0 "Exhaust" "rmi_EditGeometryGui exhaust" "" ""
    pack append $d.1.2 $d.1.2.0 { left padx 15 } 
  
    set rmi(MaskFormExhaustGeometry) $d.1.2.0
    set rmi(MaskFormExhaustGeometryNumber) ""
    for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
  
      form_radiobutton $d.1.2.$i "$i" rmi(current_exhaust_valve) $i "" "" \
    			         "rmi_ResetLayers exhaust Exhaust"
      pack append $d.1.2 $d.1.2.$i { left padx 25 }
      lappend rmi(MaskFormExhaustGeometryNumber) $d.1.2.$i
    }

    form_frame $d.1.4 flat 3 ""
    form_button $d.1.4.1 "Combustion dome" "rmi_EditGeometryGui comb" "" ""    
    pack append $d.1.4 $d.1.4.1 { left padx 5 } 
  
    form_frame $d.1.5 flat 3 ""
    form_button $d.1.5.1 " Piston " "rmi_EditGeometryGui piston" "" ""
    pack append $d.1.5 $d.1.5.1 { left padx 5 }

    pack append $d.1 \
    $d.1.0 { top pady 2 fillx expand } $d.1.2 { top pady 2 fillx expand } \
    $d.1.4 { top pady 2 fillx expand } $d.1.5 { top pady 2 fillx expand }   
    
    pack append $d $d.1 {top}

    form_finish +200+200 1 0
  }
  
  set rmi(FrameEditGeometry) .rmieditgeometry
  
  foreach item {intake exhaust} {
    set rmi(CopyParameters,$item) "max_height_below_valve_ \
      n_layer_tip_valve_ n_cells_circumfrential_ \
      tetra_ref_size_ use_geometr_set_ n_cells_in_gap_ \
      n_layers_next_tip_ n_layers_valve_seat_"
  }
  foreach item {Intake Exhaust} {
    set rmi(CopyParameters,$item) "NLayerBelow MaxHeightLayerBelow NLayerTip NCellsCircum"
  }
  foreach item {comb piston} {
    set rmi(CopyParameters,$item) "tetra_ref_size_ use_geometr_set_ \
      n_cells_in_gap_"
  }
  
  set num 0
  if { $rmi(intake_motion) == "1" } { set num 1 }
  if { $rmi(exhaust_motion) == "1" } {  set num 2 }
  if { $rmi(intake_motion) == "1" && $rmi(exhaust_motion) == "1" } { set num 3 }
  if {$rmi(TetinSelected) || $rmi(tetin_fn) != ""} {
    rmi_SetMotion $num GrayOut
  }
}

proc rmi_CreateWinEditGeometryChadice {} {
global rmi
  
  if [winfo exists .rmieditgeometry] { destroy .rmieditgeometry } 
  
  set buttons {
    {" Done " "destroy .rmieditgeometry"}
  }  
  
  set d [ form_init .rmieditgeometry "Edit geometry parameters" "" $buttons ]    
  if { $d != "" } {    
    form_frame $d.b ridge 3 "0 0"
    form_frame $d.b.1 flat 3 "0 0"
    form_label $d.b.1.1 "Number of prism layers in the cylinder" "0 0 -padx 5"
    form_entry $d.b.1.1e rmi(number_layers_cyl_BDC) float "0 1 -padx 5"
    form_label $d.b.1.2 "Intake" "1 0" ;$d.b.1.2 configure -fg blue
    form_label $d.b.1.3 "Mesh layers in valve curtain at max lift" "2 0"
    form_entry $d.b.1.3e rmi(intake_valve_nlayers) float "2 1"  
    set rmi(CheckOut,intake_valve_nlayers) "$d.b.1.3e >5"
    form_label $d.b.1.4 "Exhaust" "3 0" ;$d.b.1.4 configure -fg blue
    form_label $d.b.1.5 "Mesh layers in valve curtain at max lift" "4 0"
    form_entry $d.b.1.5e rmi(exhaust_valve_nlayers) float "4 1"
    set rmi(CheckOut,exhaust_valve_nlayers) "$d.b.1.5e >5"
    
    form_frame $d.1 ridge 3 ""
    form_frame $d.1.0 flat 3 ""
    form_button $d.1.0.0 " Intake  " "rmi_EditGeometryGui intake" "" ""
    pack append $d.1.0 $d.1.0.0 { left padx 15 }               
    set rmi(MaskFormIntakeGeometry) $d.1.0.0
    set rmi(MaskFormIntakeGeometryNumber) ""
    for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
  
      form_radiobutton $d.1.0.$i "$i" rmi(current_intake_valve) $i "" "" \
      	"rmi_ResetLayers intake Intake"
      pack append $d.1.0 $d.1.0.$i { left padx 25 }
      lappend rmi(MaskFormIntakeGeometryNumber) $d.1.0.$i
    }
  
    form_button $d.1.0.copy "  Copy  " "rmi_CopyStart geo" "" ""
    pack append $d.1.0 $d.1.0.copy { right padx 10 }
    
    form_frame $d.1.2 flat 3 ""
    form_button $d.1.2.0 "Exhaust" "rmi_EditGeometryGui exhaust" "" ""
    pack append $d.1.2 $d.1.2.0 { left padx 15 } 
  
    set rmi(MaskFormExhaustGeometry) $d.1.2.0
    set rmi(MaskFormExhaustGeometryNumber) ""
    for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
  
      form_radiobutton $d.1.2.$i "$i" rmi(current_exhaust_valve) $i "" "" \
      	"rmi_ResetLayers exhaust Exhaust"
      pack append $d.1.2 $d.1.2.$i { left padx 25 }
      lappend rmi(MaskFormExhaustGeometryNumber) $d.1.2.$i
    }

    form_frame $d.1.4 flat 3 ""
    form_button $d.1.4.1 "Combustion dome" "rmi_EditGeometryGui comb" "" ""    
    pack append $d.1.4 $d.1.4.1 { left padx 15 } 
  
    form_frame $d.1.5 flat 3 ""
    form_button $d.1.5.1 " Piston " "rmi_EditGeometryGui piston" "" ""
    pack append $d.1.5 $d.1.5.1 { left padx 15 }

    pack append $d.b $d.b.1 { left pady 2 } 

    pack append $d.1 \
    $d.1.0 { top pady 2 fillx expand } $d.1.2 { top pady 2 fillx expand } \
    $d.1.4 { top pady 2 fillx expand } $d.1.5 { top pady 2 fillx expand }   
    
    pack append $d $d.b {top expand fillx} $d.1 {top expand fillx}

    form_finish +200+100 1 0
  }
  
  set rmi(FrameEditGeometry) .rmieditgeometry
  
  foreach item {intake exhaust} {
    set rmi(CopyParameters,$item) "max_height_below_valve_ \
      n_layer_tip_valve_ n_cells_circumfrential_ \
      tetra_ref_size_ use_geometr_set_ n_cells_in_gap_ \
      n_smooth_hexa_ n_layers_at_bnd_"
  }
  foreach item {Intake Exhaust} {
    set rmi(CopyParameters,$item) "NLayerBelow MaxHeightLayerBelow NLayerTip NCellsCircum"
  }
  foreach item {comb piston} {
    set rmi(CopyParameters,$item) "tetra_ref_size_ use_geometr_set_ \
      n_cells_in_gap_"
  }
  
  set rmi(GrayOutEngineMeshingIntakeButtons) "$d.b.1.3 $d.b.1.3e"
  set rmi(GrayOutEngineMeshingExhaustButtons) "$d.b.1.5 $d.b.1.5e"
  set rmi(GrayOutEngineMeshingButtons) "$rmi(GrayOutEngineMeshingIntakeButtons) \
  					$rmi(GrayOutEngineMeshingExhaustButtons)"  
  
  set num 0
  if { $rmi(intake_motion) == "1" } { set num 1 }
  if { $rmi(exhaust_motion) == "1" } {  set num 2 }
  if { $rmi(intake_motion) == "1" && $rmi(exhaust_motion) == "1" } { set num 3 }
  rmi_SetMotion $num GrayOut 
}

proc rmi_EditGeometryGui { what } {
global rmi col

  if [rmi_CopyActive $what] { return }
  
  set edit_win .rmieditgeometry$what
  catch { destroy $edit_win }
  
  if { $what == "intake" } {
    set which Intake; set label "$what valve"
    set label_diag "Run diagnostics on port mesh"
  } elseif { $what == "exhaust" } {
    set which Exhaust; set label "$what valve"
    set label_diag "Run diagnostics on port mesh"
  } elseif { $what == "comb" } {
    set which Comb; set label "combustion dome"
    set label_diag "Run diagnostics on mesh"
  } elseif { $what == "piston" } {
    set which Piston; set label "$what"
    set label_diag "Run diagnostics on mesh"
  }  
  
  if {$what == "intake" || $what == "exhaust"} {
    set rmi(Frame${which}EditGeometry) .rmieditgeometry$what
    rmi_SetBackup $what 0 geo
    if {$what == "intake"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup intake 1 geo reset"} \
        {"Cancel" "destroy .rmieditgeometryintake; rmi_SetBackup intake 1 geo"}
      }
    } elseif {$what == "exhaust"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup exhaust 1 geo reset"} \
        {"Cancel" "destroy .rmieditgeometryexhaust; rmi_SetBackup exhaust 1 geo"}
      }    
    }
  } else {
    set buttons {
      {accept} {reset} {cancel}
    }
  }
  
  set d [ form_init $edit_win "Edit $label geometry parameters" "" $buttons ]
  if { $d != "" } {
    set row 0
    if { $what == "intake" || $what == "exhaust" } {
        
      form_frame $d.0 flat 1 "$row 0"
      form_label $d.0.1 "Parameters for Hexa Mesher" "0 0"
      $d.0.1 configure -fg blue        
      incr row
        
      form_frame $d.1 flat 1 "$row 0"
      form_button $d.1.1 "" "rmi_ShowValveMeshImage $what 1" "0 0" ""
      $d.1.1 configure -image $rmi(ValveMesh1Small)
      form_button $d.1.2 "" "rmi_ShowValveMeshImage $what 2" "0 1" "" 
      $d.1.2 configure -image $rmi(ValveMesh2Small) 
      incr row

      if $rmi(chadice) {
        form_frame $d.16 flat 3 "$row 0"
        form_label $d.16.1 "# of smoothing steps" "0 0"
        $d.16.1 configure -width 23
        form_entry $d.16.1e rmi(n_smooth_hexa_${what}) float "0 1"
        incr row
      }
    }

    form_frame $d.5 flat 1 "$row 0"
    form_label $d.5.1 "Parameters for Tetra Mesher" "0 0"
    $d.5.1 configure -fg blue
    incr row 
        
    form_frame $d.6 flat 1 "$row 0"
    form_label $d.6.1 "Tetra Reference Size" "0 0"
    form_entry $d.6.1e rmi(tetra_ref_size_${what}) float "0 1"
    set com "rmi_CalculateTetraRefSize $what"
    form_button $d.6.1b "Calculate" "$com" "0 2" ""
    incr row
    
    form_frame $d.7 flat 3 "$row 0"
    set com "rmi_OnOffEdit use_geometr_set_${what} $d.7.1"
    form_checkbutton $d.7.1 "Use existing mesh factors from tetin file" rmi(use_geometr_set_${what}) "0 0" "" "$com"
    incr row
    
    form_frame $d.8 flat 3 "$row 0"
    form_label $d.8.1 "N cells in gap" "0 0"
    $d.8.1 configure -width 20
    form_entry $d.8.1e rmi(n_cells_in_gap_${what}) float "0 1"
    incr row
     
    if $rmi(chadice) {
      form_frame $d.12 flat 1 "$row 0"
      set com "rmi_CreateEditMenuAdvancedTetra $what $which {$label_diag} {$label}"
      form_button $d.12.1b "Advanced Tetra Options" "$com" "0 0" ""
      incr row
    } 
    
    if {$what == "comb"} {
      form_frame $d.9 flat 3 "$row 0"
      form_checkbutton $d.9.1 "Clean geometry" rmi(clean_geometry_${what}) "0 0" "" ""
      incr row     
    }

    if $rmi(chadice) {
      form_frame $d.13 flat 1 "$row 0"
      form_label $d.13.1 "Parameters for Prism Mesher" "0 0"
      $d.13.1 configure -fg blue
      incr row      
      if {$what == "intake" || $what == "exhaust"} {
        form_frame $d.14 flat 1 "$row 0"
        form_label $d.14.1 "# of prism layers at port boundary" "0 0"
        form_entry $d.14.1e rmi(n_layers_at_bnd_${what}) float "0 1"
        incr row
      }
      form_frame $d.15 flat 1 "$row 0"
      set com "rmi_CreateEditMenuAdvancedPrism $what $which {$label_diag} {$label}"
      form_button $d.15.1b "Advanced Prism Options" "$com" "0 0" ""
      incr row
    }
    form_finish +200+200 1 0
  }
  if {$what == "intake" || $what == "exhaust"} {
    bind $d.6.1e <KeyRelease> "rmi_Set${which}Array tetra_ref_size_$what %W"
    bind $d.8.1e <KeyRelease> "rmi_Set${which}Array n_cells_in_gap_$what %W"
    if $rmi(chadice) {
      bind $d.14.1e <KeyRelease> "rmi_Set${which}Array n_layers_at_bnd_$what %W"
      bind $d.16.1e <KeyRelease> "rmi_Set${which}Array n_smooth_hexa_$what %W"  
    }
  }
    
  if { $what == "intake" || $what == "exhaust" } {
    set rmi(MaskForm${which}EditGeometry) "$d"  
    set cur $rmi(current_${what}_valve)
    $rmi(MaskForm${which}EditGeometry) configure -highlightbackground $col($cur) -highlightthickness 1
  }
}

proc rmi_ShowValveMeshImage {what num} {
global rmi
  
  set win .rmivalvemeshimage
  if [winfo exists $win] { destroy $win }
  set buttons {
    {"Done" "rmi_DoneValveMeshImage"}
  }
  set rmi(Image,What) $what
  set rmi(Image,Num) $num
  if { $what == "intake" } {
    set rmi(Image,Which) Intake
  } elseif { $what == "exhaust" } {
    set rmi(Image,Which) Exhaust
  }
  set d [form_init $win "$rmi(Image,Which) parameters for Hexa Mesher" "" $buttons]
  if { $d != "" } { 
    form_frame $d.1 ridge 0 "0 0"
    set c [form_canvas $d.1.c 1000x714 ridge 3 "2 0" 0 0]
    $c create image 500 357 -image $rmi(ValveMesh$num)
    bind $c <ButtonRelease-1> { rmi_GetEdgeXY %W %X %Y }
    form_finish +0+0 1 0
  }
}

proc rmi_ShowValveTipImage {} {
global rmi
  
  set win .rmivalvetipimage
  if [winfo exists $win] { destroy $win }
  set buttons {
    {"Done" "rmi_DoneValveTipImage"}
  }
  set d [form_init $win "$rmi(Image,Which) parameters for Hexa Mesher" "" $buttons]
  if { $d != "" } { 
    form_frame $d.1 ridge 0 "0 0"
    set c [form_canvas $d.1.c 774x496 ridge 3 "2 0" 0 0]
    $c create image 387 248 -image $rmi(ValveTip)
    bind $c <ButtonRelease-1> { rmi_GetEdgeTipXY %W %X %Y }
    form_finish +0+0 1 0
  }
}

proc rmi_GetEdgeXY {win x y} {
global rmi
  
  set xx [winfo x $win]; set yy [winfo y $win]
  set rx [winfo rootx $win]; set ry [winfo rooty $win]
  set cx [expr ($x - $rx) - $xx]; set cy [expr ($y - $ry) - $yy]
###rmi_ShowMessage "cx == $cx\n"
###rmi_ShowMessage "cy == $cy\n"
  if {$rmi(Image,Num) == "1"} {
    if {$cx >= 607 && $cx <= 657 && $cy >= 7 && $cy <= 17} {
      rmi_EdgeValveMeshInput 0 1 "Edge 1" edge1_$rmi(Image,What)
    } elseif {$cx >= 607 && $cx <= 657 && $cy >= 22 && $cy <= 32} {
      rmi_EdgeValveMeshInput 0 2 "Edge 2" edge2_$rmi(Image,What)
    } elseif {$cx >= 607 && $cx <= 657 && $cy >= 37 && $cy <= 47} {
      rmi_EdgeValveMeshInput 0 3 "Edge 3" edge3_$rmi(Image,What)
    } elseif {$cx >= 911 && $cx <= 961 && $cy >= 86 && $cy <= 102} {
      rmi_EdgeValveMeshInput 0 4 "Edge 4" edge4_$rmi(Image,What)
    } elseif {$cx >= 911 && $cx <= 961 && $cy >= 112 && $cy <= 128} {
      rmi_EdgeValveMeshInput 0 5 "Edge 5" edge5_$rmi(Image,What)
    } elseif {$cx >= 738 && $cx <= 820 && $cy >= 589 && $cy <= 602} {
      set cur $rmi(current_${rmi(Image,What)}_valve)
      if [info exists rmi($rmi(Image,What),$cur,tip_edges)] {
        rmi_ShowValveTipImage
      } else {
        rmi_EdgeValveMeshInput 0 6 "Layers on the valve seat" n_layers_valve_seat_$rmi(Image,What)
      }
    } elseif {$cx >= 738 && $cx <= 820 && $cy >= 614 && $cy <= 627} {
      rmi_EdgeValveMeshInput 0 7 "Layers next to valve tip" n_layers_next_tip_$rmi(Image,What)
    } elseif {$cx >= 738 && $cx <= 820 && $cy >= 638 && $cy <= 651} {
      rmi_EdgeValveMeshInput 1 8 "# of layers at valve tip" n_layer_tip_valve_$rmi(Image,What) \
      				${rmi(Image,Which)}NLayerTip
    } elseif {$cx >= 738 && $cx <= 820 && $cy >= 662 && $cy <= 675} {
      rmi_EdgeValveMeshInput 1 9 "# of layers below valve" n_layer_below_valve_$rmi(Image,What) \
      				${rmi(Image,Which)}NLayerBelow \
      				"Max height below valve" max_height_below_valve_$rmi(Image,What) \
      				${rmi(Image,Which)}MaxHeightLayerBelow
    } elseif {$cx >= 357 && $cx <= 438 && $cy >= 588 && $cy <= 600} {
      rmi_EdgeValveMeshInputCheck
    }
  } elseif {$rmi(Image,Num) == "2"} {
    if !$rmi(chadice) {
      if {$cx >= 613 && $cx <= 689 && $cy >= 609 && $cy <= 626} {
        rmi_EdgeValveMeshInputCheck
      } elseif {$cx >= 617 && $cx <= 691 && $cy >= 643 && $cy <= 659} {
        rmi_EdgeValveMeshInput 0 11 "Edge 11" edge11_$rmi(Image,What)
      } elseif {$cx >= 620 && $cx <= 691 && $cy >= 674 && $cy <= 691} {
        rmi_EdgeValveMeshInput 0 7 "Layers next to valve tip" n_layers_next_tip_$rmi(Image,What)
      } elseif {$cx >= 395 && $cx <= 456 && $cy >= 581 && $cy <= 601} {
        rmi_EdgeValveMeshInput 1 12 "# of cells in circumferential direction" \
        			n_cells_circumfrential_$rmi(Image,What) \
      				${rmi(Image,Which)}NCellsCircum
      }
    } else {
      if {$cx >= 395 && $cx <= 454 && $cy >= 543 && $cy <= 560} {
        rmi_EdgeValveMeshInput 1 10 "# of cells in circumferential direction" \
        			n_cells_circumfrential_$rmi(Image,What) \
      				${rmi(Image,Which)}NCellsCircum        
      } elseif {$cx >= 619 && $cx <= 691 && $cy >= 630 && $cy <= 645} {
        rmi_EdgeValveMeshInput 0 7 "Layers next to valve tip" n_layers_next_tip_$rmi(Image,What)
      } 
    }
  }
}

proc rmi_EdgeValveMeshInput {check num label what {which ""} {label2 ""} {what2 ""} {which2 ""}} {
  global rmi

  set win .rmivalvemeshedge
  if [winfo exists $win] { destroy $win }
  set buttons {
    {"Done" "destroy .rmivalvemeshedge"}
  }
  set d [form_init $win "Edge $num" "" $buttons]
  if { $d != "" } { 
    form_frame $d.1 ridge 3 "0 0"
    if $check {
      set com "rmi_OnOffEdit $which $d.1.1"
      form_checkbutton $d.1.1 "$label" rmi($which) "0 0 -ipadx 2 -ipady 2" "" "$com"
      set rmi(${which}Button) $d.1.1
    } else {
      form_label $d.1.1 "$label" "0 0 -ipadx 2 -ipady 2"
    }
    form_entry $d.1.1e rmi($what) float "0 1 -ipadx 2 -ipady 2"
    bind $d.1.1e <KeyRelease>  "rmi_Set${rmi(Image,Which)}Array $what %W"
    if {$num == "9"} {
      set com "rmi_OnOffEdit $which2 $d.1.2"
      form_checkbutton $d.1.2 "$label2" rmi($which2) "1 0 -ipadx 2 -ipady 2" "" "$com"
      form_entry $d.1.2e rmi($what2) float "1 1 -ipadx 2 -ipady 2"
      set rmi(${which2}Button) $d.1.2
      bind $d.1.2e <KeyRelease> "rmi_Set${rmi(Image,Which)}Array $what %W"
    }
    form_finish +250+250 1 0
  }
  if $check {
    rmi_OnOffEdit $which $rmi(${which}Button)
    if {$num == "9"} {
      rmi_OnOffEdit $which2 $rmi(${which2}Button)
    }
  }
  bind $win <Visibility> "raise $win"
}

proc rmi_EdgeValveMeshInputCheck {} {
  global rmi

  set win .rmivalvemeshedge
  set what $rmi(Image,What)
  set which $rmi(Image,Which)
  if [winfo exists $win] { destroy $win }
  set buttons {
    {"Done" "destroy .rmivalvemeshedge"}
  }
  set d [form_init $win "Edge 10" "" $buttons]
  if { $d != "" } { 
    form_frame $d.1 ridge 3 "0 0"
    set com "rmi_OnOffEdit ${which}VBDY $d.1.1 $d.1.2 $d.1.3"
    form_checkbutton $d.1.check "Enable/Disable boundary layer mesh feature" \
        rmi(${which}VBDY) "0 0 -ipadx 2 -ipady 2" "" "$com"
    form_label $d.1.1 "Edge 10" "1 0 -ipadx 2 -ipady 2"
    form_entry $d.1.1e rmi(edge10_$what) float "1 1 -ipadx 2 -ipady 2"
    bind $d.1.1e <KeyRelease>  "rmi_Set${rmi(Image,Which)}Array edge10_$what %W"
    form_label $d.1.2 "Height of boundary layer mesh" "2 0 -ipadx 2 -ipady 2"
    form_entry $d.1.2e rmi(vbdyh_$what) float "2 1 -ipadx 2 -ipady 2"
    bind $d.1.2e <KeyRelease>  "rmi_Set${rmi(Image,Which)}Array vbdyh_$what %W"
    form_label $d.1.3 "Number of cells in boundary layer mesh" "3 0 -ipadx 2 -ipady 2"
    form_entry $d.1.3e rmi(vbdynum_$what) float "3 1 -ipadx 2 -ipady 2"
    bind $d.1.3e <KeyRelease> "rmi_Set${rmi(Image,Which)}Array vbdynum_$what %W"
    form_finish +250+250 1 0
  }
  rmi_OnOffEdit ${which}VBDY $d.1.1 $d.1.2 $d.1.3
  bind $win <Visibility> "raise $win"
}


proc rmi_GetEdgeTipXY {win x y} {
global rmi
  
  set xx [winfo x $win]; set yy [winfo y $win]
  set rx [winfo rootx $win]; set ry [winfo rooty $win]
  set cx [expr ($x - $rx) - $xx]; set cy [expr ($y - $ry) - $yy]
  if {$cx >= 695 && $cx <= 748 && $cy >= 272 && $cy <= 284} {
    rmi_EdgeValveMeshInput 0 6 "Layers on the valve seat" n_layers_valve_seat_$rmi(Image,What)
  } elseif {$cx >= 694 && $cx <= 747 && $cy >= 303 && $cy <= 313} {
    rmi_EdgeValveMeshInput 0 13 "Edge 13" edge13_$rmi(Image,What)
  } elseif {$cx >= 699 && $cx <= 752 && $cy >= 441 && $cy <= 451} {
    rmi_EdgeValveMeshInput 0 14 "Edge 14" edge14_$rmi(Image,What)
  } elseif {$cx >= 699 && $cx <= 752 && $cy >= 469 && $cy <= 480} {
    rmi_EdgeValveMeshInput 0 15 "Edge 15" edge15_$rmi(Image,What)
  }
}

proc rmi_EdgeValveTipInput {check num label what {which ""} {label2 ""} {what2 ""} {which2 ""}} {
  global rmi

  set win .rmivalvemeshedge
  if [winfo exists $win] { destroy $win }
  set buttons {
    {"Done" "destroy .rmivalvemeshedge"}
  }
  set d [form_init $win "Edge $num" "" $buttons]
  if { $d != "" } { 
    form_frame $d.1 ridge 3 "0 0"
    if $check {
      set com "rmi_OnOffEdit $which $d.1.1"
      form_checkbutton $d.1.1 "$label" rmi($which) "0 0 -ipadx 2 -ipady 2" "" "$com"
      set rmi(${which}Button) $d.1.1
    } else {
      form_label $d.1.1 "$label" "0 0 -ipadx 2 -ipady 2"
    }
    form_entry $d.1.1e rmi($what) float "0 1 -ipadx 2 -ipady 2"
    bind $d.1.1e <KeyRelease>  "rmi_Set${rmi(Image,Which)}Array $what %W"
    if {$num == "9"} {
      set com "rmi_OnOffEdit $which2 $d.1.2"
      form_checkbutton $d.1.2 "$label2" rmi($which2) "1 0 -ipadx 2 -ipady 2" "" "$com"
      form_entry $d.1.2e rmi($what2) float "1 1 -ipadx 2 -ipady 2"
      set rmi(${which2}Button) $d.1.2
      bind $d.1.2e <KeyRelease> "rmi_Set${rmi(Image,Which)}Array $what %W"     
    }
    form_finish +250+250 1 0
  }
  if $check {
    rmi_OnOffEdit $which $rmi(${which}Button)
    if {$num == "9"} {
      rmi_OnOffEdit $which2 $rmi(${which2}Button)
    }
  }
  bind $win <Visibility> "raise $win"
}

proc rmi_DoneValveMeshImage {} {
  global rmi
  
  unset rmi(Image,Num)
  unset rmi(Image,What)
  unset rmi(Image,Which)
  if [winfo exists .rmivalvemeshedge] { destroy .rmivalvemeshedge }
  destroy .rmivalvemeshimage
}

proc rmi_DoneValveTipImage {} {
  global rmi

  if [winfo exists .rmivalvetipedge] { destroy .rmivalvetipedge }
  destroy .rmivalvetipimage
}

proc rmi_CreateWinValveMotionAngle {} {
global rmi

  if [winfo exists .rmivalvemotionangle] { destroy  .rmivalvemotionangle}
  set buttons {
    {"Done" "destroy .rmivalvemotionangle"}
  }
  set d [ form_init .rmivalvemotionangle "Valve motion/angle" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0}
    form_label $d.1.1 "Valve motion " {0 0}
    form_button $d.1.2 "No valve motion" "rmi_SetMotion 0 $d.1.2" {0 1} ""
    $d.1.2 configure -image $rmi(NoValveBitmap) -bd 3
    form_button $d.1.3 "Intake valve motion" "rmi_SetMotion 1 $d.1.3" {0 2} ""
    $d.1.3 configure -image $rmi(IntakeValveBitmap) -bd 3
    form_button $d.1.4 "Exhaust valve motion" "rmi_SetMotion 2 $d.1.4" {0 3} ""
    $d.1.4 configure -image $rmi(ExhaustValveBitmap) -bd 3
    form_button $d.1.5 "Two valve motion" "rmi_SetMotion 3 $d.1.5" {0 4} ""
    $d.1.5 configure -image $rmi(TwoValveBitmap) -bd 3
    set rmi(MotionBitmap) "$d.1.2 $d.1.3 $d.1.4 $d.1.5"
    if 0 {
      form_label $d.1.6 "Valve angle " {0 5}
      form_button $d.1.7 "" "rmi_SetAskForValveAngle 0 $d.1.7" {0 6} ""
      $d.1.7 configure -image $rmi(Alpha0Bitmap) -bd 3
      form_button $d.1.8 "" "rmi_SetAskForValveAngle 1 $d.1.8" {0 7} ""
      $d.1.8 configure -image $rmi(Alpha1Bitmap) -bd 3
      set rmi(ValveAngleBitmap) "$d.1.6 $d.1.7 $d.1.8"
    } else {
      set rmi(ValveAngleBitmap) ""
    }          
    form_finish +200+200 1 0
  }
  
  set num 0; set button $d.1.2
  if { $rmi(intake_motion) == "1" } { 
    set num 1; set button $d.1.3
  }
  if { $rmi(exhaust_motion) == "1" } { 
    set num 2; set button $d.1.4
  }
  if { $rmi(intake_motion) == "1" && $rmi(exhaust_motion) == "1" } {
    set num 3; set button $d.1.5
  }

  rmi_SetMotion $num $button
  
  if 0 {
    set num 1; set button $d.1.8
    if { $rmi(ask_valve_angle) == "0" } { 
      set num 0; set button $d.1.7
    }
    rmi_SetAskForValveAngle $num $button
    set rmi(FrameValveEditMotionAngle) .rmivalvemotionangle
  } else {
    set rmi(FrameValveEditMotionAngle) ""
  }
}

proc rmi_CreateFieldProcessors { w } {
global rmi

  if $rmi(hpc) {
    if [ winfo exists $w.5 ] {
      return
    }
    form_label $w.5 "  Number of processors " {0 4}
    form_entry $w.5e rmi(number_processors) float {0 5}
  } else {
    destroy $w.5 $w.5e
  }  
}

proc rmi_CreateFormEngineMeshing { w } {
global rmi
  
  set sw [form_scroller $w.stuff 550 "1 0"]
  form_frame $sw.2 ridge 3 {0 0}
  
  form_frame $sw.2.1 flat 0 {0 0}
  form_label $sw.2.1.1 "Intake valves" {0 0 -padx 3}; $sw.2.1.1 configure -fg blue -bd 5    
  form_frame $sw.2.2 flat 3 {1 0}
  form_label $sw.2.2.1 "Mesh layers in valve curtain at max lift" {0 0}; $sw.2.2.1 configure -width 50
  form_entry $sw.2.2.1e rmi(intake_valve_nlayers) float {0 1}  
  set rmi(CheckOut,intake_valve_nlayers) "$sw.2.2.1e >5"
  form_frame $sw.2.4 flat 0 {3 0}
  form_label $sw.2.4.1 "Exhaust valves" {0 0}; $sw.2.4.1 configure -fg blue -bd 5              
  form_frame $sw.2.5 flat 3 {4 0}
  form_label $sw.2.5.1 "Mesh layers in valve curtain at max lift" {0 0}; $sw.2.5.1 configure -width 50
  form_entry $sw.2.5.1e rmi(exhaust_valve_nlayers) float {0 1}
  set rmi(CheckOut,exhaust_valve_nlayers) "$sw.2.5.1e >5"

  form_frame $sw.4 flat 3 {1 0}
  form_label $sw.4.0 "In Cylinder" {0 0 -padx 3}; $sw.4.0 configure -fg blue
  form_label $sw.4.1 "Mesh layers at TDC    " {1 0}
  form_entry $sw.4.1e rmi(number_layers_cyl_TDC) float {1 1} 
  set rmi(CheckOut,number_layers_cyl_TDC) "$sw.4.1e >0"
  form_label $sw.4.2 "   Mesh layers at BDC    " {1 2}
  form_entry $sw.4.2e rmi(number_layers_cyl_BDC) float {1 3}
  set rmi(CheckOut,number_layers_cyl_BDC) "$sw.4.2e >10"
  form_label $sw.4.3 "  Pref. thickness" {1 4}
  form_entry $sw.4.3e rmi(bdc_layer_thickness) float {1 5}
  set rmi(CheckOut,bdc_layer_thickness) "$sw.4.3e >0.0"
 
  form_label $sw.edit "Edit Mesh Parameters" {6 0}; $sw.edit configure -fg blue -bd 5
  
  form_frame $sw.3 ridge 3 {7 0}

  form_frame $sw.3.0 flat 3 ""
  form_button $sw.3.0.0 " Intake  " "rmi_CreateWinEdit intake" "" ""
  pack append $sw.3.0 $sw.3.0.0 { left padx 15 } 
               
  set rmi(MaskFormIntakeLayers) $sw.3.0
  for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
  
    form_radiobutton $sw.3.0.$i "$i" rmi(current_intake_valve) $i "" "" \
    			       "rmi_ResetLayers intake Intake"
    pack append $sw.3.0 $sw.3.0.$i { left padx 25 }
    lappend rmi(MaskFormIntakeLayersNumber) $sw.3.0.$i
  }
  
  form_button $sw.3.0.copy "  Copy  " "rmi_CopyStart" "" ""
  pack append $sw.3.0 $sw.3.0.copy { right padx 10 }
    
  form_frame $sw.3.2 flat 3 ""
  form_button $sw.3.2.0 "Exhaust" "rmi_CreateWinEdit exhaust" "" ""
  pack append $sw.3.2 $sw.3.2.0 { left padx 15 } 
  
  set rmi(MaskFormExhaustLayers) $sw.3.2
  for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
  
    form_radiobutton $sw.3.2.$i "$i" rmi(current_exhaust_valve) $i "" "" \
    			       "rmi_ResetLayers exhaust Exhaust"
    pack append $sw.3.2 $sw.3.2.$i { left padx 25 }
    lappend rmi(MaskFormExhaustLayersNumber) $sw.3.2.$i
  }

  form_frame $sw.3.4 flat 3 ""
  form_button $sw.3.4.1 "Combustion dome" "rmi_CreateWinEdit comb" "" ""    
  pack append $sw.3.4 $sw.3.4.1 { left padx 5 } 
  
  form_frame $sw.3.5 flat 3 ""
  form_button $sw.3.5.1 " Piston " "rmi_CreateWinEdit piston" "" ""
  pack append $sw.3.5 $sw.3.5.1 { left padx 5 } 
  
  pack append $sw.3 \
    $sw.3.0 { top pady 2 fillx expand } $sw.3.2 { top pady 2 fillx expand } \
    $sw.3.4 { top pady 2 fillx expand } $sw.3.5 { top pady 2 fillx expand }
  
  form_frame $sw.5 flat 0 {13 0}
  form_button $sw.5.1 "Compute mesh" "rmi_ComputeMeshGui" {0 0} ""; $sw.5.1 configure -width 15
  define_strings $sw.5.1 "" {
    Compute mesh for the given geometry
  } "" ""  

  form_frame $sw.9 flat 3 {8 0}
  form_label $sw.9.1 "Simulation start (DCA) " {0 0}
  form_entry $sw.9.1e rmi(degrees_simulated_start) float {0 1}
  form_label $sw.9.2 "  Simulation stop (DCA) " {0 2}
  form_entry $sw.9.2e rmi(degrees_simulated_stop) float {0 3}
  form_button $sw.9.3 "Calculate" "rmi_CalculateMotion" {0 4 -padx 20} ""

  form_frame $sw.ac_flux ridge 3 {9 0}
  form_frame $sw.ac_flux.1 flat 3 {9 0}
  form_label $sw.ac_flux.1.1 "ACFluX Mesh Stations" {0 0}; $sw.ac_flux.1.1 configure -fg blue -bd 4
  form_frame $sw.ac_flux.1.2  flat 3 {10 0}
  form_label $sw.ac_flux.1.2.11 "DCA " {0 0}
  form_entry $sw.ac_flux.1.2.1 rmi(ac_flux_station_angle_dca) float {0 1}
  form_label $sw.ac_flux.1.2.21 "Range " {0 2}
  form_entry $sw.ac_flux.1.2.2 rmi(range_ac_flux_station_angle_dca,1) float {0 3}
  form_entry $sw.ac_flux.1.2.3 rmi(range_ac_flux_station_angle_dca,2) float {0 4}
  form_button $sw.ac_flux.1.2.4 "Create" "rmi_MeshPreview create_station" {0 5 -padx 20} ""
  form_frame $sw.ac_flux.1.21  flat 3 {11 0}
  form_button $sw.ac_flux.1.21.1 "Delete" "rmi_ACFluXDeleteStation" {0 0 -padx 20} ""
  form_button $sw.ac_flux.1.21.2 "Calculate" "rmi_ACFluXFillStationLine" {0 1 -padx 20} ""

  form_frame $sw.ac_flux.1.3  flat 3 {12 0}
  form_label $sw.ac_flux.1.31 "Stations at DCA" {12 0}
  form_entry $sw.ac_flux.1.32 rmi(ac_flux_station_list) default {12 1}; $sw.ac_flux.1.32 configure -width 50

  form_frame $sw.ac_flux.1.4  flat 3 {13 0}
  form_label $sw.ac_flux.1.41 "Increment between stations" {13 0}
  form_entry $sw.ac_flux.1.42 rmi(ac_flux_incr_list) default {13 1}; $sw.ac_flux.1.42 configure -width 50
  form_button $sw.ac_flux.1.5 "ACFluX Mesh preview" "rmi_ACFluXOutput preview_ac_flux" {14 0} ""; $sw.ac_flux.1.5 configure -width 19
  form_button $sw.ac_flux.1.6 "Output mesh to ACFluX" "rmi_ACFluXOutput output_ac_flux" {15 0} ""; $sw.ac_flux.1.6 configure -width 21

  form_frame $sw.7 flat 0 {16 0}
  form_button $sw.7.1 "Mesh preview" "rmi_MeshPreviewGui" {0 0} ""; $sw.7.1 configure -width 15
                
  form_frame $sw.8 flat 0 {17 0}
  form_button $sw.8.1 "Platform for STAR-CD run" "rmi_SelectObjectFileGui" {0 0} ""
  form_label $sw.8.2 "      hpc" {0 1}
  form_radiobutton $sw.8.3 "Yes" rmi(hpc) 1 {0 2} "" "rmi_CreateFieldProcessors $sw.8"
  form_radiobutton $sw.8.4 "No " rmi(hpc) 0 {0 3} "" "rmi_CreateFieldProcessors $sw.8"
  set rmi(HpcButton) $sw.8

  form_frame $sw.6 flat 0 {18 0}
  set m $sw.6.1.m
  form_menubutton_regular $sw.6.1 "Output mesh to STAR-CD" $m {0 0} ""
  $sw.6.1 configure -width 24
  menu $m
  $m add command -label "Transient mesh" -command "rmi_TransientMeshGui starcd"
  $m add command -label "Steady state mesh" -command "rmi_SteadyStateMeshGui starcd"
  define_strings $sw.6.1 "" {
    Output mesh to STAR-CD for the given geometry
  } "" ""
  form_label $sw.6.2 "Timestep in DCA" {0 1}
  $sw.6.2 configure -width 15
  form_entry $sw.6.2e rmi(time_step_dca) float {0 2}
  form_button $sw.6.2b "Adjust time step" "rmi_CalculateTimestep time_step" {0 3} ""
  $sw.6.2b configure -width 20
  
  form_frame $sw.10 flat 0 {19 0}
  set m $sw.10.1.m
  form_menubutton_regular $sw.10.1 "Output mesh to Fluent" $m {0 0} ""
  $sw.10.1 configure -width 24
  menu $m
  $m add command -label "Transient mesh" -command "rmi_TransientMeshGui fluent"
  $m add command -label "Steady state mesh" -command "rmi_SteadyStateMeshGui fluent"
  define_strings $sw.10.1 "" {
    Output mesh to Fluent for the given geometry
  } "" ""
  form_label $sw.10.2 "Timestep" {0 1}
  $sw.10.2 configure -width 15
  form_entry $sw.10.2e rmi(time_step) float {0 2}
  form_button $sw.10.2b "Adjust engine RPM" "rmi_CalculateTimestep engine_RPM" {0 3} ""
  $sw.10.2b configure -width 20
  
  pack $sw.2 -side top -anchor w -padx 3 -pady 1 -expand 1
  pack $sw.4 -side top -anchor w -padx 3 -pady 1 -expand 1
  pack $sw.edit -side top -anchor w -padx 3 -pady 1 -expand 1
  pack $sw.3 -side top -anchor w -padx 3 -pady 1 -expand 1 -fill x
  pack $sw.5 -side top -anchor w -padx 5 -pady 2 -expand 1
  pack $sw.9 -side top -anchor w -padx 3 -pady 2
  pack $sw.ac_flux -side top -anchor w -padx 5 -pady 2 -expand 1
  pack $sw.7 -side top -anchor w -padx 5 -pady 2 -expand 1
  pack $sw.8 -side top -anchor w -padx 5 -pady 2 -expand 1
  pack $sw.6 -side top -anchor w -padx 5 -pady 2 -expand 1
  
  pack $sw.10 -side top -anchor w -padx 5 -pady 2 -expand 1

  foreach item {intake exhaust} {
    set rmi(CopyParameters,$item) "n_smooth_hexa_ n_layers_at_bnd_ diagnostic_ \
      tetra_violate_geom_ tetra_smooth_ratio_ n_tetra_smooth_steps_ \
      n_prism_layers_ rel_prism_height_ prism_ratio_ max_prism_angle_ \
      n_prism_smoothing_steps_ prism_smooth_ratio_ n_prism_smooth_steps_ \
      prismtetra_smooth_ratio_ n_prismtetra_smooth_steps_ \
      duplicate_ single_ single_2_ multiple_ single_multiple_ \
      uncovered_ missing_internal_ triangle_box_ nmanvert_ vol_orient_ \
      surf_orient_ disconnected_vert_ metric_"
  }
  foreach item {Intake Exhaust Comb Piston} {
    set rmi(CopyParameters,$item) "TetraViolateGeom"
  }
  foreach item {comb piston} {
    set rmi(CopyParameters,$item) "diagnostic_ tetra_violate_geom_ \
      tetra_smooth_ratio_ n_tetra_smooth_steps_ n_prism_layers_ \
      rel_prism_height_ prism_ratio_ max_prism_angle_ \
      n_prism_smoothing_steps_ prism_smooth_ratio_ n_prism_smooth_steps_ \
      prismtetra_smooth_ratio_ n_prismtetra_smooth_steps_ \
      duplicate_ single_ single_2_  multiple_ single_multiple_ \
      uncovered_ missing_internal_ triangle_box_ nmanvert_ vol_orient_ \
      surf_orient_ disconnected_vert_ metric_"
  }
  append rmi(CopyParameters,comb) " n_prism_layers_on_curtain_ gt_prism_layers_on_curtain_"

  set rmi(IntakeLayerButtons) "$sw.3.0.0"
  set rmi(ExhaustLayerButtons) "$sw.3.2.0"
  set rmi(GrayOutEngineMeshingIntakeButtons) "$sw.2.2.1 $sw.2.2.1e $sw.3.0.0"
  set rmi(GrayOutEngineMeshingExhaustButtons) "$sw.2.5.1 $sw.2.5.1e $sw.3.2.0"
  set rmi(GrayOutEngineMeshingButtons) "$rmi(GrayOutEngineMeshingIntakeButtons) \
  				        $rmi(GrayOutEngineMeshingExhaustButtons)"
  set rmi(MaskFormEngineMeshing) $w
  lappend rmi(MaskForm) $w
}

proc rmi_CreateFormEngineMeshingChadice { w } {
global rmi

  set rmi(MaskFormIntakeLayersNumber) ""
  set rmi(MaskFormExhaustLayersNumber) ""
  set rmi(IntakeLayerButtons) ""
  set rmi(ExhaustLayerButtons) ""
  set rmi(GrayOutEngineMeshingIntakeButtons) ""
  set rmi(GrayOutEngineMeshingExhaustButtons) ""
  set rmi(GrayOutEngineMeshingButtons) ""
  set rmi(MaskFormEngineMeshing) $w
  lappend rmi(MaskForm) $w
}

proc rmi_ComputeGeometryChadiceGui {} {
global rmi
  
  if [winfo exists .rmicomputegeochadice] { destroy .rmicomputegeochadice }
  set buttons {
    {"Compute" "destroy .rmicomputegeochadice; rmi_ComputeTheStations 0"} {cancel}
  }
  
  if {$rmi(Stations) == ""} {
     rmi_ShowMessage "Do \"Compute stations\" before using \"Compute geometry\".\n"
     return
  }
  set number [ llength $rmi(Stations) ]
  for { set i 1 } { $i <= $number } { incr i } {
    set rmi(Stations,Number,$i) 0
    set list_elem [ lindex $rmi(Stations) [expr $i - 1] ]
    set tmp(station_type_$i) [lindex $list_elem 0]
    set tmp(station_dca_$i) [ lindex $list_elem 1 ]
    set tmp(station_color_$i) [lindex $list_elem 2]
  }
  
  set found_geo 0
  set found_mesh 0
  set n [ rmi_SetMeshParams 1 ]
  if [ic_rmi_check_base_station_exist 1] { set found_geo 1 }
  if [ic_rmi_check_base_station_exist 0] { set found_mesh 1 }
  
  set rmi(Stations,OnOff) 0
  set rmi(Stations,Numbers) ""  
  
  set d [ form_init .rmicomputegeochadice "Compute geometry" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {1 0 -pady 1}
    form_frame $d.1.1 flat 0 {0 0}    
    form_frame $d.1.2 flat 0 {0 1}
    form_frame $d.1.3 flat 0 {0 2}
    
    form_checkbutton $d.1.1.a "Stations" rmi(Stations,OnOff) "0 0 -pady 5" "" "rmi_SetStationsOnOff"
    $d.1.1.a configure -fg blue -width 11
    if {!$found_geo || !$found_mesh} { $d.1.1.a configure -state disabled }
    form_label $d.1.2.a "Degree crank angle" {0 0 -pady 5}
    $d.1.2.a configure -fg blue -width 19
    form_label $d.1.3.a "Type" {0 0 -pady 5}
    $d.1.3.a configure -fg blue -width 20
    set c [form_canvas $d.c 440x300 ridge 3 {2 0} 0 1]
    for { set i 1 } { $i <= $number } { incr i } {
      set frame [frame $c.$i]
      $c create window 0 [expr ($i - 1) * 20] -window $frame -anchor nw
      form_checkbutton $frame.num$i "Station $i" rmi(Stations,Number,$i) "$i 0 -pady 1" "" ""
      set rmi(Stations,Number,$i) 0
      lappend rmi(Stations,Numbers) $i
      if {!$found_geo || !$found_mesh} { $frame.num$i configure -state disabled }
      $frame.num$i configure -relief sunken -width 10
      form_label $frame.dca$i "$tmp(station_dca_$i)" "$i 1 -pady 1 -padx 10"
      $frame.dca$i configure -relief sunken -bg white -width 18
      form_label $frame.type$i "$tmp(station_type_$i)" "$i 2 -pady 1"
      $frame.type$i configure -relief sunken -bg white -fg $tmp(station_color_$i) -width 18
    }
    if {$number < 15} {
      $c configure -height [expr 20 * $number]
    }
    set sh [expr 20 * $number]
    if {$number != "0"} {
      set rw [winfo reqwidth $frame]
    } else {
      set rw 0
    }
    $c configure -scrollregion "0 0 $rw $sh"
    form_finish +200+200 1 0
  }
}

proc rmi_ComputeBaseStations {type} {
global rmi
  
  if {$type == "0"} {
    rmi_ComputeGeometryGui
  } elseif {$type == "1"} {
    set n [ rmi_SetMeshParams 1 ]
    if ![ic_rmi_check_base_station_exist 1] {
      rmi_ShowMessage "ERROR: Static geometry does not exists. Compute\
      	static geometry before.\n" red
      return
    }
    rmi_ComputeMeshGui
  } elseif {$type == "2"} {
    rmi_ShowMessage "Merge static mesh...\n"
    set n [ rmi_SetMeshParams 1 ]  
    set n [ ic_rmi_chad_merge_base_mesh ]
    rmi_ShowMessage "Done.\n"
  }
}

proc rmi_ComputeTheStations {type} {
global rmi

  set n [ rmi_SetMeshParams 1 ]
  if ![ic_rmi_check_base_station_exist 1] {
    rmi_ShowMessage "ERROR: Static geometry does not exists. Compute\
      	static geometry before.\n" red
    return
  }
  if ![ic_rmi_check_base_station_exist 0] {
    rmi_ShowMessage "ERROR: Static mesh does not exists. Compute\
      	static mesh before.\n" red
    return
  }
   
  if {$type == "0"} {
    rmi_ShowMessage "Compute geometry...\n"
  } elseif {$type == "1"} {
    rmi_ShowMessage "Compute mesh...\n"
  }
  
  set sort_by [rmi_SortBy]
  set rmi(Stations,Numbers) [lsort $sort_by $rmi(Stations,Numbers)]
  rmi_IconifyGui 1
  foreach i $rmi(Stations,Numbers) {
    if $rmi(Stations,Number,$i) {
      ic_rmi_compute_stations $type $i
    }
  }
  rmi_IconifyGui 0
  rmi_ShowMessage "Done.\n"
}

proc rmi_SetStationsOnOff {} {
global rmi
  
  foreach i $rmi(Stations,Numbers) {
    set rmi(Stations,Number,$i) $rmi(Stations,OnOff)
  }
}

proc rmi_ComputeStationsGui {} {
global rmi

  set rmi(Stations,w) .rmicomputestations
  if [winfo exists $rmi(Stations,w)] { destroy $rmi(Stations,w) }
  set buttons {
    { "Update" rmi_UpdateStations } { "Done" rmi_DoneStations }
  }
  
  set angle_tolerance 5.0
  if {$rmi(Stations) == ""} {
     rmi_ShowMessage "Compute stations...\n"
     set n [ rmi_SetMeshParams 1 ]
     busy 1
     set rmi(Stations) [ic_rmi_compute_station_list $angle_tolerance]
     set rmi(StationsList) [ic_rmi_get_station_list 0]
     set rmi(StationsInfo) [ic_rmi_get_station_info]
     busy 0
     rmi_ShowMessage "Done.\n"
     set rmi(update_station) 1
  }
  
  set d [ form_init $rmi(Stations,w) "Compute stations" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0 -pady 1}
    form_frame $d.1.1 flat 0 {0 0}    
    form_frame $d.1.2 flat 0 {0 1}
    form_frame $d.1.3 flat 0 {0 2}
    form_frame $d.2 ridge 3 {2 0 -pady 1}
    
    form_label $d.1.1.a "Station #" {0 0 -pady 5}
    $d.1.1.a configure -fg blue -width 8
    form_label $d.1.2.a "Degree crank angle" {0 0 -pady 5}
    $d.1.2.a configure -fg blue -width 18
    form_label $d.1.3.a "Type" {0 0 -pady 5}
    $d.1.3.a configure -fg blue -width 20
    set rmi(Stations,c) [form_canvas $d.c 400x300 ridge 3 {1 0} 0 1]
    set number [rmi_CreateStations]
    form_radiobutton $d.2.1 "Add station DCA" rmi(update_station) 1 {1 0 -pady 3} "" ""
    form_entry $d.2.1e rmi(add_station_dca) float {1 1 -pady 3 -padx 50}
    $d.2.1 configure -width 20
    form_radiobutton $d.2.2 "Delete station number" rmi(update_station) 0 {2 0 -pady 3} "" ""
    form_entry $d.2.2e rmi(delete_station_dca) float {2 1 -pady 3 -padx 50}
    $d.2.2 configure -width 20
    form_radiobutton $d.2.3 "Delete all stations" rmi(update_station) 2 {3 0 -pady 3} "" ""
    $d.2.3 configure -width 20
    form_finish +200+200 1 0
  }

  set w [winfo reqwidth $rmi(Stations,w)]; set h [winfo reqheight $rmi(Stations,w)]
  set rmi(Stations,xmin) $w
  set rmi(Stations,ymin) $h
  set rmi(Stations,xmax) $w
  set rmi(Stations,ymax) [expr ($h - 300) + (20 * $number)] 
  wm minsize $rmi(Stations,w) $rmi(Stations,xmin) $rmi(Stations,ymin)
  wm maxsize $rmi(Stations,w) $rmi(Stations,xmax) $rmi(Stations,ymax)
}

proc rmi_CreateStations {} {
global rmi
    
  if [info exists rmi(Stations,Frames)] {
    foreach item $rmi(Stations,Frames) {
      destroy $item
    }
  }
  set c $rmi(Stations,c)
  set rmi(Stations,Frames) ""
  set number [llength $rmi(Stations)]
  for { set i 1 } { $i <= $number } { incr i } {
    set tmp(station_num_$i) $i
    set list_elem [lindex $rmi(Stations) [expr $i - 1]]
    set tmp(station_type_$i) [lindex $list_elem 0]
    set tmp(station_dca_$i) [lindex $list_elem 1]
    set tmp(station_color_$i) [lindex $list_elem 2]
  }    
  set rmi(Stations,Frames) ""
  for { set i 1 } { $i <= $number } { incr i } {
    set frame [frame $c.$i]
    lappend rmi(Stations,Frames) $frame
    $c create window 0 [expr ($i - 1) * 20] -window $frame -anchor nw
    form_label $frame.num$i "$tmp(station_num_$i)" "$i 0 -pady 1"
    $frame.num$i configure -relief sunken -bg white -width 8
    form_label $frame.dca$i "$tmp(station_dca_$i)" "$i 1 -pady 1 -padx 10"
    $frame.dca$i configure -relief sunken -bg white -width 18
    form_label $frame.type$i "$tmp(station_type_$i)" "$i 2 -pady 1"
    $frame.type$i configure -relief sunken -bg white -fg $tmp(station_color_$i) -width 18
  }
  if {$number < 15} {
    $c configure -height [expr 20 * $number]
  }
  set sh [expr 20 * $number]
  if {$number != "0"} {
    set rw [winfo reqwidth $frame]
  } else {
    set rw 0
  }
  $c configure -scrollregion "0 0 $rw $sh"
  return $number
}

proc rmi_UpdateStations {} {
global rmi

  if {$rmi(update_station) == "0"} {
    if {$rmi(delete_station_dca) == ""} {
      rmi_ShowMessage "ERROR: Missing station number.\n" red
      return
    }
    set delete [lindex $rmi(Stations) [expr $rmi(delete_station_dca) - 1]]
    set delete [lindex $delete 1]
    ic_rmi_adddel_stations 0 $delete
  } elseif {$rmi(update_station) == "1"} {
    if {$rmi(add_station_dca) == ""} {
      rmi_ShowMessage "ERROR: Missing station DCA.\n" red
      return
    }
    ic_rmi_adddel_stations 1 $rmi(add_station_dca)
  } elseif {$rmi(update_station) == "2"} {
    ic_rmi_adddel_stations 0
    set rmi(StationsList) ""
    set rmi(StationsInfo) ""
    set rmi(Stations) ""
  }
  if {$rmi(update_station) != "2"} {
    set rmi(StationsList) [ic_rmi_get_station_list 0]
    set rmi(Stations) [ic_rmi_get_station_list 1]
  }
  set number [rmi_CreateStations]
  if {$rmi(update_station) == "1" || $rmi(update_station) == "2"} {
   if {$rmi(update_station) == "1"} {
     set min 20
   } elseif {$rmi(update_station) == "2"} {
     set min [expr 20 * $number]
   }
   set rmi(Stations,ymin) [expr $rmi(Stations,ymin) - $min]
  }
  set rmi(Stations,ymax) [expr ($rmi(Stations,ymin) - 300) + (20 * $number)] 
  wm minsize $rmi(Stations,w) $rmi(Stations,xmin) $rmi(Stations,ymin)
  wm maxsize $rmi(Stations,w) $rmi(Stations,xmax) $rmi(Stations,ymax)
}

proc rmi_DoneStations {} {
global rmi

  if {$rmi(Stations) != ""} {
    set list $rmi(StationsList)
    set file $rmi(configuration)/transfer/mesh_station
    set f [open $file w]
    for { set i 0 } { $i < [llength $list] } { incr i } {
      set a [lindex [lindex $list $i] 1]
      set b "$rmi(configuration)/station_$a/transfer/chad_$a"
      puts $f "$a $b"
    }
    close $f
    rmi_ShowMessage "File $file created.\n"
  }
  if [winfo exists $rmi(Stations,w)] { destroy $rmi(Stations,w) }
  unset rmi(Stations,w); unset rmi(Stations,c); unset rmi(Stations,Frames)
  unset rmi(Stations,xmin); unset rmi(Stations,ymin)
  unset rmi(Stations,xmax); unset rmi(Stations,ymax)
}

proc rmi_ComputeMeshStationsGui {} {
global rmi

  if [winfo exists .rmicomputemeshchadice] { destroy .rmicomputemeshchadice }
  set buttons {
    {"Compute" "destroy .rmicomputemeshchadice; rmi_ComputeTheStations 1"} {cancel}
  }

  if {$rmi(Stations) == ""} {
     rmi_ShowMessage "Do \"Compute stations\" before using \"Compute mesh\".\n"
     return
  }  
  
  set found 0
  set n [ rmi_SetMeshParams 1 ]
  set number [llength $rmi(Stations)]
  for { set i 1 } { $i <= $number } { incr i } {
    set rmi(Stations,Number,$i) 0
    set list_elem [lindex $rmi(Stations) [expr $i - 1]]
    set tmp(station_type_$i) [lindex $list_elem 0]
    set tmp(station_dca_$i) [lindex $list_elem 1]
    set tmp(station_color_$i) [lindex $list_elem 2]
    set tmp(station_exists_$i) [ic_rmi_chk_station_exist 1 $i]
    if $tmp(station_exists_$i) { set found 1 }
  }

  if !$found {
     rmi_ShowMessage "Do \"Compute geometry\" before using \"Compute mesh\".\n"
     return
  }  
  
  set rmi(Stations,OnOff) 0
  set rmi(Stations,Numbers) ""
  
  set d [ form_init .rmicomputemeshchadice "Compute mesh" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0 -pady 1}
    form_frame $d.1.1 flat 0 {0 0}    
    form_frame $d.1.2 flat 0 {0 1}
    form_frame $d.1.3 flat 0 {0 2}
    
    form_checkbutton $d.1.1.a "Stations" rmi(Stations,OnOff) "0 0 -pady 5" "" "rmi_SetStationsOnOff"
    $d.1.1.a configure -fg blue -width 10
    form_label $d.1.2.a "Degree crank angle" {0 0 -pady 5}
    $d.1.2.a configure -fg blue -width 20
    form_label $d.1.3.a "Type" {0 0 -pady 5}
    $d.1.3.a configure -fg blue -width 20
    set c [form_canvas $d.c 440x300 ridge 3 {1 0} 0 1]
    for { set i 1 } { $i <= $number } { incr i } {
      set frame [frame $c.$i]
      $c create window 0 [expr ($i - 1) * 20] -window $frame -anchor nw
      form_checkbutton $frame.num$i "Station $i" rmi(Stations,Number,$i) "$i 0 -pady 1" "" ""
      set rmi(Stations,Number,$i) 0
      lappend rmi(Stations,Numbers) $i
      $frame.num$i configure -relief sunken -width 10
      form_label $frame.dca$i "$tmp(station_dca_$i)" "$i 1 -pady 1 -padx 10"
      $frame.dca$i configure -relief sunken -bg white -width 18
      form_label $frame.type$i "$tmp(station_type_$i)" "$i 2 -pady 1"
      $frame.type$i configure -relief sunken -bg white -fg $tmp(station_color_$i) -width 18
      if !$tmp(station_exists_$i) {
        $frame.num$i configure -state disabled -fg gray
        $frame.dca$i configure -fg gray
        $frame.type$i configure -fg gray
      }
    }
    if {$number < 15} {
      $c configure -height [expr 20 * $number]
    }
    set sh [expr 20 * $number]
    if {$number != "0"} {
      set rw [expr [winfo reqwidth $frame]]
    } else {
      set rw 0
    }
    $c configure -scrollregion "0 0 $rw $sh"
    form_finish +200+200 1 0
  }
}

proc rmi_MergeMeshChadiceGui {} {
global rmi

  if [winfo exists .rmimergemeshchadice] { destroy .rmimergemeshchadice }  
  if {$rmi(Stations) == ""} {
     rmi_ShowMessage "Do \"Compute stations\" before using \"Merge mesh\".\n"
     return
  }
  
  set found 0
  set n [ rmi_SetMeshParams 1 ]
  set number [llength $rmi(Stations)]
  for { set i 1 } { $i <= $number } { incr i } {
    set rmi(Stations,Number,$i) 0
    set list_elem [lindex $rmi(Stations) [expr $i - 1]]
    set tmp(station_type_$i) [lindex $list_elem 0]
    set tmp(station_dca_$i) [lindex $list_elem 1]
    set tmp(station_color_$i) [lindex $list_elem 2]
    set tmp(station_exists_$i) [ic_rmi_chk_chad_comb_exist $i]
    if $tmp(station_exists_$i) { set found 1 }
  }
  
  if !$found {
     rmi_ShowMessage "Do \"Compute mesh\" before using \"Merge mesh\".\n"
     return
  }  
 
  set buttons {
    {"Compute" "destroy .rmimergemeshchadice; rmi_MergeTheStations"} {cancel}
  }
  
  set rmi(Stations,OnOff) 0
  set rmi(Stations,Numbers) ""
  
  set d [ form_init .rmimergemeshchadice "Merge mesh" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0 -pady 1}
    form_frame $d.1.1 flat 0 {0 0}    
    form_frame $d.1.2 flat 0 {0 1}
    form_frame $d.1.3 flat 0 {0 2}
    
    form_checkbutton $d.1.1.a "Stations" rmi(Stations,OnOff) "0 0 -pady 5" "" "rmi_SetStationsOnOff"
    $d.1.1.a configure -fg blue -width 10
    form_label $d.1.2.a "Degree crank angle" {0 0 -pady 5}
    $d.1.2.a configure -fg blue -width 20
    form_label $d.1.3.a "Type" {0 0 -pady 5}
    $d.1.3.a configure -fg blue -width 20
    set c [form_canvas $d.c 440x300 ridge 3 {1 0} 0 1]
    for { set i 1 } { $i <= $number } { incr i } {
      set frame [frame $c.$i]
      $c create window 0 [expr ($i - 1) * 20] -window $frame -anchor nw
      form_checkbutton $frame.num$i "Station $i" rmi(Stations,Number,$i) "$i 0 -pady 1" "" ""
      set rmi(Stations,Number,$i) 0
      lappend rmi(Stations,Numbers) $i
      $frame.num$i configure -relief sunken -width 10
      form_label $frame.dca$i "$tmp(station_dca_$i)" "$i 1 -pady 1 -padx 10"
      $frame.dca$i configure -relief sunken -bg white -width 18
      form_label $frame.type$i "$tmp(station_type_$i)" "$i 2 -pady 1"
      $frame.type$i configure -relief sunken -bg white -fg $tmp(station_color_$i) -width 18
      if !$tmp(station_exists_$i) {
        $frame.num$i configure -state disabled -fg gray
        $frame.dca$i configure -fg gray
        $frame.type$i configure -fg gray
      }
    }
    if {$number < 15} {
      $c configure -height [expr 20 * $number]
    }
    set sh [expr 20 * $number]
    if {$number != "0"} {
      set rw [expr [winfo reqwidth $frame]]
    } else {
      set rw 0
    }
    $c configure -scrollregion "0 0 $rw $sh"
    form_finish +200+200 1 0
  }
}

proc rmi_MergeTheStations {} {
global rmi

  set n [ rmi_SetMeshParams 1 ]   
  rmi_ShowMessage "Merge mesh...\n"  
  set sort_by [rmi_SortBy]
  set rmi(Stations,Numbers) [lsort $sort_by $rmi(Stations,Numbers)]
  rmi_IconifyGui 1
  foreach i $rmi(Stations,Numbers) {
    if $rmi(Stations,Number,$i) {
      ic_rmi_merge_station $i
    }
  }
  rmi_IconifyGui 0
  rmi_ShowMessage "Done.\n"
}

proc rmi_CreateMenuMask { w } {
global rmi
  
  form_radiobutton $w.1 "File           " rmi(Mask) 1 "" "" "rmi_ChangeMask"
  form_radiobutton $w.2 "Engine Geometry" rmi(Mask) 2 "" "" "rmi_ChangeMask"
  if !$rmi(chadice) {
    form_radiobutton $w.3 "Engine Meshing " rmi(Mask) 3 "" "" "rmi_ChangeMask"
  }
  pack append $w \
              $w.1 { left fillx expand pady 10 } \
              $w.2 { left fillx expand pady 10 }
  if !$rmi(chadice) {
    pack append $w $w.3 { left fillx expand pady 10 }
  }
  
  define_strings $w.1 "" {
    Open or save an engine meshing information
  } "" ""
  define_strings $w.2 "" {
    Geometry information
  } "" ""
  if !$rmi(chadice) {
    define_strings $w.3 "" {
      Meshing parameters
    } "" ""
  }
}

proc rmi_CreateFileMenu { w } {
global rmi
  
  set rmi(FrameFile) $w
  form_frame $w.1 flat 1 ""
  form_label $w.1.1  "Settings:" ""
  form_button $w.1.2 "Open" "rmi_File \"Open settings\" settings_fn" "" ""
  form_button $w.1.3 "Save" "rmi_File \"Save settings\" settings_fn" "" ""
  form_button $w.1.4 "Quit" "rmi_Quit" "" ""  
  set rmi(FrameFileSettings) $w.1
  set rmi(MaskFileSettings) "$w.1.1 $w.1.2 $w.1.3 $w.1.4"
  form_frame $w.2 flat 1 ""
  form_label $w.2.1 "Casename " ""
  form_entry $w.2.1e rmi(casename) default ""
  $w.2.1e configure -width 40
  set rmi(FrameFileCasename) $w.2
  set rmi(MaskFileCasename) "$w.2.1 $w.2.1e"
  pack $rmi(FrameFile) $rmi(FrameFileSettings) $rmi(FrameFileCasename) \
  		       -side top -expand 1 -fill x
  set rmi(MaskFile) "$w.1 $w.2"
  
  define_strings $w.1.2 "" {
    Open existing settings file
  } "" ""
  define_strings $w.1.3 "" {
    Save the settings to a file
  } "" ""
  if !$rmi(chadice) {
    define_strings $w.1.4 "" {
      Quit ic3m
    } "" ""
  } else {
    define_strings $w.1.4 "" {
      Quit chadice
    } "" ""
  }
  define_strings $w.2.1e "" {
    Prostar case name
  } "" ""
}

proc rmi_CreateEngineMeshingMenu { w } {
global rmi
  
  form_button $w.5 "Check settings" "rmi_CheckSettings" "" ""
  form_label $w.6 "                         " "" 
  form_label $w.7 "                         " ""
  set rmi(FrameEngineMeshing) $w
  set rmi(MaskEngineMeshing) "$w.5 $w.6 $w.7"
}

proc rmi_CreateEngineGeometryMenu { w } {
global rmi
  
  form_button $w.8 "Check settings" "rmi_CheckSettings" "" ""
  form_label $w.9 "                         " ""
  form_label $w.10 "                         " ""
  set rmi(FrameEngineGeometry) $w
  set rmi(MaskEngineGeometry) "$w.8 $w.9 $w.10"
}

proc rmi_CreateWinEdit { what } {
global rmi 

  set edit_win .rmiedit$what
  catch { destroy $edit_win }
  
  if { $what == "intake" } {
    set which Intake; set label "$what valve"
  } elseif { $what == "exhaust" } {
    set which Exhaust; set label "$what valve"
  } elseif { $what == "comb" } {
    set which Comb; set label "combustion dome"
  } elseif { $what == "piston" } {
    set which Piston; set label "$what"
  }
  if [rmi_CopyActive $what] { return }
  
  if { $what == "intake" || $what == "exhaust" } {
    if { $rmi(${what}_number_valve) < $rmi(current_${what}_valve) } {
      set rmi(current_${what}_valve) 1
    }
  }
  if {$what == "intake" || $what == "exhaust"} {
    if {$what == "intake"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup intake 1 main reset"} \
        {"Cancel" "destroy .rmieditintake; rmi_SetBackup intake 1 main"}
      }
    } elseif {$what == "exhaust"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup exhaust 1 main reset"} \
        {"Cancel" "destroy .rmieditexhaust; rmi_SetBackup exhaust 1 main"}
      }    
    }
  } else {
    set buttons {
      {accept} {reset} {cancel}
    }
  }
  set d [ form_init $edit_win "Edit $label" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 flat 1 {0 0}   
    rmi_CreateEditMenu $d.1 $what $which $label
    form_finish +200+200 1 0
  }
  set rmi(Frame${which}Edit) $edit_win
}

proc rmi_CreateEditMenu { win what which label } {
global rmi col

  if { $what == "intake" || $what == "exhaust" } {
    rmi_SetBackup $what 0 main
    set label_diag "Run diagnostics on port mesh"
  } elseif { $what == "comb" || $what == "piston" } {
    set label_diag "Run diagnostics on mesh"
  }
  
  set w $win.$what
  form_frame $win.$what ridge 3 {0 0}
  
  form_frame $w.1 flat 3 {0 0}; form_frame $w.2 flat 3 {1 0}
  form_frame $w.3 flat 3 {2 0}; form_frame $w.4 flat 3 {3 0}
  form_frame $w.5 flat 3 {4 0}; form_frame $w.6 flat 3 {5 0}
  form_frame $w.7 flat 3 {6 0}; form_frame $w.8 flat 3 {7 0}
  form_frame $w.9 flat 3 {8 0}; form_frame $w.10 flat 3 {9 0}
  form_frame $w.11 flat 3 {10 0}; form_frame $w.12 flat 3 {11 0}
  
  if { $what == "intake" || $what == "exhaust" } {
    form_label $w.1.1 "Parameters for Hexa Mesher" {0 0}; $w.1.1 configure -fg blue    
    form_label $w.2.1 "# of smoothing steps" {0 0}
    form_entry $w.2.1e rmi(n_smooth_hexa_${what}) float {0 1}

    form_label $w.3.1 "Mesh layers at valve closure" {0 0}
    form_entry $w.3.1e rmi(n_layers_valve_closure_${what}) float {0 1}
    form_entry $w.4.3e rmi(n_layers_at_lift_${what}) float {0 0}
    $w.4.3e configure -width 2
    form_label $w.4.1 " layers at lift of " {0 1}
    form_entry $w.4.2e rmi(value_layers_at_lift_${what}) float {0 2}
    $w.4.2e configure -width 6
    form_label $w.4.4 " mm" {0 3}
    form_checkbutton $w.5.1 "Exclude port meshes at valve closure" \
    	rmi(exclude_port_valve_closure_${what}) "0 0" "" \
    	"rmi_OnOffEdit exclude_port_valve_closure_${what} $w.5.1"
  }  

  form_label $w.7.1 "Parameters for Tetra Mesher" {0 0}; $w.7.1 configure -fg blue 
  form_button $w.9.1b "Advanced Tetra Options" "rmi_CreateEditMenuAdvancedTetra $what $which {$label_diag} {$label}" {0 0} ""  
  
  form_label $w.10.1 "Parameters for Prism Mesher" {0 0}; $w.10.1 configure -fg blue 
  if { $what == "intake" || $what == "exhaust" } {
    form_label $w.11.1 "# of prism layers at port boundary" {0 0}
    form_entry $w.11.1e rmi(n_layers_at_bnd_${what}) float {0 1}
  }
  form_button $w.12.1b "Advanced Prism Options" "rmi_CreateEditMenuAdvancedPrism $what $which {$label_diag} {$label}" {0 0} ""
  
  if {$what == "intake" || $what == "exhaust"} {
    bind $w.2.1e <KeyRelease>  "rmi_Set${which}Array n_smooth_hexa_$what %W"
    bind $w.11.1e <KeyRelease> "rmi_Set${which}Array n_layers_at_bnd_$what %W"
    bind $w.3.1e <KeyRelease> "rmi_Set${which}Array n_layers_valve_closure_$what %W"
    bind $w.4.2e <KeyRelease> "rmi_Set${which}Array value_layers_at_lift_$what %W"
    bind $w.4.3e <KeyRelease> "rmi_Set${which}Array n_layers_at_lift_$what %W"
    set rmi(MaskForm${which}Edit) "$win"
    set cur $rmi(current_${what}_valve)
    $rmi(MaskForm${which}Edit) configure -highlightbackground $col($cur) -highlightthickness 1
  }
}

proc rmi_CreateEditMenuAdvancedTetra {what which label_diag label} {
global rmi col

  if [winfo exists .rmiedit_tetra$what] { destroy .rmiedit_tetra$what }
  if {$what == "intake" || $what == "exhaust"} {
    set rmi(Frame${which}EditTetra) .rmiedit_tetra$what
    rmi_SetBackup $what 0 tetra
    if {$what == "intake"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup intake 1 tetra reset"} \
        {"Cancel" "destroy .rmiedit_tetraintake; rmi_SetBackup intake 1 tetra"}
      }
    } elseif {$what == "exhaust"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup exhaust 1 tetra reset"} \
        {"Cancel" "destroy .rmiedit_tetraexhaust; rmi_SetBackup exhaust 1 tetra"}
      }    
    }
  } else {
    set buttons {
      {accept} {reset} {cancel}
    }
  }

  set d [ form_init .rmiedit_tetra$what "Advanced Tetra Options for $label" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0}   
    form_frame $d.1.1 flat 3 {1 0}    
    form_checkbutton $d.1.1.1 $label_diag rmi(diagnostic_${what}) {0 0} "" "rmi_OnOffEdit diagnostic_${what} $d.1.1.1"
    form_frame $d.1.2 flat 3 {2 0}
    form_button $d.1.2.1 "Diagnostic settings" "rmi_CreateWinDiagnostic $what" {0 0} ""
    form_frame $d.1.3 flat 3 {3 0}
    form_checkbutton $d.1.3.1 "Relaxed Constraint Smoothing below aspect ratio" rmi(${which}TetraViolateGeom) \
                              {0 0} "" "rmi_OnOffEdit ${which}TetraViolateGeom $d.1.3.1"
    form_entry $d.1.3.1e rmi(tetra_violate_geom_${what}) float {0 1}
    set rmi(${which}TetraViolateGeomButton) $d.1.3.1
    if {$what == "comb"} {
      form_frame $d.1.6 flat 3 {6 0}
      form_label $d.1.6.1 "# of prism layers on valve curtain " {0 0}; $d.1.6.1 configure -width 40
      form_entry $d.1.6.1e rmi(n_prism_layers_on_curtain_${what}) float {0 1}
      form_frame $d.1.7 flat 3 {7 0}
      form_label $d.1.7.1 "Max. height of prism layers on valve curtain" {0 0}; $d.1.7.1 configure -width 40
      form_entry $d.1.7.1e rmi(hgt_prism_layers_on_curtain_${what}) float {0 1}
    }
    form_frame $d.1.8 flat 3 {8 0}
    form_label $d.1.8.1 "Parameters for smoothing tetra mesh" {0 0}; $d.1.8.1 configure -fg blue 
    form_frame $d.1.9 flat 3 {9 0}
    form_label $d.1.9.1 "Max. aspect ratio" {0 0}; $d.1.9.1 configure -width 20
    form_entry $d.1.9.1e rmi(tetra_smooth_ratio_${what}) float {0 1}  
    form_frame $d.1.10 flat 3 {10 0}
    form_label $d.1.10.1 "# of smoothing steps" {0 0}; $d.1.10.1 configure -width 20
    form_entry $d.1.10.1e rmi(n_tetra_smooth_steps_${what}) float {0 1}
    form_frame $d.1.11 flat 3 {11 0}
    form_checkbutton $d.1.11.1 "Clean tetra mesh" rmi(clean_tetra_mesh_${what}) {0 0} "" "rmi_OnOffEdit clean_tetra_mesh_${what} $d.1.11.1"
    
    form_finish +200+200 1 0
  }
  if {$what == "intake" || $what == "exhaust"} {
    bind $d.1.3.1e <KeyRelease>  "rmi_Set${which}Array tetra_violate_geom_$what %W"
    bind $d.1.9.1e <KeyRelease>  "rmi_Set${which}Array tetra_smooth_ratio_$what %W"
    bind $d.1.10.1e <KeyRelease> "rmi_Set${which}Array n_tetra_smooth_steps_$what %W"
    set rmi(MaskForm${which}EditTetra) "$d.1"
    set cur $rmi(current_${what}_valve)
    $rmi(MaskForm${which}EditTetra) configure -highlightbackground $col($cur) -highlightthickness 1
  } 
  rmi_OnOffEdit ${which}TetraViolateGeom $rmi(${which}TetraViolateGeomButton)
}

proc rmi_CreateEditMenuAdvancedPrism {what which label_diag label} {
global rmi col
  
  if [winfo exists .rmiedit_prism$what] { destroy .rmiedit_prism$what }
  if { $what == "intake" || $what == "exhaust" } {
    set rmi(Frame${which}EditPrism) .rmiedit_prism$what
    rmi_SetBackup $what 0 prism
    if {$what == "intake"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup intake 1 prism reset"} \
        {"Cancel" "destroy .rmiedit_prismintake; rmi_SetBackup intake 1 prism"}
      }
    } elseif {$what == "exhaust"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup exhaust 1 prism reset"} \
        {"Cancel" "destroy .rmiedit_prismexhaust; rmi_SetBackup exhaust 1 prism"}
      }    
    }
  } else {
    set buttons {
      {accept} {reset} {cancel}
    }
  }

  set d [ form_init .rmiedit_prism$what "Advanced Prism Options for $label" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 ridge 3 {0 0}

    form_frame $d.1.1 flat 3 {0 0}
    form_label $d.1.1.1 "# of prism layers" {0 0}; $d.1.1.1 configure -width 25
    form_entry $d.1.1.1e rmi(n_prism_layers_${what}) float {0 1} 
    form_frame $d.1.2 flat 3 {1 0}
    form_label $d.1.2.1 "Prism Initial height (relative)" {0 0}; $d.1.2.1 configure -width 25
    form_entry $d.1.2.1e rmi(rel_prism_height_${what}) float {0 1} 
    form_frame $d.1.3 flat 3 {2 0}
    form_label $d.1.3.1 "Prism height ratio" {0 0}; $d.1.3.1 configure -width 25
    form_entry $d.1.3.1e rmi(prism_ratio_${what}) float {0 1}
    form_frame $d.1.4 flat 3 {3 0}
    form_label $d.1.4.1 "Max. prism angle" {0 0}; $d.1.4.1 configure -width 25
    form_entry $d.1.4.1e rmi(max_prism_angle_${what}) float {0 1}
    form_frame $d.1.5 flat 3 {4 0}
    form_label $d.1.5.1 "# of smoothing steps" {0 0}; $d.1.5.1 configure -width 25
    form_entry $d.1.5.1e rmi(n_prism_smoothing_steps_${what}) float {0 1}
    form_frame $d.1.6 flat 3 {5 0}
    form_label $d.1.6.1 "Parameters for smoothing prism mesh" {0 0}; $d.1.6.1 configure -fg blue 
    form_frame $d.1.7 flat 3 {6 0}
    form_label $d.1.7.1 "Max. aspect ratio (prism elements)" {0 0}; $d.1.7.1 configure -width 30
    form_entry $d.1.7.1e rmi(prism_smooth_ratio_${what}) float {0 1}
    form_frame $d.1.8 flat 3 {7 0}
    form_label $d.1.8.1 "# of smoothing steps" {0 0}; $d.1.8.1 configure -width 30
    form_entry $d.1.8.1e rmi(n_prism_smooth_steps_${what}) float {0 1}
    form_frame $d.1.9 flat 3 {8 0}
    form_label $d.1.9.1 "Max. aspect ratio (tetra elements)" {0 0}; $d.1.9.1 configure -width 30
    form_entry $d.1.9.1e rmi(prismtetra_smooth_ratio_${what}) float {0 1}
    form_frame $d.1.10 flat 3 {9 0}
    form_label $d.1.10.1 "# of smoothing steps" {0 0}; $d.1.10.1 configure -width 30
    form_entry $d.1.10.1e rmi(n_prismtetra_smooth_steps_${what}) float {0 1}
    
    form_finish +200+200 1 0
  }
  if {$what == "intake" || $what == "exhaust"} {
    bind $d.1.1.1e <KeyRelease>  "rmi_Set${which}Array n_prism_layers_$what %W"
    bind $d.1.2.1e <KeyRelease>  "rmi_Set${which}Array rel_prism_height_$what %W"
    bind $d.1.3.1e <KeyRelease>  "rmi_Set${which}Array prism_ratio_$what %W"
    bind $d.1.4.1e <KeyRelease>  "rmi_Set${which}Array max_prism_angle_$what %W"
    bind $d.1.5.1e <KeyRelease>  "rmi_Set${which}Array n_prism_smoothing_steps_$what %W"
    bind $d.1.7.1e <KeyRelease>  "rmi_Set${which}Array prism_smooth_ratio_$what %W"
    bind $d.1.8.1e <KeyRelease>  "rmi_Set${which}Array n_prism_smooth_steps_$what %W"
    bind $d.1.9.1e <KeyRelease>  "rmi_Set${which}Array prismtetra_smooth_ratio_$what %W"
    bind $d.1.10.1e <KeyRelease> "rmi_Set${which}Array n_prismtetra_smooth_steps_$what %W"
    set rmi(MaskForm${which}EditPrism) "$d.1"
    set cur $rmi(current_${what}_valve)
    $rmi(MaskForm${which}EditPrism) configure -highlightbackground $col($cur) -highlightthickness 1
  } 
}

proc rmi_CreateWinDiagnostic { what } {
global rmi 

  catch { destroy .rmisetdiagnostic$what }
  if { $what == "intake" } {
    set which Intake; set label "$what valve"
  } elseif { $what == "exhaust" } {
    set which Exhaust; set label "$what valve"
  } elseif { $what == "comb" } {
    set which Comb; set label "combustion dome"
  } elseif { $what == "piston" } {
    set which Piston; set label "$what"
  }
  if { $what == "intake" || $what == "exhaust" } {
    rmi_SetBackup $what 0 diagnostic
    if {$what == "intake"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup intake 1 diagnostic reset"} \
        {"Cancel" "destroy .rmisetdiagnosticintake; rmi_SetBackup intake 1 diagnostic"}
      }
    } elseif {$what == "exhaust"} {
      set buttons {
        {accept} {"Reset" "rmi_SetBackup exhaust 1 diagnostic reset"} \
        {"Cancel" "destroy .rmisetdiagnosticexhaust; rmi_SetBackup exhaust 1 diagnostic"}
      }    
    }
  } else {
    set buttons {
      {accept} {reset} {cancel}
    }
  }
  
  set d [ form_init .rmisetdiagnostic$what "Diagnostics $label" "" $buttons ]
  if { $d != "" } {
    form_frame $d.1 flat 3 {0 0}
    set rmi(MaskForm${which}SetDiagnostic) $d.1
    set rmi(Frame${what}DiagnosticWindow) .rmisetdiagnostic$what
    rmi_CreateDiagnosticMenu $d.1 $what $which               
    form_finish +200+200 1 0
  }
}

proc rmi_CreateDiagnosticMenu { w what which } {
global rmi col
  
  set proc rmi_OnOffEdit
  form_frame $w.1 ridge 3 {0 0}
  form_label $w.1.0 "Diagnostic settings" {0 0}
  form_checkbutton $w.1.1 "Duplicate vertices" rmi(duplicate_${what}) {1 0} "" "$proc duplicate_${what} $w.1.1"
  form_checkbutton $w.1.2 "Single vertices" rmi(single_${what}) {2 0} ""  "$proc single_${what} $w.1.2"
  form_checkbutton $w.1.3 "Single 2" rmi(single_2_${what}) {3 0} "" "$proc single_2_${what} $w.1.3"
  form_checkbutton $w.1.4 "Multiple" rmi(multiple_${what}) {4 0} "" "$proc multiple_${what} $w.1.4" 
  form_checkbutton $w.1.5 "Single multiple" rmi(single_multiple_${what}) {5 0} "" "$proc single_multiple_${what} $w.1.5"
  form_checkbutton $w.1.6 "Uncovered faces" rmi(uncovered_${what}) {6 0} "" "$proc uncovered_${what} $w.1.6"
  form_checkbutton $w.1.7 "Missing internal" rmi(missing_internal_${what}) {7 0} "" "$proc missing_internal_${what} $w.1.7"
  form_checkbutton $w.1.8 "Triangle box" rmi(triangle_box_${what}) {8 0} "" "$proc triangle_box_${what} $w.1.8"
  form_checkbutton $w.1.9 "Non manifold vertices" rmi(nmanvert_${what}) {9 0} "" "$proc nmanvert_${what} $w.1.9"
  form_checkbutton $w.1.10 "Volume oriention" rmi(vol_orient_${what}) {10 0} "" "$proc vol_orient_${what} $w.1.10"
  form_checkbutton $w.1.11 "Surface oriention" rmi(surf_orient_${what}) {11 0} "" "$proc surf_orient_${what} $w.1.11"
  form_checkbutton $w.1.12 "Disconnected vertices" rmi(disconnected_vert_${what}) {12 0} "" "$proc disconnected_vert_${what} $w.1.12"
  form_checkbutton $w.1.13 "Aspect ratio" rmi(metric_${what}) {13 0} "" "$proc metric_${what} $w.1.13"

  if { $what == "intake" || $what == "exhaust" } {
    set cur $rmi(current_${what}_valve)
    set rmi(Frame${which}SetDiagnostic) $rmi(MaskForm${which}SetDiagnostic)
    set win $rmi(Frame${which}SetDiagnostic)
    $win configure -highlightbackground $col($cur) -highlightthickness 1
  }
}

proc rmi_ComputeGeometryGui {} {
global rmi

  if [winfo exists .rmicomputegeo] { destroy .rmicomputegeo }
  busy 1  
  set n [ rmi_SetMeshParams 1 ]
  
  if {$rmi(intake_motion) == "1"} {
    set tmp(FoundGeo,compute_port_intake,1) [ic_rmi_chk_filename 1 3 1 $rmi(chadice)]
    set tmp(FoundGeo,compute_valve_intake,1) [ic_rmi_chk_filename 1 1 1 $rmi(chadice)]
    if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
      set tmp(FoundGeo,compute_port_intake,2) [ic_rmi_chk_filename 1 3 2 $rmi(chadice)]
      set tmp(FoundGeo,compute_valve_intake,2) [ic_rmi_chk_filename 1 1 2 $rmi(chadice)]
      if {$rmi(intake_number_valve) == "3"} {
        set tmp(FoundGeo,compute_port_intake,3) [ic_rmi_chk_filename 1 3 3 $rmi(chadice)]
        set tmp(FoundGeo,compute_valve_intake,3) [ic_rmi_chk_filename 1 1 3 $rmi(chadice)]
      }
    } 
  }
  if {$rmi(exhaust_motion) == "1"} {
    set tmp(FoundGeo,compute_port_exhaust,1) [ic_rmi_chk_filename 1 4 1 $rmi(chadice)]
    set tmp(FoundGeo,compute_valve_exhaust,1) [ic_rmi_chk_filename 1 2 1 $rmi(chadice)]
    if {$rmi(exhaust_number_valve) == "2"} {
      set tmp(FoundGeo,compute_port_exhaust,2) [ic_rmi_chk_filename 1 4 2 $rmi(chadice)]
      set tmp(FoundGeo,compute_valve_exhaust,2) [ic_rmi_chk_filename 1 2 2 $rmi(chadice)]
    }
  }
  if !$rmi(chadice) {
    set tmp(FoundGeo,compute_comb) [ic_rmi_chk_filename 1 5 1 $rmi(chadice)]
    set tmp(FoundGeo,compute_piston) [ic_rmi_chk_filename 1 6 1 $rmi(chadice)]
  }
  
  if $rmi(chadice) {
    if {$rmi(intake_motion) == "1"} {
      if [ic_rmi_check_ext_port_geom 1 1] {
        set tmp(FoundGeo,compute_ext_port_intake,1) ""
      }
      if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
        if [ic_rmi_check_ext_port_geom 1 2] {
          set tmp(FoundGeo,compute_ext_port_intake,2) ""
        }
        if {$rmi(intake_number_valve) == "3"} {
          if [ic_rmi_check_ext_port_geom 1 3] {
            set tmp(FoundGeo,compute_ext_port_intake,3) ""
          }
        }
      } 
    }
    if {$rmi(exhaust_motion) == "1"} {
      if [ic_rmi_check_ext_port_geom 2 1] {
        set tmp(FoundGeo,compute_ext_port_exhaust,1) ""
      }
      if {$rmi(exhaust_number_valve) == "2"} {
        if [ic_rmi_check_ext_port_geom 2 2] {
          set tmp(FoundGeo,compute_ext_port_exhaust,2) ""
        }
      }
    }
  }
  
  if $rmi(chadice) {
    set port "base port"
    set valve "base valve"
  } else {
    set port "port"
    set valve "valve"
  }
  
  set buttons {
    {"Compute" "destroy .rmicomputegeo; rmi_ComputeGeometry"}
    {cancel}
  }
  set opt1 "configure -width 22"
  set opt2 "configure -width 8 -relief sunken"
  set d [ form_init .rmicomputegeo "Compute geometry" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.f flat 3 {0 0}
    form_label $d.1.f.m "Geometry" {0 0}
    $d.1.f.m configure -width 34 -fg blue -anchor e
    if {$rmi(intake_motion) == "1"} {
      form_frame $d.1.f1 flat 1 {1 0}
      form_checkbutton $d.1.f1.1 "Intake $port 1" rmi(compute_port_intake,1) {0 0} "" ""
      form_label $d.1.f1.b $tmp(FoundGeo,compute_port_intake,1) {0 1}
      eval $d.1.f1.1 $opt1; eval $d.1.f1.b $opt2
      if $rmi(chadice) {
        if [info exists tmp(FoundGeo,compute_ext_port_intake,1)] {
          form_checkbutton $d.1.f1.2 "Intake extended port 1" rmi(compute_ext_port_intake,1) {1 0} "" ""
          form_label $d.1.f1.b2 $tmp(FoundGeo,compute_ext_port_intake,1) {1 1}
          eval $d.1.f1.2 $opt1; eval $d.1.f1.b2 $opt2
        }
      }
      if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
        form_frame $d.1.f2 flat 1 {2 0}
        form_checkbutton $d.1.f2.1 "Intake $port 2" rmi(compute_port_intake,2) {0 0} "" ""
        form_label $d.1.f2.b $tmp(FoundGeo,compute_port_intake,2) {0 1}
        eval $d.1.f2.1 $opt1; eval $d.1.f2.b $opt2
        if $rmi(chadice) {
          if [info exists tmp(FoundGeo,compute_ext_port_intake,2)] {
            form_checkbutton $d.1.f2.2 "Intake extended port 2" rmi(compute_ext_port_intake,2) {1 0} "" ""
            form_label $d.1.f2.b2 $tmp(FoundGeo,compute_ext_port_intake,2) {1 1}
            eval $d.1.f2.2 $opt1; eval $d.1.f2.b2 $opt2
          }
        }
      }
      if {$rmi(intake_number_valve) == "3"} {
        form_frame $d.1.f3 flat 1 {3 0}
        form_checkbutton $d.1.f3.1 "Intake $port 3" rmi(compute_port_intake,3) {0 0} "" ""
        form_label $d.1.f3.b $tmp(FoundGeo,compute_port_intake,3) {0 1}
        eval $d.1.f3.1 $opt1; eval $d.1.f3.b $opt2
        if $rmi(chadice) {
          if [info exists tmp(FoundGeo,compute_ext_port_intake,3)] {
            form_checkbutton $d.1.f3.2 "Intake extended port 3" rmi(compute_ext_port_intake,3) {1 0} "" ""
            form_label $d.1.f3.b2 $tmp(FoundGeo,compute_ext_port_intake,3) {1 1}
            eval $d.1.f3.2 $opt1; eval $d.1.f3.b2 $opt2
          }
        }
      }
      form_frame $d.1.f4 flat 1 {4 0}
      form_checkbutton $d.1.f4.1 "Intake $valve 1" rmi(compute_valve_intake,1) {0 0} "" ""
      form_label $d.1.f4.b $tmp(FoundGeo,compute_valve_intake,1) {0 1}      
      eval $d.1.f4.1 $opt1; eval $d.1.f4.b $opt2
      if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
        form_frame $d.1.f5 flat 1 {5 0}
        form_checkbutton $d.1.f5.1 "Intake $valve 2" rmi(compute_valve_intake,2) {0 0} "" ""
        form_label $d.1.f5.b $tmp(FoundGeo,compute_valve_intake,2) {0 1}      
        eval $d.1.f5.1 $opt1; eval $d.1.f5.b $opt2
      }
      if {$rmi(intake_number_valve) == "3"} {
        form_frame $d.1.f6 flat 1 {6 0}
        form_checkbutton $d.1.f6.1 "Intake $valve 3" rmi(compute_valve_intake,3) {0 0} "" ""
        form_label $d.1.f6.b $tmp(FoundGeo,compute_valve_intake,3) {0 1}     
        eval $d.1.f6.1 $opt1; eval $d.1.f6.b $opt2
      }
    }
    if {$rmi(exhaust_motion) == "1"} {
      form_frame $d.1.f7 flat 1 {7 0}
      form_checkbutton $d.1.f7.1 "Exhaust $port 1" rmi(compute_port_exhaust,1) {0 0} "" ""
      form_label $d.1.f7.b $tmp(FoundGeo,compute_port_exhaust,1) {0 1}     
      eval $d.1.f7.1 $opt1; eval $d.1.f7.b $opt2
      if $rmi(chadice) {
        if [info exists tmp(FoundGeo,compute_ext_port_exhaust,1)] {
          form_checkbutton $d.1.f7.2 "Exhaust extended port 1" rmi(compute_ext_port_exhaust,1) {1 0} "" ""
          form_label $d.1.f7.b2 $tmp(FoundGeo,compute_ext_port_exhaust,1) {1 1}     
          eval $d.1.f7.2 $opt1; eval $d.1.f7.b2 $opt2
        }
      }
      if {$rmi(exhaust_number_valve) == "2"} {
        form_frame $d.1.f8 flat 1 {8 0}
        form_checkbutton $d.1.f8.1 "Exhaust $port 2" rmi(compute_port_exhaust,2) {0 0} "" ""
        form_label $d.1.f8.b $tmp(FoundGeo,compute_port_exhaust,2) {0 1}      
        eval $d.1.f8.1 $opt1; eval $d.1.f8.b $opt2
        if $rmi(chadice) {
          if [info exists tmp(FoundGeo,compute_ext_port_exhaust,2)] {
            form_checkbutton $d.1.f8.2 "Exhaust extended port 2" rmi(compute_ext_port_exhaust,2) {1 0} "" ""
            form_label $d.1.f8.b2 $tmp(FoundGeo,compute_ext_port_exhaust,2) {1 1}      
            eval $d.1.f8.2 $opt1; eval $d.1.f8.b2 $opt2
          }
        }
      }
      form_frame $d.1.f9 flat 1 {9 0}
      form_checkbutton $d.1.f9.1 "Exhaust $valve 1" rmi(compute_valve_exhaust,1) {0 0} "" ""
      form_label $d.1.f9.b $tmp(FoundGeo,compute_valve_exhaust,1) {0 1}     
      eval $d.1.f9.1 $opt1; eval $d.1.f9.b $opt2
      if {$rmi(exhaust_number_valve) == "2"} {
        form_frame $d.1.f10 flat 1 {10 0}
        form_checkbutton $d.1.f10.1 "Exhaust $valve 2" rmi(compute_valve_exhaust,2) {0 0} "" ""
        form_label $d.1.f10.b $tmp(FoundGeo,compute_valve_exhaust,2) {0 1}
        eval $d.1.f10.1 $opt1; eval $d.1.f10.b $opt2
      }
    }
    if !$rmi(chadice) {
      form_frame $d.1.f11 flat 1 {11 0}
      form_checkbutton $d.1.f11.1 "Combustion dome" rmi(compute_comb) {0 0} "" ""
      form_label $d.1.f11.b $tmp(FoundGeo,compute_comb) {0 1}
      eval $d.1.f11.1 $opt1; eval $d.1.f11.b $opt2
      form_frame $d.1.f12 flat 1 {12 0}
      form_checkbutton $d.1.f12.1 "Piston bowl" rmi(compute_piston) {0 0} "" ""
      form_label $d.1.f12.b $tmp(FoundGeo,compute_piston) {0 1}      
      eval $d.1.f12.1 $opt1; eval $d.1.f12.b $opt2
    }
    form_finish +200+200 1 0
  }
  busy 0
}

proc rmi_ComputeMeshGui {} {
global rmi

  if [winfo exists .rmicomputemesh] { destroy .rmicomputemesh }
  busy 1
  set n [ rmi_SetMeshParams 1 ]
  if {$rmi(intake_motion) == "1"} {
    set tmp(FoundGeo,no_mesh_tet_intake,1) [ic_rmi_chk_filename 1 3 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_tet_intake,1) [ic_rmi_chk_filename 2 3 1 $rmi(chadice)]
    set tmp(FoundGeo,no_mesh_hex_intake,1) [ic_rmi_chk_filename 1 1 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_hex_intake,1) [ic_rmi_chk_filename 2 1 1 $rmi(chadice)]
    if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
      set tmp(FoundGeo,no_mesh_tet_intake,2) [ic_rmi_chk_filename 1 3 2 $rmi(chadice)]
      set tmp(FoundMesh,no_mesh_tet_intake,2) [ic_rmi_chk_filename 2 3 2 $rmi(chadice)]
      set tmp(FoundGeo,no_mesh_hex_intake,2)  [ic_rmi_chk_filename 1 1 2 $rmi(chadice)]
      set tmp(FoundMesh,no_mesh_hex_intake,2) [ic_rmi_chk_filename 2 1 2 $rmi(chadice)]
      if {$rmi(intake_number_valve) == "3"} {
        set tmp(FoundGeo,no_mesh_tet_intake,3) [ic_rmi_chk_filename 1 3 3 $rmi(chadice)]
        set tmp(FoundMesh,no_mesh_tet_intake,3) [ic_rmi_chk_filename 2 3 3 $rmi(chadice)]
        set tmp(FoundGeo,no_mesh_hex_intake,3)  [ic_rmi_chk_filename 1 1 3 $rmi(chadice)]
        set tmp(FoundMesh,no_mesh_hex_intake,3) [ic_rmi_chk_filename 2 1 3 $rmi(chadice)]
      }
    }
  }
  if {$rmi(exhaust_motion) == "1"} {
    set tmp(FoundGeo,no_mesh_tet_exhaust,1) [ic_rmi_chk_filename 1 4 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_tet_exhaust,1) [ic_rmi_chk_filename 2 4 1 $rmi(chadice)]
    set tmp(FoundGeo,no_mesh_hex_exhaust,1) [ic_rmi_chk_filename 1 2 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_hex_exhaust,1) [ic_rmi_chk_filename 2 2 1 $rmi(chadice)]
    if {$rmi(exhaust_number_valve) == "2"} {
      set tmp(FoundGeo,no_mesh_tet_exhaust,2) [ic_rmi_chk_filename 1 4 2 $rmi(chadice)]
      set tmp(FoundMesh,no_mesh_tet_exhaust,2) [ic_rmi_chk_filename 2 4 2 $rmi(chadice)]
      set tmp(FoundGeo,no_mesh_hex_exhaust,2) [ic_rmi_chk_filename 1 2 2 $rmi(chadice)]
      set tmp(FoundMesh,no_mesh_hex_exhaust,2) [ic_rmi_chk_filename 2 2 2 $rmi(chadice)]
    }
  }
  if !$rmi(chadice) {
    set tmp(FoundGeo,no_mesh_comb) [ic_rmi_chk_filename 1 5 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_comb) [ic_rmi_chk_filename 2 5 1 $rmi(chadice)]
    set tmp(FoundGeo,no_mesh_piston) [ic_rmi_chk_filename 1 6 1 $rmi(chadice)]
    set tmp(FoundMesh,no_mesh_piston) [ic_rmi_chk_filename 2 6 1 $rmi(chadice)]
  }
  set buttons {
    {"Compute" "destroy .rmicomputemesh; rmi_ComputeMesh"}
    {cancel}
  }
  set opt1 "configure -onvalue 0 -offvalue 1 -width 18"
  set opt2 "configure -width 8 -relief sunken"
  set d [ form_init .rmicomputemesh "Compute mesh" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.f flat 1 {0 0}
    form_label $d.1.f.m "Mesh    " {0 1}
    form_label $d.1.f.g "Geometry" {0 2}
    $d.1.f.m configure -width 30 -fg blue -anchor e; $d.1.f.g configure -width 8 -fg blue -anchor e
    if {$rmi(intake_motion) == "1"} {
      form_frame $d.1.f1 flat 1 {1 0}
      form_checkbutton $d.1.f1.1 "Intake port 1" rmi(no_mesh_tet_intake,1) {0 0} "" ""
      eval $d.1.f1.1 $opt1
      form_label $d.1.f1.b $tmp(FoundMesh,no_mesh_tet_intake,1) {0 1}
      form_label $d.1.f1.a $tmp(FoundGeo,no_mesh_tet_intake,1) {0 2}
      eval $d.1.f1.a $opt2; eval $d.1.f1.b $opt2
      if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
        form_frame $d.1.f2 flat 1 {2 0}
        form_checkbutton $d.1.f2.1 "Intake port 2" rmi(no_mesh_tet_intake,2) {0 0} "" ""
        eval $d.1.f2.1 $opt1
        form_label $d.1.f2.b $tmp(FoundMesh,no_mesh_tet_intake,2) {0 1}
        form_label $d.1.f2.a $tmp(FoundGeo,no_mesh_tet_intake,2) {0 2}
        eval $d.1.f2.a $opt2; eval $d.1.f2.b $opt2 
      }
      if {$rmi(intake_number_valve) == "3"} {
        form_frame $d.1.f3 flat 1 {3 0}
        form_checkbutton $d.1.f3.1 "Intake port 3" rmi(no_mesh_tet_intake,3) {0 0} "" ""
        eval $d.1.f3.1 $opt1
        form_label $d.1.f3.b $tmp(FoundMesh,no_mesh_tet_intake,3) {0 1}
        form_label $d.1.f3.a $tmp(FoundGeo,no_mesh_tet_intake,3) {0 2}
        eval $d.1.f3.a $opt2; eval $d.1.f3.b $opt2 
      }
      form_frame $d.1.f4 flat 1 {4 0}
      form_checkbutton $d.1.f4.1 "Intake valve 1" rmi(no_mesh_hex_intake,1) {0 0} "" ""
      eval $d.1.f4.1 $opt1
      form_label $d.1.f4.b $tmp(FoundMesh,no_mesh_hex_intake,1) {0 1}
      form_label $d.1.f4.a $tmp(FoundGeo,no_mesh_hex_intake,1) {0 2}   
      eval $d.1.f4.a $opt2; eval $d.1.f4.b $opt2
      if {$rmi(intake_number_valve) == "2" || $rmi(intake_number_valve) == "3" } {
        form_frame $d.1.f5 flat 1 {5 0}
        form_checkbutton $d.1.f5.1 "Intake valve 2" rmi(no_mesh_hex_intake,2) {0 0} "" ""
        eval $d.1.f5.1 $opt1
        form_label $d.1.f5.b $tmp(FoundMesh,no_mesh_hex_intake,2) {0 1}
        form_label $d.1.f5.a $tmp(FoundGeo,no_mesh_hex_intake,2) {0 2}
        eval $d.1.f5.a $opt2; eval $d.1.f5.b $opt2
      }
      if {$rmi(intake_number_valve) == "3"} {
        form_frame $d.1.f6 flat 1 {6 0}
        form_checkbutton $d.1.f6.1 "Intake valve 3" rmi(no_mesh_hex_intake,3) {0 0} "" ""
        eval $d.1.f6.1 $opt1
        form_label $d.1.f6.b $tmp(FoundMesh,no_mesh_hex_intake,3) {0 1}
        form_label $d.1.f6.a $tmp(FoundGeo,no_mesh_hex_intake,3) {0 2}
        eval $d.1.f6.a $opt2; eval $d.1.f6.b $opt2
      }
    }
    if {$rmi(exhaust_motion) == "1"} {
      form_frame $d.1.f7 flat 1 {7 0}
      form_checkbutton $d.1.f7.1 "Exhaust port 1" rmi(no_mesh_tet_exhaust,1) {0 0} "" ""
      eval $d.1.f7.1 $opt1
      form_label $d.1.f7.b $tmp(FoundMesh,no_mesh_tet_exhaust,1) {0 1}
      form_label $d.1.f7.a $tmp(FoundGeo,no_mesh_tet_exhaust,1) {0 2}
      eval $d.1.f7.a $opt2; eval $d.1.f7.b $opt2
      if {$rmi(exhaust_number_valve) == "2"} {
        form_frame $d.1.f8 flat 1 {8 0}
        form_checkbutton $d.1.f8.1 "Exhaust port 2" rmi(no_mesh_tet_exhaust,2) {0 0} "" ""
        eval $d.1.f8.1 $opt1
        form_label $d.1.f8.b $tmp(FoundMesh,no_mesh_tet_exhaust,2) {0 1}
        form_label $d.1.f8.a $tmp(FoundGeo,no_mesh_tet_exhaust,2) {0 2}
        eval $d.1.f8.a $opt2; eval $d.1.f8.b $opt2
      }
      form_frame $d.1.f9 flat 1 {9 0}
      form_checkbutton $d.1.f9.1 "Exhaust valve 1" rmi(no_mesh_hex_exhaust,1) {0 0} "" ""
      eval $d.1.f9.1 $opt1
      form_label $d.1.f9.b $tmp(FoundMesh,no_mesh_hex_exhaust,1) {0 1}
      form_label $d.1.f9.a $tmp(FoundGeo,no_mesh_hex_exhaust,1) {0 2}
      eval $d.1.f9.a $opt2; eval $d.1.f9.b $opt2
      if {$rmi(exhaust_number_valve) == "2"} {
        form_frame $d.1.f10 flat 1 {10 0}
        form_checkbutton $d.1.f10.1 "Exhaust valve 2" rmi(no_mesh_hex_exhaust,2) {0 0} "" ""
        eval $d.1.f10.1 $opt1
        form_label $d.1.f10.b $tmp(FoundMesh,no_mesh_hex_exhaust,2) {0 1}
        form_label $d.1.f10.a $tmp(FoundGeo,no_mesh_hex_exhaust,2) {0 2}
        eval $d.1.f10.a $opt2; eval $d.1.f10.b $opt2
      }
    }
    if !$rmi(chadice) {
      form_frame $d.1.f11 flat 1 {11 0}
      form_checkbutton $d.1.f11.1 "Combustion dome" rmi(no_mesh_comb) {0 0} "" ""
      eval $d.1.f11.1 $opt1
      form_label $d.1.f11.b $tmp(FoundMesh,no_mesh_comb) {0 1}
      form_label $d.1.f11.a $tmp(FoundGeo,no_mesh_comb) {0 2}
      eval $d.1.f11.a $opt2; eval $d.1.f11.b $opt2 
      form_frame $d.1.f12 flat 1 {12 0}
      form_checkbutton $d.1.f12.1 "Piston bowl" rmi(no_mesh_piston) {0 0} "" ""
      eval $d.1.f12.1 $opt1
      form_label $d.1.f12.b $tmp(FoundMesh,no_mesh_piston) {0 1}
      form_label $d.1.f12.a $tmp(FoundGeo,no_mesh_piston) {0 2}
      eval $d.1.f12.a $opt2; eval $d.1.f12.b $opt2
    
      set csq1 0
      if {[info exists rmi(configuration)] && [info exists rmi(tetin_fn)]} {
        set tetin_file $rmi(configuration)/mesh/$rmi(tetin_fn)
        if {[file exists $tetin_file] && [file isfile $tetin_file]} {
          set f [open $tetin_file r]
          while {![eof $f]} {
            set line [ string toupper [gets $f]]
            if [string match "*FAMILY*CSQ1*" $line] {
              if [string match "*DEFINE_SURFACE*" $line] {
                set csq1 1
                break
              }
            }
          }
          close $f
        }
      }
    
      if $csq1 {
        form_frame $d.1.f13 flat 1 {13 0}
        form_checkbutton $d.1.f13.1 "Quad on flat part of deck" rmi(extrude_comb) {0 0} "" ""
      }
    } 
    
    form_finish +200+200 1 0
  }
  busy 0
}

proc rmi_TransientMeshGui {output_to} {
global rmi

  set file $rmi(configuration)/domains/domain_rmi_merge
  if [file exists $file] {
    if [ rmi_ShowQuestion QUESTION "Remove existing base mesh file ?" ] {
      file delete $file
    }
  }
  if {$rmi(outp_lift_curves) || $rmi(outp_para_file)} {
    rmi_OutputRunSetup
  }
  rmi_OutputMesh transient $output_to
}

proc rmi_MeshPreviewGui {} {
global rmi
  
  if [winfo exists .rmimeshpreview] { destroy .rmimeshpreview }
  set buttons {
    {"Preview" "destroy .rmimeshpreview; rmi_MeshPreview preview starcd"}
    {cancel}
  }
  set d [ form_init .rmimeshpreview "Mesh preview" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.1 flat 3 {0 0}
    form_label $d.1.1.1 "Starting angle DCA" {0 1}; $d.1.1.1 configure -width 20
    form_entry $d.1.1.2 rmi(starting_angle_dca) float {0 2}
    form_frame $d.1.2 flat 3 {1 0}
    form_label $d.1.2.1 "Ending angle DCA" {0 1}; $d.1.2.1 configure -width 20
    form_entry $d.1.2.2 rmi(ending_angle_dca) float {0 2}
    form_frame $d.1.3 flat 3 {2 0}
    form_label $d.1.3.1 "Increment angle DCA" {0 1}; $d.1.3.1 configure -width 20
    form_entry $d.1.3.2 rmi(increment_angle_dca) float {0 2}
    form_finish +200+200 1 0
  }
}

proc rmi_GeometryPreviewGui {} {
global rmi
  
  if [winfo exists .rmigeometrypreview] { destroy .rmigeometrypreview }
  set buttons {
    {"Preview" "destroy .rmigeometrypreview; rmi_GeometryPreview"}
    {cancel}
  }
  set d [ form_init .rmigeometrypreview "Geometry preview" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.1 flat 3 {0 0}
    form_label $d.1.1.1 "Starting angle DCA" {0 1}; $d.1.1.1 configure -width 20
    form_entry $d.1.1.2 rmi(geo_starting_angle_dca) float {0 2}
    form_frame $d.1.2 flat 3 {1 0}
    form_label $d.1.2.1 "Ending angle DCA" {0 1}; $d.1.2.1 configure -width 20
    form_entry $d.1.2.2 rmi(geo_ending_angle_dca) float {0 2}
    form_frame $d.1.3 flat 3 {2 0}
    form_label $d.1.3.1 "Increment angle DCA" {0 1}; $d.1.3.1 configure -width 20
    form_entry $d.1.3.2 rmi(geo_increment_angle_dca) float {0 2}
    form_finish +200+200 1 0
  }
}

proc rmi_SteadyStateMeshGui {output_to} {
global rmi
  
  if [winfo exists .rmisteadystatemesh] { destroy .rmisteadystatemesh }
  
  if {$output_to == "starcd"} {
    set buttons {
      {"Steady state mesh" "destroy .rmisteadystatemesh; rmi_SteadyStateMesh starcd"}
      {cancel}
    }
  } elseif {$output_to == "fluent"} {
    set buttons {
      {"Steady state mesh" "destroy .rmisteadystatemesh; rmi_SteadyStateMesh fluent"}
      {cancel}
    }
  }
  set d [ form_init .rmisteadystatemesh "Steady state mesh" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.1 flat 3 {0 0}
    form_label $d.1.1.1 "Angle DCA" {0 1}
    form_entry $d.1.1.2 rmi(angle_dca) float {0 2}
    set com "rmi_UseCylinderLength $d"
    form_checkbutton $d.1.1.3 "Cylinder length" rmi(UseCylinderLength) {0 3} "" "$com"
    form_entry $d.1.1.4 rmi(cyl_length) float {0 4}
    form_label $d.1.1.5 "Number of layers" {0 5}
    form_entry $d.1.1.6 rmi(nlayers_cyl) float {0 6}
    form_finish +200+200 1 0
  }
  rmi_UseCylinderLength $d
}

proc rmi_UseCylinderLength {d} {
global rmi 
 
  if $rmi(UseCylinderLength) {
    $d.1.1.3 configure -fg black
    $d.1.1.4 configure -state normal -fg black
    $d.1.1.5 configure -fg black
    $d.1.1.6 configure -state normal -fg black
  } else {
    $d.1.1.3 configure -fg gray
    $d.1.1.4 configure -state disabled -fg gray
    $d.1.1.5 configure -fg gray
    $d.1.1.6 configure -state disabled -fg gray
  }
}

proc rmi_SteadyStateMesh {output_to} {
global rmi
  
  set file $rmi(configuration)/domains/domain_rmi_merge
  if [file exists $file] {
    if [ rmi_ShowQuestion QUESTION "Remove existing base mesh file ?" ] {
      file delete $file
    }
  }
  rmi_OutputMesh steady_state $output_to
}

proc rmi_MeshPreview {type {output ""}} {
global rmi
  
  if $rmi(chadice) {
    rmi_AppMeshPreview
  } else {
    set file $rmi(configuration)/domains/domain_rmi_merge
    if { $type == "create_station" } {
      set file $rmi(configuration)/domains/domain_rmi_merge_ac_flux
    }
    if [file exists $file] {
      if [ rmi_ShowQuestion QUESTION "Remove existing base mesh file ?" ] {
        file delete $file
      }
    }
    if { $type == "preview" } {
      rmi_OutputMesh $type $output
    } elseif { $type == "create_station" } {
      rmi_ACFluXCreateStation
    }
  }
}

proc rmi_GeometryPreview {} {
global rmi

  set file $rmi(configuration)/mesh/tetin_rmi_preview
  if [file exists $file] {
    if [ rmi_ShowQuestion QUESTION "Remove existing tetin preview file ?" ] {
      file delete $file
    }
  }
  rmi_AppGeometryPreview
}

proc rmi_SelectObjectFileGui {} {
global rmi
  
  if [winfo exists .rmiselectobjectfile] { destroy .rmiselectobjectfile }
  set buttons {
    {"Select" "destroy .rmiselectobjectfile"}
    {cancel}
  }
  
  set d [ form_init .rmiselectobjectfile "Platform for STAR-CD run" "" $buttons ]
  if {$d != ""} {
    form_frame $d.1 ridge 3 {0 0}
    form_frame $d.1.1 flat 3 {0 0}
    form_radiobutton $d.1.1.1 "IRIX Mips4" rmi(Platform) mips4 {0 1}
    form_radiobutton $d.1.1.2 "IRIX64 Mips4" rmi(Platform) mips64 {1 1}
    form_radiobutton $d.1.1.3 "SunOS 5.7" rmi(Platform) sol7 {2 1}
    form_radiobutton $d.1.1.4 "SunOS64 UltraSparc"  rmi(Platform) sol64 {3 1}
    form_frame $d.1.2 flat 3 {0 1}
    form_radiobutton $d.1.2.1 "HP-UX 11.x PA-RISC 2.0" rmi(Platform) hp11 {0 1}
    form_radiobutton $d.1.2.2 "HP-UX 11.x PA-RISC 2.0 32 bit" rmi(Platform) hp11_32 {1 1}
    form_radiobutton $d.1.2.3 "AIX 4.1" rmi(Platform) ibm41 {2 1}
    form_radiobutton $d.1.2.4 "AIX 4.3" rmi(Platform) ibm43 {3 1}
    form_radiobutton $d.1.2.5 "AIX 5.1 64bit" rmi(Platform) ibm64 {4 1}
    form_frame $d.1.3 flat 3 {0 2}
    form_radiobutton $d.1.3.1 "Linux" rmi(Platform) linux {0 1}
    form_radiobutton $d.1.3.2 "Linux 64 bit" rmi(Platform) linux64 {1 1}
    form_radiobutton $d.1.3.3 "Linux 64 bit AMD" rmi(Platform) linux64_amd {2 1}
    form_radiobutton $d.1.3.4 "OSF1" rmi(Platform) alpha40 {3 1} 
    form_frame $d.2 ridge 3 {1 0 -pady 3}
    form_frame $d.2.1 flat 3 {0 0}
    form_label $d.2.1.1 "Precision:   " {0 0}
    form_radiobutton $d.2.1.2 "Single" rmi(UseForPlatform) 1 {0 1}
    form_radiobutton $d.2.1.3 "Double" rmi(UseForPlatform) 2 {0 2}         
    form_finish +200+200 1 0
  }
}
