2 #include "../../misc/hdf5_utils.hpp" 3 #include "../../misc/lazy_list.hpp" 5 #include "../../tessellation/VoronoiMesh.hpp" 51 template<
class T> vector<T> read_vector_from_hdf5
53 const string& caption,
54 const DataType& datatype)
56 DataSet dataset = file.openDataSet(caption);
57 DataSpace filespace = dataset.getSpace();
59 filespace.getSimpleExtentDims(dims_out, NULL);
60 const size_t NX =
static_cast<size_t>(dims_out[0]);
62 dataset.read(&result[0], datatype);
66 vector<double> read_double_vector_from_hdf5
67 (Group& file,
string const& caption)
69 return read_vector_from_hdf5<double>
72 PredType::NATIVE_DOUBLE);
75 vector<int> read_int_vector_from_hdf5
77 const string& caption)
79 return read_vector_from_hdf5<int>
82 PredType::NATIVE_INT);
87 class MeshGeneratingPointCoordinate :
public LazyList<double>
93 tess_(tess), component_(component) {}
95 size_t size(
void)
const 97 return static_cast<size_t>(tess_.GetPointNo());
100 double operator[](
size_t i)
const 102 return tess_.GetMeshPoint(static_cast<int>(i)).*component_;
110 class CMGeneratingPointCoordinate :
public LazyList<double>
116 tess_(tess), component_(component) {}
118 size_t size(
void)
const 120 return static_cast<size_t>(tess_.GetPointNo());
123 double operator[](
size_t i)
const 125 return tess_.GetCellCM(static_cast<int>(i)).*component_;
133 class SingleCellPropertyExtractor
139 virtual ~SingleCellPropertyExtractor(
void) {}
142 class ThermalPropertyExtractor :
public SingleCellPropertyExtractor
158 class CellVelocityComponentExtractor :
public SingleCellPropertyExtractor
162 explicit CellVelocityComponentExtractor(
double Vector2D::* component) :
163 component_(component) {}
174 class CellsPropertyExtractor :
public LazyList<double>
178 CellsPropertyExtractor(
const hdsim& sim,
179 const SingleCellPropertyExtractor& scpe) :
180 sim_(sim), scpe_(scpe) {}
182 size_t size(
void)
const 184 return static_cast<size_t>(sim_.getTessellation().GetPointNo());
187 double operator[](
size_t i)
const 189 return scpe_(sim_.getAllCells()[i]);
194 const SingleCellPropertyExtractor& scpe_;
201 vector<double> xvert;
202 vector<double> yvert;
203 vector<double> nvert;
210 xvert.reserve(7 * static_cast<size_t>(tess.
GetPointNo()));
211 yvert.reserve(7 * static_cast<size_t>(tess.
GetPointNo()));
213 vector<Vector2D> convhull;
215 for (
size_t j = 0; j < convhull.size(); ++j) {
216 xvert.push_back(convhull[j].x);
217 yvert.push_back(convhull[j].y);
219 nvert[
static_cast<size_t>(i)] = static_cast<int>(convhull.size());
224 class StickerSlice :
public LazyList<double>
228 StickerSlice(
const hdsim& sim,
230 sim_(sim), index_(index) {}
232 size_t size(
void)
const 234 return static_cast<size_t>(sim_.getTessellation().GetPointNo());
237 double operator[](
size_t i)
const 239 return static_cast<double>(sim_.getAllCells()[i].stickers[index_]);
247 class TracerSlice :
public LazyList<double>
251 TracerSlice(
const hdsim& sim,
253 sim_(sim), index_(index) {}
255 size_t size(
void)
const 257 return static_cast<size_t>(sim_.getTessellation().GetPointNo());
260 double operator[](
size_t i)
const 262 return sim_.getAllCells()[i].tracers[index_];
272 const vector<DiagnosticAppendix*>& appendices)
275 H5File file(H5std_string(fname), H5F_ACC_TRUNC);
276 Group geometry = file.createGroup(
"/geometry");
277 Group gappendices = file.createGroup(
"/appendices");
278 Group hydrodynamic = file.createGroup(
"/hydrodynamic");
279 Group tracers = file.createGroup(
"/tracers");
280 Group stickers = file.createGroup(
"/stickers");
282 Group mpi = file.createGroup(
"/mpi");
288 vector<double>(1, sim.
getTime()),
296 for(
size_t i=0;i<area.size();++i)
304 (MeshGeneratingPointCoordinate
310 (MeshGeneratingPointCoordinate
316 (CMGeneratingPointCoordinate
322 (CMGeneratingPointCoordinate
342 (MeshGeneratingPointCoordinate
344 "proc_x_coordinate");
348 (MeshGeneratingPointCoordinate
350 "proc_y_coordinate");
357 (CellsPropertyExtractor
363 (CellsPropertyExtractor
369 (CellsPropertyExtractor
370 (sim, CellVelocityComponentExtractor(&
Vector2D::x))),
375 (CellsPropertyExtractor
376 (sim, CellVelocityComponentExtractor(&
Vector2D::y))),
381 size_t Ntracers = sim.
getAllCells().front().tracers.size();
382 for (
size_t i = 0; i < Ntracers; ++i)
386 size_t Nstickers = sim.
getAllCells().front().stickers.size();
387 for (
size_t i = 0; i < Nstickers; ++i)
391 for (
size_t i = 0; i < appendices.size(); ++i)
392 write_std_vector_to_hdf5
394 (*(appendices.at(i)))(sim),
395 appendices.at(i)->getName());
399 (
const string& fname,
bool mpioverride)
402 H5File file(fname, H5F_ACC_RDONLY);
403 Group g_geometry = file.openGroup(
"geometry");
404 Group g_hydrodynamic = file.openGroup(
"hydrodynamic");
405 Group g_tracers = file.openGroup(
"tracers");
406 Group g_stickers = file.openGroup(
"stickers");
410 mpi = file.openGroup(
"/mpi");
419 const vector<double> x =
420 read_double_vector_from_hdf5(g_geometry,
"x_coordinate");
421 const vector<double> y =
422 read_double_vector_from_hdf5(g_geometry,
"y_coordinate");
424 for (
size_t i = 0; i < x.size(); ++i)
433 const vector<double> x =
434 read_double_vector_from_hdf5(mpi,
"proc_x_coordinate");
435 const vector<double> y =
436 read_double_vector_from_hdf5(mpi,
"proc_y_coordinate");
438 for (
size_t i = 0; i < x.size(); ++i)
446 const vector<double> density =
447 read_double_vector_from_hdf5(g_hydrodynamic,
"density");
448 const vector<double> pressure =
449 read_double_vector_from_hdf5(g_hydrodynamic,
"pressure");
450 const vector<double> x_velocity =
451 read_double_vector_from_hdf5(g_hydrodynamic,
"x_velocity");
452 const vector<double> y_velocity =
453 read_double_vector_from_hdf5(g_hydrodynamic,
"y_velocity");
455 vector<vector<double> > tracers(g_tracers.getNumObjs());
456 vector<string> tracernames(tracers.size());
457 for (hsize_t n = 0; n < g_tracers.getNumObjs(); ++n)
459 const H5std_string name = g_tracers.getObjnameByIdx(n);
460 tracernames[n] = name;
461 tracers[n]= read_double_vector_from_hdf5(g_tracers, name);
464 vector<vector<int> > stickers(g_stickers.getNumObjs());
465 vector<string> stickernames(stickers.size());
466 for (hsize_t n = 0; n < g_stickers.getNumObjs(); ++n)
468 const H5std_string name = g_stickers.getObjnameByIdx(n);
469 stickernames[n] = name;
470 stickers[n] =read_int_vector_from_hdf5(g_stickers, name);
474 res.
cells.resize(density.size());
475 for (
size_t i = 0; i < res.
cells.size(); ++i)
477 res.
cells.at(i).density = density.at(i);
478 res.
cells.at(i).pressure = pressure.at(i);
479 res.
cells.at(i).velocity.x = x_velocity.at(i);
480 res.
cells.at(i).velocity.y = y_velocity.at(i);
481 res.
cells.at(i).tracers.resize(tracernames.size());
482 for (
size_t j = 0; j < tracernames.size(); ++j)
483 res.
cells.at(i).tracers.at(j) = tracers.at(j).at(i);
484 res.
cells.at(i).stickers.resize(stickernames.size());
485 for (
size_t j = 0; j < stickernames.size(); ++j)
486 res.
cells.at(i).stickers.at(j) = stickers.at(j).at(i)==1;
492 const vector<double>
time =
493 read_double_vector_from_hdf5(file,
"time");
494 res.
time = time.at(0);
495 const vector<int>
cycle =
496 read_int_vector_from_hdf5(file,
"cycle");
497 res.
cycle = cycle.at(0);
505 vector<Vector2D>
const& cor = tri.
getCor();
506 vector<double> x_cor, y_cor;
510 H5File file(H5std_string(filename), H5F_ACC_TRUNC);
512 for (
size_t i = 0; i < cor.size(); ++i)
514 x_cor.push_back(cor[i].x);
515 y_cor.push_back(cor[i].y);
518 for (
int i = 0; i < nfacets; ++i)
536 MPI_Comm_size(MPI_COMM_WORLD, &ws);
537 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
539 static_cast<double>(snapshot_number)*1.0 /
540 static_cast<double>(ws);
542 int start =
static_cast<int> 544 (static_cast<double>(rank)*read_num + 0.1));
545 int stop =
static_cast<int> 546 (floor((1 + rank)*read_num - 0.9));
548 for (
int i = start; i < stop; ++i)
560 vector<vector<Vector2D> > chull(static_cast<size_t>(ws));
561 vector<vector<ComputationalCell> > cell_recv(chull.size());
562 vector<vector<Vector2D> > mesh_recv(chull.size());
563 vector<vector<size_t> > indeces(chull.size());
564 for (
size_t i = 0; i < chull.size(); ++i)
565 ConvexHull(chull[i], proctess, static_cast<int>(i));
566 for (
size_t i = 0; i < snap.
mesh_points.size(); ++i)
569 for (
size_t j = 0; j < chull.size(); ++j)
573 indeces[j].push_back(i);
582 vector<MPI_Request> req(chull.size() * 2);
583 vector<vector<double> > tosend(chull.size() * 2);
584 vector<double> temprecv;
586 for (
size_t i = 0; i < chull.size(); ++i)
588 if (i == static_cast<size_t>(rank))
591 req[2 * i] = MPI_REQUEST_NULL;
595 if (tosend[i * 2].empty())
596 MPI_Isend(&dtemp, 1, MPI_DOUBLE, static_cast<int>(i), 2, MPI_COMM_WORLD, &req[2 * i]);
598 MPI_Isend(&tosend[i * 2][0], static_cast<int>(tosend[i * 2].size()), MPI_DOUBLE, static_cast<int>(i), 0, MPI_COMM_WORLD, &req[2 * i]);
600 for (
size_t i = 0; i < chull.size(); ++i)
602 if (i == static_cast<size_t>(rank))
605 req[2 * i + 1] = MPI_REQUEST_NULL;
609 if (tosend[i * 2 + 1].empty())
610 MPI_Isend(&dtemp, 1, MPI_DOUBLE, static_cast<int>(i), 3, MPI_COMM_WORLD, &req[2 * i + 1]);
612 MPI_Isend(&tosend[i * 2 + 1][0], static_cast<int>(tosend[i * 2 + 1].size()), MPI_DOUBLE, static_cast<int>(i), 1, MPI_COMM_WORLD, &req[2 * i + 1]);
614 for (
size_t i = 0; i < 2 * chull.size() - 2; ++i)
617 MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
619 MPI_Get_count(&status, MPI_DOUBLE, &count);
620 temprecv.resize(static_cast<size_t>(count));
621 MPI_Recv(&temprecv[0], count, MPI_DOUBLE, status.MPI_SOURCE, status.MPI_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
622 if (status.MPI_TAG == 0)
623 cell_recv[
static_cast<size_t>(status.MPI_SOURCE)] = list_unserialize(temprecv, snap.
cells[0]);
624 if (status.MPI_TAG == 1)
625 mesh_recv[
static_cast<size_t>(status.MPI_SOURCE)] = list_unserialize(temprecv, snap.
mesh_points[0]);
626 if (status.MPI_TAG > 3)
629 MPI_Waitall(static_cast<int>(req.size()), &req[0], MPI_STATUSES_IGNORE);
631 for (
size_t i = 0; i < chull.size(); ++i)
633 if (i == static_cast<size_t>(rank))
635 res.
cells.insert(res.
cells.end(), cell_recv[i].begin(), cell_recv[i].end());
648 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
651 for (
int i = 0; i < static_cast<int>(snapshot_number); ++i)
663 vector<Vector2D> chull;
665 vector<size_t> indeces;
666 for (
size_t i = 0; i < snap.
mesh_points.size(); ++i)
668 indeces.push_back(i);
677 ConvexHullData chd(tess);
678 H5File file(H5std_string(fname), H5F_ACC_TRUNC);
679 Group geometry = file.createGroup(
"/geometry");
const Tessellation & GetProcTessellation(void) const
Returns the processor tessellation.
vector< T > VectorValues(vector< T > const &v, vector< int > const &index)
Returns only the values with indeces in index.
Container for snapshot data.
Snapshot ReDistributeData2(string const &filename, Tessellation const &proctess, size_t snapshot_number, bool mpioverride=false)
Reads an HDF5 snapshot file in order to restart the simulation with a different cpu number...
std::vector< std::string > sticker_names
The names of the stickers.
Abstract class for tessellation.
Snapshot ReDistributeData(string const &filename, Tessellation const &proctess, size_t snapshot_number)
Reads an HDF5 snapshot file in order to restart the simulation with a different cpu number...
virtual int GetPointNo(void) const =0
Get Total number of mesh generating points.
const facet & get_facet(int index) const
Returns a facet.
Container for error reports.
const vector< Vector2D > & getCor(void) const
Access to coordinates.
bool PointInCell(vector< Vector2D > const &cpoints, Vector2D const &vec)
Checks if a point is inside a Voronoi cell.
Triplet< int > vertices
Indices of vertices.
Newtonian hydrodynamic simulation.
vector< T > serial_generate(const LazyList< T > &ll)
Creates a vector from an LazyList.
double getTime(void) const
Returns the time.
void write_std_vector_to_hdf5(const Group &file, const vector< T > &data, const string &caption, const DataType &dt)
Master function for writing vectors to hdf5 files.
vector< Vector2D > mesh_points
Mesh points.
void WriteDelaunay(Delaunay const &tri, string const &filename)
Writes the Delaunay triangulation data into an HDF5 file.
virtual ~DiagnosticAppendix(void)
Class destructor.
const Tessellation & getTessellation(void) const
Returns the tessellation.
std::string int2str(int n)
Converts an integer to a string.
double y
Component in the y direction.
Ordered list whose terms are evaluated lazily.
TracerStickerNames const & GetTracerStickerNames(void) const
Returns the TracerStickerNames.
int getCycle(void) const
Returns the number cycles.
const CacheData & getCacheData(void) const
Returns reference to the cached data.
void ConvexHull(vector< Vector2D > &result, Tessellation const &tess, int index)
Returns the ConvexHull for a set of points.
Snapshot & operator=(const Snapshot &other)
Copy operator.
int get_num_facet(void) const
Returns the number of facets.
The Delaunay data structure. Gets a set of points and constructs the Delaunay tessellation.
void write_snapshot_to_hdf5(hdsim const &sim, string const &fname, const vector< DiagnosticAppendix *> &appendices=vector< DiagnosticAppendix *>())
Writes the simulation data into an HDF5 file.
const CachedLazyList< double > volumes
List of cell volumes.
Simulation output to hdf5 file format.
std::vector< std::string > tracer_names
The names of the tracers.
Class for keeping the names of the tracers and stickers.
void WriteTess(Tessellation const &tess, string const &filename)
Writes the tessellation data into an HDF5 file.
Snapshot(void)
Default constructor.
int GetOriginalLength(void) const
Returns the original length of the points (without duplicated points)
Vector2D velocity
Velocity.
const vector< ComputationalCell > & getAllCells(void) const
Returns a list of all computational cells.
vector< Vector2D > proc_points
Locations of cpus.
double x
Component in the x direction.
TracerStickerNames tracerstickernames
THe names of the tracers and stickers.
vector< ComputationalCell > cells
Computational cells.
Snapshot read_hdf5_snapshot(const string &fname, bool mpioverride=false)
Load snapshot data into memory.