Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
VtuMeshWriter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 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/* VtuMeshWriter.cc (C) 2000-2018 */
9/* */
10/* Lecture/Ecriture d'un fichier au format VtuMeshWriter. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15#include "arcane/utils/Iostream.h"
16#include "arcane/utils/StdHeader.h"
17#include "arcane/utils/HashTableMap.h"
18#include "arcane/utils/ValueConvert.h"
19#include "arcane/utils/ScopedPtr.h"
20#include "arcane/utils/ITraceMng.h"
21#include "arcane/utils/String.h"
22#include "arcane/utils/IOException.h"
23#include "arcane/utils/Collection.h"
24#include "arcane/utils/Enumerator.h"
25#include "arcane/utils/NotImplementedException.h"
26#include "arcane/utils/Real3.h"
27
28#include "arcane/FactoryService.h"
29#include "arcane/IMainFactory.h"
30#include "arcane/IMeshReader.h"
31#include "arcane/ISubDomain.h"
32#include "arcane/IMesh.h"
33#include "arcane/IMeshSubMeshTransition.h"
34#include "arcane/IItemFamily.h"
35#include "arcane/Item.h"
36#include "arcane/ItemEnumerator.h"
37#include "arcane/VariableTypes.h"
38#include "arcane/IVariableAccessor.h"
39#include "arcane/IParallelMng.h"
40#include "arcane/IIOMng.h"
41#include "arcane/IXmlDocumentHolder.h"
42#include "arcane/XmlNodeList.h"
43#include "arcane/XmlNode.h"
44#include "arcane/IMeshUtilities.h"
45#include "arcane/IMeshWriter.h"
46#include "arcane/BasicService.h"
47#include "arcane/utils/PlatformUtils.h"
48
49#include "arcane/AbstractService.h"
50
51#include <vtkXMLUnstructuredGridReader.h>
52#include <vtkIdTypeArray.h>
53#include "vtkXMLUnstructuredGridWriter.h"
54#include <vtkUnstructuredGrid.h>
55#include <vtkCell.h>
56#include <vtkObject.h>
57#include <vtkDataObject.h>
58#include <vtkDataSet.h>
59#include <vtkCellData.h>
60#include <vtkPointData.h>
61#include <vtkDataArray.h>
62#include <vtkLongArray.h>
63#include <vtkIntArray.h>
64#include <vtkFieldData.h>
65
66#include <map>
67
68/*---------------------------------------------------------------------------*/
69/*---------------------------------------------------------------------------*/
70
71ARCANE_BEGIN_NAMESPACE
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
79: public AbstractService
80, public IMeshWriter
81{
82 public:
85
86 virtual void build() {}
87 virtual bool writeMeshToFile(IMesh* mesh,const String& file_name);
88
89 protected:
90 void _writeFieldGroupsFromData(vtkFieldData* field_data, ItemGroup groups);
91};
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96ARCANE_REGISTER_SERVICE(VtuMeshWriter,
97 ServiceProperty("VtuNewMeshWriter",ST_SubDomain),
98 ARCANE_SERVICE_INTERFACE(IMeshWriter));
99
100/*---------------------------------------------------------------------------*/
101/*---------------------------------------------------------------------------*/
102
103/*****************************************************************\
104 * _writeFieldGroupsFromData
105 uniqueId().asInteger
106 localId
107\*****************************************************************/
108void VtuMeshWriter::
109_writeFieldGroupsFromData(vtkFieldData*fieldData,ItemGroup group)
110{
111 vtkLongArray* a = vtkLongArray::New();
112 a->SetName(group.name().localstr());
113
114 a->InsertNextValue(group.itemKind());
115
116 ENUMERATE_ITEM(iitem, group){
117 a->InsertNextValue(iitem->uniqueId());
118 }
119
120 a->SetNumberOfTuples(a->GetNumberOfTuples());
121 fieldData->AddArray(a);
122 a->Delete();
123}
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
137bool VtuMeshWriter::
138writeMeshToFile(IMesh* mesh,const String& file_name)
139{
140
141 Integer mesh_nb_node = mesh->nbNode();
142 Integer mesh_nb_cell = mesh->nbCell();
143 std::map<Int64,Integer> uid_to_idx_map; // Unique nodes-IDs array
144 info() << "[VtuMeshWriter::writeMeshToFile] mesh_nb_node=" <<mesh_nb_node << " mesh_nb_cell="<< mesh_nb_cell
145 << " all=" << mesh->allNodes().size() << ", own=" << mesh->ownNodes().size();
146
147
148 /*************************\
149 * VTK-side initialisation *
150 \*************************/
151 vtkPoints* points = vtkPoints::New();
152 points->SetDataTypeToDouble();
153 vtkUnstructuredGrid* grid = vtkUnstructuredGrid::New();
154 grid->Allocate(mesh_nb_cell, mesh_nb_cell);
155
156 /*************************\
157 * ARC-side initialisation *
158 \*************************/
159 VariableItemReal3& nodes_coords = PRIMARYMESH_CAST(mesh)->nodesCoordinates();
160
161 /*************************\
162 * Saving Cells Unique IDs *
163 \*************************/
164 info() << "[writeMeshToFile] Creating array of CELLS Unique IDs";
165 vtkCellData* vtk_cell_data = grid->GetCellData(); // Data associated to Cells
166 vtkLongArray* vtk_cell_uids = vtkLongArray::New();
167 vtk_cell_uids->SetName("CellsUniqueIDs");
169 Cell cell = *iCell;
170 vtk_cell_uids->InsertNextValue(cell.uniqueId());
171 }
172 vtk_cell_data->AddArray(vtk_cell_uids); // Now add our Cells' UniqueIDs array
173 vtk_cell_uids->Delete();
174
175 /*************************\
176 * Saving Nodes Unique IDs *
177 \*************************/
178 info() << "[writeMeshToFile] Creating array of NODES Unique IDs";
179 vtkPointData *vtk_point_data=grid->GetPointData(); // Data associated to Points
180 vtkLongArray *vtk_point_uids = vtkLongArray::New();
181 vtk_point_uids->SetName("NodesUniqueIDs");
182 vtkIntArray *vtk_point_owners = vtkIntArray::New();
183 vtk_point_owners->SetName("NodesOwner");
184 Integer index = 0;
186 Node node = *inode;
187 Int64 uid = node.uniqueId();
189 points->InsertNextPoint(Convert::toDouble(coord.x),
190 Convert::toDouble(coord.y),
191 Convert::toDouble(coord.z));
192 uid_to_idx_map.insert(std::make_pair(uid,index));
193 vtk_point_uids->InsertNextValue(uid);
194 vtk_point_owners->InsertNextValue(node.owner());
195 ++index;
196 }
198 vtk_point_uids->Delete();
199
201 vtk_point_owners->Delete();
202
203 /*****************************\
204 * Now setting point into grid *
205 \*****************************/
206 info() << "[writeMeshToFile] Now setting point into grid";
207 grid->SetPoints(points);
208
209 /**********************************************\
210 * Scanning cells' nodes to create connectivity *
211 \**********************************************/
212 info() << "[writeMeshToFile] Now scanning cells' nodes to create connectivity";
213 vtkIdList* vtk_point_ids = vtkIdList::New();
215 Cell cell = *iCell;
216 int nb_node = cell.nbNode();
217 vtk_point_ids->Allocate(nb_node);
218
219 for( Integer j=0; j<nb_node; ++j ){
220 Int64 node_uid = cell.node(j).uniqueId();
221 auto x = uid_to_idx_map.find(node_uid);
222 if (x==uid_to_idx_map.end())
223 ARCANE_FATAL("InternalError: no index for uid '{0}'",node_uid);
224 vtk_point_ids->InsertNextId(x->second);
225 }
226
227 int vtk_item = IT_NullType;
228 switch(cell.type()){
229 // Linear cells
230 case(IT_Tetraedron4):
231 vtk_item = VTK_TETRA;
232 break;
233 case(IT_Hexaedron8):
234 vtk_item = VTK_HEXAHEDRON;
235 break;
236 case(IT_Pyramid5):
237 vtk_item = VTK_PYRAMID;
238 break;
239
240 // Mesh Generator simple-1&2
241 case(IT_Octaedron12):
242 vtk_item = VTK_HEXAGONAL_PRISM;
243 break;
244 case(IT_Heptaedron10):
245 vtk_item = VTK_PENTAGONAL_PRISM;
246 break;
247
248 // Prisme
249 case(IT_Pentaedron6):
250 vtk_item = VTK_WEDGE;
251 break;
252
253 // A demander
254 case(IT_HemiHexa7):
255 info() << "VTK_POLY_VERTEX";
256 vtk_item = VTK_POLY_VERTEX;
257 break;
258 case(IT_HemiHexa6):
259 info() << "VTK_POLY_VERTEX";
260 vtk_item = VTK_POLY_VERTEX;
261 break;
262 case(IT_HemiHexa5):
263 info() << "VTK_POLY_VERTEX";
264 vtk_item = VTK_POLY_VERTEX;
265 break;
266 case(IT_AntiWedgeLeft6):
267 info() << "VTK_POLY_VERTEX";
268 vtk_item = VTK_POLY_VERTEX;
269 break;
270 case(IT_AntiWedgeRight6):
271 info() << "VTK_POLY_VERTEX";
272 vtk_item = VTK_POLY_VERTEX;
273 break;
274 case(IT_DiTetra5):
275 info() << "VTK_POLY_VERTEX";
276 vtk_item = VTK_POLY_VERTEX;
277 break;
278 /* Others not yet implemented */
279 default:
280 info() << "[writeMeshToFile] Cell type not suported (" << cell.type() << ")";
281 throw NotSupportedException(A_FUNCINFO);
282 }
283 grid->InsertNextCell(vtk_item, vtk_point_ids);
284 vtk_point_ids->Reset();
285 }
286 vtk_point_ids->Delete();
287
288
289 /***********************\
290 * Fetching Other Groups *
291 \***********************/
292 info() << "[writeMeshToFile] ## Now Fetching Groups ##";
293 vtkFieldData* vtkFieldDataGroups = grid->GetFieldData();
294
296 if (igroup->isAllItems())
297 continue;
298 info() << "[writeMeshToFile] Found a " << igroup->itemKind() << "-group " << igroup->name();
299 _writeFieldGroupsFromData(vtkFieldDataGroups, *igroup);
300 }
301
302 /************************\
303 * Now prepare for output *
304 \************************/
305 vtkXMLUnstructuredGridWriter* vtk_grid_writer = vtkXMLUnstructuredGridWriter::New();
306 vtk_grid_writer->SetInputData(grid);
308 vtk_grid_writer->SetFileName(fileNameDotVtu.localstr());
309 info() << "[writeMeshToFile] SetFileName " << fileNameDotVtu;
310 String isAscii = platform::getEnvironmentVariable("ARCANE_VTU_DATA_MODE_TO_ASCII");
311 if (!isAscii.null())
312 vtk_grid_writer->SetDataModeToAscii();
313 else
314 vtk_grid_writer->SetDataModeToBinary();
315 vtk_grid_writer->Write();
316
317 /**********************\
318 * And cleanup a little *
319 \**********************/
320 points->Delete();
321 grid->Delete();
322 vtk_grid_writer->Delete();
323 info() << "[writeMeshToFile] Done";
324 return false;
325}
326
327/*---------------------------------------------------------------------------*/
328/*---------------------------------------------------------------------------*/
329
330ARCANE_END_NAMESPACE
331
332/*---------------------------------------------------------------------------*/
333/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro envoyant une exception FatalErrorException.
#define ENUMERATE_CELL(name, group)
Enumérateur générique d'un groupe de mailles.
#define ENUMERATE_ITEM(name, group)
Enumérateur générique d'un groupe de noeuds.
#define ENUMERATE_NODE(name, group)
Enumérateur générique d'un groupe de noeuds.
#define ARCANE_SERVICE_INTERFACE(ainterface)
Macro pour déclarer une interface lors de l'enregistrement d'un service.
Classe de base d'un service.
Maille d'un maillage.
Definition Item.h:1178
virtual NodeGroup ownNodes()=0
Groupe de tous les noeuds propres au domaine.
virtual Integer nbCell()=0
Nombre de mailles du maillage.
virtual Integer nbNode()=0
Nombre de noeuds du maillage.
virtual NodeGroup allNodes()=0
Groupe de tous les noeuds.
virtual CellGroup allCells()=0
Groupe de toutes les mailles.
Interface d'un service d'écriture d'un maillage.
Definition IMeshWriter.h:36
virtual ItemGroupCollection groups()=0
Liste des groupes.
Groupe d'entités de maillage.
Definition ItemGroup.h:49
Node node(Int32 i) const
i-ème noeud de l'entité
Definition Item.h:768
Int32 nbNode() const
Nombre de noeuds de l'entité
Definition Item.h:765
Int32 owner() const
Numéro du sous-domaine propriétaire de l'entité
Definition Item.h:229
ItemUniqueId uniqueId() const
Identifiant unique sur tous les domaines.
Definition Item.h:216
Int16 type() const
Type de l'entité
Definition Item.h:232
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Noeud d'un maillage.
Definition Dom.h:204
Classe gérant un vecteur de réel de dimension 3.
Definition Real3.h:132
Structure contenant les informations pour créer un service.
Ecriture des fichiers de maillage au format VTU (de VTK).
virtual void build()
Construction de niveau build du service.
Exception lorsqu'une opération n'est pas supportée.
Chaîne de caractères unicode.
#define ARCANE_REGISTER_SERVICE(aclass, a_service_property,...)
Macro pour enregistrer un service.