Monthly Archives: October 2014

Dirty script for dirty meshes

If you have a messy polyMesh you might use this script. Modify it to suit your needs.

#!/bin/sh
# dirty script for dirty meshes
# author Bjorn Bergqvist
# http://www.discretizer.se
# removes cells which is/have:
# zeroVolumeCells, skewFaces, underdeterminedCells and lowQualityTetFaces
#
# if you have to type something before OpenFOAM commands
# qsub... or somthing
submit=""

# create a batch file for setSet
createBatchFile() {
  counter=0
  echo "cellSet cellsToKeep new" > $2
  for i in `grep Writing $1 |awk '{ print $NF }'`
    do

      if [ $i == "zeroVolumeCells" ]; then
        echo "cellSet cellsToKeep add cellToCell zeroVolumeCells any" >> $2
        echo `grep $i log.checkMesh`
        counter=$((counter+1)) 
      fi
      if [ $i == "skewFaces" ]; then
        echo "cellSet cellsToKeep add faceToCell skewFaces any" >> $2
        echo `grep $i log.checkMesh`
        counter=$((counter+1)) 
      fi
      if [ $i == "underdeterminedCells" ]; then
        echo "cellSet cellsToKeep add cellToCell underdeterminedCells any" >> $2
        echo `grep $i log.checkMesh`
        counter=$((counter+1)) 
      fi
      if [ $i == "lowQualityTetFaces" ]; then
        echo "cellSet cellsToKeep add faceToCell lowQualityTetFaces any" >> $2
        echo `grep $i log.checkMesh`
        counter=$((counter+1)) 
      fi
    done
    echo "cellSet cellsToKeep invert" >> $2
    echo "cellSet cellsToKeep subset" >> $2
    echo "quit" >> $2
    return $counter
}

logfile=log.txt
echo "" > $logfile

while :
do
  $submit checkMesh -allGeometry -allTopology -latestTime > log.checkMesh
  checkString=`grep 'The mesh has multiple regions' log.checkMesh`
  if [ ! ${#checkString} == 0 ]; then
    echo "multiple regions: splitMeshRegions"
    $submit splitMeshRegions -largestOnly >> $logfile
    $submit subsetMesh -overwrite region0 >> $logfile
    $submit createPatch -overwrite >> $logfile
    echo "rerun checkMesh"
    $submit checkMesh -allGeometry -allTopology -latestTime > log.checkMesh
  fi
  createBatchFile log.checkMesh batchForDeleting
  fileOk=$?
  echo "fileOk $fileOk"
  if [ $fileOk == 0 ]; then
    echo "nothing to do"
    exit 0
  fi
  $submit setSet -latestTime -batch batchForDeleting > $logfile
  $submit subsetMesh cellsToKeep >> $logfile
  $submit createPatch -overwrite >> $logfile
done

Typical output:

<<Writing 4 zero volume cells to set zeroVolumeCells
<<Writing 232 skew faces to set skewFaces
<<Writing 118 faces with low quality or negative volume decomposition tets to set lowQualityTetFaces
<<Writing 3806 under-determined cells to set underdeterminedCells
fileOk 4
multiple regions: splitMeshRegions
rerun checkMesh
<<Writing 114 skew faces to set skewFaces
<<Writing 1732 under-determined cells to set underdeterminedCells
fileOk 2
multiple regions: splitMeshRegions
rerun checkMesh
<<Writing 6 skew faces to set skewFaces
<<Writing 608 under-determined cells to set underdeterminedCells
fileOk 2
multiple regions: splitMeshRegions
rerun checkMesh
<<Writing 247 under-determined cells to set underdeterminedCells
fileOk 1
multiple regions: splitMeshRegions
rerun checkMesh
<<Writing 125 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 72 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 44 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 27 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 20 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 12 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 5 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 3 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 1 under-determined cells to set underdeterminedCells
fileOk 1
<<Writing 1 under-determined cells to set underdeterminedCells
fileOk 1
fileOk 0
nothing to do