Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MeshReaderMng.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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/* MeshReaderMng.h (C) 2000-2024 */
9/* */
10/* Mesh reader manager. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/core/MeshReaderMng.h"
15
16#include "arcane/utils/UniqueArray.h"
17#include "arcane/utils/ScopedPtr.h"
18
19#include "arcane/core/ISubDomain.h"
20#include "arcane/core/IMainFactory.h"
21#include "arcane/core/IMeshReader.h"
22#include "arcane/core/ServiceBuilder.h"
23#include "arcane/core/IPrimaryMesh.h"
24#include "arcane/core/XmlNode.h"
25#include "arcane/core/Properties.h"
26#include "arcane/core/IXmlDocumentHolder.h"
27#include "arcane/core/IParallelMng.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38class IMeshReader;
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
44{
45 public:
46
47 explicit Impl(ISubDomain* sd)
48 : m_sub_domain(sd)
49 {}
50 ~Impl() = default;
51
52 public:
53
54 void checkInit()
55 {
56 if (m_is_init)
57 return;
58 m_is_init = true;
59 ServiceBuilder<IMeshReader> builder(m_sub_domain);
60 m_mesh_readers = builder.createAllInstances();
61 }
62
63 public:
64
65 ConstArrayView<Ref<IMeshReader>> readers() const { return m_mesh_readers; }
66
67 public:
68
69 ISubDomain* m_sub_domain = nullptr;
70 UniqueArray<Ref<IMeshReader>> m_mesh_readers;
71 bool m_is_init = false;
72 bool m_is_use_unit = true;
73};
74
75/*---------------------------------------------------------------------------*/
76/*---------------------------------------------------------------------------*/
77
78MeshReaderMng::
79MeshReaderMng(ISubDomain* sd)
80: m_p(new Impl(sd))
81{
82}
83
84/*---------------------------------------------------------------------------*/
85/*---------------------------------------------------------------------------*/
86
87MeshReaderMng::
88~MeshReaderMng()
89{
90 delete m_p;
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
97readMesh(const String& mesh_name, const String& file_name)
98{
99 ISubDomain* sd = m_p->m_sub_domain;
101 return readMesh(mesh_name, file_name, pm);
102}
103
104/*---------------------------------------------------------------------------*/
105/*---------------------------------------------------------------------------*/
106
107// TODO: merge this method with ISubDomain::cell.
109readMesh(const String& mesh_name, const String& file_name, IParallelMng* parallel_mng)
110{
111 m_p->checkInit();
112 String extension;
113 {
114 // Searches for the file extension and keeps it in \a extension
115 std::string_view fview = file_name.toStdStringView();
116 std::size_t extension_pos = fview.find_last_of('.');
117 if (extension_pos == std::string_view::npos)
118 ARCANE_FATAL("file name '{0}' has no extension", file_name);
119 fview.remove_prefix(extension_pos + 1);
120 extension = fview;
121 }
122 // TODO: eventually, create the mesh via the reader.
123 ISubDomain* sd = m_p->m_sub_domain;
124 IParallelMng* pm = parallel_mng;
125 IPrimaryMesh* mesh = sd->mainFactory()->createMesh(sd, pm, mesh_name);
126
127 // Mesh created.
128 // The mesh may already exist.
129 // In our case, it is an error if it is already allocated.
130 if (mesh->isAllocated())
131 ARCANE_FATAL("Mesh '{0}' already exists and is allocated", mesh_name);
132
133 mesh->properties()->setBool("dump", false);
134
135 String use_unit_str = (m_p->m_is_use_unit) ? "true" : "false";
136 String use_unit_xml = "<?xml version=\"1.0\"?><file use-unit='" + use_unit_str + "' />";
137
138 ITraceMng* tm = sd->traceMng();
140 XmlNode mesh_xml_node = xml_doc->documentNode().documentElement();
141
142 String dir_name;
143 bool is_bad = true;
144 bool use_internal_partition = pm->isParallel();
145 for (auto& reader_ref : m_p->readers()) {
146 IMeshReader* reader = reader_ref.get();
147 if (!reader->allowExtension(extension))
148 continue;
149
150 auto ret = reader->readMeshFromFile(mesh, mesh_xml_node,
151 file_name, dir_name,
152 use_internal_partition);
153 if (ret == IMeshReader::RTOk) {
154 is_bad = false;
155 break;
156 }
157 if (ret == IMeshReader::RTError) {
158 ARCANE_FATAL("Can not read mesh file '{0}'", file_name);
159 }
160 }
161
162 if (is_bad)
163 ARCANE_FATAL("No mesh reader is available for mesh file '{0}'", file_name);
164
165 return mesh;
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
172setUseMeshUnit(bool v)
173{
174 m_p->m_is_use_unit = v;
175}
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
181isUseMeshUnit() const
182{
183 return m_p->m_is_use_unit;
184}
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
188
189} // End namespace Arcane
190
191/*---------------------------------------------------------------------------*/
192/*---------------------------------------------------------------------------*/
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Constant view of an array of type T.
virtual ITraceMng * traceMng() const =0
Trace manager.
virtual IPrimaryMesh * createMesh(ISubDomain *sub_domain, const String &name)=0
Creates or retrieves a mesh.
Interface of the service managing the reading of a mesh.
Definition IMeshReader.h:33
virtual bool allowExtension(const String &str)=0
Checks if the service supports files with the extension str.
virtual eReturnType readMeshFromFile(IPrimaryMesh *mesh, const XmlNode &mesh_element, const String &file_name, const String &dir_name, bool use_internal_partition)=0
Reads a mesh from a file.
@ RTError
Error during the operation.
Definition IMeshReader.h:40
@ RTOk
Operation successfully performed.
Definition IMeshReader.h:39
Interface of the parallelism manager for a subdomain.
virtual IParallelMng * sequentialParallelMng()=0
Returns a sequential parallelism manager.
virtual bool isParallel() const =0
Returns true if the execution is parallel.
Interface of the subdomain manager.
Definition ISubDomain.h:75
virtual IParallelMng * parallelMng()=0
Returns the parallelism manager.
virtual IMainFactory * mainFactory()=0
Main factory.
static IXmlDocumentHolder * loadFromBuffer(Span< const Byte > buffer, const String &name, ITraceMng *tm)
Loads an XML document.
Definition DomUtils.cc:425
IMesh * readMesh(const String &mesh_name, const String &file_name)
Reads the mesh whose file name is file_name.
void setUseMeshUnit(bool v)
If true, indicates that the unit system possibly present in the file format is used (true by default)...
bool isUseMeshUnit() const
Indicates whether the unit system present in the file is used.
Encapsulation of an automatically destructing pointer.
Definition ScopedPtr.h:44
Utility class for instantiating a service of a given interface.
UniqueArray< Ref< InterfaceType > > createAllInstances()
Creates an instance of every service that implements InterfaceType.
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
Definition String.cc:293
std::string_view toStdStringView() const
Returns an STL view of the current string.
Definition String.cc:350
1D data vector with value semantics (STL style).
Node of a DOM tree.
Definition XmlNode.h:51
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --