CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (https://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Sample a volume field (https://www.cfd-online.com/Forums/openfoam-post-processing/81853-sample-volume-field.html)

gwierink November 9, 2010 03:47

Sample a volume field
 
Dear all,

I would like to "sample a volume", i.e. write out a volumetric field as raw data. I normally use the sample utility, but that samples points, lines, and surfaces. Does anyone know a trick to write out a volumetric field in the same way sample writes out e.g. a surface? Is the only option writing a "volume function" in the sample utility or python scripting with paraview? Thanks in advance!

olesen November 10, 2010 09:42

Quote:

Originally Posted by gwierink (Post 282717)
Dear all,

I would like to "sample a volume", i.e. write out a volumetric field as raw data. I normally use the sample utility, but that samples points, lines, and surfaces. Does anyone know a trick to write out a volumetric field in the same way sample writes out e.g. a surface? Is the only option writing a "volume function" in the sample utility or python scripting with paraview? Thanks in advance!


"foamToVTK -cellSet" might help.

gwierink November 10, 2010 10:36

Hi Mark,

Thanks for your reply.

I would like to get the alpha field with xyz coordinates out of a twoPhaseEulerFoam case. So, what I now tried is to run cellSet with the following cellSetDict:

Code:

// Name of set to operate on
name alpha;//c0;

// One of clear/new/invert/add/delete|subset/list
action new;

// Actions to apply to cellSet. These are all the topoSetSource's ending
// in ..ToCell (see the meshTools library).

topoSetSources
(
    // Cells with cell centre within box
    boxToCell
    {
      box  (0 0 0) (1 1.5 1);
    }

    // values of field within certain range
    fieldToCell
    {
        fieldName  alpha;//U;      // Note: uses mag(U) since volVectorField
        min        0.0;
        max        1.0;
    }
);

This created a list of cellID's (?) in constant/polyMesh/sets/alpha. Then I did
Code:

foamToVTK -cellSet alpha
which seemingly created the same VTK case but named alpha. Am I doing something wrong here to get out xyz-alpha or overlooking something? Many thanks in advance!

Gitesh P May 8, 2014 07:18

Sampling data in twoPhaseEulerFoam
 
Hello,

I want to store volume averaged values of some fields (i.e., alpha, k and epsilon) only in particular area of interest (around the interface region) in twoPhaseEulerFoam.

Does we have any utility in OpenFOAM which can be useful to achieve the data?

Thanks in advanced!

With regards,
GP

renatogsousa June 5, 2014 06:54

topoSet and foamToVtk
 
take a look at topoSet utility with topoSetDict, I'm still exploring it but it might create a set with the cells that you want according to different filter options.
then use the foamToVtk with the -cellSet option to extract the values from your set.

lukasf March 11, 2021 06:01

Sample volumes instead of only surfaces or points
 
Hi,

there are functions to sample data from points and surfaces.


However, is there a libsampling function which can sample volumes in my domain during runtime?


There are 6 Valid function types :
patchProbes
probes
psiReactionThermoMoleFractions
rhoReactionThermoMoleFractions
sets
surfaces

I tried to use "sets".
Moreover, I used a topoSetDict to define the cell volume of interest:


Code:

actions
(
    {
        name volume_of_interest;
        type cellSet;
        action new;
        source boxToCell;
        sourceInfo
        {
        box (0.1 -0.06 -0.06) (0.16 0.06 0.06);
        }
    }
  );

How can I tell OpenFoam to use this set and sample the cell data?

I tried this but it is not working so far.


Code:

    sample_volume //Name of the sample plane folder in /postprocessing
    {
            type sets;       
            libs (sampling);
            writeControl timeStep;
            timeStart 0;
            writeInterval 1;
            surfaceFormat vtk;
            interpolationScheme cell;

            fields
            (
                U
            );

            sets
            (volume_of_interest);
        }


Agavi June 30, 2021 16:23

Hi lukasf,

any luck with that?

Thank you

lukasf July 1, 2021 02:48

Unfortunately not, any suggestions?

Agavi July 1, 2021 10:57

Hi Lukas,

I couldn't figure a staightforward way. The volFieldValue solution posted in this thread ( https://www.cfd-online.com/Forums/op...tml#post772258 ) produced an empty file in latestTime/ instead of the values at the cells of the cellSet. I'm using OF 2006 so maybe there are some differences there.

I ended up making a coded function object that reads the field and the cellSet Id list and prints just the field values of the cells in the cellSet.

Here is the function object:


sampleCellSet
{
type coded;
libs (utilityFunctionObjects);
name sampleCellSet;
writeControl timeStep;
writeInterval 1;
codeWrite
#{
Info << " Sampling cellSet..." ;

const Time& runTime = mesh().time();

cellSet sampleSet(mesh(), "wakeSet");
volVectorField U = mesh().lookupObject<volVectorField>("U");

fileName timeDir = runTime.path()/runTime.timeName();

if (!isDir(timeDir))
{
mkDir(timeDir);
}

OFstream file(timeDir/"U_wake.dat");

forAll(mesh().C(), id)
{
if (sampleSet[id])
{
file << U[id] << endl;

}
}
Info << "done. " <<endl;

#};
codeInclude
#{
#include "cellSet.H"
#};
codeOptions
#{
#};
}


Surely there are better ways to go about it but for me this is a simple way to avoid interpolations of data, as long as I need the values at the cell centres.

Best

lukasf July 6, 2021 02:44

Hi Agavi,

thanks for sharing.

You are right that the following code returns emtpy fields (OF v1912).

Code:

volFieldValue2
{
    // Mandatory entries (unmodifiable)
    type            volFieldValue;
    libs            (fieldFunctionObjects);

    // Mandatory entries (runtime modifiable)
    fields          (U);
    operation      none;
    regionType      cellZone;
    name              box;

    // Optional entries (runtime modifiable)
    postOperation  none;
    //weightField    alpha1;

    // Optional (inherited) entries

    writeFields    true; //false;

    scalingFactor  1.0;
    region          region0;
    enabled        true;
    log            true;
    writePrecision  8;
    writeToFile    true;
    useUserTime    true;

    timeStart      0;
    timeEnd        1000;

    executeControl  timeStep;
    executeInterval 1;
    writeControl    timeStep; //adjustableRunTime;
    writeInterval  1;
}

The following code from "(https://www.cfd-online.com/Forums/op...tml#post772258)" works for OF 4,5... but not for the ESI OF because "type volRegion" does not exist.

Code:


cellObj1
    {
        type            volRegion;
        libs ("libfieldFunctionObjects.so");
        enabled        true;
        writeControl    adjustableRunTime;
        writeInterval  0.01;
        log            true;
        writeFields    true;
        regionType      cellZone;
        name      d0z;
        operation      none;

        fields
        (
            p
            U
        );
    }

One could probably move this Code from OF 4,5 to the OF ESI Versions (v1912, v2106...)

Your code does work to create .dat files with the field content. Thank you! How do you post process this format to visualize it in ParaView or load it into Matlab / Python? How do you read the matching coordinates of the cells from the cellSet (or cellZone?)

lukasf January 21, 2022 06:29

Hi,

I am planing to do the following to sample volumes and apply the DMD.

1. I need to sample a volume within my simulation. I could not find a function which does this for me. So there are 2 options:
Option1: use a topoSetDict to define a cellSet. Export the cellCenters with paraview to a .csv file. I could read this file and create probe points at those locations of the cellSet. I am not sure how good this idea is if your region of interest has e.g. 10 million cells which would lead to 10 million files.

Option2 is which I prefer. I just save the whole 3D field. Afterwards I use the function foamToVTK to create VTK files which are readable by ParaView:
Code:

foamToVTK -cellSet DMD_cellset -time '0.1:0.2' -useTimeName -ascii -excludePatches '(".*")' -noFaceZones
Then I need a e.g. python script which reads the VTK files into it. Then I will adjust a DMD code from e.g.
https://github.com/mathLab/PyDMD#readme.

Afterwards, I need to save the postprocessing result of the DMD into the VTK format again so that I can postprocess the solution further with Paraview.

I am happy for any ideas to improve this approach.


Lukas

lukasf February 2, 2022 09:23

The problem was using v1912. v2112 does not have a problem using the volFieldValue function.


Code:

    volFieldValue1
    {
        type                volFieldValue;
        libs                ("libfieldFunctionObjects.so");
        log                  false; //true;
        writeControl adjustableRunTime;
        writeFormat    ascii; //binary;
        writeInterval 1e-5;
        writeFields      true;//writes the fields of the volume
        //timeStart      0;
        //timeEnd        1000;
        regionType      cellSet; //cellZone;
        name              box; // box is the cellSet or cellZone defined by the topoSetDict
        operation        none;
        fields
        (
            U T p
        );
    }

This code did not work for the ESI version v1912. The fields where just empty. However, using version V2112 (same code) results in all values written into the files.

One receives a file in postProcessing/volFieldValue1 which is not important.
The volume field is directly written to the time directories or time directories of the processor0.

In this directory I run this command.

Code:

                        foamToVTK -cellSet box -useTimeName -excludePatches '(".*")' -noFaceZones
This way I get .vtk files which are visualized more nicely in ParaView.
Moreover, I source a non ESI openfoam Version for the foamToVTK command because this way the "-useTimeName" is available which I prefer.

Luiz September 19, 2022 11:45

I confirm that this solution also works for v2206, with a small change into the last step, because -useTimeName and -excludePatches entries are now deprecated.
Code:

foamToVTK -cellSet extendedPorousZone -exclude-patches '(".*")' -noFaceZones
However, w
hile OpenFOAM is writing the variable values at the specified writeInterval, foamToVTK is ignoring the variable specific to the volFieldValue (U_cellSet-box). The write intervals are different, the whole simulation is save in a bigger interval than the volFieldValue function, and I hypothesise that this might be the main issue. So far I tried to:
  1. Change -exclude-patches '(".*")' to -no-boundary
  2. Change from cellSet to cellZone
  3. Rename the created variable U_cellSet-box to U
  4. Suppress time 0, as this one has extra variables
Does anyone know how to convert this data into vtk?

lukasf September 20, 2022 03:34

Hi Luiz,

I ended up not using the sample volume field method because of this problem.

My Workflow right now:

1. Saving the whole flow field (Downside: One has to stop the simulation before the sampling interval, to adjust PurgeWrite and the WriteInterval in the controlDict)
2. Reconstructing of the time range of interest
3. Formatting the OpenFOAM solution to VTK and selecting specific volume region (cellSet)
foamToVTK -cellSet DMDVolume -no-point-data -fields '(T U)' -time '0.164960439:0.1699812643'

I think I used "no point data" to reduce the file size.
4. Remove reconstructed fields with foamListTimes -time ‘:’ -rm .. (save disk space)

I am happy if you find a better solution and share it here.

Luiz September 22, 2022 10:23

Hi Lukas,


This sounds like a good workflow, at the end this is what I've doing in the past as well to increase the saving frequency. I did not think about exporting into vtk for long storage, although I will certainly give it a try. I would only suggest a small change into your first step. You can automate changes into any OF file using the following procedure, this implies that you do not have to stop the simulation if you have runTimeModifiable option as yes in controlDict:

Code:

functions
{
    saveFrequencyUpdate
    {
        type                      timeActivatedFileUpdate;
       libs                        ("libutilityFunctionObjects.so");
        writeControl            timeStep;
        writeInterval          1;
        fileToUpdate            "$FOAM_CASE/system/controlDict";
        timeVsFile
        (
            (-1 "$FOAM_CASE/system/controlDict.0")    // Initial time
            (150 "$FOAM_CASE/system/controlDict.150")  // Begin sampling
        );
    }
}

This command will substitute the controlDict file on the run. You only need to specify what is the name of the file (source) and when you want to substitute it.


PS: Since we are both interested into saving disk space. I recommend saving all the data into binary instead of ASCII. This saved a great amount of space in all my simulations, even if I was using compressed ASCII before.

lukasf September 22, 2022 11:49

I think the good thing about vtk format is that:

1. you could reduce your domain further to save disk space

-cellSet DMDVolume

2. you can only extract the fields of interest

-fields '(T U)'

3. python can read and manipulate the files easily

4. paraview can visualize the files



All times are GMT -4. The time now is 20:18.