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

Copy mesh instance and save it to another place...

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 22, 2020, 08:15
Default Copy mesh instance and save it to another place...
  #1
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi everybody, today I am playing around with the mesh object and actually I just want to do simple things. Right now, I am working on a new dynamic mesh library. Right at the beginning, I am checking if the mesh optional mesh was already created, if yes, I want to load this one (not a big deal), otherwise I want to save the current available mesh into a new location (this makes trouble). Actually I expect this not be a big deal. However, I am facing some problems. E.g., if I create a new instance of the mesh with the new folder, it (obviously) tries to search for the polyMesh files such as points and so on but actually this folder does not exist right now. In the code below, I create the actual mesh named meshPtr and copy it. Now, I want to save the copy to a new folder. The IOobject however does not allow one to change the path and if I create a new instance of the mesh using the IOobject including the correct path, it tries to search for mesh files which does not exist.

Code:
                                                                                            
        // Generate the actual mesh                                                                          
        autoPtr<dynamicFvMesh> meshPtr                                                                   
        (                                                                                                
            dynamicFvMesh::New                                                                           
            (                                                                                            
                IOobject                                                                                 
                (                                                                                        
                    dynamicFvMesh::defaultRegion,                                                        
                    time().timeName(),                                                                   
                    time(),                                                                              
                    IOobject::MUST_READ                                                                  
                )                                                                                        
            )                                                                                            
        );                                                                                               
                                                                                                 
         dynamicFvMesh& dynmesh = meshPtr();                                                              
        

        // Copy of the dynamic mesh                                                                                   
        Foam::fvMesh mesh = dynmesh;                                                                     


        // Change the save location ... ? IOobject does not allow this
        mesh.newPath("constant/polyMesh2");
        mesh.write();
Any hint related to the c++ objects are welcomed. I believe there is a simple function that can handle that.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   May 22, 2020, 11:01
Default
  #2
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi all, an extension.


Can one tell me why the mesh (object) is not written? I first create a new object that stores the mesh constant/polyMesh. Then, I remove the constant/polyMesh folder and want to write the new mesh, which should actually create the polyMesh folder and the mesh files, shouldn't it? However, even if the write() functions return »true« no files are written.



Code:
        // Create static mesh                                                   
        word location = "";                                                     
        Foam::fvMesh backgroundMesh                                             
        (                                                                       
            Foam::IOobject                                                      
            (                                                                   
                Foam::fvMesh::defaultRegion,                                    
                time().timeName(),                                              
                time(),                                                         
                Foam::IOobject::MUST_READ                                       
            )                                                                   
        );                                                                      
                                                                                
        rmDir ("constant/polyMesh");                                            
                                                                                
        //backgroundMesh = *this;                                               
        backgroundMesh.write();
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 12, 2020, 18:47
Default
  #3
Senior Member
 
Joachim Herb
Join Date: Sep 2010
Posts: 650
Rep Power: 21
jherb is on a distinguished road
Have you tried
Code:
IOobject::AUTO_WRITE
IOobject::READ_IF_PRESENT,
in the call to

Foam::IOobject?
jherb is offline   Reply With Quote

Old   July 13, 2020, 05:57
Default
  #4
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Yes, I also spoke with Henry Weller and what I wanted to do is not the idea of the way FOAM is programmed. After the time object is called (update or what ever), the paths and related instances are updated.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 15, 2020, 06:00
Default
  #5
New Member
 
Yisu Huang
Join Date: Jul 2020
Posts: 2
Rep Power: 0
hys262144 is on a distinguished road
well,I am a new former.I am also troubled by this problem, and the solution I found is as follows.Create an extra polymesh by copying the MY_mesh file and output it by defining its own IOobject.

Code:
        Foam::polyMesh *MY_mesh;
        MY_mesh = new Foam::polyMesh
		(
		Foam::IOobject
		(
		"mybody",
		time_.constant(),
		time_,
		IOobject::MUST_READ,
		IOobject::NO_WRITE
		)
		);


        Foam::IOobject meshIO
		(
		"myoutput",
		time_.timeName(),
		time_,
		IOobject::NO_READ
		);
	Foam::polyMesh mesh_tem
		(
		meshIO,
		Xfer < pointField > ((*MY_mesh).points()),
		Xfer < faceList > ((*MY_mesh).faces()),
		Xfer < labelList > ((*MY_mesh).faceOwner()),
		Xfer < labelList > ((*MY_mesh).faceNeighbour())
		);
        pointField MovingPoints;
	MovingPoints = (*MY_mesh).points() + vector(0, -1, 0);
	mesh_tem.movePoints(MovingPoints);
        mesh_tem.write();
However, there is no boundary information in mesh_tem, so addPatches() may be required.
I hope this helps.
hys262144 is offline   Reply With Quote

Old   July 15, 2020, 06:34
Default
  #6
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi,


but you have to re-create the object always. If the time-step gets updated, the paths gets updated to. For simple geometries this is nice to have but if you have large meshes, the IO/Operations are terrible and I wanted to avoid this. But thank you for your hint. I am thinking about that. By the way, I did not want to have "constant" or the time-folders to take out the mesh. I wanted to have another location such as /constant/backupMesh
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 16, 2020, 10:08
Default
  #7
New Member
 
Yisu Huang
Join Date: Jul 2020
Posts: 2
Rep Power: 0
hys262144 is on a distinguished road
One solution might be to directly output related files,but it looks like terrible

Can I modify runTimeWrite() to output results into one folder?

https://www.openfoam.com/documentati...8C_source.html
hys262144 is offline   Reply With Quote

Old   July 16, 2020, 11:46
Default
  #8
Member
 
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 40
Rep Power: 9
ELwardi is on a distinguished road
Hi,


I'm not sure I got your question right, but I have a suggestion that might work for you:
- You can drop using the copy constructor and construct the copy mesh from
components instead. This way you can fully control the IOobject at construction time.


Be careful though; because these constructors now use move semantics so they can be very fast (Check this one for example from OF7 docs). These are available for both fvMesh and dynamicFvMesh naturally.


I'm ready to help in further discussions of these ideas if you at least clarify the following points:
1. You want a copy of the mesh. Then what's the status of the old one after that? Should be in idle state? Should it be used seperatly from the copy?
2. What's the relationship of the two mesh objects as time progresses? do they have to be kept in sync? Should one be dynamic and the other be a static snapshot of the dynamic one in some past time level?
ELwardi is offline   Reply With Quote

Old   July 19, 2020, 15:31
Default
  #9
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi,



thanks for the reply. The idea is the following:


- Solver start (initialization)

- Load a background mesh (from another location) and save it in the memory
- Mesh the geometry using the background mesh and use this mesh for the calculation
- Solver iterations...
- Save actual mesh
- Move the mesh (dynamic solver)
- Check the quality
- If quality is fine, go on
- if quality is bad, return to the saved mesh and save all patches as stl. Use the background mesh again for creating a new mesh
- Map the values from the old (deformed) mesh to the new mesh
- Go on in the iteration and do the steps above

Tobi
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 20, 2020, 07:37
Default
  #10
Member
 
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 40
Rep Power: 9
ELwardi is on a distinguished road
I see. Well then, two ideas crossed my mind when I read your reply (although not tested):


1. Did you consider using "polMesh::resetMotion()" ? or you could even use "oldPoints() and oldCellCenters()" (from the same class) to some extent. I assume resetMotion takes the mesh back to the "constant" or "0" state, but I'm not quite sure; we'll have to ask around.


Like I said, if you're not expecting Face/cell count changes, you probably can make use of old* methods to efficiently build new meshes as time progresses.


2. I doubt this will work out-of-the-box, but you should be able to make use of "mesh regions". It's certainly a possibility but I think it requires digging into the code and crafting new classes. Here is the general idea:


Code:
// I didn't test this code, so it might not be that accurate

// IOobject::rename() renames mesh region!
polyMesh& pMesh = mesh;
IOobject&  oMesh = pMesh;


oMesh.rename("secondMesh");
pMesh.write()

  // Now the mesh should be written to secondMesh Region instead of the
 default one
 
 // Question is: what happens to default "region0"? if it's still there, we're 

golden. I assume you're told that all paths get updated at the start of the 

timestep, so you need to perform the renaming at least twice in a timestep.
 


  // Note that if the mesh quality is not acceptable, you should be able to 

reset the mesh from "DISK" (simply by reading it again from the correct 

time step, because nothing is written yet!), no need to grab another mesh for this.
In any case, you are attempting to manage two mesh objects at the same time, so it'll be an IO intensive operation.



Hope it helps, at at least, inspire you!
ELwardi is offline   Reply With Quote

Old   July 20, 2020, 08:11
Default
  #11
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Tussenhausen
Posts: 2,708
Blog Entries: 6
Rep Power: 51
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi,



I want to avoid IO operations. Thus, I want to keep everything in the memory. So the idea is for internal combustion engines. Valves and piston motion in general. The idea was to have the background mesh in the memory and mesh the geometry out of the memory. After the mesh motion is to »crazy« and the mesh is distorted too much, I want to remesh the geometry and map the data to the new mesh (from the old one). As faces, cells etc. will change, I am not able to use anything with old points/cells/etc.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   July 20, 2020, 09:38
Default
  #12
Member
 
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 40
Rep Power: 9
ELwardi is on a distinguished road
Oh, I got your problem now.


In fact, I had a similar issue in the past:


I once wanted to build a Unit/Integration testing framework for my libraries and solvers. I had the idea of constructing an in-memory-only case.


Yeah, that was not possible at all, OpenFOAM assigns IOobjects for everything. But if Disk-efficiency is what holds you back, the solution
for your problem may not be related to OpenFOAM altogether!


Take a look at the output of
Code:
df -H
There should be a "/dev/shm" entry in there whose size is about half the total RAM capacity of your system.
That's a ramdisk: It’s a portion of your RAM that is formatted with a file system.


Of course, for my problem, it made no difference and I didn't achieve my goal (still doing tests with embedded cases).


But for you, if Disk-efficiency is the only reason you're doing this, you can put the mesh in that device

(or create your own ramdisks for that matter). It'll workout as if all files are in memory (and they are!).

This will result in some duplication but it's the only way I think of for now .


Also I don't think symlinking to the ramdisk from the hard disk hinders performance (easy to test),

so you can symlink your background mesh to the ramdisk (assuming your case is on the hard disk) and fetch it from there as needed.
ELwardi is offline   Reply With Quote

Old   July 21, 2021, 09:34
Default question on remeshing
  #13
New Member
 
Sócrates Fernández
Join Date: Mar 2020
Location: Cádiz, Spain
Posts: 10
Rep Power: 6
sfernaferna is on a distinguished road
Send a message via Skype™ to sfernaferna
Hi all,

Very interesting thread. I'm a beginner to OpenFOAM programming and I didn't manage to get some things clear from all this. I'm gonna throw the question just in case you can help me clarify some concepts.

I'm trying to develop approximately the same approach as Tobias, but I do not really care about the I/O cost for the moment. Plus, I am calling an external remeshing routine to generate a brand new mesh to substitute the current one. The process I have sketched is:
  1. Do solver iteration (pimple in this case)
  2. Check mesh quality before writing and going back to the beginning of the loop and advance time
  3. If quality not good call the external mesh generation and store it in an auxiliary case directory
  4. map the current flow state to the (built after generation) new mesh
  5. "substitute" the bad quality mesh with the new mesh
  6. Move on to next time step as usual

I'm pretty sure I am not getting the concept right or not describing the process well enough, I apologize in advance for that.

My point is: is the meshToMesh class suitable for doing this? Am I missing some piece?

I've attached my current solver code, see lines 260 to 273 for what I intend to do.

Thanks a lot for contributing so much to the community!
Attached Files
File Type: c pimpleRemeshFoam.C (8.5 KB, 8 views)
sfernaferna is offline   Reply With Quote

Old   July 21, 2021, 15:31
Default
  #14
Member
 
Elwardi Fadeli
Join Date: Dec 2016
Location: Boumerdes, Algeria
Posts: 40
Rep Power: 9
ELwardi is on a distinguished road
Hmm, I'll provide the general guidance to the best of my knowledge:
1. In recent OpenFOAM, fvMesh got interesting (move) constructors:
Code:
// Particularly this one is very easy to use
fvMesh(const IOobject &io, pointField &&points, faceList &&faces, cellList &&cells, const bool syncPar=true)
// So, you can just use: mesh.points(), mesh.faces(), and mesh.cells()
// To copy the mesh to a new object

// A suitable IOobject would look like (no immediate reading):
IOobject oMesh
(
    "meshCopy",                     // region name under constant/ (polyMesh by default)
    runTime.timeName(),
    runTime,
    Foam::IOobject::NO_READ,
    Foam::IOobject::AUTO_WRITE
);
2. Now, after moving the mesh to another object, you can generate the new mesh in constant/polyMesh, read it in, test for quality, and map fields (You can take a look on how mapFields utility does this for better ideas).

3. I usually search the code base for usage examples to see if what I'm trying to do is legit:

Code:
# Look for fvMesh constructor usage, probably I wouldn't use anything that
# was not used in the code base already :)
grep -r $FOAM_SRC -e "fvMesh("
ELwardi is offline   Reply With Quote

Old   July 23, 2021, 13:18
Default thanks for the advice!
  #15
New Member
 
Sócrates Fernández
Join Date: Mar 2020
Location: Cádiz, Spain
Posts: 10
Rep Power: 6
sfernaferna is on a distinguished road
Send a message via Skype™ to sfernaferna
Dear Elwardi,

Thank you so much for the guidance. Sorry for the late reply, I've been fighting with another thing these days. I will start now working on your advice, I'll come back to the thread.

Thanks again.
sfernaferna 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
[Other] dynamicTopoFVMesh and pointDisplacement RandomUser OpenFOAM Meshing & Mesh Conversion 6 April 26, 2018 07:30
Mesh connectivity requirements maero21 SU2 4 July 25, 2016 11:34


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