#Source files and check license
#----------------------------------------------------
proc loadIC3M {} {
 global rmi env rmi_license tk_version tcl_platform file_name
 global small_screen_hack is_win has_blt bg_clr get_number_of_valves

 if [info exists env(ICEM_CHADICE)] {
    set rmi(chadice) 1
    set rmi(Product) "CHADICE"
    } else {
       set rmi(chadice) 0
       set rmi(Product) "IC3M"
       }

# if {![info exists rmi_license($rmi(Product))] || $rmi_license($rmi(Product)) == 0} {
#    set ansys_aiic3m_code 326
#    if [expr {[check_license_exists icem ic3m] || [check_license_exists ansys $ansys_aiic3m_code]}] {
#       mess "ICEM CFD Engineering license for $rmi(Product) obtained\n"
#       set rmi_license($rmi(Product)) 1
#       } else {
#          mess "No $rmi(Product) license obtained.\n" red
#          set rmi_license($rmi(Product)) 0
#          return
#          }
#    }
 set rmi_license($rmi(Product)) 1

 ###Load up IC3M
 set nm ic3m
 if [file exists $env(ICEM_ACN)/lib/va/IC3M/src/$nm.tcl] {
    set env(AI_IC3M) 1
    set va_script_file ""
    if {1 == 1}  {
       foreach f [glob $env(ICEM_ACN)/lib/va/IC3M/src/${nm}*.tcl] {
          source $f
          }
       } else {
          tk_messageBox -message  "sdf License for IC3M not obtained. . . "
          exit
          }
    } else {
       tk_messageBox -message "sdf IC3M is not installed"
       exit
       }

 if ![ info exists is_win ] {
    set is_win 0
    set has_blt 0
    set small_screen_hack 0
    }

 if {$rmi(Platform) == ""} {
    set os $tcl_platform(os)
    if {$os == "IRIX"} {
       set rmi(Platform) mips4
       } elseif {$os == "IRIX64"} {
          set rmi(Platform) mips64
          } elseif {$os == "AIX"} {
             set rmi(Platform) ibm64
             } elseif {$os == "SunOS"} {
                set rmi(Platform) sol64
                } elseif {$os == "OSF1"} {
                   set rmi(Platform) alpha40
                   } elseif {$os == "HP-UX"} {
                      set rmi(Platform) hp11
                      } elseif {$os == "Linux"} {
                         set rmi(Platform) linux
                         } else {
                            set rmi(Platform) hp11
                            }
    }
 set bg_clr "black"
 set get_number_of_valves 0
}

###===================================================================================

proc onic3mSettingsDEZLoad {} {
  global createstatus

 set createstatus [mw_ic3m_delete_lift_profile]

}

###===================================================================================
proc onic3mSettingsDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mSettingsDEZ cancel}

}

###===================================================================================

proc onic3mSimulationDEZLoad {} {
 global rmi
 global createstatus

 set engine_RPM              $rmi(engine_RPM)
 set degrees_simulated_start $rmi(degrees_simulated_start)
 set degrees_simulated_stop  $rmi(degrees_simulated_stop)
 
 if {[info exists rmi(intake_motion)] && [info exists rmi(exhaust_motion)]} {
    if {$rmi(intake_motion) == 1 && $rmi(exhaust_motion) == 1} {set valveselectVal 0}
    if {$rmi(intake_motion) == 1 && $rmi(exhaust_motion) == 0} {set valveselectVal 1}
    if {$rmi(intake_motion) == 0 && $rmi(exhaust_motion) == 0} {set valveselectVal 2}
    if {$rmi(intake_motion) == 0 && $rmi(exhaust_motion) == 1} {set valveselectVal 3}
    }
 
 set createstatus [mw_ic3m_delete_lift_profile]
}

###===================================================================================
proc onic3mSimulationDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mSimulationDEZ cancel}

}

###===================================================================================

proc onic3mSimulationDEZOK {} {
 global rmi

 set rmi(engine_RPM)              $engine_RPM
 mw_ic3m_CalculateTimestep
 set rmi(degrees_simulated_start) $degrees_simulated_start
 set rmi(degrees_simulated_stop)  $degrees_simulated_stop

 switch [set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] {
    0 {set rmi(intake_motion) 1; set rmi(exhaust_motion) 1}
    1 {set rmi(intake_motion) 1; set rmi(exhaust_motion) 0}
    2 {set rmi(intake_motion) 0; set rmi(exhaust_motion) 0}
    3 {set rmi(intake_motion) 0; set rmi(exhaust_motion) 1}
    }
}

###===================================================================================

proc onic3mSimulationDEZApply {} {
 global rmi

 set rmi(engine_RPM)              $engine_RPM
 mw_ic3m_CalculateTimestep
 set rmi(degrees_simulated_start) $degrees_simulated_start
 set rmi(degrees_simulated_stop)  $degrees_simulated_stop

 switch [set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] {
    0 {set rmi(intake_motion) 1; set rmi(exhaust_motion) 1}
    1 {set rmi(intake_motion) 1; set rmi(exhaust_motion) 0}
    2 {set rmi(intake_motion) 0; set rmi(exhaust_motion) 0}
    3 {set rmi(intake_motion) 0; set rmi(exhaust_motion) 1}
    }
}

###===================================================================================

proc onic3mParametersDEZLoad {} {
 global rmi
 global createstatus

 set bore_diameter $rmi(bore_diameter)
 set stroke_length $rmi(stroke_length)
 set connecting_rod_length $rmi(connecting_rod_length)
 set piston_TDC_position $rmi(piston_TDC_position)

 set number_layers_cyl_TDC $rmi(number_layers_cyl_TDC)
 set number_layers_cyl_BDC $rmi(number_layers_cyl_BDC)
 set bdc_layer_thickness $rmi(bdc_layer_thickness)
 set edge_criterion_cmb $rmi(edge_criterion_cmb)
 
 set createstatus [mw_ic3m_delete_lift_profile]
}

###===================================================================================

proc onic3mParametersDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mParametersDEZ cancel}
 set gif $env(ICEM_ACN)/lib/va/IC3M/icons/mw_params.gif
 set d .v.f1.frame.p3.f1.frame.p4.f1.frame 
 frame $d.1
 pack $d.1 -side right
 label  $d.1.img0  -image [image create photo -file $gif]
 pack $d.1.img0  -side right
}

###===================================================================================

proc onic3mParametersDEZOK {} {
 global rmi

 set rmi(bore_diameter) $bore_diameter
 set rmi(stroke_length) $stroke_length
 set rmi(connecting_rod_length) $connecting_rod_length
 set rmi(piston_TDC_position) $piston_TDC_position
 set d .v.f1.frame.p3.f1.frame.p4.f1.frame
 destroy $d.1

 set rmi(number_layers_cyl_TDC) $number_layers_cyl_TDC
 set rmi(number_layers_cyl_BDC) $number_layers_cyl_BDC
 set rmi(bdc_layer_thickness) $bdc_layer_thickness
 set rmi(edge_criterion_cmb) $edge_criterion_cmb
}

###===================================================================================

proc onic3mParametersDEZApply {} {
 global rmi

 set rmi(bore_diameter) $bore_diameter
 set rmi(stroke_length) $stroke_length
 set rmi(connecting_rod_length) $connecting_rod_length
 set rmi(piston_TDC_position) $piston_TDC_position

 set rmi(number_layers_cyl_TDC) $number_layers_cyl_TDC
 set rmi(number_layers_cyl_BDC) $number_layers_cyl_BDC
 set rmi(bdc_layer_thickness) $bdc_layer_thickness
 set rmi(edge_criterion_cmb) $edge_criterion_cmb
}

###===================================================================================

proc onic3mParametersDEZCancel {} {

 set d .v.f1.frame.p3.f1.frame.p4.f1.frame
 destroy $d.1
}

###===================================================================================

proc onic3mIntakeValveProfileDEZLoad {} {
 global rmi

 if {$rmi(intake_motion) == 1} {
    set intake_valve_nlayers $rmi(intake_valve_nlayers)
    set edge1_intake $rmi(edge1_intake)
    set edge2_intake $rmi(edge2_intake)
    set edge3_intake $rmi(edge3_intake)
    set edge4_intake $rmi(edge4_intake)
    set edge5_intake $rmi(edge5_intake)
    set n_layers_valve_seat_intake $rmi(n_layers_valve_seat_intake)
    set n_layers_next_tip_intake $rmi(n_layers_next_tip_intake)
    set edge10_intake $rmi(edge10_intake)
    set edge11_intake $rmi(edge11_intake)
    set n_cells_circumfrential_intake $rmi(n_cells_circumfrential_intake)
    set edge13_intake $rmi(edge13_intake)
    set edge14_intake $rmi(edge14_intake)
    set edge15_intake $rmi(edge15_intake)
    set vbdyh_intake $rmi(vbdyh_intake)
    set vbdynum_intake $rmi(vbdynum_intake)
    set edge_criterion_intake $rmi(edge_criterion_intake)
    }
}

###===================================================================================

proc onic3mIntakeValveProfileDEZOK {} {
 global rmi

 if {$rmi(intake_motion) == 1} {
    if {$rmi(all_intake_valves) == 1} {
       set rmi(intake_max_lift) $rmi(intake_max_lift,1)
       for {set i 2} {$i <= $rmi(intake_number_valve)} {incr i} {
          set rmi(intake_max_lift,$i) $rmi(intake_max_lift,1)
          }
       }
    set rmi(edge1_intake)                  $edge1_intake
    set rmi(edge2_intake)                  $edge2_intake
    set rmi(edge3_intake)                  $edge3_intake
    set rmi(edge4_intake)                  $edge4_intake
    set rmi(edge5_intake)                  $edge5_intake
    set rmi(n_layers_valve_seat_intake)    $n_layers_valve_seat_intake
    set rmi(n_layers_next_tip_intake)      $n_layers_next_tip_intake
    set rmi(edge11_intake)                 $edge11_intake
    set rmi(n_cells_circumfrential_intake) $n_cells_circumfrential_intake
    set rmi(edge13_intake)                 $edge13_intake
    set rmi(edge14_intake)                 $edge14_intake
    set rmi(edge15_intake)                 $edge15_intake
    set rmi(edge10_intake)                 $edge10_intake
    set rmi(intake_valve_nlayers)          $intake_valve_nlayers
    set rmi(edge_criterion_intake)         $edge_criterion_intake
    
    for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
       set rmi(edge1_intake,$i)                  $edge1_intake
       set rmi(edge2_intake,$i)                  $edge2_intake
       set rmi(edge3_intake,$i)                  $edge3_intake
       set rmi(edge4_intake,$i)                  $edge4_intake
       set rmi(edge5_intake,$i)                  $edge5_intake
       set rmi(n_layers_valve_seat_intake,$i)    $n_layers_valve_seat_intake
       set rmi(n_layers_next_tip_intake,$i)      $n_layers_next_tip_intake
       set rmi(edge11_intake,$i)                 $edge11_intake
       set rmi(n_cells_circumfrential_intake,$i) $n_cells_circumfrential_intake
       set rmi(edge13_intake,$i)                 $edge13_intake
       set rmi(edge14_intake,$i)                 $edge14_intake
       set rmi(edge15_intake,$i)                 $edge15_intake
       set rmi(edge10_intake,$i)                 $edge10_intake
       set rmi(edge_criterion_intake,$i)         $edge_criterion_intake
       }
    if {[set ::AIEnv::DEZ::ic3mIntakeValveProfileDEZ::boundarylayerintakeVal] == 0} {
       for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
          set rmi(IntakeVBDY,$i) 0
          }
       } else {
          set rmi(vbdyh_intake) $vbdyh_intake
          set rmi(vbdynum_intake) $vbdynum_intake
          for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
             set rmi(IntakeVBDY,$i) 1
             }
          }
    }

 mw_ic3m_delete_lift_profile
}

###===================================================================================

proc onic3mIntakeValveProfileDEZApply {} {
 global rmi

 if {$rmi(intake_motion) == 1} {
    if {$rmi(all_intake_valves) == 1} {
       set rmi(intake_max_lift) $rmi(intake_max_lift,1)
       for {set i 2} {$i <= $rmi(intake_number_valve)} {incr i} {
          set rmi(intake_max_lift,$i) $rmi(intake_max_lift,1)
          }
       }
    set rmi(edge1_intake)                  $edge1_intake
    set rmi(edge2_intake)                  $edge2_intake
    set rmi(edge3_intake)                  $edge3_intake
    set rmi(edge4_intake)                  $edge4_intake
    set rmi(edge5_intake)                  $edge5_intake
    set rmi(n_layers_valve_seat_intake)    $n_layers_valve_seat_intake
    set rmi(n_layers_next_tip_intake)      $n_layers_next_tip_intake
    set rmi(edge11_intake)                 $edge11_intake
    set rmi(n_cells_circumfrential_intake) $n_cells_circumfrential_intake
    set rmi(edge13_intake)                 $edge13_intake
    set rmi(edge14_intake)                 $edge14_intake
    set rmi(edge15_intake)                 $edge15_intake
    set rmi(edge10_intake)                 $edge10_intake
    set rmi(intake_valve_nlayers)          $intake_valve_nlayers
    set rmi(edge_criterion_intake)         $edge_criterion_intake
    
    for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
       set rmi(edge1_intake,$i)                  $edge1_intake
       set rmi(edge2_intake,$i)                  $edge2_intake
       set rmi(edge3_intake,$i)                  $edge3_intake
       set rmi(edge4_intake,$i)                  $edge4_intake
       set rmi(edge5_intake,$i)                  $edge5_intake
       set rmi(n_layers_valve_seat_intake,$i)    $n_layers_valve_seat_intake
       set rmi(n_layers_next_tip_intake,$i)      $n_layers_next_tip_intake
       set rmi(edge11_intake,$i)                 $edge11_intake
       set rmi(n_cells_circumfrential_intake,$i) $n_cells_circumfrential_intake
       set rmi(edge13_intake,$i)                 $edge13_intake
       set rmi(edge14_intake,$i)                 $edge14_intake
       set rmi(edge15_intake,$i)                 $edge15_intake
       set rmi(edge10_intake,$i)                 $edge10_intake
       set rmi(edge_criterion_intake,$i)         $edge_criterion_intake
       }
    if {[set ::AIEnv::DEZ::ic3mIntakeValveProfileDEZ::boundarylayerintakeVal] == 0} {
       for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
          set rmi(IntakeVBDY,$i) 0
          }
       } else {
          set rmi(vbdyh_intake) $vbdyh_intake
          set rmi(vbdynum_intake) $vbdynum_intake
          for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
             set rmi(IntakeVBDY,$i) 1
             }
          }
    }
}

###===================================================================================

proc onic3mIntakeValveProfileDEZCancel {} {

 mw_ic3m_delete_lift_profile
}

###===================================================================================

proc onic3mExhaustValveProfileDEZLoad {} {
 global rmi

 if {$rmi(exhaust_motion) == 1} {
    set exhaust_valve_nlayers          $rmi(exhaust_valve_nlayers)
    set edge1_exhaust                  $rmi(edge1_exhaust)
    set edge2_exhaust                  $rmi(edge2_exhaust)
    set edge3_exhaust                  $rmi(edge3_exhaust)
    set edge4_exhaust                  $rmi(edge4_exhaust)
    set edge5_exhaust                  $rmi(edge5_exhaust)
    set n_layers_valve_seat_exhaust    $rmi(n_layers_valve_seat_exhaust)
    set n_layers_next_tip_exhaust      $rmi(n_layers_next_tip_exhaust)
    set edge10_exhaust                 $rmi(edge10_exhaust)
    set edge11_exhaust                 $rmi(edge11_exhaust)
    set n_cells_circumfrential_exhaust $rmi(n_cells_circumfrential_exhaust)
    set edge13_exhaust                 $rmi(edge13_exhaust)
    set edge14_exhaust                 $rmi(edge14_exhaust)
    set edge15_exhaust                 $rmi(edge15_exhaust)
    set vbdyh_exhaust                  $rmi(vbdyh_exhaust)
    set vbdynum_exhaust                $rmi(vbdynum_exhaust)
    set edge_criterion_exhaust         $rmi(edge_criterion_exhaust)
    }
}

###===================================================================================

proc onic3mExhaustValveProfileDEZOK {} {
 global rmi

 if {$rmi(exhaust_motion) == 1} {
    if {$rmi(all_exhaust_valves) == 1} {
       set rmi(exhaust_max_lift) $rmi(exhaust_max_lift,1)
       for {set i 2} {$i <= $rmi(exhaust_number_valve)} {incr i} {
          set rmi(exhaust_max_lift,$i) $rmi(exhaust_max_lift,1)
          }
       }
    
    set rmi(edge1_exhaust)                  $edge1_exhaust
    set rmi(edge2_exhaust)                  $edge2_exhaust
    set rmi(edge3_exhaust)                  $edge3_exhaust
    set rmi(edge4_exhaust)                  $edge4_exhaust
    set rmi(edge5_exhaust)                  $edge5_exhaust
    set rmi(n_layers_valve_seat_exhaust)    $n_layers_valve_seat_exhaust
    set rmi(n_layers_next_tip_exhaust)      $n_layers_next_tip_exhaust
    set rmi(edge11_exhaust)                 $edge11_exhaust
    set rmi(n_cells_circumfrential_exhaust) $n_cells_circumfrential_exhaust
    set rmi(edge13_exhaust)                 $edge13_exhaust
    set rmi(edge14_exhaust)                 $edge14_exhaust
    set rmi(edge15_exhaust)                 $edge15_exhaust
    set rmi(edge10_exhaust)                 $edge10_exhaust
    set rmi(exhaust_valve_nlayers)          $exhaust_valve_nlayers
    set rmi(edge_criterion_exhaust)         $edge_criterion_exhaust
    
    for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
       set rmi(edge1_exhaust,$i)                  $edge1_exhaust
       set rmi(edge2_exhaust,$i)                  $edge2_exhaust
       set rmi(edge3_exhaust,$i)                  $edge3_exhaust
       set rmi(edge4_exhaust,$i)                  $edge4_exhaust
       set rmi(edge5_exhaust,$i)                  $edge5_exhaust
       set rmi(n_layers_valve_seat_exhaust,$i)    $n_layers_valve_seat_exhaust
       set rmi(n_layers_next_tip_exhaust,$i)      $n_layers_next_tip_exhaust
       set rmi(edge11_exhaust,$i)                 $edge11_exhaust
       set rmi(n_cells_circumfrential_exhaust,$i) $n_cells_circumfrential_exhaust
       set rmi(edge13_exhaust,$i)                 $edge13_exhaust
       set rmi(edge14_exhaust,$i)                 $edge14_exhaust
       set rmi(edge15_exhaust,$i)                 $edge15_exhaust
       set rmi(edge10_exhaust,$i)                 $edge10_exhaust
       set rmi(edge_criterion_exhaust,$i)         $edge_criterion_exhaust
       }
    if {[set ::AIEnv::DEZ::ic3mExhaustValveProfileDEZ::boundarylayerexhaustVal] == 0} {
       for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
          set rmi(ExhaustVBDY,$i) 0
          }
       } else {
          set rmi(vbdyh_exhaust) $vbdyh_exhaust
          set rmi(vbdynum_exhaust) $vbdynum_exhaust
          for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
             set rmi(ExhaustVBDY,$i) 1
             }
          }
    }

 mw_ic3m_delete_lift_profile
}

###===================================================================================

proc onic3mExhaustValveProfileDEZApply {} {
 global rmi

 if {$rmi(exhaust_motion) == 1} {
    if {$rmi(all_exhaust_valves) == 1} {
       set rmi(exhaust_max_lift) $rmi(exhaust_max_lift,1)
       for {set i 2} {$i <= $rmi(exhaust_number_valve)} {incr i} {
          set rmi(exhaust_max_lift,$i) $rmi(exhaust_max_lift,1)
          }
       }
    
    set rmi(edge1_exhaust)                  $edge1_exhaust
    set rmi(edge2_exhaust)                  $edge2_exhaust
    set rmi(edge3_exhaust)                  $edge3_exhaust
    set rmi(edge4_exhaust)                  $edge4_exhaust
    set rmi(edge5_exhaust)                  $edge5_exhaust
    set rmi(n_layers_valve_seat_exhaust)    $n_layers_valve_seat_exhaust
    set rmi(n_layers_next_tip_exhaust)      $n_layers_next_tip_exhaust
    set rmi(edge11_exhaust)                 $edge11_exhaust
    set rmi(n_cells_circumfrential_exhaust) $n_cells_circumfrential_exhaust
    set rmi(edge13_exhaust)                 $edge13_exhaust
    set rmi(edge14_exhaust)                 $edge14_exhaust
    set rmi(edge15_exhaust)                 $edge15_exhaust
    set rmi(edge10_exhaust)                 $edge10_exhaust
    set rmi(exhaust_valve_nlayers)          $exhaust_valve_nlayers
    set rmi(edge_criterion_exhaust)         $edge_criterion_exhaust
    
    for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
       set rmi(edge1_exhaust,$i)                  $edge1_exhaust
       set rmi(edge2_exhaust,$i)                  $edge2_exhaust
       set rmi(edge3_exhaust,$i)                  $edge3_exhaust
       set rmi(edge4_exhaust,$i)                  $edge4_exhaust
       set rmi(edge5_exhaust,$i)                  $edge5_exhaust
       set rmi(n_layers_valve_seat_exhaust,$i)    $n_layers_valve_seat_exhaust
       set rmi(n_layers_next_tip_exhaust,$i)      $n_layers_next_tip_exhaust
       set rmi(edge11_exhaust,$i)                 $edge11_exhaust
       set rmi(n_cells_circumfrential_exhaust,$i) $n_cells_circumfrential_exhaust
       set rmi(edge13_exhaust,$i)                 $edge13_exhaust
       set rmi(edge14_exhaust,$i)                 $edge14_exhaust
       set rmi(edge15_exhaust,$i)                 $edge15_exhaust
       set rmi(edge10_exhaust,$i)                 $edge10_exhaust
       set rmi(edge_criterion_exhaust,$i)         $edge_criterion_exhaust
       }
    if {[set ::AIEnv::DEZ::ic3mExhaustValveProfileDEZ::boundarylayerexhaustVal] == 0} {
       for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
          set rmi(ExhaustVBDY,$i) 0
          }
       } else {
          set rmi(vbdyh_exhaust) $vbdyh_exhaust
          set rmi(vbdynum_exhaust) $vbdynum_exhaust
          for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
             set rmi(ExhaustVBDY,$i) 1
             }
          }
    }
}

###===================================================================================

proc onic3mExhaustValveProfileDEZCancel {} {

 mw_ic3m_delete_lift_profile
}

###===================================================================================

proc onic3mGeometryPreviewDEZLoad {} {
 global rmi
 global createstatus

 set geo_starting_angle_dca $rmi(geo_starting_angle_dca)
 set geo_ending_angle_dca $rmi(geo_ending_angle_dca)
 set geo_increment_angle_dca $rmi(geo_increment_angle_dca)
 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mGeometryPreviewDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mGeometryPreviewDEZ cancel}

}

###===================================================================================

proc onic3mGeometryPreviewDEZOK {} {
 global rmi

 set rmi(geo_starting_angle_dca) $geo_starting_angle_dca
 set rmi(geo_ending_angle_dca) $geo_ending_angle_dca
 set rmi(geo_increment_angle_dca) $geo_increment_angle_dca
 mw_ic3m_GeometryPreview
}

###===================================================================================

proc onic3mGeometryPreviewDEZApply {} {
 global rmi

 set rmi(geo_starting_angle_dca) $geo_starting_angle_dca
 set rmi(geo_ending_angle_dca) $geo_ending_angle_dca
 set rmi(geo_increment_angle_dca) $geo_increment_angle_dca
 mw_ic3m_GeometryPreview
}

###===================================================================================

proc onic3mComputeGeometryDEZLoad {} {
 global rmi
 global createstatus

 if ![info exists ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] {
    set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal 0
    }

 set opt [list int0ex0 int1ex0 int0ex1 int1ex1 int2ex0 int0ex2 int2ex1 int1ex2 \
               int2ex2 int3ex0 int3ex1 int3ex2]
 set str "int${rmi(intake_number_valve)}ex$rmi(exhaust_number_valve)"
 
 if {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 3} {
    set str "int0ex$rmi(exhaust_number_valve)"
    } elseif {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 1} {
       set str "int${rmi(intake_number_valve)}ex0"
       } elseif {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 2} {
          set str "int0ex0"
          }
 set ::AIEnv::DEZ::ic3mComputeGeometryDEZ::existingvalvesVal [lsearch $opt $str]
 
 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mComputeGeometryDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mComputeGeometryDEZ cancel}

}

###===================================================================================

proc onic3mComputeGeometryDEZOK {} {
 global rmi

 foreach item {intake exhaust} {
    if {$rmi(${item}_motion) == 1} {
       for {set i 1} {$i <= $rmi(${item}_number_valve)} {incr i} {
          set rmi(compute_port_${item},$i) [set compute_port_${item}_$i]
          set rmi(compute_valve_${item},$i) [set compute_valve_${item}_$i]
          }
       }
    }
 set rmi(compute_comb) $compute_comb
 set rmi(compute_piston) $compute_piston
 mw_ic3m_ComputeGeometry
}

###===================================================================================

proc onic3mComputeGeometryDEZApply {} {
 global rmi

 foreach item {intake exhaust} {
    if {$rmi(${item}_motion) == 1} {
       for {set i 1} {$i <= $rmi(${item}_number_valve)} {incr i} {
          set rmi(compute_port_${item},$i) [set compute_port_${item}_$i]
          set rmi(compute_valve_${item},$i) [set compute_valve_${item}_$i]
          }
       }
    }
 set rmi(compute_comb) $compute_comb
 set rmi(compute_piston) $compute_piston
 mw_ic3m_ComputeGeometry
}

###===================================================================================

proc onic3mComputeMeshDEZLoad {} {
 global rmi
 global createstatus

 if ![info exists ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] {
    set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal 0
    }

 set opt [list mint0ex0 mint1ex0 mint0ex1 mint1ex1 mint2ex0 mint0ex2 mint2ex1 mint1ex2 \
               mint2ex2 mint3ex0 mint3ex1 mint3ex2]
 set str "mint${rmi(intake_number_valve)}ex$rmi(exhaust_number_valve)"

 if {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 3} {
    set str "mint0ex$rmi(exhaust_number_valve)"
    } elseif {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 1} {
       set str "mint${rmi(intake_number_valve)}ex0"
       } elseif {[set ::AIEnv::DEZ::ic3mSimulationDEZ::valveselectVal] == 2} {
          set str "mint0ex0"
          }
 set ::AIEnv::DEZ::ic3mComputeMeshDEZ::mexistingvalvesVal [lsearch $opt $str]

 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mComputeMeshDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mComputeMeshDEZ cancel}

}

###===================================================================================

proc onic3mComputeMeshDEZOK {} {
 global rmi

 foreach item {intake exhaust} {
    if {$rmi(${item}_motion) == 1} {
       for {set i 1} {$i <= $rmi(${item}_number_valve)} {incr i} {
          if {[set no_mesh_tet_${item}_$i] == 0} {
             set rmi(no_mesh_tet_${item},$i) 1
             } else {set rmi(no_mesh_tet_${item},$i) 0}
          if {[set no_mesh_hex_${item}_$i] == 0} {
             set rmi(no_mesh_hex_${item},$i) 1
             } else {set rmi(no_mesh_hex_${item},$i) 0}
          }
       }
    }
 if {$no_mesh_comb == 0} {
    set rmi(no_mesh_comb) 1
    } else {set rmi(no_mesh_comb) 0}
 if {$no_mesh_piston == 0} {
    set rmi(no_mesh_piston) 1
    } else {set rmi(no_mesh_piston) 0}
 mw_ic3m_ComputeMesh
}

###===================================================================================

proc onic3mComputeMeshDEZApply {} {
 global rmi

 foreach item {intake exhaust} {
    if {$rmi(${item}_motion) == 1} {
       for {set i 1} {$i <= $rmi(${item}_number_valve)} {incr i} {
          if {[set no_mesh_tet_${item}_$i] == 0} {
             set rmi(no_mesh_tet_${item},$i) 1
             } else {set rmi(no_mesh_tet_${item},$i) 0}
          if {[set no_mesh_hex_${item}_$i] == 0} {
             set rmi(no_mesh_hex_${item},$i) 1
             } else {set rmi(no_mesh_hex_${item},$i) 0}
          }
       }
    }
 if {$no_mesh_comb == 0} {
    set rmi(no_mesh_comb) 1
    } else {set rmi(no_mesh_comb) 0}
 if {$no_mesh_piston == 0} {
    set rmi(no_mesh_piston) 1
    } else {set rmi(no_mesh_piston) 0}
 mw_ic3m_ComputeMesh
}

###===================================================================================

proc onic3mACFluxStationsDEZLoad {} {
 global rmi
 global createstatus
 
 if ![info exists rmi(ic3m_ac_flux_station_no)] {set rmi(ic3m_ac_flux_station_no) 5}
 set ic3m_ac_flux_station_no  $rmi(ic3m_ac_flux_station_no)
 
 set createstatus [mw_ic3m_delete_lift_profile]
 mw_ic3m_ACFluXFillStationLine

}
###===================================================================================
proc onic3mACFluxStationsDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mACFluxStationsDEZ cancel}

}

###===================================================================================

proc onic3mACFluxStationsDEZOK {} {
 global rmi
 
 set rmi(ic3m_ac_flux_station_no) $ic3m_ac_flux_station_no
 
 mw_ic3m_ACFluXFillStationLine
}

###===================================================================================

proc onic3mACFluxStationsDEZApply {} {
 global rmi
 
 set rmi(ic3m_ac_flux_station_no) $ic3m_ac_flux_station_no
 
 mw_ic3m_ACFluXFillStationLine
}

###===================================================================================

proc onic3mMeshPreviewDEZLoad {} {
 global rmi ic3m
 global createstatus

 set starting_angle_dca $rmi(starting_angle_dca)
 set ending_angle_dca $rmi(ending_angle_dca)
 set increment_angle_dca $rmi(increment_angle_dca)
 set rmi(ac_flux_incr_list) ""
 for {set j 0} {$j < [expr [llength $rmi(ac_flux_station_list)]-1]} {incr j} {
    lappend rmi(ac_flux_incr_list) $ic3m(station_incr,$j)
    }
 set incr_list $rmi(ac_flux_incr_list)

 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mMeshPreviewDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mMeshPreviewDEZ cancel}

}

###===================================================================================

proc onic3mMeshPreviewDEZOK {} {
 global rmi

 set rmi(starting_angle_dca) $starting_angle_dca
 set rmi(ending_angle_dca) $ending_angle_dca
 set rmi(increment_angle_dca) $increment_angle_dca
 if {[set ::AIEnv::DEZ::ic3mMeshPreviewDEZ::previewmeshacfluxVal] == 0} {
    mw_ic3m_MeshPreview preview starcd
    } else {
       set rmi(degrees_simulated_start) $rmi(starting_angle_dca)
       set rmi(degrees_simulated_stop) $rmi(ending_angle_dca)
       set rmi(ac_flux_incr_list) $incr_list
       mw_ic3m_ACFluXOutput preview_ac_flux
       }
#aie_after_open_mesh 1 0
}

###===================================================================================

proc onic3mMeshPreviewDEZApply {} {
 global rmi

 set rmi(starting_angle_dca) $starting_angle_dca
 set rmi(ending_angle_dca) $ending_angle_dca
 set rmi(increment_angle_dca) $increment_angle_dca
 if {[set ::AIEnv::DEZ::ic3mMeshPreviewDEZ::previewmeshacfluxVal] == 0} {
    mw_ic3m_MeshPreview preview starcd
    } else {
       set rmi(degrees_simulated_start) $rmi(starting_angle_dca)
       set rmi(degrees_simulated_stop) $rmi(ending_angle_dca)
       set rmi(ac_flux_incr_list) $incr_list
       mw_ic3m_ACFluXOutput preview_ac_flux
       }
#aie_after_open_mesh 1 0
}

###===================================================================================

proc onic3mCompressionRatioDEZLoad {} {
 global rmi
 global createstatus

 if [info exist rmi(cal_comp_ratio)] {
    set cal_comp_ratio $rmi(cal_comp_ratio)
    }

 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mCompressionRatioDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mCompressionRatioDEZ cancel}

}

###===================================================================================

proc onic3mCompressionRatioDEZApply {} {
 global rmi

 if {[set ::AIEnv::DEZ::ic3mCompressionRatioDEZ::fixcompratioVal] == 1} {
    set rmi(fix_comp_ratio) $fix_comp_ratio
    mw_ic3m_CalculateCompressionRatio
    }
}

###===================================================================================

proc onic3mCompressionRatioDEZOK {} {
 global rmi

 if {[set ::AIEnv::DEZ::ic3mCompressionRatioDEZ::fixcompratioVal] == 1} {
    set rmi(fix_comp_ratio) $fix_comp_ratio
    mw_ic3m_CalculateCompressionRatio
    }
}

###===================================================================================

proc onic3mOutputDEZLoad {} {
 global rmi
 global createstatus

 set time_step_dca $rmi(time_step_dca)
 set time_step $rmi(time_step)
 set angle_dca $rmi(angle_dca)
 set cyl_length $rmi(cyl_length)
 set nlayers_cyl $rmi(nlayers_cyl)
 set number_processors $rmi(number_processors)
 set fix_cell_type 500

 set createstatus [mw_ic3m_delete_lift_profile]
}

proc onic3mOutputDEZAfterLoad {} {
  global createstatus

 if {!$createstatus} {ic3mOutputDEZ cancel}

}

###===================================================================================

proc onic3mOutputDEZOK {} {
 global rmi

 set rmi(time_step_dca) $time_step_dca
 set rmi(time_step) $time_step
 set rmi(angle_dca) $angle_dca
 set rmi(cyl_length) $cyl_length
 set rmi(nlayers_cyl) $nlayers_cyl
 set rmi(number_processors) $number_processors

 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 0} {
    set rmi(hpc) [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZprocessorVal]
    set rmi(UseForPlatform) [expr [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStartDEZprecisionVal]+1]
    set platform [list linux linux64 linux64_amd mips4 mips64 sol7 sol64 hp11 hp11_32 ibm41 ibm43 ibm64 alpha40]
    set rmi(Platform) [lindex $platform [set ::AIEnv::DEZ::ic3mOutputDEZ::platformVal]]

    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::fixStarVal] == 0} {
       set rmi(fix_cell_type) $fix_cell_type
       }

    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZmethodVal] == 0} {
       mw_ic3m_TransientMeshGui starcd
       }
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZmethodVal] == 1} {
       set rmi(UseCylinderLength) [set ::AIEnv::DEZ::ic3mOutputDEZ::cylinderStarVal]
       mw_ic3m_SteadyStateMesh starcd
       }
    }

 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 1} {
    set rmi(hpc) [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZprocessorVal]
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZmethodVal] == 0} {
       mw_ic3m_TransientMeshGui fluent
       }
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZmethodVal] == 1} {
       set rmi(UseCylinderLength) [set ::AIEnv::DEZ::ic3mOutputDEZ::cylinderFluentVal]
       mw_ic3m_SteadyStateMesh fluent
       }
    }
 
 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 2} {
    mw_ic3m_ACFluXOutput output_ac_flux
    }
}

###===================================================================================

proc onic3mOutputDEZApply {} {
 global rmi

 set rmi(time_step_dca) $time_step_dca
 set rmi(time_step) $time_step
 set rmi(angle_dca) $angle_dca
 set rmi(cyl_length) $cyl_length
 set rmi(nlayers_cyl) $nlayers_cyl
 set rmi(number_processors) $number_processors
 
 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 0} {
    set rmi(hpc) [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZprocessorVal]
    set rmi(UseForPlatform) [expr [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStartDEZprecisionVal]+1]
    set platform [list linux linux64 linux64_amd mips4 mips64 sol7 sol64 hp11 hp11_32 ibm41 ibm43 ibm64 alpha40]
    set rmi(Platform) [lindex $platform [set ::AIEnv::DEZ::ic3mOutputDEZ::platformVal]]

    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::fixStarVal] == 0} {
       set rmi(fix_cell_type) $fix_cell_type
       }

    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZmethodVal] == 0} {
       mw_ic3m_TransientMeshGui starcd
       }
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputStarDEZmethodVal] == 1} {
       set rmi(UseCylinderLength) [set ::AIEnv::DEZ::ic3mOutputDEZ::cylinderStarVal]
       mw_ic3m_SteadyStateMesh starcd
       }
    }

 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 1} {
    set rmi(hpc) [set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZprocessorVal]
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZmethodVal] == 0} {
       mw_ic3m_TransientMeshGui fluent
       }
    if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputFluentDEZmethodVal] == 1} {
       set rmi(UseCylinderLength) [set ::AIEnv::DEZ::ic3mOutputDEZ::cylinderFluentVal]
       mw_ic3m_SteadyStateMesh fluent
       }
    }

 if {[set ::AIEnv::DEZ::ic3mOutputDEZ::ic3mOutputDEZexportVal] == 2} {
    mw_ic3m_ACFluXOutput output_ac_flux
    }
}

###===================================================================================

proc mw_ic3m_delete_lift_profile {} {
 global file_name rmi bg_clr tdv_default is_win get_number_of_valves
 
 if ![info exists rmi(configuration)] {set rmi(configuration) ""}
 if {$rmi(configuration) == ""  || ![file exists $rmi(configuration)/ic3m_input]} {
    set olddir [pwd]
    set newdir [tk_chooseDirectory  -title "Project directory"  -initialdir $olddir]
    if {$newdir == ""} {return 0}
    if $is_win {
       set rmi(configuration) [win_shortname $newdir]
       } else {
          set rmi(configuration) $newdir
          }
    set rmi(ConfigurationSelected) 1
    set file_name(project_dir) $rmi(configuration)
    if {![info exists file_name(settings)] || $file_name(settings) == ""} {
       set prj_file "$rmi(configuration)/ic3m.prj"
       set file_name(settings) $prj_file
       mess "Project file set to $prj_file\n" blue
    }
    }
 
 if {$rmi(configuration) != ""} {  
   if {![file exists $rmi(configuration)/ic3m_input]} {
      set answer [tk_messageBox -title "Project directory" -message "IC3M Setting Project does not exist.  Do you want to create one?" -type yesno -icon question]
      if {$answer == "no"} {return 0}
   }
   foreach dir "domains ic3m_input mesh stations transfer" { 
      if ![file exists $rmi(configuration)/$dir] {cmd_mkdir $rmi(configuration)/$dir}
      }
   }
 
 if {$get_number_of_valves == 0} {
    if [ic_geo_is_loaded] {
       set rmi(tetin_fn) [file tail $file_name(tetin)]
       set rmi(TetinSelected) 1
       mw_ic3m_GetNumberOfValves
       }
    }

 set d .v.f1.frame.p3.f1.frame.p4.f1.frame
 if [winfo exists $d.1] {destroy $d.1}
 set d .v.f1.frame.p3.f0.frame.view
 if [winfo exists $d.1] {destroy $d.1}

 foreach item {intake exhaust} {
    for {set i 1} {$i <= $rmi(${item}_number_valve) } { incr i } {
       set rmi(show_${item}_lift_profile,$i) 0
       }
    }
 mw_ic3m_draw_lift_profile
 return 1 
}

###===================================================================================
loadIC3M


