Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
MeshFactoryMng.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/* MeshFactoryMng.cc (C) 2000-2023 */
9/* */
10/* Mesh factory manager. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/impl/internal/MeshFactoryMng.h"
15
16#include "arcane/impl/internal/MeshMng.h"
17
18#include "arcane/utils/FatalErrorException.h"
19
20#include "arcane/core/MeshHandle.h"
21#include "arcane/core/IPrimaryMesh.h"
22#include "arcane/core/ServiceBuilder.h"
23#include "arcane/core/IMeshFactory.h"
24#include "arcane/core/ItemGroup.h"
25#include "arcane/core/MeshBuildInfo.h"
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane
31{
32
33namespace
34{
35
37 _getMeshFactory(IApplication* app, const MeshBuildInfo& mbi)
38 {
39 String factory_name = mbi.factoryName();
40 ServiceBuilder<IMeshFactory> service_builder(app);
41 Ref<IMeshFactory> mf = service_builder.createReference(factory_name, SB_AllowNull);
42 if (!mf) {
43 StringUniqueArray valid_names;
44 service_builder.getServicesNames(valid_names);
45 String valid_str = String::join(", ", valid_names);
46 ARCANE_FATAL("No mesh factory named '{0}' found for creating mesh '{1}'."
47 " Valid values are {2}",
48 factory_name, mbi.name(), valid_str);
49 }
50 return mf;
51 }
52} // namespace
53
54/*---------------------------------------------------------------------------*/
55/*---------------------------------------------------------------------------*/
56
57MeshFactoryMng::
58MeshFactoryMng(IApplication* app, MeshMng* mesh_mng)
59: m_application(app)
60, m_mesh_mng(mesh_mng)
61{
62}
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67IMeshMng* MeshFactoryMng::
68meshMng() const
69{
70 return m_mesh_mng;
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
76void MeshFactoryMng::
77_checkValidBuildInfo(const MeshBuildInfo& build_info)
78{
79 if (build_info.name().empty())
80 ARCANE_FATAL("Can not create mesh with null name()");
81 if (build_info.factoryName().empty())
82 ARCANE_FATAL("Can not create mesh with null factoryName()");
83}
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88IPrimaryMesh* MeshFactoryMng::
89createMesh(const MeshBuildInfo& build_info)
90{
91 _checkValidBuildInfo(build_info);
92 ItemGroup parent_group = build_info.parentGroup();
93 if (parent_group.null())
94 return _createMesh(build_info);
95 return _createSubMesh(build_info);
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101IPrimaryMesh* MeshFactoryMng::
102_createMesh(const MeshBuildInfo& build_info)
103{
104 if (build_info.parallelMngRef().isNull())
105 ARCANE_FATAL("Can not create mesh with null parallelMngRef()");
106 String name = build_info.name();
107 IMeshMng* mesh_mng = m_mesh_mng;
108 MeshHandle* old_mesh_handle = mesh_mng->findMeshHandle(name, false);
109 if (old_mesh_handle) {
110 IMesh* old_mesh = old_mesh_handle->_internalMeshOrNull();
111 if (old_mesh) {
112 IPrimaryMesh* prm = dynamic_cast<IPrimaryMesh*>(old_mesh);
113 if (!prm)
114 ARCANE_FATAL("A mesh with same name already exists and is not a primary mesh");
115 return prm;
116 }
117 }
118
119 Ref<IMeshFactory> mesh_factory(_getMeshFactory(m_application, build_info));
120
121 // Register the handle first because createMesh() uses it.
122 // It is possible that a handle for this mesh has already been created
123 // (This is especially true for the default mesh).
124 // In this case, check that the mesh to be created does not have a handle
125 // and if findMeshHandle() returns null, the handle will be created.
126 MeshHandle* mh = mesh_mng->findMeshHandle(name, false);
127 if (!mh)
128 mesh_mng->createMeshHandle(name);
129 IPrimaryMesh* mesh = mesh_factory->createMesh(m_mesh_mng, build_info);
130 m_mesh_mng->addMesh(mesh);
131 mesh->build();
132 return mesh;
133}
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
138IPrimaryMesh* MeshFactoryMng::
139_createSubMesh(const MeshBuildInfo& orig_build_info)
140{
141 ItemGroup group = orig_build_info.parentGroup();
142 IMesh* mesh = group.mesh();
144
145 if (mesh->meshMng() != m_mesh_mng)
146 ARCANE_FATAL("This mesh has not been created by this factory");
147 if (group.isAllItems())
148 ARCANE_FATAL("Cannot create sub-mesh from all-items group");
149
150 MeshBuildInfo build_info(orig_build_info);
151 String name = build_info.name();
152 // For a sub-mesh, the IParallelMng must necessarily be that of
153 // the parent mesh
154 build_info.addParallelMng(makeRef(mesh->parallelMng()));
155
156 Ref<IMeshFactory> mesh_factory(_getMeshFactory(m_application, build_info));
157
158 // Register the handle first because createMesh() uses it.
159 // TODO: do this in the 'DynamicMesh' constructor and position
160 // the handle also in the constructor.
161 m_mesh_mng->createMeshHandle(name);
162 IPrimaryMesh* sub_mesh = mesh_factory->createMesh(m_mesh_mng, build_info);
163 m_mesh_mng->addMesh(sub_mesh);
164
165 sub_mesh->defineParentForBuild(mesh, group);
166 sub_mesh->build();
167 mesh->addChildMesh(sub_mesh);
168 return sub_mesh;
169}
170
171/*---------------------------------------------------------------------------*/
172/*---------------------------------------------------------------------------*/
173
174} // End namespace Arcane
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
#define ARCANE_CHECK_POINTER(ptr)
Macro returning the pointer ptr if it is not null or throwing an exception if it is null.
#define ARCANE_FATAL(...)
Macro throwing a FatalErrorException.
Application interface.
Mesh manager interface.
Definition IMeshMng.h:41
virtual MeshHandle createMeshHandle(const String &name)=0
Creates and returns a handle for a mesh with name name.
virtual MeshHandle * findMeshHandle(const String &name, bool throw_exception)=0
Searches for the mesh with name name.
Mesh entity group.
Definition ItemGroup.h:51
bool null() const
true means the group is the null group
Definition ItemGroup.h:75
Parameters necessary for building a mesh.
const String & name() const
Name of the new mesh.
const ItemGroup & parentGroup() const
Parent group in the case of a sub-mesh, null otherwise.
MeshBuildInfo & addParallelMng(Ref< IParallelMng > pm)
Sets the parallelism manager to create the mesh.
Ref< IParallelMng > parallelMngRef() const
Parallelism manager in the case of a new mesh.
const String & factoryName() const
Factory name to create the mesh (via IMeshFactory).
Handle on a mesh.
Definition MeshHandle.h:48
IMesh * mesh() const
Associated mesh.
IMesh * _internalMeshOrNull() const
Definition MeshHandle.h:180
Reference to an instance.
Utility class for instantiating a service of a given interface.
bool empty() const
True if the string is empty (null or "").
Definition String.cc:317
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
@ SB_AllowNull
Allows the service to be absent.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
UniqueArray< String > StringUniqueArray
Dynamic 1D array of strings.
Definition UtilsTypes.h:359