Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
MeshMng.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MeshMng.cc (C) 2000-2023 */
9/* */
10/* Classe gérant la liste des maillages. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/internal/MeshMng.h"
15
16#include "arcane/utils/String.h"
17#include "arcane/utils/FatalErrorException.h"
18
19#include "arcane/core/IPrimaryMesh.h"
20#include "arcane/core/MeshHandle.h"
21#include "arcane/core/IVariableMng.h"
22#include "arcane/core/internal/IVariableMngInternal.h"
23
24#include "arcane/impl/internal/MeshFactoryMng.h"
25
26/*---------------------------------------------------------------------------*/
27/*---------------------------------------------------------------------------*/
28
29namespace Arcane
30{
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35MeshMng::
36MeshMng(IApplication* app,IVariableMng* vm)
37: TraceAccessor(vm->traceMng())
38, m_variable_mng(vm)
39, m_mesh_factory_mng(new MeshFactoryMng(app,this))
40{
41}
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46MeshMng::
47~MeshMng()
48{
49 delete m_mesh_factory_mng;
50}
51
52/*---------------------------------------------------------------------------*/
53/*---------------------------------------------------------------------------*/
54
55void MeshMng::
56addMesh(IMesh* mesh)
57{
59 String name = mesh->name();
60 info() << "Add mesh name=" << mesh->name() << " count=" << meshes().size();
61 //std::cout << " ** ** ADD_MESH name=" << name << " this=" << mesh << '\n';
62 // Regarde si un handle sur le maillage est déjà présent.
63 // Si c'est le cas, l'utilise.
64 MeshHandle* handle_ptr = findMeshHandle(name,false);
65 if (handle_ptr){
66 // Vérifie que le handle n'a pas déjà de maillage associé ou si c'est le
67 // cas que c'est le même maillage que celui qu'on a déjà
68 // TODO: cela ne devrait pas être autorisé quand même car le maillage
69 // va être ajouté plusieurs fois dans la liste des maillages via
70 // m_meshes.add().
71 if (handle_ptr->hasMesh()){
72 IMesh* current_mesh = handle_ptr->mesh();
73 //std::cout << " ** ** FOUND_HANDLE ref=" << handle_ptr->reference()
74 // << " old_mesh=" << current_mesh << " new_mesh=" << mesh << '\n';
75 if (current_mesh!=mesh)
76 ARCANE_FATAL("MeshHandle '{0}' already have mesh",name);
77 }
78 else
79 handle_ptr->_setMesh(mesh);
80 }
81 else{
82 MeshHandle handle = _addMeshHandle(name);
83 handle._setMesh(mesh);
84 }
85 _rebuildMeshList();
86}
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90
91IMesh* MeshMng::
92getMesh(Integer index) const
93{
94 return m_meshes_handle[index].mesh();
95}
96
97/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
100IPrimaryMesh* MeshMng::
101getPrimaryMesh(Integer index) const
102{
103 return getMesh(index)->toPrimaryMesh();
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
109MeshHandle* MeshMng::
110findMeshHandle(const String& name,bool throw_exception)
111{
112 for( auto& handle : m_meshes_handle )
113 if (handle.meshName()==name)
114 return &handle;
115 if (throw_exception)
116 ARCANE_FATAL("no MeshHandle named '{0}' found",name);
117 return nullptr;
118}
119
120/*---------------------------------------------------------------------------*/
121/*---------------------------------------------------------------------------*/
122
123MeshHandle MeshMng::
124findMeshHandle(const String& name)
125{
126 MeshHandle* handle = findMeshHandle(name,true);
127 //std::cout << " ** ** FOUND_HANDLE name=" << name << " ptr=" << handle->reference()
128 // << " mesh=" << handle->mesh() << '\n';
129 return MeshHandle(*handle);
130}
131
132/*---------------------------------------------------------------------------*/
133/*---------------------------------------------------------------------------*/
134
135MeshHandle MeshMng::
136createMeshHandle(const String& name)
137{
138 MeshHandle* old_handle = findMeshHandle(name,false);
139 if (old_handle)
140 ARCANE_FATAL("mesh handle already exists for name '{0}'",name);
141 return _addMeshHandle(name);
142}
143
144/*---------------------------------------------------------------------------*/
145/*---------------------------------------------------------------------------*/
146
147IMesh* MeshMng::
148findMesh(const String& name,bool throw_exception)
149{
150 const MeshHandle* handle = findMeshHandle(name,throw_exception);
151 if (!handle)
152 return nullptr;
153 IMesh* mesh = handle->mesh();
154 if (!mesh && throw_exception)
155 ARCANE_FATAL("no mesh named '{0}' found",name);
156 return mesh;
157}
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162ConstArrayView<IMesh*> MeshMng::
163meshes() const
164{
165 return m_meshes;
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171void MeshMng::
172destroyMeshes()
173{
174 for( MeshHandle& handle : m_meshes_handle ){
175 IMesh* x = handle.mesh();
176 if (x){
177 m_variable_mng->_internalApi()->detachMeshVariables(x);
178 handle._destroyMesh();
179 }
180 }
181 m_meshes_handle.clear();
182 m_meshes.clear();
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
188void MeshMng::
189destroyMesh(MeshHandle handle)
190{
191 if (!handle.hasMesh())
192 return;
193 IMesh* mesh = handle.mesh();
194 if (!mesh->isPrimaryMesh())
195 ARCANE_FATAL("Can not destroy mesh '{0}' because it is not a primary mesh",mesh->name());
196 _destroyMesh(mesh->toPrimaryMesh());
197}
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202void MeshMng::
203_destroyMesh(IPrimaryMesh* primary_mesh)
204{
205 IMesh* m = primary_mesh;
206 String name = m->name();
207 MeshHandle handle = m->handle();
208 m_variable_mng->_internalApi()->detachMeshVariables(m);
209 handle._destroyMesh();
210
211 // Supprime le maillage de la liste.
212 for( Integer i=0, n=m_meshes.size(); i<n; ++i ){
213 if (m_meshes[i]==m){
214 m_meshes.remove(i);
215 break;
216 }
217 }
218
219 // Supprime le MeshHandle de la liste
220 for( Integer i=0, n=m_meshes_handle.size(); i<n; ++i ){
221 if (m_meshes_handle[i].meshName()==name){
222 m_meshes_handle.remove(i);
223 break;
224 }
225 }
226
227 _rebuildMeshList();
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
233MeshHandle MeshMng::
234_addMeshHandle(const String& name)
235{
236 //std::cout << "_ADD_MESH_HANDLE handle=" << name << "\n";
237 MeshHandle handle(m_variable_mng->_internalApi()->internalSubDomain(),name);
238 m_meshes_handle.add(handle);
239 return handle;
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
245void MeshMng::
246_rebuildMeshList()
247{
248 m_meshes.clear();
249 for( MeshHandle& h : m_meshes_handle ){
250 if (!h.hasMesh())
251 continue;
252 m_meshes.add(h.mesh());
253 }
254}
255
256/*---------------------------------------------------------------------------*/
257/*---------------------------------------------------------------------------*/
258
260meshFactoryMng() const
261{
262 return m_mesh_factory_mng;
263}
264
265/*---------------------------------------------------------------------------*/
266/*---------------------------------------------------------------------------*/
267
268MeshHandle MeshMng::
269createDefaultMeshHandle(const String& name)
270{
271 m_default_mesh_handle = createMeshHandle(name);
272 return m_default_mesh_handle;
273}
274
275/*---------------------------------------------------------------------------*/
276/*---------------------------------------------------------------------------*/
277
278} // End namespace Arcane
279
280/*---------------------------------------------------------------------------*/
281/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
virtual MeshHandle handle() const =0
Handle sur ce maillage.
virtual String name() const =0
Nom du maillage.
Interface du gestionnaire de fabriques de maillages.
virtual IMeshInternal * _internalApi()=0
API interne à Arcane.
virtual IPrimaryMesh * toPrimaryMesh()=0
Retourne l'instance sous la forme d'un IPrimaryMesh.
virtual bool isPrimaryMesh() const =0
Indique si l'instance est un maillage primaire.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Handle sur un maillage.
Definition MeshHandle.h:47
bool hasMesh() const
Indique si le maillage associé a déjà été créé (i.e: mesh() est valide)
IMesh * mesh() const
Maillage associé.
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-