CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Running, Solving & CFD

Prevent OpenFOAM from creating a new postProcessing folder after restart

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 3 Post By floquation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 30, 2017, 05:01
Default Prevent OpenFOAM from creating a new postProcessing folder after restart
  #1
Senior Member
 
Join Date: Mar 2015
Posts: 250
Rep Power: 12
KateEisenhower is on a distinguished road
Hello OpenFOAM Users,

is there a quick and easy way to prevent OpenFOAM from creating a new time folder in the postProcessing directory after restarting a calculation?
For example I use functionObjects to write out forces in runtime. And I would like to have OF add the new data to the initial forces file instead.

Best regards and thank you for your help,

Kate
KateEisenhower is offline   Reply With Quote

Old   January 30, 2017, 10:02
Default
  #2
Senior Member
 
floquation's Avatar
 
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 20
floquation will become famous soon enough
I never heard of that possibility.
Luckily, it is not too difficult to combine the files into a new file and work with it.
The script below is an excerpt from a larger script of mine that will do the job for you.

Code:
#! /usr/bin/sh
# @Author: Kevin van As; PhD Student; TU Delft - Transport Phenomena Research Group
# @Date: 30/01/2017
# Assumptions:
#   1. There is only one datafile within each time directory. Warning will print if this is not the case.
#   2. The time of the restart must be contained in the previous time's data file.
#      This is always the case in a restart.
#      In case this is not the case, the files will simply be merged without removing an eventual overlap.
# Changelog:
#   [30/01/2017] Creation.
#   [31/01/2017] Improved comments / error messages. $((lineNumber-1)) --> $((lineNumber))


args="${@}"

###################
#### FUNCTIONS ####
###################

usage () # prints a usage message. $1=error code (0 sends message to stdout, other number to errout)
{
    channelOut="2"
    if [ "$1" = "0" ]; then
        channelOut="1"
    fi

    echo \
    "
    Received input:
    '$0 $args'

    Usage: $0 args [> outputFile]
    " 1>&$channelOut


    echo \
    "
    Obligatory Arguments:
       -i : The directory name that holds multiple time directories. For example: \"./postProcessing/funcName\".
    Optional Arguments:
       -ro, --remove-overlap : Remove temporal data overlap [default behavior]
       -nro, --no-remove-overlap : Keep temporal data overlap
       -h, --h, -help, --help : Prints this message and quit
    " 1>&$channelOut

    exit $1
}

printerr () # echos a message to the error stream
{
    echo "$*" >&2
}


###############################
#### READ INPUT PARAMETERS ####
###############################

# Declare optional arguments with a default parameter
removeOverlap=true

# Iterate over arguments
while [ $# -gt 0 ]
do
    case "$1" in
        -i) parentDir="$2"; shift 2 ;;
        -ro | --remove-overlap) removeOverlap=true; shift ;;
        -nro | --no-remove-overlap) removeOverlap=false; shift ;;
        --help | -help | -h | --h) usage 0 ;;
        *) printerr ""
           printerr "ERROR: Unknown argument \"$1\""
           printerr ""
           usage 1 ;; # Unknown argument -> error
    esac
done
# Enforce obligatory arguments
[ "$parentDir" = "" ] && usage 1


###########################
#### THE ACTUAL SCRIPT ####
###########################

# Find the data:
cd "$parentDir" >/dev/null
    # Analyse structure of the directory
    timeDirs=(*) # Array of all time directories
    numTimeDirs="${#timeDirs[@]}"

    # Sort timeDirs numerically:
    IFS=$'\n' timeDirs=($(sort -g <<<"${timeDirs[*]}"))
    unset IFS

    #echo "timeDirs=${timeDirs[@]}"

    # Now start combining data files
    #echo "Matching lines..."
    timeDirCounter=0
    for timeDir in ${timeDirs[@]} # Iterate over timeDirs to combine the files
    do
        #echo "  Time: $timeDir"
        timeDirCounter=$((timeDirCounter+1))

        time=${timeDirs[$timeDirCounter]} # The name of the next timedir is the "cut-off" point for the datafile of the current timedir. Note: it is blank "" for the last timedir.
        #echo "   Find time = $time"
        dataFiles=("$timeDir"/*) #TODO: Iterate over datafiles inside the timedir (just in case there are multiple)?
        dataFile="${dataFiles[0]}"
        if [ "${#dataFiles[@]}" -gt 1 ]; then
            printerr ""
            printerr "WARNING:"
            printerr "  This code assumes there is only one dataFile inside each time directory, but ${#dataFiles[@]} were found!"
            printerr "  These are: ${dataFiles[@]}"
            printerr "  The code will continue using only the first dataFile: $(basename $dataFile)"
            printerr ""
        fi

        #echo "    Datafile: $dataFile"

        # Print header if first.
        if [ "$timeDirCounter" = 1 ]; then
            cat "$dataFile" | head -1
        fi

        # This is the last file, treat it differently.
        if [ "$time" == "" ]; then
            cat "$dataFile" | tail -n +2
            break
        fi

        # For all other files, scan the overlap, and then print the relevant part
        if [ "$removeOverlap" = true ]; then # Disallow overlap
            timeLine=`grep -n -P -o "^\s*$time\s+" "$dataFile" | head -1 | tail -1`;
            theTime=`echo "$timeLine" | sed 's/.*://' | sed 's/\s//g'` # for sanity check
            lineNumber=`echo "$timeLine" | sed 's/:.*//'` # for 'head'
            #echo "theTime=$theTime"
            #echo "lineNumber=$lineNumber"

            if [ "$timeLine" = "" ]; then # There is no overlap between files!
                cat "$dataFile" | tail -n +2
                continue
            fi

            if [ "$theTime" != "$time" ]; then # sanity-check
                printerr "FATAL ERROR"
                printerr "  ERROR IN CODE LOGIC (probably the regex): SANITY CHECK FAILED: FIRSTNUMBER \"$theTime\" IS NOT EQUAL TO TIME \"$time\""
                exit
            fi

            # All is OK, so output the relevant part of $dataFile: before $time, but without header
            cat "$dataFile" | head -$((lineNumber)) | tail -n +2
            continue
        else # Allow overlap, so just cat the file.
            cat "$dataFile" | tail -n +2
            continue
        fi

        #echo "  timeDir(\"$timeDir\") read. Next file."
    done # timeDir
cd - >/dev/null



## Done!

Last edited by floquation; January 31, 2017 at 03:50. Reason: Small error in script: "-1" mistake.
floquation is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenFOAM Training: Programming CFD Course 12-13 and 19-20 April 2016 cfd.direct OpenFOAM Announcements from Other Sources 0 January 14, 2016 10:19
OpenFOAM v3.0.1 Training, London, Houston, Berlin, Jan-Mar 2016 cfd.direct OpenFOAM Announcements from Other Sources 0 January 5, 2016 03:18
OpenFOAM Training, London, Chicago, Munich, Sep-Oct 2015 cfd.direct OpenFOAM Announcements from Other Sources 2 August 31, 2015 13:36
[OpenFOAM.org] How to install OpenFoam from its folder immortality OpenFOAM Installation 12 August 9, 2015 11:20
Superlinear speedup in OpenFOAM 13 msrinath80 OpenFOAM Running, Solving & CFD 18 March 3, 2015 05:36


All times are GMT -4. The time now is 02:45.