proc rmi_SetMeshParams { what } {
global rmi

  for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
    lappend listIntakeLayers $rmi(intake_valve_nlayers)
    lappend listIntakeTetraRefSize $rmi(tetra_ref_size_intake,$i)
    lappend listIntakePrismRelHeight $rmi(rel_prism_height_intake,$i)
    lappend listIntakePrismRatio $rmi(prism_ratio_intake,$i)
    lappend listIntakePrismNumLayers $rmi(n_prism_layers_intake,$i)
    lappend listIntakeHexNoMesh $rmi(no_mesh_hex_intake,$i)
    lappend listIntakeTetNoMesh $rmi(no_mesh_tet_intake,$i)
    lappend listIntakeUseGeoSet $rmi(use_geometr_set_intake,$i)
    lappend listIntakeDiagnostic $rmi(diagnostic_intake,$i)
    lappend listIntakeCellsInGap $rmi(n_cells_in_gap_intake,$i)
    lappend listIntakePrismSteps $rmi(n_prism_smoothing_steps_intake,$i)
    lappend listIntakePrismMaxAngle $rmi(max_prism_angle_intake,$i)
    lappend listIntakePrismSmoothRatio $rmi(prism_smooth_ratio_intake,$i)
    lappend listIntakeTetraSmoothRatio $rmi(tetra_smooth_ratio_intake,$i)
    lappend listIntakePrismTetraSmooth $rmi(prismtetra_smooth_ratio_intake,$i)
    lappend listIntakePrismTetraSteps $rmi(n_prismtetra_smooth_steps_intake,$i)
    lappend listIntakePrismSmoothSteps $rmi(n_prism_smooth_steps_intake,$i)
    lappend listIntakeTetraSmoothSteps $rmi(n_tetra_smooth_steps_intake,$i)
    lappend listIntakeCleanTetraMesh $rmi(clean_tetra_mesh_intake,$i)
    lappend listIntakeDiagDuplicate $rmi(duplicate_intake,$i)
    lappend listIntakeDiagSingle $rmi(single_intake,$i)
    lappend listIntakeDiagSingle_2 $rmi(single_2_intake,$i)
    lappend listIntakeDiagMultiple $rmi(multiple_intake,$i)
    lappend listIntakeDiagSingle_multiple $rmi(single_multiple_intake,$i)
    lappend listIntakeDiagUncovered $rmi(uncovered_intake,$i)
    lappend listIntakeDiagMissing_internal $rmi(missing_internal_intake,$i)
    lappend listIntakeDiagTriangle_box $rmi(triangle_box_intake,$i)
    lappend listIntakeDiagNmanvert $rmi(nmanvert_intake,$i)
    lappend listIntakeDiagVol_orient $rmi(vol_orient_intake,$i)
    lappend listIntakeDiagSurf_orient $rmi(surf_orient_intake,$i)
    lappend listIntakeDiagDisconnected_vert $rmi(disconnected_vert_intake,$i)
    lappend listIntakeDiagMetric $rmi(metric_intake,$i)
    lappend listIntakeHexaNSteps $rmi(n_smooth_hexa_intake,$i)
    lappend listIntakeLayersAtBnd $rmi(n_layers_at_bnd_intake,$i)
    lappend listIntakeComputeTetinValve $rmi(compute_valve_intake,$i)
    lappend listIntakeComputeTetinPort $rmi(compute_port_intake,$i)
    lappend listIntakeComputeTetinPortExt $rmi(compute_ext_port_intake,$i)
    lappend listIntakeOutputMeshValve $rmi(output_mesh_valve_intake,$i)
    lappend listIntakeOutputMeshPort $rmi(output_mesh_port_intake,$i)
    lappend listIntakeLiftFiles $rmi(intake_lift_fn,$i)
    lappend listIntakeMaxLift $rmi(intake_max_lift,$i)
    lappend listIntakeLayersNextTip $rmi(n_layers_next_tip_intake,$i)
    lappend listIntakeLayersValveSeat $rmi(n_layers_valve_seat_intake,$i)
    lappend listIntakeNLayersValveClosure $rmi(n_layers_valve_closure_intake,$i)
    lappend listIntakeValueLayersAtLift $rmi(value_layers_at_lift_intake,$i)
    lappend listIntakeNLayersAtLift $rmi(n_layers_at_lift_intake,$i)
    lappend listIntakeExcludePortValve $rmi(exclude_port_valve_closure_intake,$i)
    lappend listIntakeEdge1 $rmi(edge1_intake,$i)
    lappend listIntakeEdge2 $rmi(edge2_intake,$i)
    lappend listIntakeEdge3 $rmi(edge3_intake,$i)
    lappend listIntakeEdge4 $rmi(edge4_intake,$i)
    lappend listIntakeEdge5 $rmi(edge5_intake,$i)
    lappend listIntakeEdge10 $rmi(edge10_intake,$i)
    lappend listIntakeEdge11 $rmi(edge11_intake,$i)
    lappend listIntakeEdge13 $rmi(edge13_intake,$i)
    lappend listIntakeEdge14 $rmi(edge14_intake,$i)
    lappend listIntakeEdge15 $rmi(edge15_intake,$i)
    lappend listIntakeEdgeCrit $rmi(edge_criterion_intake,$i)
    
    if $rmi(IntakeTetraViolateGeom,$i) {
      lappend listIntakeTetraViolateGeom $rmi(tetra_violate_geom_intake,$i)
    } else {
      lappend listIntakeTetraViolateGeom 0.0
    }
    if $rmi(IntakeNLayerBelow,$i) {
      lappend listIntakeNLayerBelow $rmi(n_layer_below_valve_intake,$i)
    } else {
      lappend listIntakeNLayerBelow 0
    }
    if $rmi(IntakeMaxHeightLayerBelow,$i) {
      lappend listIntakeMaxHeightLayerBelow $rmi(max_height_below_valve_intake,$i)
    } else {
      lappend listIntakeMaxHeightLayerBelow 0.0
    }
    if $rmi(IntakeNLayerTip,$i) {
      lappend listIntakeNLayerTip $rmi(n_layer_tip_valve_intake,$i)
    } else {
      lappend listIntakeNLayerTip 0
    }
    if $rmi(IntakeNCellsCircum,$i) {
      lappend listIntakeNCellsCircum $rmi(n_cells_circumfrential_intake,$i)
    } else {
      lappend listIntakeNCellsCircum 0
    }
    if $rmi(IntakeVBDY,$i) {
      lappend listIntakeVBDYH $rmi(vbdyh_intake,$i)
      lappend listIntakeVBDYNUM $rmi(vbdynum_intake,$i)
    } else {
      lappend listIntakeVBDYH -1
      lappend listIntakeVBDYNUM -1
    }
  }   

  for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
    lappend listExhaustLayers $rmi(exhaust_valve_nlayers)
    lappend listExhaustTetraRefSize $rmi(tetra_ref_size_exhaust,$i)
    lappend listExhaustPrismRelHeight $rmi(rel_prism_height_exhaust,$i)
    lappend listExhaustPrismRatio $rmi(prism_ratio_exhaust,$i)
    lappend listExhaustPrismNumLayers $rmi(n_prism_layers_exhaust,$i)
    lappend listExhaustHexNoMesh $rmi(no_mesh_hex_exhaust,$i)
    lappend listExhaustTetNoMesh $rmi(no_mesh_tet_exhaust,$i)
    lappend listExhaustUseGeoSet $rmi(use_geometr_set_exhaust,$i)
    lappend listExhaustDiagnostic $rmi(diagnostic_exhaust,$i)
    lappend listExhaustCellsInGap $rmi(n_cells_in_gap_exhaust,$i)
    lappend listExhaustPrismSteps $rmi(n_prism_smoothing_steps_exhaust,$i)
    lappend listExhaustPrismMaxAngle $rmi(max_prism_angle_exhaust,$i)
    lappend listExhaustPrismSmoothRatio $rmi(prism_smooth_ratio_exhaust,$i)
    lappend listExhaustTetraSmoothRatio $rmi(tetra_smooth_ratio_exhaust,$i)
    lappend listExhaustPrismTetraSmooth $rmi(prismtetra_smooth_ratio_exhaust,$i)
    lappend listExhaustPrismTetraSteps $rmi(n_prismtetra_smooth_steps_exhaust,$i)
    lappend listExhaustPrismSmoothSteps $rmi(n_prism_smooth_steps_exhaust,$i)
    lappend listExhaustTetraSmoothSteps $rmi(n_tetra_smooth_steps_exhaust,$i)
    lappend listExhaustCleanTetraMesh $rmi(clean_tetra_mesh_exhaust,$i)
    lappend listExhaustDiagDuplicate $rmi(duplicate_exhaust,$i)
    lappend listExhaustDiagSingle $rmi(single_exhaust,$i)
    lappend listExhaustDiagSingle_2 $rmi(single_2_exhaust,$i)
    lappend listExhaustDiagMultiple $rmi(multiple_exhaust,$i)
    lappend listExhaustDiagSingle_multiple $rmi(single_multiple_exhaust,$i)
    lappend listExhaustDiagUncovered $rmi(uncovered_exhaust,$i)
    lappend listExhaustDiagMissing_internal $rmi(missing_internal_exhaust,$i)
    lappend listExhaustDiagTriangle_box $rmi(triangle_box_exhaust,$i)
    lappend listExhaustDiagNmanvert $rmi(nmanvert_exhaust,$i)
    lappend listExhaustDiagVol_orient $rmi(vol_orient_exhaust,$i)
    lappend listExhaustDiagSurf_orient $rmi(surf_orient_exhaust,$i)
    lappend listExhaustDiagDisconnected_vert $rmi(disconnected_vert_exhaust,$i)
    lappend listExhaustDiagMetric $rmi(metric_exhaust,$i)
    lappend listExhaustHexaNSteps $rmi(n_smooth_hexa_exhaust,$i)
    lappend listExhaustLayersAtBnd $rmi(n_layers_at_bnd_exhaust,$i)
    lappend listExhaustComputeTetinValve $rmi(compute_valve_exhaust,$i)
    lappend listExhaustComputeTetinPort $rmi(compute_port_exhaust,$i)
    lappend listExhaustComputeTetinPortExt $rmi(compute_ext_port_exhaust,$i)
    lappend listExhaustOutputMeshValve $rmi(output_mesh_valve_exhaust,$i)
    lappend listExhaustOutputMeshPort $rmi(output_mesh_port_exhaust,$i)
    lappend listExhaustLiftFiles $rmi(exhaust_lift_fn,$i)
    lappend listExhaustMaxLift $rmi(exhaust_max_lift,$i)
    lappend listExhaustLayersNextTip $rmi(n_layers_next_tip_exhaust,$i)
    lappend listExhaustLayersValveSeat $rmi(n_layers_valve_seat_exhaust,$i)
    lappend listExhaustNLayersValveClosure $rmi(n_layers_valve_closure_exhaust,$i)
    lappend listExhaustValueLayersAtLift $rmi(value_layers_at_lift_exhaust,$i)
    lappend listExhaustNLayersAtLift $rmi(n_layers_at_lift_exhaust,$i)
    lappend listExhaustExcludePortValve $rmi(exclude_port_valve_closure_exhaust,$i)
    lappend listExhaustEdge1 $rmi(edge1_exhaust,$i)
    lappend listExhaustEdge2 $rmi(edge2_exhaust,$i)
    lappend listExhaustEdge3 $rmi(edge3_exhaust,$i)
    lappend listExhaustEdge4 $rmi(edge4_exhaust,$i)
    lappend listExhaustEdge5 $rmi(edge5_exhaust,$i)
    lappend listExhaustEdge10 $rmi(edge10_exhaust,$i)
    lappend listExhaustEdge11 $rmi(edge11_exhaust,$i)        
    lappend listExhaustEdge13 $rmi(edge13_exhaust,$i)
    lappend listExhaustEdge14 $rmi(edge14_exhaust,$i)
    lappend listExhaustEdge15 $rmi(edge15_exhaust,$i)
    lappend listExhaustEdgeCrit $rmi(edge_criterion_exhaust,$i)
        
    if $rmi(ExhaustTetraViolateGeom,$i) {
      lappend listExhaustTetraViolateGeom $rmi(tetra_violate_geom_exhaust,$i)
    } else {
      lappend listExhaustTetraViolateGeom 0.0
    }
    if $rmi(ExhaustNLayerBelow,$i) {
      lappend listExhaustNLayerBelow $rmi(n_layer_below_valve_exhaust,$i)
    } else {
      lappend listExhaustNLayerBelow 0
    }
    if $rmi(ExhaustMaxHeightLayerBelow,$i) {
      lappend listExhaustMaxHeightLayerBelow $rmi(max_height_below_valve_exhaust,$i)
    } else {
      lappend listExhaustMaxHeightLayerBelow 0.0
    }
    if $rmi(ExhaustNLayerTip,$i) {
      lappend listExhaustNLayerTip $rmi(n_layer_tip_valve_exhaust,$i)
    } else {
      lappend listExhaustNLayerTip 0
    }
    if $rmi(ExhaustNCellsCircum,$i) {
      lappend listExhaustNCellsCircum $rmi(n_cells_circumfrential_exhaust,$i)
    } else {
      lappend listExhaustNCellsCircum 0
    }
    if $rmi(ExhaustVBDY,$i) {
      lappend listExhaustVBDYH $rmi(vbdyh_exhaust,$i)
      lappend listExhaustVBDYNUM $rmi(vbdynum_exhaust,$i)
    } else {
      lappend listExhaustVBDYH -1
      lappend listExhaustVBDYNUM -1
    }
  }
  
  if !$rmi(CombTetraViolateGeom) {
    set rmi(tetra_violate_geom_comb) 0.0
  }  
  if !$rmi(PistonTetraViolateGeom) {
    set rmi(tetra_violate_geom_piston) 0.0
  }
  
  ic_rmi_set_params $what \
  	$rmi(scale_factor) \
	$rmi(configuration) \
    	$rmi(tetin_fn) \
    	$rmi(bore_diameter) \
    	$rmi(stroke_length) \
    	$rmi(connecting_rod_length) \
    	$rmi(piston_TDC_position) \
    	$rmi(intake_number_valve) \
    	$rmi(exhaust_number_valve) \
    	$rmi(valves_intake_fn) \
    	$rmi(valves_exhaust_fn) \
    	$listIntakeLiftFiles \
    	$listExhaustLiftFiles \
    	$rmi(intake_lift_factor) \
    	$rmi(exhaust_lift_factor) \
   	$rmi(intake_min_lift) \
    	$rmi(exhaust_min_lift) \
    	$listIntakeMaxLift \
    	$listExhaustMaxLift \
    	$rmi(engine_RPM) \
    	$rmi(degrees_simulated_start) \
    	$rmi(degrees_simulated_stop) \
    	$rmi(hpc) \
    	$rmi(number_processors) \
    	$rmi(casename) \
    	$listIntakeLayers \
    	$listExhaustLayers \
    	$rmi(number_layers_cyl_TDC) \
    	$rmi(number_layers_cyl_BDC) \
    	$rmi(bdc_layer_thickness) \
    	$rmi(edge_criterion_cmb) \
    	$rmi(display_logfile) \
 	$listIntakeHexNoMesh \
 	$listExhaustHexNoMesh \
 	$listIntakeTetNoMesh \
 	$listExhaustTetNoMesh \
    	$listIntakeTetraRefSize \
    	$listExhaustTetraRefSize \
    	$rmi(tetra_ref_size_comb) \
    	$rmi(tetra_ref_size_piston) \
    	$listIntakePrismRelHeight \
    	$listExhaustPrismRelHeight \
    	$rmi(rel_prism_height_comb) \
    	$rmi(rel_prism_height_piston) \
    	$listIntakePrismRatio \
    	$listExhaustPrismRatio \
    	$rmi(prism_ratio_comb) \
    	$rmi(prism_ratio_piston) \
    	$listIntakePrismNumLayers \
    	$listExhaustPrismNumLayers \
    	$rmi(n_prism_layers_comb) \
    	$rmi(n_prism_layers_piston) \
 	$rmi(no_mesh_comb) \
 	$rmi(no_mesh_piston) \
 	$listIntakeUseGeoSet \
 	$listExhaustUseGeoSet \
 	$rmi(use_geometr_set_comb) \
 	$rmi(use_geometr_set_piston) \
 	$listIntakeDiagnostic \
 	$listExhaustDiagnostic \
 	$rmi(diagnostic_comb) \
 	$rmi(diagnostic_piston) \
 	$listIntakeCellsInGap \
 	$listExhaustCellsInGap \
 	$rmi(n_cells_in_gap_comb) \
 	$rmi(n_cells_in_gap_piston) \
 	$listIntakePrismSteps \
 	$listExhaustPrismSteps \
 	$rmi(n_prism_smoothing_steps_comb) \
 	$rmi(n_prism_smoothing_steps_piston) \
 	$listIntakePrismMaxAngle \
 	$listExhaustPrismMaxAngle \
 	$rmi(max_prism_angle_comb) \
 	$rmi(max_prism_angle_piston) \
 	$listIntakePrismSmoothRatio \
 	$listExhaustPrismSmoothRatio \
 	$rmi(prism_smooth_ratio_comb) \
 	$rmi(prism_smooth_ratio_piston) \
 	$listIntakeTetraSmoothRatio \
 	$listExhaustTetraSmoothRatio \
 	$rmi(tetra_smooth_ratio_comb) \
 	$rmi(tetra_smooth_ratio_piston) \
 	$listIntakePrismTetraSmooth \
 	$listExhaustPrismTetraSmooth \
 	$rmi(prismtetra_smooth_ratio_comb) \
 	$rmi(prismtetra_smooth_ratio_piston) \
 	$listIntakePrismTetraSteps \
 	$listExhaustPrismTetraSteps \
 	$rmi(n_prismtetra_smooth_steps_comb) \
 	$rmi(n_prismtetra_smooth_steps_piston) \
 	$listIntakePrismSmoothSteps \
 	$listExhaustPrismSmoothSteps \
 	$rmi(n_prism_smooth_steps_comb) \
 	$rmi(n_prism_smooth_steps_piston) \
 	$listIntakeTetraSmoothSteps \
 	$listExhaustTetraSmoothSteps \
 	$rmi(n_tetra_smooth_steps_comb) \
 	$rmi(n_tetra_smooth_steps_piston) \
 	$rmi(clean_geometry_comb) \
 	$listIntakeCleanTetraMesh \
 	$listExhaustCleanTetraMesh \
 	$rmi(clean_tetra_mesh_comb) \
 	$rmi(clean_tetra_mesh_piston) \
	$rmi(intake_motion) \
	$rmi(exhaust_motion) \
	$rmi(ask_valve_angle) \
	$listIntakeTetraViolateGeom \
	$listExhaustTetraViolateGeom \
	$rmi(tetra_violate_geom_comb) \
	$rmi(tetra_violate_geom_piston) \
	$listIntakeDiagDuplicate \
        $listIntakeDiagSingle \
        $listIntakeDiagSingle_2 \
        $listIntakeDiagMultiple \
        $listIntakeDiagSingle_multiple \
        $listIntakeDiagUncovered \
        $listIntakeDiagMissing_internal \
        $listIntakeDiagTriangle_box \
        $listIntakeDiagNmanvert \
        $listIntakeDiagVol_orient \
        $listIntakeDiagSurf_orient \
        $listIntakeDiagDisconnected_vert \
        $listIntakeDiagMetric \
        $listExhaustDiagDuplicate \
        $listExhaustDiagSingle \
        $listExhaustDiagSingle_2 \
        $listExhaustDiagMultiple \
        $listExhaustDiagSingle_multiple \
        $listExhaustDiagUncovered \
        $listExhaustDiagMissing_internal \
        $listExhaustDiagTriangle_box \
        $listExhaustDiagNmanvert \
        $listExhaustDiagVol_orient \
        $listExhaustDiagSurf_orient \
        $listExhaustDiagDisconnected_vert \
        $listExhaustDiagMetric \
        $rmi(duplicate_comb) \
        $rmi(single_comb) \
        $rmi(single_2_comb) \
        $rmi(multiple_comb) \
        $rmi(single_multiple_comb) \
        $rmi(uncovered_comb) \
        $rmi(missing_internal_comb) \
        $rmi(triangle_box_comb) \
        $rmi(nmanvert_comb) \
        $rmi(vol_orient_comb) \
        $rmi(surf_orient_comb) \
        $rmi(disconnected_vert_comb) \
        $rmi(metric_comb) \
        $rmi(duplicate_piston) \
        $rmi(single_piston) \
        $rmi(single_2_piston) \
        $rmi(multiple_piston) \
        $rmi(single_multiple_piston) \
        $rmi(uncovered_piston) \
        $rmi(missing_internal_piston) \
        $rmi(triangle_box_piston) \
        $rmi(nmanvert_piston) \
        $rmi(vol_orient_piston) \
        $rmi(surf_orient_piston) \
        $rmi(disconnected_vert_piston) \
        $rmi(metric_piston) \
        $rmi(extrude_comb) \
        $listIntakeHexaNSteps \
        $listExhaustHexaNSteps \
        $rmi(piston_at_tdc) \
        $listIntakeNLayerBelow \
        $listExhaustNLayerBelow \
        $listIntakeMaxHeightLayerBelow \
        $listExhaustMaxHeightLayerBelow \
        $listIntakeNLayerTip \
        $listExhaustNLayerTip \
        $listIntakeNCellsCircum \
        $listExhaustNCellsCircum \
        $listIntakeLayersAtBnd \
        $listExhaustLayersAtBnd \
        $listIntakeComputeTetinValve \
        $listExhaustComputeTetinValve \
        $listIntakeComputeTetinPort \
        $listExhaustComputeTetinPort \
        $listIntakeComputeTetinPortExt \
        $listExhaustComputeTetinPortExt \
        $rmi(compute_comb) \
        $rmi(compute_piston) \
        $listIntakeOutputMeshValve \
        $listExhaustOutputMeshValve \
        $listIntakeOutputMeshPort \
        $listExhaustOutputMeshPort \
        $rmi(output_mesh_comb) \
        $rmi(output_mesh_piston) \
        $rmi(n_prism_layers_on_curtain_comb) \
        $rmi(hgt_prism_layers_on_curtain_comb) \
        $listIntakeLayersNextTip \
        $listExhaustLayersNextTip \
        $listIntakeLayersValveSeat \
        $listExhaustLayersValveSeat \
        $listIntakeNLayersValveClosure \
        $listExhaustNLayersValveClosure \
        $listIntakeValueLayersAtLift \
        $listExhaustValueLayersAtLift \
        $listIntakeNLayersAtLift \
        $listExhaustNLayersAtLift \
        $listIntakeExcludePortValve \
        $listExhaustExcludePortValve \
        $listIntakeEdge1 \
        $listExhaustEdge1 \
        $listIntakeEdge2 \
        $listExhaustEdge2 \
        $listIntakeEdge3 \
        $listExhaustEdge3 \
        $listIntakeEdge4 \
        $listExhaustEdge4 \
        $listIntakeEdge5 \
        $listExhaustEdge5 \
        $listIntakeEdge10 \
        $listExhaustEdge10 \
        $listIntakeEdge11 \
        $listExhaustEdge11 \
        $listIntakeEdge13 \
        $listExhaustEdge13 \
        $listIntakeEdge14 \
        $listExhaustEdge14 \
        $listIntakeEdge15 \
        $listExhaustEdge15 \
        $listIntakeVBDYH \
        $listExhaustVBDYH \
        $listIntakeVBDYNUM \
        $listExhaustVBDYNUM \
        $listIntakeEdgeCrit \
        $listExhaustEdgeCrit \
        $rmi(time_step)
  
}

proc rmi_AppGeometryPreview {} {
global rmi
  
  set n [ rmi_SetMeshParams 1 ]
  rmi_IconifyGui 1
  set ret [ ic_rmi_geometry_preview $rmi(chadice) $rmi(geo_starting_angle_dca) \
               $rmi(geo_ending_angle_dca) $rmi(geo_increment_angle_dca) ]
  rmi_IconifyGui 0
}

proc rmi_AppRunBatch {} {
global rmi  
  
  set error 0
  set error [ic_rmi_chad_batch ./chad_ice.param ./chad_ice.node]
  if $error {
    rmi_ShowMessage "ERROR: ...\n"
    return 1
  } else {
    return 0
  }
}

proc rmi_AppMeshPreview {} {
global rmi
  
  set n [ rmi_SetMeshParams 1 ]
  rmi_IconifyGui 1
  set ret [ ic_rmi_mesh_preview $rmi(chadice) $rmi(starting_angle_dca) \
               $rmi(ending_angle_dca) $rmi(increment_angle_dca) ]
  rmi_IconifyGui 0
}

proc rmi_clear_display {} {
  global fam_visible
  foreach f [array names fam_visible] {
    if $fam_visible($f) {
      ic_visible family $f 0
    }
  }
}

proc rmi_display_all_fam {} {
  global fam_visible
  foreach f [array names fam_visible] {
    if !$fam_visible($f) { 
      ic_visible family $f 1
    }
  }
}

proc rmi_ComputeMesh {} {
global rmi file_name batch_mode

  busy 1  
  set stuff [ glob -nocomplain logfile.* ]
  foreach item $stuff { set temp($item) 1 }
  if $rmi(chadice) {
    rmi_ShowMessage "Compute static mesh ...\n"
  } else {
    rmi_ShowMessage "Compute mesh ...\n"
  }
  set domains_dir $rmi(configuration)/domains
  if { ! [ file exists $domains_dir ] } {
    mess "Creating directory $domains_dir\n"
    ic_mkdir $domains_dir
  }
  set n [ rmi_SetMeshParams 1 ]
  if !$batch_mode {rmi_IconifyGui 1}
  if ![info exists file_name(tetin)] { set file_name(tetin) "" }
  set rmi(MeshValid) 0
  ic_undo_stop
  if {$file_name(tetin) != ""} {
    if $rmi(chadice) {
      ic_rmi_compute_stations 1 0
    } else {
      set tf $file_name(tetin)
      set n [ ic_rmi_compute_mesh $tf 1 $rmi(chadice) ]
    }
  } else {
    if $rmi(chadice) {
      ic_rmi_compute_stations 1 0
    } else {
      set n [ ic_rmi_compute_mesh "" 1 $rmi(chadice) ]
    }
  }
  if { $n == 0 } {
    set rmi(MeshValid) 1
  }    
  if !$batch_mode {rmi_IconifyGui 0}
  set stuff [ glob -nocomplain logfile.* ]
  foreach item $stuff { 
    if ![info exists temp($item)] {
      cmd_rm $item
    }
  }  
  ic_undo_start
  busy 0
}

proc rmi_ComputeGeometry {} {
global rmi file_name batch_mode

  busy 1
  if $rmi(chadice) {
    rmi_ShowMessage "Compute static geometry ...\n"
  } else {
    rmi_ShowMessage "Compute geometry ...\n"
  }
  set domains_dir $rmi(configuration)/domains
  if { ! [ file exists $domains_dir ] } {
    mess "Creating directory $domains_dir\n"
    ic_mkdir $domains_dir
  }
  set hexa_dir "$rmi(configuration)/../hexa"
  if { ! [ file exists $hexa_dir ] } {
    mess "Creating directory $hexa_dir\n"
    ic_mkdir $hexa_dir
  }
  set n [ rmi_SetMeshParams 1 ]
  if !$batch_mode {rmi_IconifyGui 1}
  if ![info exists file_name(tetin)] { set file_name(tetin) "" }
  ic_undo_stop
  if {$file_name(tetin) != ""} {
    set tf $file_name(tetin)
    if $rmi(chadice) {
      ic_rmi_compute_stations 0 0
    } else {
      set n [ ic_rmi_compute_mesh $tf 0 $rmi(chadice) ]
    }
  } else {
    if $rmi(chadice) {
      ic_rmi_compute_stations 0 0
    } else {
      set n [ ic_rmi_compute_mesh "" 0 $rmi(chadice) ]
    }
  }
  if !$batch_mode {rmi_IconifyGui 0}
  ic_undo_start
  busy 0
}

proc rmi_SortList { one two } {
  set diff [ expr [ lindex $one 0 ] - [ lindex $two 0 ] ]
  set ret 0
  if { $diff < 0 } {
    set ret -1
  } elseif { $diff > 0 } {
    set ret 1
  }
  return $ret
}

proc rmi_ACFluXOutput {what} {
  global rmi
  set len [llength $rmi(ac_flux_station_list)]
  if { $len > 0 } {
    set list1 ""
    set list2 ""
    set i 0
    foreach num $rmi(ac_flux_station_list) {
# check if station file exists
      if { $num == "" } continue
      if { ![string is double $num] && ![string is integer $num] } continue
      lappend list1 $num
      incr i
    }
    set rmi(n_ac_flux_stations) $i
    set i 1
    foreach num $rmi(ac_flux_incr_list) {
# check if station file exists
      if { $num == "" } continue
      if { ![string is double $num] && ![string is integer $num] } continue
      lappend list2 $num
      incr i
      if { $i > $rmi(n_ac_flux_stations) } break
    }
    for { set j $i } { $j <= $rmi(n_ac_flux_stations) } { incr j } {
      lappend list2 2
    }
    set comb_list ""
    foreach v1 $list1 v2 $list2 {
      set inner_list ""
      lappend inner_list $v1
      lappend inner_list $v2
      lappend comb_list $inner_list
    }
    set sort_list [ lsort -command rmi_SortList $comb_list ]
    set list1 ""
    set list2 ""
    set i 1
    foreach v $sort_list {
      set rmi(ac_flux_station_angle,$i) [ lindex $v 0 ]
      set rmi(ac_flux_station_incr,$i)  [ lindex $v 1 ]
      incr i
    }
    set rmi(n_ac_flux_stations) [ expr $i - 1 ]
    rmi_OutputMesh $what ""
  }
}



proc rmi_ACFluXFillStationLine {} {
  global rmi

  set output ""
  if ![info exists rmi(configuration)] return
  set path $rmi(configuration)/stations
  set prefix "ac_flux_station_"
  foreach file [glob -nocomplain -types f $path/*] {
#   if ![file isfile $file] continue
    if { [string match "*$prefix*" $file] } {
      set idx [ string first $prefix $file 0 ]
      if { $idx >= 0 } {
        set len [ string length $prefix ]
        set idx [ expr $idx + $len ]
        set num [ string range $file $idx end ]
        if { $num == "" } continue
        if { ![string is double $num] && ![string is integer $num] } continue
        lappend output "$num "
      }
    }
  }
  set sort_list [ lsort -real $output ]
  set output ""
  foreach v $sort_list {
    append output "$v " 
  }
  set rmi(ac_flux_station_list) $output
  return
}

proc rmi_ACFluXDeleteStation {} {
  global rmi

  if { ![info exists rmi(ac_flux_station_angle_dca)] ||
       ![info exists rmi(configuration)] } return
  set num $rmi(ac_flux_station_angle_dca)
  if { $num == "" } return
  if { ![string is double $num] && ![string is integer $num] } return
  set angle_dca $num
  set add_string "[format %.3lf $angle_dca]"
  set file $rmi(configuration)/stations/ac_flux_station_$add_string
  if [ file exists $file ] {
    catch { file delete -force $file }
    set file $rmi(configuration)/stations/ac_flux_station_param_$add_string
    if [ file exists $file ] {
      catch { file delete -force $file }
    }
    set file $rmi(configuration)/stations/ac_flux_domain$add_string
    if [ file exists $file ] {
      catch { file delete -force $file }
    }
    set file $rmi(configuration)/stations/ac_flux_boco$add_string
    if [ file exists $file ] {
      catch { file delete -force $file }
    }
    set file $rmi(configuration)/transfer/ac_flux_output$add_string
    if [ file exists $file ] {
      catch { file delete -force $file }
    }
    set file $rmi(configuration)/transfer/ac_flux_output_add$add_string
    if [ file exists $file ] {
      catch { file delete -force $file }
    }
    mess "Station $angle_dca deleted\n"
  }
}


proc rmi_ACFluXCopyStationBocoFiles {bocofiles angle} {
  if $bocofiles == "" return
  set add_string "[format %.3lf $angle_dca]
  foreach file $bocofiles {
    if ![file isfile $file] continue
    set f [file tail $file]
    cmd_cp $file_newxyz "$rmi(configuration)/stations/ac_flux_station_$f_$add_string"
  }
}


proc rmi_ACFluXCreateStation {} {
  global rmi
  if ![info exists rmi(ac_flux_station_angle_dca)] {
    rmi_ShowMessage "Please input a DCA\n"
    return
  }
  set num $rmi(ac_flux_station_angle_dca)
  if { $num == "" || (![string is double $num] && ![string is integer $num]) } {
    rmi_ShowMessage "Please input a DCA\n"
    return
  }
  if ![info exists rmi(range_ac_flux_station_angle_dca,1)] {
    rmi_ShowMessage "Please input a DCA for the range\n"
    return
  }
  set num $rmi(range_ac_flux_station_angle_dca,1)
  if { $num == "" || (![string is double $num] && ![string is integer $num]) } {
    rmi_ShowMessage "Please input a DCA for the range\n"
    return
  }
  if ![info exists rmi(range_ac_flux_station_angle_dca,2)] {
    rmi_ShowMessage "Please input a DCA for the range\n"
    return
  }
  set num $rmi(range_ac_flux_station_angle_dca,2)
  if { $num == "" || (![string is double $num] && ![string is integer $num]) } {
    rmi_ShowMessage "Please input a DCA for the range\n"
    return
  }
  if { $rmi(range_ac_flux_station_angle_dca,2) < $rmi(range_ac_flux_station_angle_dca,1) } {
    set tmp_r $rmi(range_ac_flux_station_angle_dca,2)
    set $rmi(range_ac_flux_station_angle_dca,2) $rmi(range_ac_flux_station_angle_dca,1)
    set $rmi(range_ac_flux_station_angle_dca,1) $tmp_r
  }
  rmi_OutputMesh create_station_ac_flux ""
}

proc rmi_OutputMesh {what output_to} {
global rmi env load_max_dim
global did_merge loaded_domains rmi_spl_fams rmi
global selmap file_name batch_mode tdv_default is_win

  set type_outp 0
  set stuff [ glob -nocomplain logfile.* ]
  foreach item $stuff { set temp($item) 1 }
  if { $what == "transient" } {
    set type_outp 1
    if {$output_to == "starcd"} {
      rmi_ShowMessage "Output to StarCD ...\n" blue
    } elseif {$output_to == "fluent"} {
      rmi_ShowMessage "Output to Fluent ...\n" blue
    }
    set merge_file $rmi(configuration)/domains/domain_rmi_merge
  } elseif { $what == "steady_state" } {
    set type_outp 2
    rmi_ShowMessage "Create steady state at angle $rmi(angle_dca) ...\n" blue
    set merge_file $rmi(configuration)/domains/domain_rmi_merge
  } elseif { $what == "preview" } {
    set type_outp 3
    rmi_ShowMessage "Create base mesh...\n"
    set merge_file $rmi(configuration)/domains/domain_rmi_merge
  } elseif { $what == "create_station_ac_flux" } {
    set type_outp 4
    rmi_ShowMessage "Create AC-Flux station at angle $rmi(ac_flux_station_angle_dca) ...\n"
    set merge_file $rmi(configuration)/domains/domain_rmi_merge_ac_flux
  } elseif { $what == "preview_ac_flux" } {
    if { $rmi(n_ac_flux_stations) > 0 } {
      set type_outp 5
      rmi_ShowMessage "Mesh preview AC-Flux\n"
      set add_string "[format %.3lf $rmi(ac_flux_station_angle,1)]"
      set merge_file $rmi(configuration)/stations/ac_flux_station_$add_string
    }
  } elseif { $what == "output_ac_flux" } {
    if { $rmi(n_ac_flux_stations) > 0 } {
      set type_outp 6
      rmi_ShowMessage "Output to AC-Flux\n"
      set add_string "[format %.3lf $rmi(ac_flux_station_angle,1)]"
      set merge_file $rmi(configuration)/stations/ac_flux_station_$add_string
    }
  }
  if { $type_outp == 0 } {
    return
  }
  set range1 0.0
  set range2 0.0
  set n 0
  if { $rmi(MeshValid) != 0 } {
    set n [ rmi_SetMeshParams 1 ]
    if { $n != 0 } {
      set rmi(MeshValid) 0
      rmi_ShowMessage "Parameters for mesh computation changed.\n"
      rmi_ShowMessage "Compute mesh before.\n"
    }
  } else {
    rmi_ShowMessage "No mesh has been computed in this session.\
    		     \nMesh might be different from current settings.\n"
  }
  set outp_intf 0
  if { ($type_outp == 1 || $type_outp == 2 || $type_outp == 4) } {
    set outp_intf 1
  }
  if { $n == 0 } {
    ic_undo_stop
    set loaded_display 0
    if ![info exists file_name(tetin)] { set file_name(tetin) "" }
    if {[ic_uns_is_loaded] || $file_name(tetin) != ""} {
      set loaded_display 1
    }
    ic_unload_mesh
    rmi_SetMeshParams 1
    if { $outp_intf || ![ file exists $merge_file ] } {
      set nValves [expr $rmi(intake_number_valve) + $rmi(exhaust_number_valve)]

      if { ![ file exists $merge_file ] } {
        # separate combustion dome from piston in domain file
        set file "$rmi(configuration)/domains/domain_comb_piston"
        if { [ file exists $file ] } {
          set ret [ ic_rmi_separate_domain_comb_piston $rmi(configuration) ]
        }
        # adjust families in combustion dome domain
        # (must be done if had been created by hand)
        set file "$rmi(configuration)/domains/domain_comb"
        if { [ file exists $file ] } {
          set ret [ ic_rmi_adjust_comb_families $file \
                     $rmi(intake_number_valve) $rmi(exhaust_number_valve) ]
        }
        ic_unload_mesh
      }
      set sel ""
      set bocofiles ""
      set found 1
      set check_file 1
      if { $outp_intf && !$rmi(output_mesh_comb) } {
        set check_file 0
      }
      set comb_piston 0
      if { $check_file } {
        set file "$rmi(configuration)/domains/domain_comb"
        if ![ file exists $file ] {
          rmi_ShowMessage "No $file found.\n"
          if { $type_outp != 4 } {
            set found 0 
          }
        } else {
          set sel [ list $file ]
          if [ file exists "$rmi(configuration)/mesh/fam_boco_tcomb_piston" ] {
            set bocofiles [ list $rmi(configuration)/mesh/fam_boco_tcomb_piston ]
            incr comb_piston
          } else {
            set bocofiles [ list $rmi(configuration)/mesh/fam_boco_tcomb ]
          }
        }
      }
      set check_file 1
      if { $outp_intf && !$rmi(output_mesh_piston) } {
        set check_file 0
      }
      if { $check_file } {
        set file "$rmi(configuration)/domains/domain_piston"
        if ![ file exists $file ] {
          rmi_ShowMessage "No $file found.\n"
        } else {
          lappend sel "${file}"
          if { ! $comb_piston } {
            if [ file exists "$rmi(configuration)/mesh/fam_boco_tcomb_piston" ] {
              set bocofiles [ list $rmi(configuration)/mesh/fam_boco_tcomb_piston ]
              incr comb_piston
            } else {
              lappend bocofiles $rmi(configuration)/mesh/fam_boco_piston
            }
          }
        }
      }
      for { set i 1 } { $i <= $nValves } { incr i } {
        set check_file 1
        if { $rmi(intake_number_valve) > 0 && \
             $i <= $rmi(intake_number_valve) } {
          set intake_v 1
          set i_v $i
        } else {
          set intake_v 0
          set i_v [expr $i - $rmi(intake_number_valve)]
        }
        if { ($intake_v && \
              (!$rmi(intake_motion) || \
               ($outp_intf && !$rmi(output_mesh_port_intake,$i_v)))) || \
             (!$intake_v && \
              (!$rmi(exhaust_motion) || \
               ($outp_intf && !$rmi(output_mesh_port_exhaust,$i_v)))) } {
          set check_file 0
        }
        if { $check_file } {
          set file "$rmi(configuration)/domains/domain_port${i}"
          if ![ file exists $file ] { 
            rmi_ShowMessage "No $file found.\n"
          } else {
            set cur_bocofile "$rmi(configuration)/mesh/fam_boco_tvalve${i}"
            set orig_bocofile $cur_bocofile
            set cur_file $file
            if { $type_outp == 4 } {
              set cur_file "${file}_ac_flux"
              set cur_bocofile "${orig_bocofile}_ac_flux"
# change S0 family to S0_x_$n_s0_change
              ic_rmi_change_s0_fam $file $orig_bocofile $cur_file \
                                   $cur_bocofile $i
            }
            lappend sel "$cur_file"
#           lappend bocofiles $cur_bocofile
            lappend bocofiles $orig_bocofile
          }
          set file "$rmi(configuration)/domains/domain_valve${i}"
          if ![ file exists $file ] { 
            rmi_ShowMessage "No $file found.\n"
            if { $type_outp != 4 } {
              set found 0 
            }
          } else {
            set cur_bocofile "$rmi(configuration)/mesh/fam_boco_hvalve${i}"
            set orig_bocofile $cur_bocofile
            set cur_file $file
            lappend sel "$cur_file"
#           lappend bocofiles $cur_bocofile
            lappend bocofiles $orig_bocofile
          }
        }
      }
      if !$found {
        rmi_ShowMessage "Compute mesh before.\n"
        ic_undo_start
        return
      }
      
      set outp_Para 0
      if { $outp_intf } {
        set outp_Para 1
      }
      # output lift curves and rammice parameters file
      set n [ ic_rmi_outp_star "$rmi(configuration)/domains/domain_piston" 1 $outp_Para ]
      if { $n != "0" } {
        ic_undo_start
        return
      }
      busy 1
      if { $outp_intf && [ file exists $merge_file ] } {
        set uns_mesh_ex 1
      } else {
        set uns 0
        foreach dom $sel {
          if [ file exists $dom ] {
            if !$batch_mode { rmi_ShowMessage "Loading file $dom ..." }
	    set uns [ic_load_unstruct [list $dom]]
            if !$batch_mode { rmi_ShowMessage " done.\n" }
          } else {
            rmi_ShowMessage "File $dom does not exist.\n"
          }
        }
        set uns_mesh_ex 0
        if $uns {
          set uns_mesh_ex 1
          ic_save_unstruct $merge_file
          initialize_unstruct_visibility
          disp_new_stuff    
          # substitute valve shells by vc1 shells
          ic_unload_mesh
          set ret_subst [ic_rmi_substitute_fam "$merge_file" $type_outp $rmi(chadice)]
          if { $type_outp == 4 } {
            ic_unload_mesh
            if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
            set uns [ic_load_unstruct [list $merge_file]]
            if !$batch_mode { rmi_ShowMessage " done.\n" }
# puts "vor ic_rmi_diag\n"
            set ok [ ic_rmi_diag diag_type uncovered fix "1" fix_fam CYLINDER_SIDE]
            set ok [ ic_rmi_diag diag_type standalone fix "1"]
            set nn [ ic_delete_disconnected_vertices ]
            ic_rmi_fam_delete_unused
            ic_save_unstruct $merge_file
          }
        }
      }
      if { $uns_mesh_ex && $outp_intf } {
        ic_unload_mesh

        if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
        set uns [ic_load_unstruct [list $merge_file]]
        if !$batch_mode { rmi_ShowMessage " done.\n" }

        if { $type_outp != 2 && $type_outp != 4 } {
          rmi_add_material_id $bocofiles
        }

        set ret [ rmi_set_dynamic_files2 "all" ]
        if { $ret < 0 } {
          busy 0
          ic_unload_mesh
          ic_undo_start
          return
        }

        if { $type_outp == 4 } {
          set angle_dca $rmi(ac_flux_station_angle_dca)
          set add_string "[format %.3lf $angle_dca]"
          set merge_file $rmi(configuration)/stations/ac_flux_station_$add_string
          set station_dir $rmi(configuration)/stations
          set range1 $rmi(range_ac_flux_station_angle_dca,1)
          set range2 $rmi(range_ac_flux_station_angle_dca,2)
          if { ! [ file exists $station_dir ] } {
            mess "Creating directory $station_dir\n"
            ic_mkdir $station_dir
          }
          set transfer_dir $rmi(configuration)/transfer
          if { ! [ file exists $transfer_dir ] } {
            mess "Creating directory $transfer_dir\n"
            ic_mkdir $transfer_dir
          }
        } else {
          set merge_file $rmi(configuration)/domains/domain_rmi_merge2
          set angle_dca $rmi(angle_dca)
        }
        rmi_ShowMessage "Prepare dynamic part.\n"
        set ret [ ic_rmi_dynamic_motion $type_outp \
                         $angle_dca $rmi(starting_angle_dca) \
                         $rmi(ending_angle_dca) $range1 $range2 \
                         $rmi(increment_angle_dca) $merge_file ]
        ic_unload_mesh

        if { $type_outp == 2 && $rmi(UseCylinderLength) } {
          set ret [ ic_rmi_extrude_cylbottom $merge_file \
                       $rmi(intake_number_valve) $rmi(exhaust_number_valve) \
                       $rmi(cyl_length) $rmi(nlayers_cyl) ]
          ic_unload_mesh
        }

        if { $type_outp == 2 && $rmi(hpc) && $rmi(number_processors) > 1 } {
          # partion merged domain file
          rmi_ShowMessage "Partition mesh.\n"
          set ret [ ic_rmi_partition_mesh $merge_file $merge_file ]
          ic_unload_mesh
        }
        if { $type_outp != 4} {
          set transfer_dir $rmi(configuration)/transfer
          if { ! [ file exists $transfer_dir ] } {
            mess "Creating directory $transfer_dir\n"
            ic_mkdir $transfer_dir
          }
          if { $type_outp != 2 } {
            if $is_win {
              set file_newxyz [win_shortname $env(ICEM_ACN)/icemcfd/newxyz/newxyz.o_$rmi(Platform)]
            } else {
              set file_newxyz "$env(ICEM_ACN)/icemcfd/newxyz/newxyz.o_$rmi(Platform)"
            }
            if { $rmi(UseForPlatform) == "2" } {
              set file_newxyz "${file_newxyz}_double"
            }
            if [file exists $file_newxyz] {
              rmi_ShowMessage "Copy newxyz.o for platform $rmi(Platform) to transfer directory.\n"
              cmd_cp $file_newxyz "$transfer_dir/newxyz.o"
            }
          }
          set param_file [ ic_rmi_get_param_file ]
          if {$param_file == ""} { rmi_ShowMessage "Cannot find param_file.\n" }      
          if $is_win {
            set tdir [win_shortname $env(ICEM_ACN)/icemcfd/output-interfaces]
          } else {
            set tdir $env(ICEM_ACN)/icemcfd/output-interfaces
          }
          set output_file "$transfer_dir/$rmi(casename)"
          rmi_ShowMessage "Output file(s) will be written to $output_file.\*.\n"
          if {$output_to == "starcd"} {
            set command "$tdir/starcd -dom $merge_file -sp -rmi $bocofiles $output_file"
            if { $rmi(hpc) && $rmi(number_processors) > 1 } {
              set comm_no $command
              set command "$comm_no -parti $rmi(configuration)/mesh/partition_info"
            }
          } elseif {$output_to == "fluent"} {
            ic_unload_mesh
            ic_load_unstruct $merge_file
            set ok [ ic_rmi_diag diag_type uncovered fix "1" fix_fam CYLINDER_SIDE]
            set ok [ ic_rmi_diag diag_type standalone fix "1"]
            ic_save_unstruct $merge_file
            set merged_bocofile $rmi(configuration)/rmi_merged_bocofile
            ic_rmi_merge_fambocos $bocofiles $merged_bocofile
            set command "$tdir/fluent6 -dom $merge_file -b $merged_bocofile $output_file"
          }
          set error 0
          if [ catch { eval exec $command } ret ] {
            set error 1
            rmi_ShowMessage "$ret\n" red
          } else {
            rmi_ShowMessage "$ret\n" blue
          }
          if $error {
            rmi_ShowMessage "There was an error running $command.\n" red
            ic_undo_start
            return          
          }
          if {$output_to == "starcd"} {
            if [rmi_splines_main $merge_file $output_file $param_file $type_outp] {
	      rmi_ShowMessage "Failed to create splines.\n"
            }
          }
        } else {
#      type_outp == 4
          if $is_win {                                                                      set tdir [win_shortname $env(ICEM_ACN)/icemcfd/output-interfaces]             } else {                                                                          set tdir $env(ICEM_ACN)/icemcfd/output-interfaces                             }
#      copy bocofiles to station directory
#         rmi_ACFluXCopyStationBocoFiles $bocofiles $angle_dca
          set add_string "[format %.3lf $angle_dca]"
          set dom_file "$rmi(configuration)/stations/ac_flux_domain_merged$add_string"
#
          if !$batch_mode { rmi_ShowMessage "Fix possible problems in $dom_file ..." }
          set uns [ic_load_unstruct [list $dom_file]]
          rmi_check_mesh_merged
          ic_save_unstruct $dom_file
          ic_unload_mesh
          if !$batch_mode { rmi_ShowMessage "Done ..." }
#
          set outp "$rmi(configuration)/transfer/ac_flux_output$add_string"
          set merged_bocofile "$rmi(configuration)/stations/ac_flux_boco$add_string"
          ic_rmi_merge_fambocos $bocofiles $merged_bocofile
          set used_bocofile "$rmi(configuration)/ac_flux.boco"
          if ![ file exists $used_bocofile ] {
            set used_bocofile $merged_bocofile
          }
          rmi_ShowMessage "Use boco file $used_bocofile.\n"
          set command "$tdir/gmtec -b $used_bocofile -dom $dom_file $outp"
          set error 0
          if [ catch { eval exec $command } ret ] {
            set error 1
            rmi_ShowMessage "$ret\n" red
          } else {
            rmi_ShowMessage "$ret\n" blue
          }
          if $error {
            rmi_ShowMessage "There was an error running $command.\n" red
            ic_undo_start
            return
          }
        }
      }
      if $uns_mesh_ex {
        ic_unload_mesh
        if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
	set uns [ic_load_unstruct [list $merge_file]]
        if !$batch_mode { rmi_ShowMessage " done.\n" }
        initialize_unstruct_visibility
      }
      if !$batch_mode {
        disp_new_stuff
        update_uns_disp
        update_project_mode
        $tdv_default configure -active 1
        $tdv_default redraw
      }
      busy 0
    } else {
      busy 1
      # output lift curves and ic3m parameters file
      set n [ ic_rmi_outp_star "$rmi(configuration)/domains/domain_piston" 1 0 ]
      if { $n != "0" } {
        ic_undo_start
        busy 0
        return
      }
      if { $type_outp < 5 } {
        if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
        set uns [ic_load_unstruct [list $merge_file]]
        if !$batch_mode { rmi_ShowMessage " done.\n" }
        if !$batch_mode {
          initialize_unstruct_visibility
          disp_new_stuff
          update_uns_disp
          update_project_mode
          $tdv_default configure -active 1
          $tdv_default redraw
        }
      }
      busy 0
    }
    if { $type_outp == 3 || $type_outp >= 5 } {
      if { $type_outp > 4 } {
#       set n_loop [ expr $rmi(n_ac_flux_stations) - 1]
        set n_loop $rmi(n_ac_flux_stations)
      } else {
        set n_loop 1
      }
      ic_undo_suspend
      for {set loop 1} {$loop <= $n_loop} {incr loop} {
        busy 1
  
        set subtr 0
        set station_angle_dca $rmi(angle_dca)
        # First make sure the subset menu is active.
        if { $type_outp == 3 } {
          ic_rmi_add_type 1
          set ret [ rmi_set_dynamic_files2 "all" ]
          if { $ret < 0 } {
            busy 0
            ic_unload_mesh
            ic_undo_start
            return
          }
          set station_angle_dca $rmi(angle_dca)
          set angle_dca $station_angle_dca
          set starting_angle_dca $rmi(starting_angle_dca)
          set ending_angle_dca $rmi(ending_angle_dca)
          set increment_angle_dca $rmi(increment_angle_dca)
        } else {
       # merge file is next ac-flux station file
          set next_loop [ expr $loop + 1 ]
          if { $loop < $n_loop } {
            if { $rmi(ac_flux_station_angle,$next_loop) <= $rmi(degrees_simulated_start) } {
              busy 0
              continue;
            }
          }
          set add_string "[format %.3lf $rmi(ac_flux_station_angle,$loop)]"
          set merge_file $rmi(configuration)/stations/ac_flux_station_$add_string
          set station_angle_dca $rmi(ac_flux_station_angle,$loop)
          set angle_dca $station_angle_dca
          set subtr 0
          set next_station 0
# puts "angle_dca $angle_dca $rmi(degrees_simulated_start)"
          if { $loop < $n_loop && $angle_dca < $rmi(degrees_simulated_start) } {
            while { $angle_dca < $rmi(degrees_simulated_start) } {
              set angle_dca [ expr $angle_dca + \
                                   $rmi(ac_flux_station_incr,$loop) ]
              if { $angle_dca >= $rmi(ac_flux_station_angle,$next_loop) } {
                set next_station 1
                break;
              }
            }
            if $next_station {
              busy 0
              continue;
            }
            set angle_dca $rmi(degrees_simulated_start)
          }
          set starting_angle_dca $angle_dca
          if { $loop < $n_loop } {
            set ending_angle_dca $rmi(ac_flux_station_angle,$next_loop)
            if {$type_outp != 6} {
              set subtr 1
            } else {
              set subtr 0
            }
            set internal_end [ expr $rmi(degrees_simulated_stop) - \
                               $rmi(ac_flux_station_incr,$loop) ]
# puts "internal_end $ending_angle_dca $internal_end"
            if { $ending_angle_dca > $internal_end } {
              set ending_angle_dca $rmi(degrees_simulated_stop)
              set subtr 0
            }
          } else {
# puts "loop $loop $n_loop"
            set ending_angle_dca $rmi(degrees_simulated_stop)
            set subtr 0
          }
          set increment_angle_dca $rmi(ac_flux_station_incr,$loop)
# puts "angle_dca $station_angle_dca $starting_angle_dca $ending_angle_dca $increment_angle_dca"
          ic_unload_mesh
          if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
          set uns [ic_load_unstruct [list $merge_file]]
          if !$batch_mode { rmi_ShowMessage " done.\n" }
          ic_rmi_add_type 1
          if !$batch_mode {
            initialize_unstruct_visibility
            disp_new_stuff
            update_uns_disp
            update_project_mode
            $tdv_default configure -active 1
            $tdv_default redraw
          }
        }
      # First make sure the subset menu is active.
        switch_to_subset ""
        if !$batch_mode {
          if { $loaded_display } {
            set spos [tdv_save_position $tdv_default]
          }
        }
        ic_uns_subset_add_families_and_types visible all all
        batch_interp eval { global uns_cur; [$uns_cur get_map visible] promote_type 2 }
        set selmap(cut_on) 1
        es_cut_update update
        if !$batch_mode {
          if { $loaded_display } {
            tdv_load_position $tdv_default $spos
          } else {
            tdv_home $tdv_default
          }
        }
        ic_rmi_save_map visible
        busy 0
        if !$batch_mode {rmi_IconifyGui 1}
        rmi_ShowMessage "Prepare dynamic part.\n"
        set ret [ ic_rmi_dynamic_motion $type_outp \
                           $station_angle_dca $starting_angle_dca \
                           $ending_angle_dca $range1 $range2 \
                           $increment_angle_dca "" $subtr ]
      }
      # Restore everything to how it was before
      ic_undo_resume
    }
    if { $type_outp == 1 || $type_outp == 2 } {
      set merge_file $rmi(configuration)/domains/domain_rmi_merge2
      if [file exists $merge_file] {
        file delete $merge_file
      }
    }
    if { $type_outp == 1 } {
      if {$output_to == "starcd"} {
        rmi_ShowMessage "Output to StarCD finished.\n" blue
      } elseif {$output_to == "fluent"} {
        rmi_ShowMessage "Output to Fluent finished.\n" blue
      }
    } elseif { $type_outp == 2 } {
      rmi_ShowMessage "Create steady state at angle $rmi(angle_dca) finished.\n" blue
    } elseif { $type_outp == 3 } {
      rmi_ShowMessage "Mesh Preview finished.\n" blue
    } elseif { $type_outp == 4 } {
      rmi_ShowMessage "Create AC-Flux station at angle $rmi(ac_flux_station_angle_dca) finished.\n" blue
    } elseif { $type_outp == 5 } {
      rmi_ShowMessage "Mesh preview AC-Flux finished.\n" blue
    } elseif { $type_outp == 6 } {
      rmi_ShowMessage "Output to AC-Flux finished.\n" blue
    }
    ic_undo_start
  }
  set stuff [ glob -nocomplain logfile.* ]
  foreach item $stuff {
    if ![info exists temp($item)] {
      cmd_rm $item
    }
  }
}

#	
# start of the splines code
#
proc rmi_splines_main {merge_file output_file param_file type_outp} {
    global rmi_spl_fams rmi
    
    set rmi_spl_fams(istem) "stm1"
    set rmi_spl_fams(estem) "stm2"
    set rmi_spl_fams(iface) "fac1"
    set rmi_spl_fams(eface) "fac2"
    set rmi_spl_fams(ihead) "hed1"
    set rmi_spl_fams(ehead) "hed2"
    
    ic_unload_mesh
    ic_load_unstruct [list $merge_file]
    
    # create the list of spline_family_names
    set spl_fam_list [rmi_spline_create_famlist]
    if {$spl_fam_list == "" } {
	rmi_ShowMessage "Unable to find spline family names.\n"
	return 1
    }

    # append spl family data to the paramters file
    set fp [open $param_file a]
    set tymx [expr 50 + [llength [ic_uns_list_families pids]]]
    if [rmi_spline_append_famdefs $fp $tymx $spl_fam_list] {
	rmi_ShowMessage "Error appending data to $param_file.\n" red
    }
    close $fp	
    
    # this is the call to the C++ splines code RmiGetSplines to get nodes
    set spl_node_list [ic_rmi_get_splines $spl_fam_list]
    
    # append this and other data to the .inp file
    set inpfile "${output_file}.inp"
    set fp [open $inpfile a]
    if [rmi_spline_write_nodes $fp $spl_node_list] {
	rmi_ShowMessage "Error writing spline data to $inpfile.\n" red
    }
    if [rmi_spline_append_sets $fp $type_outp] {
	rmi_ShowMessage "Error appending data to $inpfile.\n" red
    }
    
    close $fp
    ic_unload_mesh
    
    return 0
}

proc rmi_spline_create_famlist {} {
    global rmi_spl_fams rmi
    
    set spl_list ""    
    if { $rmi(intake_number_valve) > 0 } {
	lappend spl_list "$rmi_spl_fams(istem)_1"
	lappend spl_list "$rmi_spl_fams(iface)_1"
	lappend spl_list "$rmi_spl_fams(ihead)_1"
    }
    if { $rmi(exhaust_number_valve) > 0 } {
	lappend spl_list "$rmi_spl_fams(estem)_1"
	lappend spl_list "$rmi_spl_fams(eface)_1"
	lappend spl_list "$rmi_spl_fams(ehead)_1"
    }    
    for {set i 2} {$i <= $rmi(intake_number_valve)} {incr i} {	
	lappend spl_list "$rmi_spl_fams(ihead)_$i"
    }
    for {set i 2} {$i <= $rmi(exhaust_number_valve)} {incr i} {
	lappend spl_list "$rmi_spl_fams(ehead)_$i"
    }    
    return $spl_list
}

proc rmi_spline_append_famdefs {fp tymx spl_list} {
    global rmi
    
    if {![info exists fp]} { return 1 }   
    puts $fp "! tymx: New cell types start at tymx + 1\n*set tymx $tymx\n! csmx: New coord-sys start at csmx + 1\n*set csmx 9\n!"

    set j 1
    set nsplines [llength $spl_list]
    for {set i 0} {$i < $nsplines} {incr i} {

        set fam_name [string range [lindex $spl_list $i] 0 2]
        set ind [string range [lindex $spl_list $i] 3 3]
        set vlv [string range [lindex $spl_list $i] end end]
        if { !$rmi(intake_motion) && $ind == "1"} {
            continue;
        }
        if { !$rmi(exhaust_motion) && $ind == "2"} {
            continue;
        }

        switch -- [string tolower $fam_name] {
            hed {
                if {$vlv == 1} {
                    set hed_ind($ind) $j
		}
            }
            default {
	    }
	}
	incr j
    }
    set j 1
    for {set i 0} {$i < $nsplines} {incr i} {
	
	set fam_name [string range [lindex $spl_list $i] 0 2]
	set ind [string range [lindex $spl_list $i] 3 3]
	set vlv [string range [lindex $spl_list $i] end end]
	
        if { !$rmi(intake_motion) && $ind == "1"} {
            continue;
        }
        if { !$rmi(exhaust_motion) && $ind == "2"} {
            continue;
        }

	switch -- [string tolower $fam_name] {
	    stm {
		puts $fp "!spline on valve $vlv stem\n*set stm$ind $j\n!"
	    }
	    fac {
		puts $fp "!spline on edge of valve $vlv face\n*set fac$ind $j\n!"
	    }
	    hed {
		if {$vlv == 1} {
		    puts $fp "!spline on edge of valve $vlv hole in head mesh\n*set hed$ind $j\n!"
		} else {
		    set offset [expr $j - $hed_ind($ind)]
		    puts $fp "!spline on edge of valve $vlv hole in head mesh\n*set sof$ind $offset\n!"
		}
	    }
	    default {
		rmi_ShowMessage "Could not interpret name $fam_name.\n"
	    }
	}
	incr j
    }
    return 0
}

proc rmi_spline_write_nodes {fp spline_list} {

    if {![info exists fp]} { return 1 }
    
    # spline_list will look like { 1 2 3 4}...{12 13 14 15}
    for {set i 1} {$i <= [llength $spline_list]} {incr i} {
	set spl [lindex $spline_list [expr $i - 1]]
        set ind 0
	foreach nod $spl {
          if { [ expr $ind % 8 ] == "0" } {
            if { $ind != "0" } {
	      puts $fp "\n!"
            }
            puts -nonewline $fp "spl,$i,vlis"
          }
          puts -nonewline $fp ", $nod"
          incr ind
        }
	puts $fp "\n!"
    }
    return 0
}

proc rmi_spline_append_sets {fp type_outp} {
    global rmi
    
    set time_step $rmi(time_step)
    set post_frequency 5.0
    set nsteps1 [expr                                                      \
          ($rmi(degrees_simulated_stop) - $rmi(degrees_simulated_start)) / \
          (6.0 * $time_step * $rmi(engine_RPM))]
    if { $nsteps1 < 0.0 } {
      set nsteps1 [expr $nsteps1 * -1 ]
    }
    set nsteps2 [expr $post_frequency / ( $rmi(engine_RPM) * $time_step ) ]
    if {![info exists fp]} { return 1 }
    
    if { $type_outp == 1 } {
# transient output
puts $fp "!======================================================================"
puts $fp "!"
puts $fp "!"
puts $fp "! The following are general commands to:"
puts $fp "!"
puts $fp "!                   1. create arbitrary interface between "
puts $fp "!                      the various mesh regions"
puts $fp "!"
puts $fp "!                   2. define arbitrary sliding boundaries"
puts $fp "!"
puts $fp "!                   3. define attach boundaries for "
puts $fp "!                      valve closure"
puts $fp "!"
puts $fp "!                   4. define pressure boundaries "
puts $fp "!                      at the port boundaries for "
puts $fp "!                      a motored engine  "
puts $fp "!"
puts $fp "!                   5. set up for a transient run"
puts $fp "!"
puts $fp "!                   6. create the casename.evn file"
puts $fp "!"
puts $fp "! Please check the files: "
puts $fp "!                     Prostar_screen_output_mesh"
puts $fp "!                     Prostar_screen_output_for_events"
puts $fp "!"
puts $fp "! for any ERRORs"
puts $fp "!"
puts $fp "!"
puts $fp "!------------------------------------"
puts $fp "!"
puts $fp "! please make sure you run prosize"
puts $fp "!"
puts $fp "! to create a  param.prp file"
puts $fp "!"
puts $fp "! and set   MAXTAB  500"
puts $fp "! and set   NCPDIM 50"
puts $fp "!"
puts $fp "!"
puts $fp "!====================================================================="
puts $fp "!output Prostar results to a file"
puts $fp "!please check this file after input"
puts $fp "!for errors"
puts $fp "ofil,Prostar_screen_output_for_mesh,"
puts $fp "!------------------------------------"
puts $fp "!align the tets according to Prostar"
puts $fp "cset news flui"
puts $fp "cset subs tetra"
puts $fp "teta cset"
puts $fp "!"
puts $fp "!----------doing the arbitrary interface "
puts $fp "!!!!-connect port 1"
puts $fp "cset news name s0_1"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt1t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt1t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt1b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt1b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "cplot"
puts $fp "!*****please check the coupling"
puts $fp "!*****above port 1"
puts $fp "*endif"
puts $fp "! create the arbitrary for port 1"
puts $fp "cset news name s0_1"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt1_cpl"
puts $fp "!"
puts $fp "cpar type pt1t pt1b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!I had to do all the above"
puts $fp "!because prostar does not have"
puts $fp "!cpar by name  m.d."
puts $fp "!"
puts $fp "!!!!-connect port 2"
puts $fp "cset news name s0_2"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt2t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt2t "
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt2b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt2b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_2"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "cplot"
puts $fp "!*****please check the coupling"
puts $fp "!*****above port 2"
puts $fp "*endif"
puts $fp "! create the arbitrary for port 2"
puts $fp "cset news name s0_2"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt2_cpl"
puts $fp "!"
puts $fp "cpar type pt2t pt2b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!!!!-connect port 3"
puts $fp "cset news name s0_3"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt3t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt3t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt3b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt3b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_3"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "cplot"
puts $fp "!*****please check the coupling"
puts $fp "!*****above port 3"
puts $fp "*endif"
puts $fp "!"
puts $fp "! create the arbitrary for port 3"
puts $fp "cset news name s0_3"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt3_cpl"
puts $fp "!"
puts $fp "cpar type pt3t pt3b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!!!!-connect port 4"
puts $fp "cset news name s0_4"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt4t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt4t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt4b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt4b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_4"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "cplot"
puts $fp "!*****please check the coupling"
puts $fp "!*****above port 4"
puts $fp "*endif"
puts $fp "!"
puts $fp "! create the arbitrary for port 4"
puts $fp "cset news name s0_4"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt4_cpl"
puts $fp "!"
puts $fp "cpar type pt4t pt4b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!!!!-connect port 5"
puts $fp "cset news name s0_5"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs fluid"
puts $fp "*get top1 mncset"
puts $fp "*get pt5t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt5t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt5b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt5b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_5"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "cplot"
puts $fp "!*****please check the coupling"
puts $fp "!*****above port 5"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "! create the arbitrary for port 5"
puts $fp "cset news name s0_5"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt5_cpl"
puts $fp "!"
puts $fp "cpar type pt5t pt5b"
puts $fp "!"
puts $fp "*endif"
puts $fp "! "
puts $fp "!merge the small hex mesh above the valves"
puts $fp "cset news name prt11"
puts $fp "cset add name cv111"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt12"
puts $fp "cset add name cv112"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt13"
puts $fp "cset add name cv113"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt21"
puts $fp "cset add name cv121"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt22"
puts $fp "cset add name cv122"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt23"
puts $fp "cset add name cv123"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!!!!-connect piston_bowl"
puts $fp "!find the cell types for bottom of cylinder"
puts $fp "cset news name cylinder_bottom"
puts $fp "cset dele gran,,,,,0,9999"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top1 mncset"
puts $fp "*get typ1 nctype top1"
puts $fp "cset dele type typ1"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top2 mncset"
puts $fp "*get typ2 nctype top2"
puts $fp "cset dele type typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top3 mncset"
puts $fp "*get typ3 nctype top3"
puts $fp "cset dele type typ3"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top4 mncset"
puts $fp "*get typ4 nctype top4"
puts $fp "cset dele type typ4"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top5 mncset"
puts $fp "*get typ5 nctype top5"
puts $fp "cset dele type typ5"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top6 mncset"
puts $fp "*get typ6 nctype top6"
puts $fp "cset dele type typ6"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top7 mncset"
puts $fp "*get typ7 nctype top7"
puts $fp "cset dele type typ7"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top8 mncset"
puts $fp "*get typ8 nctype top8"
puts $fp "cset dele type typ8"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get top9 mncset"
puts $fp "*get typ9 nctype top9"
puts $fp "cset dele type typ9"
puts $fp "*endif"
puts $fp "!"
puts $fp "!find the cell types in the piston bowl region"
puts $fp "cset news name cdfp"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get typp nctype bot1"
puts $fp "cset dele type typp"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interfaces "
puts $fp "!between cylinder bottom and bowl"
puts $fp "!"
puts $fp "cset news name cylinder_bottom"
puts $fp "cset add  name cdfp"
puts $fp "vset news cset"
puts $fp "!"
puts $fp "!"
puts $fp "!connect under valve 1"
puts $fp "cset news type typ1"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx bowl_cylbottom"
puts $fp "!cpar type typp typ1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!connect under valve 2"
puts $fp "cset news type typ2"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 3"
puts $fp "cset news type typ3"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ3"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 4"
puts $fp "cset news type typ4"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ4"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 5"
puts $fp "cset news type typ5"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ5"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 6"
puts $fp "cset news type typ6"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ6"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 7"
puts $fp "cset news type typ7"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ7"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 8"
puts $fp "cset news type typ8"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ8"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 9"
puts $fp "cset news type typ9"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset "
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ9"
puts $fp "*endif"
puts $fp "!"
puts $fp "!Prostar creats a lot of bad couples"
puts $fp "!using the type procedure for piston bowl"
puts $fp "!so the cpar by shell is used for the bowl"
puts $fp "!find the cell types for bottom of cylinder"
puts $fp "cset news name cylinder_bottom"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top1 mncset"
puts $fp "*get typ1 nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!find the cell types in the piston bowl region"
puts $fp "cset news name cdfp"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get typ2 nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interfaces"
puts $fp "!between cylinder bottom and bowl"
puts $fp "!"
puts $fp "cset add  name cdfp"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get cchk ncset"
puts $fp "cset news  name cdfp"
puts $fp "cset add name cylinder_bottom"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*if cchk gt 1"
puts $fp "!*get acx mxcptype"
puts $fp "!*set acx acx + 1"
puts $fp "!cptable acx arbitrary,,,,,on,0.02"
puts $fp "!cptname acx bowl_cylbottom"
puts $fp "cshe typ1 cset"
puts $fp "cshe typ2 cset"
puts $fp "cpar shell typ1 typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!I had to use the above scheme"
puts $fp "!because prostar sometimes has"
puts $fp "!problems with cpar by shell type"
puts $fp "!"
puts $fp "!"
puts $fp "!---------define various boundary regions"
puts $fp "!define the arbitrary sliding"
puts $fp "rdef,1,attach,standard"
puts $fp "1,0"
puts $fp "rname,1,under_the_valves"
puts $fp "!"
puts $fp "rdef,2,attach,standard"
puts $fp "1,0"
puts $fp "rname,2,under_the_valves"
puts $fp "!"
puts $fp "rdef,3,attach,standard"
puts $fp "1,0"
puts $fp "rname,3,dummy"
puts $fp "!"
puts $fp "rdef,4,attach,standard"
puts $fp "1,0"
puts $fp "rname,4,dummy"
puts $fp "!"
puts $fp "rdef,5,attach,standard"
puts $fp "1,0"
puts $fp "rname,5,ivalve_closure"
puts $fp "!"
puts $fp "rdef,6,attach,standard"
puts $fp "1,0"
puts $fp "rname,6,ivalve_closure"
puts $fp "!"
puts $fp "rdef,7,attach,standard"
puts $fp "1,0"
puts $fp "rname,7,ivalve_closure"
puts $fp "!"
puts $fp "rdef,8,attach,standard"
puts $fp "1,0"
puts $fp "rname,8,evalve_closure"
puts $fp "!"
puts $fp "rdef,9,attach,standard"
puts $fp "1,0"
puts $fp "rname,9,evalve_closure"
puts $fp "!"
puts $fp "rdef,10,attach,standard"
puts $fp "1,0"
puts $fp "rname,10,evalve_closure"
puts $fp "!"
puts $fp "rdef,11,attach,standard"
puts $fp "1,0"
puts $fp "rname,11,evalve_closure"
puts $fp "!"
puts $fp "rdef,12,attach,standard"
puts $fp "1,0"
puts $fp "rname,12,evalve_closure"
puts $fp "!"
puts $fp "!"
puts $fp "!-----define window params"
puts $fp "!turn background white"
puts $fp "clrm reve"
puts $fp "!make Z axis up"
puts $fp "axis z"
puts $fp "!set plot type to quick hidden"
puts $fp "plty ehid"
puts $fp "zoom,off"
puts $fp "!"
puts $fp "!------define sliding boundaries below the valves"
puts $fp "cset news name asi_m_11"
puts $fp "cset add name asi_m_12"
puts $fp "cset add name asi_m_13"
puts $fp "cset add name asi_m_21"
puts $fp "cset add name asi_m_22"
puts $fp "zoom off"
puts $fp "angl 0"
puts $fp "view 1 1 1"
puts $fp "plty qhid"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 1 all"
puts $fp "*endif"
puts $fp "cset news name asi_s_11"
puts $fp "cset add name asi_s_12"
puts $fp "cset add name asi_s_13"
puts $fp "cset add name asi_s_21"
puts $fp "cset add name asi_s_22"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 2 all"
puts $fp "*endif"
puts $fp "!"
puts $fp "!------define attach boundaries for intake valve closure"
puts $fp "cset news name att_m_av11"
puts $fp "cset add name att_m_av12 "
puts $fp "cset add name att_m_av13"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 7 all"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave11"
puts $fp "cset add name att_s_ave12 "
puts $fp "cset add name att_s_ave13"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 8 all"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!-----define attach boundaries for exhaust valve closure"
puts $fp "cset news name att_m_av21"
puts $fp "cset add name att_m_av22 "
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 11 all"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave21"
puts $fp "cset add name att_s_ave22 "
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cplot"
puts $fp "bzon 12 all"
puts $fp "*endif"
puts $fp "!"
puts $fp "!--------------------------------"
puts $fp "!-----run set up input"
puts $fp "mvgrid on event"
puts $fp "rcon 12 1"
puts $fp "!"
puts $fp "pmat,1,fluid,AIR"
puts $fp "dens,ideal,y,28.96"
puts $fp "lvis,constant,1.81e-05"
puts $fp "spec,constant,1006"
puts $fp "cond,constant,0.02637"
puts $fp "turb,ke,1.,stan"
puts $fp "lowre,off"
puts $fp "coke,0.09,1.44,1.92,1.44,-0.33,0.419,1,1.219,0.9,,,"
puts $fp "temp,on"
puts $fp "!"
puts $fp "pmat,2,fluid,AIR"
puts $fp "dens,ideal,y,28.96"
puts $fp "lvis,constant,1.81e-05"
puts $fp "spec,constant,1006"
puts $fp "cond,constant,0.02637"
puts $fp "turb,ke,1,stan"
puts $fp "lowre,off"
puts $fp "coke,0.09,1.44,1.92,1.44,-0.33,0.419,1,1.219,0.9,,,"
puts $fp "temp,on"
puts $fp "!"
puts $fp "!"
puts $fp "pmat,3,fluid,AIR"
puts $fp "dens,ideal,y,28.96"
puts $fp "lvis,constant,1.81e-05"
puts $fp "spec,constant,1006"
puts $fp "cond,constant,0.02637"
puts $fp "turb,ke,1,stan"
puts $fp "lowre,off"
puts $fp "coke,0.09,1.44,1.92,1.44,-0.33,0.419,1,1.219,0.9,,,"
puts $fp "temp,on"
puts $fp "!"
puts $fp "pmat 1 fluid AIR"
puts $fp "time single,,const $rmi(time_step) rate"
puts $fp "algor piso,,20 0.25 0.7"
puts $fp "iter 999999 0.001"
puts $fp "dsch mars uvw 0.5 stan"
puts $fp "dsch ud turb,,stan"
puts $fp "dsch ud t,,stan"
puts $fp "dsch cd dens 0.01 stan"
puts $fp "cput on"
puts $fp "resd 5 10000 "
puts $fp "prch noecho nobound conv resi"
puts $fp "wdata,restart,10 "
puts $fp "!"
puts $fp "lstep,1,$nsteps1,const,$rmi(time_step),n,0,$nsteps2"
puts $fp "cprint,f,f,f,f,f,f,f,f,f,f,f,f,f"
puts $fp "cpost,t,t,t,t,t,t,t,t,t,f,f,f,t,f"
puts $fp "wprint,f,f,f"
puts $fp "wpost,f,f,f"
puts $fp "!"
puts $fp "solve,y,y,y,y,y,y,y,y,y,n,n,n,"
puts $fp "!"
puts $fp "!"
puts $fp "!-----create the pressure boundaries"
puts $fp "!-------inflow regions"
puts $fp "cset news name intake_port_inlet_1"
puts $fp "cset add name intake_port_inlet_2"
puts $fp "cset add name intake_port_inlet_siamesed"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "!"
puts $fp "rdef,20,pressure,standard"
puts $fp "static,0,n,n,0.1,0.005,293,n"
puts $fp "rname,20,intake_port_inlet_1_2"
puts $fp "!"
puts $fp "cset news name intake_port_inlet_1"
puts $fp "cset add name intake_port_inlet_2"
puts $fp "cset add name intake_port_inlet_siamesed"
puts $fp "cplot"
puts $fp "bzon 20 all"
puts $fp "!"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "*get icn mncset"
puts $fp "pmat 2 fluid"
puts $fp "moni icn"
puts $fp "pres 101325 icn"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!-------outflow regions"
puts $fp "cset news name exhaust_port_outlet_1"
puts $fp "cset add name exhaust_port_outlet_2"
puts $fp "cset add name exhaust_port_inlet_siamesed"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "!"
puts $fp "rdef,25,pressure,standard"
puts $fp "static,0,n,n,0.1,0.005,293,n"
puts $fp "rname,25,exhaust_port_outlet_1_2"
puts $fp "!"
puts $fp "cset news name exhaust_port_outlet_1"
puts $fp "cset add name exhaust_port_outlet_2"
puts $fp "cset add name exhaust_port_inlet_siamesed"
puts $fp "cplot"
puts $fp "bzon 25 all"
puts $fp "!"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "*get icn mncset"
puts $fp "pmat 3 fluid"
puts $fp "moni icn"
puts $fp "pres 101325 icn"
puts $fp "*endif"
puts $fp "!"
puts $fp "!----define reference pressure"
puts $fp "!set reference pressure for cylinder"
puts $fp "cset news name hvol"
puts $fp "vset news surf"
puts $fp "cset subs vset any"
puts $fp "cset subs gran,-1,10,,,,,2"
puts $fp "*get icn mncset"
puts $fp "pmat 1 fluid"
puts $fp "moni icn"
puts $fp "pres 101325 icn"
puts $fp "!"
puts $fp "!--------------------------------"
puts $fp "!----collect the fluid cells"
puts $fp "cset news fluid"
puts $fp "!turn the lights on"
puts $fp "lswi shade"
puts $fp "!turn the box off"
puts $fp "pldi off box"
puts $fp "!give it a general view"
puts $fp "view 1 1 1"
puts $fp "!no angle"
puts $fp "angle 0"
puts $fp "!no zoom"
puts $fp "zoom off"
puts $fp "!plot the cells"
puts $fp "plty ehid"
puts $fp "cplot"
puts $fp "!"
puts $fp "!-------------------------------"
puts $fp "!save the model file"
puts $fp "close,Prostar_screen_output_for_mesh,"
puts $fp "ofile,screen"
puts $fp "!"
puts $fp "save,,"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!creating the evn  file"
puts $fp "!"
puts $fp "ofile,Prostar_screen_output_for_events,"
puts $fp "!"
puts $fp "ifile events.inp"
puts $fp "!"
puts $fp "close,Prostar_screen_output_for_events,"
puts $fp "ofile,screen"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "resume,,"
puts $fp "replot"
puts $fp "plla 5 on 1 1 0.2 1.4"
puts $fp "Please use the section slice button"
puts $fp "plla 5 on 1 1 0.2 1.2"
puts $fp "to cut through the mesh to make sure"
puts $fp "plla 5 on 1 1 0.2 1.0"
puts $fp "all the arbitrary couples have been "
puts $fp "plla 5 on 1 1 0.2 0.8"
puts $fp "defined properly."
puts $fp "!"
puts $fp "plla 5 on 1 1 0.2 0.6"
puts $fp "Also,issue the following command"
puts $fp "plla 5 on 2 1 0.2 0.4"
puts $fp "cpcheck,0.01,all,,,newset,0.05,nofix"
puts $fp "plla 5 on 1 1 0.2 0.2"
puts $fp "to check for badly defined couples."
puts $fp "plla 5 off"
puts $fp "resum,,"
puts $fp "evfi conn,,"
puts $fp "!"
puts $fp "!"
puts $fp "!The piston must be moving along the Global Z axis"
puts $fp "!The Cylinder(DECK) center must be at  0 0 0"
puts $fp "!==========================================="
puts $fp "!"
puts $fp "!"
puts $fp "! If Prostar issues an error during the"
puts $fp "! execution of this inp file please click"
puts $fp "! on the error message to see what the"
puts $fp "! error is.  "
puts $fp "! Most of the time errors are about the"
puts $fp "! param.prp file."
puts $fp "! Please use the  STARDIR/bin/prosize"
puts $fp "! command to create the proper param.prp"
puts $fp "! file for your problem"
puts $fp "!"
puts $fp "!==========================================="
puts $fp "!==========================================="
puts $fp "!==========================================="
    } else {
# steady state mesh
puts $fp "!======================================================================"
puts $fp "!"
puts $fp "!"
puts $fp "! The following are general commands to:"
puts $fp "!"
puts $fp "!                   1. create arbitrary interface between"
puts $fp "!                      the various mesh regions"
puts $fp "!"
puts $fp "!                   2. define pressure boundaries"
puts $fp "!                      at the port boundaries for"
puts $fp "!                      a motored engine"
puts $fp "!"
puts $fp "!                   2. set up for a steady state "
puts $fp "!"
puts $fp "!"
puts $fp "! Please check the files:"
puts $fp "!                     Prostar_screen_output_for_mesh"
puts $fp "!"
puts $fp "! for any ERRORs"
puts $fp "!"
puts $fp "!"
puts $fp "!------------------------------------"
puts $fp "!"
puts $fp "! please make sure you run prosize"
puts $fp "!"
puts $fp "! to create a  param.prp file"
puts $fp "!"
puts $fp "! and set   MAXTAB  500"
puts $fp "! and set   NCPDIM 50"
puts $fp "!"
puts $fp "!"
puts $fp "!====================================================================="
puts $fp "!output Prostar results to a file"
puts $fp "!please check this file after input"
puts $fp "!for errors"
puts $fp "ofil,Prostar_screen_output_for_mesh,"
puts $fp "!------------------------------------"
puts $fp "!align the tets according to Prostar"
puts $fp "cset news flui"
puts $fp "cset subs tetra"
puts $fp "teta cset"
puts $fp "!"
puts $fp "!----merge all the nodes with .001 tolerance----"
puts $fp "cset news fluid"
puts $fp "vset news surf"
puts $fp "vmer,vset"
puts $fp "n"
puts $fp "!"
puts $fp "!"
puts $fp "!----------doing the arbitrary interface"
puts $fp "!!!!-connect port 1"
puts $fp "cset news name s0_1"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt1t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt1t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt1b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt1b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "plla 1 on 2 1 5 0.5"
puts $fp "Please check the coupling above port 1"
puts $fp "cplot"
puts $fp "plla 1 off"
puts $fp "*endif"
puts $fp "!"
puts $fp "! create the arbitrary for port 1"
puts $fp "cset news name s0_1"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt1_cpl"
puts $fp "!"
puts $fp "cpar type pt1t pt1b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!I had to do all the above"
puts $fp "!because prostar does not have"
puts $fp "!cpar by name  m.d."
puts $fp "!"
puts $fp "!!!!-connect port 2"
puts $fp "cset news name s0_2"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt2t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt2t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt2b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt2b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_2"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "plla 1 on 2 1 5 0.5"
puts $fp "Please check the coupling above port 2"
puts $fp "cplot"
puts $fp "plla 1 off"
puts $fp "*endif"
puts $fp "! create the arbitrary for port 2"
puts $fp "cset news name s0_2"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt2_cpl"
puts $fp "!"
puts $fp "cpar type pt2t pt2b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!!!!-connect port 3"
puts $fp "cset news name s0_3"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt3t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt3t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt3b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt3b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_3"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "plla 1 on 2 1 5 0.5"
puts $fp "Please check the coupling above port 3"
puts $fp "cplot"
puts $fp "plla 1 off"
puts $fp "*endif"
puts $fp "!"
puts $fp "! create the arbitrary for port 3"
puts $fp "cset news name s0_3"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt3_cpl"
puts $fp "!"
puts $fp "cpar type pt3t pt3b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!!!!-connect port 4"
puts $fp "cset news name s0_4"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get top1 mncset"
puts $fp "*get pt4t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt4t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt4b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt4b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_4"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "plla 1 on 2 1 5 0.5"
puts $fp "Please check the coupling above port 4"
puts $fp "cplot"
puts $fp "plla 1 off"
puts $fp "*endif"
puts $fp "!"
puts $fp "! create the arbitrary for port 4"
puts $fp "cset news name s0_4"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt4_cpl"
puts $fp "!"
puts $fp "cpar type pt4t pt4b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!!!!-connect port 5"
puts $fp "cset news name s0_5"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs fluid"
puts $fp "*get top1 mncset"
puts $fp "*get pt5t nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset dele type pt5t"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get pt5b nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-check coupling cells"
puts $fp "cset dele type pt5b"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset news name s0_5"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "plla 1 on 2 1 5 0.5"
puts $fp "Please check the coupling above port 5"
puts $fp "cplot"
puts $fp "plla 1 off"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "! create the arbitrary for port 5"
puts $fp "cset news name s0_5"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx prt5_cpl"
puts $fp "!"
puts $fp "cpar type pt5t pt5b"
puts $fp "!"
puts $fp "*endif"
puts $fp "!"
puts $fp "!merge the small hex mesh above the valves"
puts $fp "cset news name prt11"
puts $fp "cset add name cv111"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt12"
puts $fp "cset add name cv112"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt13"
puts $fp "cset add name cv113"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt21"
puts $fp "cset add name cv121"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt22"
puts $fp "cset add name cv122"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name prt23"
puts $fp "cset add name cv123"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "vset news surf"
puts $fp "vmer vset"
puts $fp "n"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!!!!-connect piston_bowl"
puts $fp "!find the cell types for bottom of cylinder"
puts $fp "cset news name cylinder_bottom"
puts $fp "cset dele gran,,,,,0,9999"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top1 mncset"
puts $fp "*get typ1 nctype top1"
puts $fp "cset dele type typ1"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top2 mncset"
puts $fp "*get typ2 nctype top2"
puts $fp "cset dele type typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top3 mncset"
puts $fp "*get typ3 nctype top3"
puts $fp "cset dele type typ3"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top4 mncset"
puts $fp "*get typ4 nctype top4"
puts $fp "cset dele type typ4"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top5 mncset"
puts $fp "*get typ5 nctype top5"
puts $fp "cset dele type typ5"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top6 mncset"
puts $fp "*get typ6 nctype top6"
puts $fp "cset dele type typ6"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top7 mncset"
puts $fp "*get typ7 nctype top7"
puts $fp "cset dele type typ7"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top8 mncset"
puts $fp "*get typ8 nctype top8"
puts $fp "cset dele type typ8"
puts $fp "*endif"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top9 mncset"
puts $fp "*get typ9 nctype top9"
puts $fp "cset dele type typ9"
puts $fp "*endif"
puts $fp "!"
puts $fp "!find the cell types in the piston bowl region"
puts $fp "cset news name cdfp"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get typp nctype bot1"
puts $fp "cset dele type typp"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interfaces"
puts $fp "!between cylinder bottom and bowl"
puts $fp "!"
puts $fp "cset news name cylinder_bottom"
puts $fp "cset add  name cdfp"
puts $fp "vset news cset"
puts $fp "!"
puts $fp "!"
puts $fp "!connect under valve 1"
puts $fp "cset news type typ1"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx bowl_cylbottom"
puts $fp "!cpar type typp typ1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!connect under valve 2"
puts $fp "cset news type typ2"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 3"
puts $fp "cset news type typ3"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ3"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 4"
puts $fp "cset news type typ4"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ4"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 5"
puts $fp "cset news type typ5"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ5"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 6"
puts $fp "cset news type typ6"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ6"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 7"
puts $fp "cset news type typ7"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ7"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 8"
puts $fp "cset news type typ8"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ8"
puts $fp "*endif"
puts $fp "!"
puts $fp "!connect under valve 9"
puts $fp "cset news type typ9"
puts $fp "cset subs vset any"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "cset add type typp"
puts $fp "!cpar type typp typ9"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!Prostar creats a lot of bad couples"
puts $fp "!using the type procedure for piston bowl"
puts $fp "!so the cpar by shell is used for the bowl"
puts $fp "!find the cell types for bottom of cylinder"
puts $fp "cset news name cylinder_bottom"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get top1 mncset"
puts $fp "*get typ1 nctype top1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!find the cell types in the piston bowl region"
puts $fp "cset news name cdfp"
puts $fp "!"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get bot1 mncset"
puts $fp "*get typ2 nctype bot1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interfaces"
puts $fp "!between cylinder bottom and bowl"
puts $fp "!"
puts $fp "cset add  name cdfp"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "*get cchk ncset"
puts $fp "cset news  name cdfp"
puts $fp "cset add name cylinder_bottom"
puts $fp "vset news cset"
puts $fp "cset add vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*if cchk gt 1"
puts $fp "!*get acx mxcptype"
puts $fp "!*set acx acx + 1"
puts $fp "!cptable acx arbitrary,,,,,on,0.02"
puts $fp "!cptname acx bowl_cylbottom"
puts $fp "cshe typ1 cset"
puts $fp "cshe typ2 cset"
puts $fp "cpar shell typ1 typ2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!-----define window params"
puts $fp "!turn background white"
puts $fp "clrm reve"
puts $fp "!make Z axis up"
puts $fp "axis z"
puts $fp "!set plot type to quick hidden"
puts $fp "plty ehid"
puts $fp "!"
puts $fp "!"
puts $fp "!----connect below_the_valves"
puts $fp "!prepare for coupling"
puts $fp "!"
puts $fp "!------under iv 1"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx below_iv_1_coupling"
puts $fp "!"
puts $fp "cset news name asi_s_11"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_m_11"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_s_11"
puts $fp "vset news cset"
puts $fp "cset news name asi_m_11"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name asi_s_11"
puts $fp "cset add name asi_m_11"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples below iv 1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------under iv 2"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx below_iv_2_coupling"
puts $fp "!"
puts $fp "cset news name asi_s_12"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_m_12"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_s_12"
puts $fp "vset news cset"
puts $fp "cset news name asi_m_12"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name asi_s_12"
puts $fp "cset add name asi_m_12"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples below iv 2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------under iv 3"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx below_iv_3_coupling"
puts $fp "!"
puts $fp "cset news name asi_s_13"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_m_13"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_s_13"
puts $fp "vset news cset"
puts $fp "cset news name asi_m_13"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name asi_s_13"
puts $fp "cset add name asi_m_13"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples below iv 3"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------under ev 1"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx below_ev_1_coupling"
puts $fp "!"
puts $fp "cset news name asi_s_21"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_m_21"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_s_21"
puts $fp "vset news cset"
puts $fp "cset news name asi_m_21"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name asi_s_21"
puts $fp "cset add name asi_m_21"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples below ev 1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------under ev 2"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx below_ev_2_coupling"
puts $fp "!"
puts $fp "cset news name asi_s_22"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_m_22"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name asi_s_22"
puts $fp "vset news cset"
puts $fp "cset news name asi_m_22"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name asi_s_22"
puts $fp "cset add name asi_m_22"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples below ev 2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!----connect above_the_valves"
puts $fp "!------above iv 1"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx above_iv_1_coupling"
puts $fp "!"
puts $fp "cset news name att_s_ave11"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_m_av11"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave11"
puts $fp "vset news cset"
puts $fp "cset news name att_m_av11"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name att_s_ave11"
puts $fp "cset add name att_m_av11"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples above iv 1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------above iv 2"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx above_iv_2_coupling"
puts $fp "!"
puts $fp "cset news name att_s_ave12"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_m_av12"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave12"
puts $fp "vset news cset"
puts $fp "cset news name att_m_av12"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name att_s_ave12"
puts $fp "cset add name att_m_av12"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples above iv 2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------above iv 3"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx above_iv_3_coupling"
puts $fp "!"
puts $fp "cset news name att_s_ave13"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_m_av13"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave13"
puts $fp "vset news cset"
puts $fp "cset news name att_m_av13"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name att_s_ave13"
puts $fp "cset add name att_m_av13"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples above iv 3"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------above ev 1"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx above_ev_1_coupling"
puts $fp "!"
puts $fp "cset news name att_s_ave21"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_m_av21"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave21"
puts $fp "vset news cset"
puts $fp "cset news name att_m_av21"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name att_s_ave21"
puts $fp "cset add name att_m_av21"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples above ev 1"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!------above ev 2"
puts $fp "cpset all"
puts $fp "*get arb1 mxcouple "
puts $fp "*get acx mxcptype"
puts $fp "*set acx acx + 1"
puts $fp "cptable acx arbitrary,,,,,on,0.02"
puts $fp "cptname acx above_ev_1_coupling"
puts $fp "!"
puts $fp "cset news name att_s_ave22"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num1 mncset"
puts $fp "*get sid1 nctype num1"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_m_av22"
puts $fp "*get cchk ncset"
puts $fp "*if cchk gt 1"
puts $fp "*get num2 mncset"
puts $fp "*get sid2 nctype num2"
puts $fp "*endif"
puts $fp "!"
puts $fp "cset news name att_s_ave22"
puts $fp "vset news cset"
puts $fp "cset news name att_m_av22"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "vpro,vset,,,,cset,,,norm,.02"
puts $fp "*endif"
puts $fp "!"
puts $fp "!create the arbitrary interface"
puts $fp "cset news name att_s_ave22"
puts $fp "cset add name att_m_av22"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "!"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "cshe sid1 cset"
puts $fp "cshe sid2 cset"
puts $fp "cpar shel sid1 sid2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!check for proper creation of arbs"
puts $fp "cpset all"
puts $fp "*get arb2 mxcouple "
puts $fp "*set cpck arb2 - arb1"
puts $fp "*if cpck lt 10"
puts $fp "cpset news type acx"
puts $fp "plla 1 on 2 1 1 8"
puts $fp "Please check the couples above ev 2"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!----collect the fluid cells"
puts $fp "cset news fluid"
puts $fp "!turn the lights on"
puts $fp "lswi shade"
puts $fp "!turn the box off"
puts $fp "pldi off box"
puts $fp "!give it a general view"
puts $fp "view 1 1 1"
puts $fp "!no angle"
puts $fp "angle 0"
puts $fp "!no zoom"
puts $fp "zoom,off"
puts $fp "!plot the cells"
puts $fp "!--------------------------------"
puts $fp "!-----run set up input"
puts $fp "pmat,1,fluid,AIR"
puts $fp "dens,ideal,y,28.96"
puts $fp "lvis,constant,1.81e-05"
puts $fp "spec,constant,1006"
puts $fp "cond,constant,0.02637"
puts $fp "turb,ke,1,stan"
puts $fp "lowre,off"
puts $fp "coke,0.09,1.44,1.92,1.44,-0.33,0.419,1,1.219,0.9,,,"
puts $fp "temp,on"
puts $fp "!"
puts $fp "pmat 1 fluid AIR"
puts $fp "iter 1000 0.001"
puts $fp "dsch mars uvw 0.5 stan"
puts $fp "dsch ud turb,,stan"
puts $fp "dsch ud t,,stan"
puts $fp "dsch cd dens 0.01 stan"
puts $fp "cput on"
puts $fp "resd 5 10000"
puts $fp "prch noecho nobound conv resi"
puts $fp "wdata,restart,10"
puts $fp "!"
puts $fp "!-----create the pressure boundaries"
puts $fp "!inflow regions"
puts $fp "cset news name intake_port_inlet_1"
puts $fp "cset add name intake_port_inlet_2"
puts $fp "cset add name intake_port_inlet_siamesed"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "!"
puts $fp "rdef,20,pressure,standard"
puts $fp "static,0,n,n,0.1,0.005,293,n"
puts $fp "rname,20,intake_port_inlet_1_2"
puts $fp "!"
puts $fp "cset news name intake_port_inlet_1"
puts $fp "cset add name intake_port_inlet_2"
puts $fp "cset add name intake_port_inlet_siamesed"
puts $fp "cplot"
puts $fp "bzon 20 all"
puts $fp "!"
puts $fp "vset news cset"
puts $fp "cset news vset any"
puts $fp "cset subs flui"
puts $fp "*get icn mncset"
puts $fp "moni icn"
puts $fp "pres 101325 icn"
puts $fp "*endif"
puts $fp "!"
puts $fp "!"
puts $fp "!outflow regions"
puts $fp "cset news name exhaust_port_outflow_1"
puts $fp "cset add name exhaust_port_outflow_2"
puts $fp "cset add name exhaust_port_inlet_siamesed"
puts $fp "*get ncset,ncset"
puts $fp "*if,ncset,gt,0"
puts $fp "!"
puts $fp "rdef,25,pressure,standard"
puts $fp "static,0,n,n,0.1,0.005,293,n"
puts $fp "rname,25,exhaust_port_outlet_1_2"
puts $fp "!"
puts $fp "cset news name exhaust_port_outflow_1"
puts $fp "cset add name exhaust_port_outflow_2"
puts $fp "cset add name exhaust_port_inlet_siamesed"
puts $fp "cplot"
puts $fp "bzon 25 all"
puts $fp "!"
puts $fp "!"
puts $fp "!--------------------------------"
puts $fp "!"
puts $fp "cset news flui"
puts $fp "plty ehid"
puts $fp "cplot"
puts $fp "close,Prostar_screen_output_for_mesh,"
puts $fp "save,,"
puts $fp "plla 5 on 1 1 0.2 1.4"
puts $fp "Please use the section slice button"
puts $fp "plla 5 on 1 1 0.2 1.2"
puts $fp "to cut through the mesh to make sure"
puts $fp "plla 5 on 1 1 0.2 1.0"
puts $fp "all the arbitrary couples have been"
puts $fp "plla 5 on 1 1 0.2 0.8"
puts $fp "defined properly."
puts $fp "plla 5 on 1 1 0.2 0.6"
puts $fp "Also,issue the following command"
puts $fp "plla 5 on 2 1 0.2 0.4"
puts $fp "cpcheck,0.01,all,,,newset,0.05,nofix"
puts $fp "plla 5 on 1 1 0.2 0.2"
puts $fp "to check for badly defined couples."
puts $fp "plla 5 off"
puts $fp "close all"
puts $fp "resum,,"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!"
puts $fp "!==========================================="
puts $fp "!"
puts $fp "!"
puts $fp "! If Prostar issues an error during the"
puts $fp "! execution of this inp file please click"
puts $fp "! on the error message to see what the"
puts $fp "! error is. "
puts $fp "! Most of the time errors are about the"
puts $fp "! param.prp file."
puts $fp "! Please use the  STARDIR/bin/prosize"
puts $fp "! command to create the proper param.prp"
puts $fp "! file for your problem"
puts $fp "!"
puts $fp "!==========================================="
puts $fp "!==========================================="
puts $fp "!==========================================="
    }
    return 0
}
proc rmi_add_material_id { bocofiles } {
    global rmi

    if { $rmi(intake_motion) } {
        set bc "\"FLUID\" \"TYPE\" 2"
    
        set families ""   
        for { set i 1 } { $i <= $rmi(intake_number_valve) } { incr i } {
          set letter [format %c [expr $i + 64]]
          set prt_fam "PRT${letter}1"
          if [ic_uns_count_in_families $prt_fam] {
            lappend families $prt_fam
          }
          lappend families "VBK1${i}"
          lappend families "PRT1${i}"
          lappend families "CV11${i}"
#         lappend families "CV21${i}"
        }

        foreach fam $families {
	    if [rmi_add_bc $bocofiles $fam $bc] {
	        rmi_ShowMessage "Error adding $bc to $fam.\n" red
	    }
        }
    }
    if { $rmi(exhaust_motion) } {
        set bc "\"FLUID\" \"TYPE\" 3"
        set families ""
  
        for { set i 1 } { $i <= $rmi(exhaust_number_valve) } { incr i } {
          set letter [format %c [expr $i + 64]]
          set prt_fam "PRT${letter}2"
          if [ic_uns_count_in_families $prt_fam] {
            lappend families $prt_fam
          }
          lappend families "VBK2${i}"
          lappend families "PRT2${i}"
          lappend families "CV12${i}"
#         lappend families "CV22${i}"
        }

        foreach fam $families {
            if [rmi_add_bc $bocofiles $fam $bc] {
                rmi_ShowMessage "Error adding $bc to $fam.\n" red
            }
        }
    }
}

proc rmi_add_bc { bocofiles family bc } {

    foreach bcf $bocofiles {
	if [catch {open $bcf r} fp] {
	    rmi_ShowMessage "Could not open bc file $bcf.\n"
	    continue
	}
	set newdata ""
	while { ![eof $fp] } {
	    set line [string trim [gets $fp]]
	    append newdata "$line\n"
	    if [string compare $family [string trim [lindex $line 0] \"]] continue
	    if {[lindex $line 1] != ""} continue
	    
	    append newdata "\"$family\" $bc\n"
	    set added $bcf
	}
	close $fp
	
	if [info exists added] {
	    set fp [open $added w]
	    puts -nonewline $fp $newdata
	    close $fp
	    break
	}	
    }
    if [info exists added]  { return 0 }
    return 1
}

proc rmi_valve_is_closed { param_file } {
    
    set closed 1
    set have_cast 0
    set have_vop1 0
    
    if [catch {open $param_file r} fp] {
	rmi_ShowMessage "Error opening $param_file.\n" red
	return 1
    }
    while { ![eof $fp] } {
	set line [string trim [gets $fp]]
	if ![string compare [string index $line 0] "!"] { continue }
	if {![string compare "vop1" [lindex $line 1]]} {
	    set vop1 [lindex $line 2]
	    set have_vop1 1
	} elseif {![string compare "cast" [lindex $line 1]]} {
	    set cast [lindex $line 2]
	    set have_cast 1
	}
	if {$have_vop1 && $have_cast} {
	    # if vop1 <= cast then open, else closed
	    if {[expr $cast - $vop1] >= 0 } { set closed 0 }
	    break 
	}
    }
    if {!($have_vop1) && !($have_cast)} {
	rmi_ShowMessage "Error finding valve status, assuming closed.\n"
    }
    close $fp
    
    return !$closed
}

proc rmi_OutputRunSetup {} {
global rmi

  rmi_SetMeshParams 1
  if { $rmi(outp_para_file) || $rmi(outp_lift_curves) } {
    set n [ ic_rmi_outp_star "$rmi(configuration)/domains/domain_piston" \
            $rmi(outp_lift_curves) $rmi(outp_para_file) ]
    if { $n != "0" } {
      return
    }
  }
}

proc rmi_set_node_map2 { name_node_map n_maps get_node get_node_number \
                        families n_lay type type_valve number } {
  set n 0
  if {$name_node_map != ""} {
    if [ic_uns_subset_exists $name_node_map] {
      set n [ic_rmi_read_node_map $name_node_map \
     	   1 $n_maps $get_node $get_node_number "" \
           $n_lay $type $type_valve $number]
    }
  } else {
    if [ic_uns_subset_exists visible] {
      ic_uns_subset_add_families_and_types visible $families MIXED
      set n [ic_rmi_read_node_map visible \
             0 $n_maps $get_node $get_node_number "" \
             $n_lay $type $type_valve $number]      
    }
  }
  return $n
}

# merge valve meshes and port meshes on S0 families
proc rmi_merge_valve_port { merge_file } {
global rmi batch_mode

  ic_unload_mesh
  set n_merge_nodes 0
  if { [ file exists $merge_file ] } {
    if !$batch_mode { rmi_ShowMessage "Loading file $merge_file ..." }
    ic_load_unstruct [list $merge_file]
    if !$batch_mode { rmi_ShowMessage " done.\n" }
    rmi_SetMeshParams 1
    set nValves [expr $rmi(intake_number_valve) + $rmi(exhaust_number_valve)]
    set families ""
    for { set i 1 } { $i <= $nValves } { incr i } {
      lappend families "S0_$i"
    }
    if { $families != "" } {
      ic_merge_meshes $families ""
    }
    ic_save_unstruct $merge_file
  }
  ic_unload_mesh
}

proc rmi_set_dynamic_files2 { what } {
global rmi n_maps_extrude

  set n_maps 0
  set n_maps_extrude 0
  ic_rmi_nodes_storage_init
  set n [ rmi_set_node_map2  "map_rmi_ICCH" $n_maps 1 1 "" \
                              $rmi(number_layers_cyl_BDC) 1 0 0  ]
  if { $n < 0 } {
    rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
    rmi_ShowMessage "Probably the number of layers is different.\n"
    rmi_ShowMessage "Please recreate base mesh file.\n"
    return $n
  }
  if $n {
    incr n_maps
  }
  if { $what == "all" } {
    set chk_fam "BOWL"
    set pid [ic_uns_lookup_family_pid $chk_fam]
    if { $pid != "" } {
      set families [ list $chk_fam ]
      set n [ rmi_set_node_map2  "" $n_maps 1 1 $families \
                                  -1 2 0 0  ]
      if { $n < 0 } {
        rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
        rmi_ShowMessage "Please recreate base mesh file.\n"
        return $n
      }
      if $n {
        incr n_maps
      }
    }

    set chk_fam "HEAD"
    set pid [ic_uns_lookup_family_pid $chk_fam]
    if { $pid != "" } {
      set families [ list $chk_fam ]
      set n [ rmi_set_node_map2  "" $n_maps 1 1 $families \
                                  -1 3 0 0  ]
      if { $n < 0 } {
        rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
        rmi_ShowMessage "Please recreate base mesh file.\n"
        return $n
      }
      if $n {
        incr n_maps
      }
    }
    set families ""
    set chk_fam "HEAD"
    set pid [ic_uns_lookup_family_pid $chk_fam]
    if { $pid != "" } {
      lappend families $chk_fam
    }
    for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
      set chk_fam "BOT1${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
    }
    for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
      set chk_fam "BOT2${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
    }
    if { $families != "" } {
      set n [ rmi_set_node_map2  "" $n_maps 0 0 $families \
                                  -1 3 0 0  ]
      if { $n < 0 } {
        rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
        rmi_ShowMessage "Please recreate base mesh file.\n"
        return $n
      }
      if $n {
        incr n_maps
      }
    }
  }

  for {set i 1} {$i <= $rmi(intake_number_valve)} {incr i} {
    set node_map "map_rmi_CV11${i}"
    if $rmi(chadice) {
      set no $i
    } else {
      set no [expr 2 * ($i - 1) + 1 ]
    }
    set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                $rmi(intake_valve_nlayers) 4 1 $no  ]
    if { $n < 0 } {
      rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
      rmi_ShowMessage "Probably the number of layers is different.\n"
      rmi_ShowMessage "Please recreate base mesh file.\n"
      return $n
    }
    if $n {
      incr n_maps
    }

    if !$rmi(chadice) {
      set node_map "map_rmi_CV31${i}"
      set no [expr 2 * ($i - 1) + 2 ]
      set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                  $rmi(intake_valve_nlayers) 4 1 $no  ]
      if { $n < 0 } {
        rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
        rmi_ShowMessage "Probably the number of layers is different.\n"
        rmi_ShowMessage "Please recreate base mesh file.\n"
        return $n
      }
      if $n {
        incr n_maps
      }
    }

    set node_map "map_rmi_CBV1${i}"
    set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                $rmi(number_layers_cyl_BDC) 5 1 $i  ]
    if { $n < 0 } {
      rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
      rmi_ShowMessage "Probably the number of layers is different.\n"
      rmi_ShowMessage "Please recreate base mesh file.\n"
      return $n
    }
    if $n {
      incr n_maps
    }
    if { $what == "all" } {
      set families ""
      set chk_fam "VBK1${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
      set chk_fam "VVL1${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
      if { $families != "" } {
        set n [ rmi_set_node_map2  "" $n_maps 1 1 $families \
                                  -1 6 1 $i  ]
        if { $n < 0 } {
          rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
          rmi_ShowMessage "Please recreate base mesh file.\n"
          return $n
        }
        if $n {
          incr n_maps
        }
      }
    }
  }
  for {set i 1} {$i <= $rmi(exhaust_number_valve)} {incr i} {
    set node_map "map_rmi_CV12${i}"
    if $rmi(chadice) {
      set no $i
    } else {
      set no [expr 2 * ($i - 1) + 1 ]
    }
    set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                $rmi(exhaust_valve_nlayers) 4 2 $no  ]
    if { $n < 0 } {
      rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
      rmi_ShowMessage "Probably the number of layers is different.\n"
      rmi_ShowMessage "Please recreate base mesh file.\n"
      return $n
    }
    if $n {
      incr n_maps
    }

    if !$rmi(chadice) {
      set node_map "map_rmi_CV32${i}"
      set no [expr 2 * ($i - 1) + 2 ]
      set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                  $rmi(exhaust_valve_nlayers) 4 2 $no  ]
      if { $n < 0 } {
        rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
        rmi_ShowMessage "Probably the number of layers is different.\n"
        rmi_ShowMessage "Please recreate base mesh file.\n"
        return $n
      }
      if $n {
        incr n_maps
      }
    }

    set node_map "map_rmi_CBV2${i}"
    set n [ rmi_set_node_map2  $node_map $n_maps 1 1 "" \
                                $rmi(number_layers_cyl_BDC) 5 2 $i  ]
    if { $n < 0 } {
      rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
      rmi_ShowMessage "Probably the number of layers is different.\n"
      rmi_ShowMessage "Please recreate base mesh file.\n"
      return $n
    }
    if $n {
      incr n_maps
    }
    if { $what == "all" } {
      set families ""
      set chk_fam "VBK2${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
      set chk_fam "VVL2${i}"
      set pid [ic_uns_lookup_family_pid $chk_fam]
      if { $pid != "" } {
        lappend families $chk_fam
      }
      if { $families != "" } {
        set n [ rmi_set_node_map2  "" $n_maps 1 1 $families \
                                  -1 6 2 $i  ]
        if { $n < 0 } {
          rmi_ShowMessage "Base mesh file seems to be corrupt.\n"
          rmi_ShowMessage "Please recreate base mesh file.\n"
          return $n
        }
        if $n {
          incr n_maps
        }
      }
    }
  }
  set n_maps_extrude $n_maps
  return 0
}


proc rmi_check_mesh_merged {} {
   
 set fam_lst [ic_uns_non_empty_families]
 
 #Looking for uncovered faces for each family seperatly. 
 foreach fam $fam_lst {
   
    #Create an empty subset
    set subst_nm [ic_uns_subset_create]
    
    #Create a subset for diagnostic results
    set subst_diagnostic [ic_uns_create_diagnostic_subset] 
    
    #Add $fam to the empty subset
    ic_uns_subset_make_families $subst_nm $fam
    
    
    #Check for uncovered faces
    set rst [ic_uns_diagnostic subset $subst_nm diag_type uncovered  fix_fam FIX_UNCOVERED disp_subset $subst_diagnostic]
    
    #Check the results, if there are any uncovered faces, try to fix them
    if {[lindex $rst 0] > 0} { 
       ic_uns_new_family UNCOVERED_$fam 
       ic_uns_fix_uncovered_faces $subst_diagnostic UNCOVERED_$fam
       }
    #Delete diagnostic subset
    ic_uns_subset_delete $subst_diagnostic
   
    #Delete  subset
    ic_uns_subset_delete $subst_nm
    
    }
  
  
  ### Check for duplicate elements
  #Create a subset for diagnostic results
   set subst_diagnostic [ic_uns_create_diagnostic_subset]
  #Check for duplicate elements
   set rst [ic_uns_diagnostic subset all diag_type duplicate fix_fam FIX_DUPLICATE disp_subset $subst_diagnostic]
  #Check the results, if there are any duplicate faces, try to fix them
   if {[lindex $rst 0] > 0} { 
      ic_uns_delete_contents $subst_diagnostic
      }
  #Delete diagnostic subset
   ic_uns_subset_delete $subst_diagnostic
 }


