OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
results_writer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
46#ifndef PFC_RESULTS_WRITER_HPP
47#define PFC_RESULTS_WRITER_HPP
48
49#include "types.hpp"
50#include "utils.hpp"
51#include <array>
52#include <iostream>
53#include <mpi.h>
54#include <vector>
55
56namespace pfc {
57
183public:
208 ResultsWriter(const std::string &filename) { m_filename = filename; }
209 virtual ~ResultsWriter() = default;
210
241 virtual void set_domain(const std::array<int, 3> &arr_global,
242 const std::array<int, 3> &arr_local,
243 const std::array<int, 3> &arr_offset) = 0;
244
270 virtual MPI_Status write(int increment, const RealField &data) = 0;
271
285 virtual MPI_Status write(int increment, const ComplexField &data) = 0;
286
287 template <typename T> MPI_Status write(const std::vector<T> &data) {
288 return write(0, data);
289 }
290
291protected:
292 std::string m_filename;
293};
294
360 using ResultsWriter::ResultsWriter;
361
362private:
363 MPI_Datatype m_filetype;
364
365 static MPI_Datatype get_type(RealField) { return MPI_DOUBLE; }
366 static MPI_Datatype get_type(ComplexField) { return MPI_DOUBLE_COMPLEX; }
367
368public:
369 void set_domain(const std::array<int, 3> &arr_global,
370 const std::array<int, 3> &arr_local,
371 const std::array<int, 3> &arr_offset) {
374 &m_filetype);
375 MPI_Type_commit(&m_filetype);
376 };
377
378 MPI_Status write(int increment, const RealField &data) {
379 return write_(increment, data);
380 }
381
382 MPI_Status write(int increment, const ComplexField &data) {
383 return write_(increment, data);
384 }
385
386 template <typename T>
387 MPI_Status write_(int increment, const std::vector<T> &data) {
388 MPI_File fh;
389 std::string filename2 = utils::format_with_number(m_filename, increment);
394 const unsigned int disp = 0;
395 MPI_Datatype type = get_type(data);
396 MPI_File_set_size(fh, filesize); // force overwriting existing data
397 MPI_File_set_view(fh, disp, type, m_filetype, "native", MPI_INFO_NULL);
398 MPI_File_write_all(fh, data.data(), data.size(), type, &status);
400 return status;
401 }
402};
403} // namespace pfc
404
405#endif // PFC_RESULTS_WRITER_HPP
Definition results_writer.hpp:359
MPI_Status write(int increment, const ComplexField &data)
Write a complex-valued field to file at specified time step.
Definition results_writer.hpp:382
Definition results_writer.hpp:182
virtual MPI_Status write(int increment, const ComplexField &data)=0
Write a complex-valued field to file at specified time step.
Represents the global simulation domain (the "world").
Definition world.hpp:91
Common type aliases used throughout OpenPFC.
General utility functions.