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

How to save data to a file from OpenFoam code structure?

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 6, 2021, 04:59
Default How to save data to a file from OpenFoam code structure?
  #1
Member
 
Sunag R A
Join Date: Jul 2019
Location: Bangalore, India
Posts: 63
Rep Power: 4
sunagra27 is on a distinguished road
Dear all,


I am working on extracting the faceIndex from a particular patch. For this, I have written the code as below.


Code:
code
    #{

    const fvPatch& mesh = this->patch();
    const fvBoundaryMesh& boundaryMesh = mesh.boundaryMesh();
 
    for (int patch = 0; patch < mesh.boundaryMesh().size(); patch++)
    {
        
        const word& patchName = mesh.boundaryMesh()[patch].name();
        Info << patchName << endl;
        
        for (int facei = 0; facei < mesh.boundaryMesh()[patch].size(); facei++)
        {
            const label& face = boundaryMesh[patch].start() + facei;
            Info << facei << "," << face << endl;
        }
    }
    #};
The code works fine and no issues with the output. But,




1. How to save the output to an external file in a particular folder.?


Any leads will be appreciated.


Regards,
Sunag R A.
sunagra27 is offline   Reply With Quote

Old   May 6, 2021, 14:20
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,273
Rep Power: 31
olesen will become famous soon enougholesen will become famous soon enough
Look somewhere in the OpenFOAM code base for 'OFstream' and you will how it is used. Can also just use a plain 'std:fstream' if you want. Just needs a little effort on your part.
olesen is offline   Reply With Quote

Old   May 6, 2021, 23:50
Default
  #3
Member
 
Sunag R A
Join Date: Jul 2019
Location: Bangalore, India
Posts: 63
Rep Power: 4
sunagra27 is on a distinguished road
Dear Olesen,


Thank you for your reply.

Quote:
Originally Posted by olesen View Post
Look somewhere in the OpenFOAM code base for 'OFstream' and you will how it is used. Can also just use a plain 'std:fstream' if you want. Just needs a little effort on your part.

I have actually used this code in 0/region/T file. Here is the region with codedFixedValue BC. So, if I try to add #include "OFstream.H" inside "codeInclude", but I am unable to continue further.

So, how to proceed with it. Below is the region BC as said.


Code:
gland
    {
        type            codedFixedValue;
        value           #include "actualData";
    name        glandsurface;


    codeInclude
     #{
                #include "OFstream.H"
     #};


     code
    #{

    const fvPatch& mesh = this->patch();
    const fvBoundaryMesh& boundaryMesh = mesh.boundaryMesh();
    
    for (int patch = 0; patch < 1; patch++)
    {
      
        const word& patchName = mesh.boundaryMesh()[patch].name();
        Info << patchName << endl;
        
        for (int facei = 0; facei < mesh.boundaryMesh()[patch].size(); facei++)
        {
            const label& face = boundaryMesh[patch].start() + facei;
            Info << facei << "," << face << endl;
        }
    }
    #};

    }
Regards,
Sunag R A.

Last edited by sunagra27; May 7, 2021 at 01:16.
sunagra27 is offline   Reply With Quote

Old   May 7, 2021, 01:33
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,273
Rep Power: 31
olesen will become famous soon enougholesen will become famous soon enough
Sorry, but I already given you what you need to do. You need to google some and read something.
olesen is offline   Reply With Quote

Old   May 7, 2021, 04:31
Default
  #5
Member
 
Sunag R A
Join Date: Jul 2019
Location: Bangalore, India
Posts: 63
Rep Power: 4
sunagra27 is on a distinguished road
Dear Oselen,


Thank you for the suggestion. I worked on it and it is saving to a file.

Below is the related code in case anyone wants it.


Code:
codeInclude
        #{
                #include "OFstream.H"
        #};  

  
    code
    #{
    const fvPatch& mesh = this->patch();
    const fvBoundaryMesh& boundaryMesh = mesh.boundaryMesh();

    for (int patch = 0; patch < 1; patch++)
    {
        
        const word& patchName = mesh.boundaryMesh()[patch].name();
       
        std::ofstream file;
        file.open("face.txt");


        for (int facei = 0; facei < mesh.boundaryMesh()[patch].size(); facei++)
        {
            const label& face = boundaryMesh[patch].start() + facei;
            file << face << endl;
            file << nl << endl;       // For new line creation.
        }
        file.close();
    }

This is now saving in a file named "file.txt"

Regards,
Sunag R A.
sunagra27 is offline   Reply With Quote

Reply

Tags
code, development, face, openfoam, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
[swak4Foam] funkyDoCalc with OF2.3 massflow NiFl OpenFOAM Community Contributions 14 November 25, 2020 03:30
[swak4Foam] Installation Problem with OF 6 version Aurel OpenFOAM Community Contributions 14 November 18, 2020 16:18
[swak4Foam] swak4foam for OpenFOAM 4.0 mnikku OpenFOAM Community Contributions 77 May 7, 2020 04:22
polynomial BC srv537 OpenFOAM Pre-Processing 4 December 3, 2016 09:07
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 01:24


All times are GMT -4. The time now is 23:13.