14#include "arcane/utils/Collection.h"
15#include "arcane/utils/Enumerator.h"
16#include "arcane/utils/Iostream.h"
17#include "arcane/utils/ScopedPtr.h"
18#include "arcane/utils/StringBuilder.h"
19#include "arcane/utils/CheckedConvert.h"
20#include "arcane/utils/JSONWriter.h"
21#include "arcane/utils/IOException.h"
23#include "arcane/core/PostProcessorWriterBase.h"
24#include "arcane/core/Directory.h"
25#include "arcane/core/FactoryService.h"
26#include "arcane/core/IDataWriter.h"
27#include "arcane/core/IData.h"
28#include "arcane/core/IItemFamily.h"
29#include "arcane/core/VariableCollection.h"
30#include "arcane/core/IParallelMng.h"
31#include "arcane/core/IMesh.h"
33#include "arcane/std/KdiPostProcessor_axl.h"
34#include "arcane/std/internal/VtkCellTypes.h"
36#include "arcane/std/internal/Kdi.h"
67 void endWrite()
override;
92 bool m_is_master_io =
false;
100 KDIChunk* m_kdi_chunk_partless =
nullptr;
107 _numpyDataSet1D(
IData* data);
114 String _getFileNameForTimeIndex(Int32 index)
122 return sb.toString();
131: TraceAccessor(mesh->traceMng())
135 static bool python_initialize =
false;
136 if (!python_initialize) {
154 python_initialize =
true;
162beginWrite(
const VariableCollection& vars)
168 IParallelMng* pm = m_mesh->parallelMng();
171 m_is_master_io =
true;
174 const bool is_first_call = (time_index < 2);
176 pwarning() <<
"L'implémentation au format 'Kdi' est expérimentale";
178 String filename = _getFileNameForTimeIndex(time_index);
185 if (time_index <= 1) {
186 if (m_is_master_io) {
187 dir.createDirectory();
191 info() <<
"KDI begin";
194 m_kdi_full_filename = full_filename.substr(0, full_filename.find_last_of(
"/\\") + 1) +
"Mesh_kdi";
197 info() <<
"KDI createBase m_kdi_base=nullptr";
200 ifstream fichier(filename.c_str());
201 if (!fichier.fail()) {
206 info() <<
"KDI createBase in file";
216 info() <<
"KDI createBase in memory";
220 info() <<
"KDI already createBase";
222 info() <<
"KDI update";
223 m_kdi_base->update(
"UnstructuredGrid",
"/mymesh");
224 info() <<
"KDI chunk";
225 double float_step =
m_times[time_index - 1];
227 info() <<
"KDI chunk " << float_step <<
" " << my_rank;
229 m_kdi_chunk_partless =
m_kdi_base->chunk(float_step);
231 CellGroup all_cells = m_mesh->allCells();
232 NodeGroup all_nodes = m_mesh->allNodes();
234 const Int32 nb_cell = all_cells.size();
235 const Int32 nb_node = all_nodes.size();
237 Int32 total_nb_connected_node = 0;
241 total_nb_connected_node += cell.nodeIds().size();
247 UniqueArray<Int64> cells_connectivity(total_nb_connected_node);
248 UniqueArray<Int64> cells_offset(nb_cell + 1);
249 UniqueArray<unsigned char> cells_ghost_type(nb_cell);
250 UniqueArray<unsigned char> cells_type(nb_cell);
251 UniqueArray<Int64> cells_uid(nb_cell);
254 Int32 connected_node_index = 0;
256 Int32 index = icell.index();
259 cells_uid[index] = icell->uniqueId();
262 bool is_ghost = !cell.isOwn();
264 ghost_type = VtkUtils::CellGhostTypes::DUPLICATECELL;
265 cells_ghost_type[index] = ghost_type;
267 unsigned char vtk_type = VtkUtils::arcaneToVtkCellType(cell.type());
268 cells_type[index] = vtk_type;
269 for (NodeLocalId node : cell.nodeIds()) {
270 cells_connectivity[connected_node_index] = node;
271 ++connected_node_index;
273 cells_offset[index + 1] = connected_node_index;
278 info() <<
"KDI chunk set connectivity begin";
279 info() <<
"KDI DTYPE C++ connectivity";
280 PyArrayObject* parray = _numpyDataSet1D<Int64>(cells_connectivity);
281 m_kdi_chunk->set(
"/mymesh/cells/connectivity", parray);
282 info() <<
"KDI chunk set connectivity end";
285 info() <<
"KDI chunk set cells types begin";
287 info() <<
"KDI DTYPE C++ cells types";
288 PyArrayObject* parray = _numpyDataSet1D<unsigned char>(cells_type);
290 info() <<
"KDI chunk set cells types end";
295 UniqueArray<Int64> nodes_uid(nb_node);
296 UniqueArray<unsigned char> nodes_ghost_type(nb_node);
298 UniqueArray2<Real> points;
299 points.resize(nb_node, 3);
301 Int32 index = inode.index();
304 nodes_uid[index] = inode->uniqueId();
307 bool is_ghost = !node.isOwn();
309 ghost_type = VtkUtils::PointGhostTypes::DUPLICATEPOINT;
310 nodes_ghost_type[index] = ghost_type;
312 Real3 pos = nodes_coordinates[inode];
313 points[index][0] = pos.x;
314 points[index][1] = pos.y;
315 points[index][2] = pos.z;
319 info() <<
"KDI chunk set connectivity begin";
320 PyArrayObject* parray = _numpyDataSet2D<Real>(points);
321 m_kdi_chunk->set(
"/mymesh/points/cartesianCoordinates", parray);
322 info() <<
"KDI chunk set connectivity end";
326 const std::string namefield =
m_kdi_base->update_fields(
"/mymesh/points/fields",
"GlobalNodeId");
327 assert(namefield ==
"/mymesh/points/fields/GlobalNodeId");
328 info() <<
"KDI chunk set GlobalNodeId begin";
329 info() <<
"KDI DTYPE C++ GlobalNodeId";
330 PyArrayObject* parray = _numpyDataSet1D<Int64>(nodes_uid);
332 info() <<
"KDI chunk set GlobalNodeId end";
335 const std::string namefield =
m_kdi_base->update_fields(
"/mymesh/points/fields",
"vtkGhostType");
336 assert(namefield ==
"/mymesh/points/fields/vtkGhostType");
337 info() <<
"KDI chunk set node vtkGhostType begin";
338 info() <<
"KDI DTYPE C++ node vtkGhostType";
339 PyArrayObject* parray = _numpyDataSet1D<unsigned char>(nodes_ghost_type);
341 info() <<
"KDI chunk set node vtkGhostType end";
347 const std::string namefield =
m_kdi_base->update_fields(
"/mymesh/cells/fields",
"GlobalCellId");
348 assert(namefield ==
"/mymesh/cells/fields/GlobalCellId");
349 info() <<
"KDI chunk set GlobalCellId begin";
350 info() <<
"KDI DTYPE C++ cell GlobalCellId";
351 PyArrayObject* parray = _numpyDataSet1D<Int64>(cells_uid);
353 info() <<
"KDI chunk set GlobalCellId end";
356 const std::string namefield =
m_kdi_base->update_fields(
"/mymesh/cells/fields",
"vtkGhostType");
357 assert(namefield ==
"/mymesh/cells/fields/vtkGhostType");
358 info() <<
"KDI chunk set cell vtkGhostType begin";
359 info() <<
"KDI DTYPE C++ cell vtkGhostType";
360 PyArrayObject* parray = _numpyDataSet1D<unsigned char>(cells_ghost_type);
362 info() <<
"KDI chunk set cell vtkGhostType end";
374 template <
typename DataType>
class KDITraits;
376 template <>
class KDITraits<
Int64>
382 std::cout <<
"KDI DTYPE C++ PyArray_LONG" << std::endl;
387 template <>
class KDITraits<
Int32>
393 std::cout <<
"KDI DTYPE C++ PyArray_INT" << std::endl;
398 template <>
class KDITraits<double>
404 std::cout <<
"KDI DTYPE C++ PyArray_DOUBLE" << std::endl;
409 template <>
class KDITraits<unsigned char>
415 std::cout <<
"KDI DTYPE C++ PyArray_UINT8" << std::endl;
426template <
typename DataType> PyArrayObject* KdiDataWriter::
427_numpyDataSet1D(Span<const DataType> values)
429 info() <<
"KDI _numpyDataSet1D begin";
431 npy_intp dims[]{ values.size() };
432 const int py_type = KDITraits<DataType>::kdiType();
433 info() <<
"KDI DTYPE C++ py_type " << py_type;
434 PyArrayObject* vec_array =
reinterpret_cast<PyArrayObject*
>(PyArray_SimpleNew(1, dims, py_type));
435 DataType* vec_array_pointer =
static_cast<DataType*
>(PyArray_DATA(vec_array));
436 KTRACE(trace, vec_array_pointer);
437 std::copy(values.data(), values.data() + values.size(), vec_array_pointer);
438 info() <<
"KDI _numpyDataSet1D end";
446template <
typename DataType> PyArrayObject* KdiDataWriter::
447_numpyDataSet2D(Span2<const DataType> values)
449 info() <<
"KDI _numpyDataSet2D begin";
451 npy_intp dims[2] = { values.dim1Size(), values.dim2Size() };
452 const int py_type = KDITraits<DataType>::kdiType();
453 PyArrayObject* vec_array =
reinterpret_cast<PyArrayObject*
>(PyArray_SimpleNew(2, dims, py_type));
454 DataType* vec_array_pointer =
static_cast<DataType*
>(PyArray_DATA(vec_array));
455 KTRACE(trace, vec_array_pointer);
456 std::copy(values.data(), values.data() + values.dim1Size() * values.dim2Size(), vec_array_pointer);
457 info() <<
"KDI _numpyDataSet2D end";
511 info(4) <<
"Write Kdi var=" <<
var->name();
515 if (
var->dimension() != 1)
516 ARCANE_FATAL(
"Only export of scalar item variable is implemented (name={0})",
var->name());
519 info() <<
"KDI chunk set " <<
var->name() <<
" begin";
529 ARCANE_FATAL(
"Only export of 'Cell' or 'Node' variable is implemented (name={0})",
var->name());
532 info() <<
"KDI DTYPE C++ var->name() " <<
var->name();
547 parray = _numpyDataSetReal3D(data);
550 parray = _numpyDataSetReal2D(data);
553 warning() << String::format(
"Export for datatype '{0}' is not supported (var_name={1})",
data_type,
var->name());
557 info() <<
"KDI chunk set " <<
var->name() <<
" end";
568_numpyDataSet1D(
IData* data)
572 info() <<
"KDI _numpyDataSet1D var/data begin";
574 info() <<
"KDI _numpyDataSet1D var/data end";
582PyArrayObject* KdiDataWriter::
583_numpyDataSetReal3D(IData* data)
585 info() <<
"KDI _numpyDataSetReal3D var/data begin";
586 auto* true_data =
dynamic_cast<IArrayDataT<Real3>*
>(data);
588 SmallSpan<const Real3> values(true_data->view());
589 Int32 nb_value = values.size();
591 UniqueArray2<Real> scalar_values;
592 scalar_values.resize(nb_value, 3);
593 for (
Int32 i = 0; i < nb_value; ++i) {
595 scalar_values[i][0] = v.x;
596 scalar_values[i][1] = v.y;
597 scalar_values[i][2] = v.z;
600 PyArrayObject* vec_array = _numpyDataSet2D<Real>(scalar_values);
601 info() <<
"KDI _numpyDataSetReal3D var/data end";
609PyArrayObject* KdiDataWriter::
610_numpyDataSetReal2D(IData* data)
612 info() <<
"KDI _numpyDataSetReal3D var/data begin";
614 auto* true_data =
dynamic_cast<IArrayDataT<Real2>*
>(data);
616 SmallSpan<const Real2> values(true_data->view());
617 Int32 nb_value = values.size();
618 UniqueArray2<Real> scalar_values;
619 scalar_values.resize(nb_value, 3);
620 for (
Int32 i = 0; i < nb_value; ++i) {
622 scalar_values[i][0] = v.x;
623 scalar_values[i][1] = v.y;
624 scalar_values[i][2] = 0.0;
627 PyArrayObject* vec_array = _numpyDataSet2D<Real>(scalar_values);
628 info() <<
"KDI _numpyDataSetReal3D var/data end";
653 auto w = std::make_unique<KdiDataWriter>(mesh(),
groups());
656 w->setDirectoryName(
dir.file(
"kdi"));
657 m_writer = std::move(
w);
667 std::unique_ptr<IDataWriter> m_writer;
673ARCANE_REGISTER_SERVICE_KDIPOSTPROCESSOR(KdiPostProcessor,
#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.
Generation de la classe de base du Service.
Classe gérant un répertoire.
Interface d'écriture des données d'une variable.
virtual Int32 commRank() const =0
Rang de cette instance dans le communicateur.
virtual Int32 commSize() const =0
Nombre d'instance dans le communicateur.
Interface d'une variable.
KDIChunk * m_kdi_chunk
Identifiant Chunk de la sortie (vtps, ipart) et (vtps, partless)
void write(IVariable *var, IData *data) override
Ecrit les données data de la variable var.
String m_directory_name
Répertoire de sortie.
void setMetaData(const String &meta_data) override
Positionne les infos des méta-données.
KDIBase * m_kdi_base
Identifiant KDI de la sortie.
UniqueArray< Real > m_times
Liste des temps.
std::string m_kdi_full_filename
Nom du fichier HDF courant.
ItemGroupCollection m_groups
Liste des groupes à sauver.
String m_full_filename
Nom du fichier HDF courant.
Post-traitement utilisant Kdi.
void notifyBeginWrite() override
Notifie qu'une sortie va être effectuée avec les paramètres courants.
void close() override
Ferme l'écrivain. Après fermeture, il ne peut plus être utilisé
void notifyEndWrite() override
Notifie qu'une sortie vient d'être effectuée.
IDataWriter * dataWriter() override
Retourne l'écrivain associé à ce post-processeur.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
virtual RealConstArrayView times()
Liste des temps sauvés.
virtual const String & baseDirectoryName()
Nom du répertoire de sortie des fichiers.
virtual ItemGroupCollection groups()
Liste des groupes à sauver.
Structure contenant les informations pour créer un service.
Integer size() const
Nombre d'éléments du vecteur.
Vue constante d'un tableau de type T.
Constructeur de chaîne de caractère unicode.
Chaîne de caractères unicode.
const char * localstr() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
Classe d'accès aux traces.
TraceMessage pwarning() const
TraceMessage warning() const
Flot pour un message d'avertissement.
TraceMessage info() const
Flot pour un message d'information.
ItemGroupT< Cell > CellGroup
Groupe de mailles.
ItemGroupT< Node > NodeGroup
Groupe de noeuds.
MeshVariableScalarRefT< Node, Real3 > VariableNodeReal3
Grandeur au noeud de type coordonnées.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Collection< ItemGroup > ItemGroupCollection
Collection de groupes d'éléments du maillage.
eItemKind
Genre d'entité de maillage.
@ IK_Node
Entité de maillage de genre noeud.
@ IK_Cell
Entité de maillage de genre maille.
unsigned char Byte
Type d'un octet.
eDataType
Type d'une donnée.
@ DT_Int32
Donnée de type entier 32 bits.
@ DT_Real3
Donnée de type vecteur 3.
@ DT_Int64
Donnée de type entier 64 bits.
@ DT_Real2
Donnée de type vecteur 2.
@ DT_Real
Donnée de type réel.