Skip to content

Commit

Permalink
seems OK
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangyu-Hu committed Aug 10, 2024
1 parent eace73e commit 9f5c6ca
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 160 deletions.
4 changes: 2 additions & 2 deletions src/for_2D_build/meshes/base_mesh_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace SPH
{
//=============================================================================================//
Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i)
Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) const
{
size_t row_size = mesh_size[1];
size_t column = i / row_size;
return Arrayi(column, i - column * row_size);
}
//=============================================================================================//
size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index)
size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) const
{
return MortonCode(mesh_index[0]) | (MortonCode(mesh_index[1]) << 1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/for_3D_build/meshes/base_mesh_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SPH
{
//=================================================================================================//
Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i)
Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) const
{
size_t row_times_column_size = mesh_size[1] * mesh_size[2];
size_t page = i / row_times_column_size;
Expand All @@ -13,7 +13,7 @@ Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i)
return Arrayi(page, column, left_over - column * row_size);
}
//=================================================================================================//
size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index)
size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) const
{
return MortonCode(mesh_index[0]) | (MortonCode(mesh_index[1]) << 1) | (MortonCode(mesh_index[2]) << 2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/common/sphinxsys_containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "base_data_package.h"
#include "base_data_type.h"
#include "sphinxsys_entity.h"
#include "sphinxsys_variable.h"

namespace SPH
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@
* *
* ------------------------------------------------------------------------- */
/**
* @file sphinxsys_entity.h
* @file sphinxsys_variable.h
* @brief Here gives classes for the constants and variables used in simulation.
* @details These variables are those discretized in spaces and time.
* @author Xiangyu Hu
*/

#ifndef SPHINXSYS_ENTITY_H
#define SPHINXSYS_ENTITY_H
#ifndef SPHINXSYS_VARIABLE_H
#define SPHINXSYS_VARIABLE_H

#include "base_data_package.h"
#include "execution_policy.h"

namespace SPH
{

class BaseEntity
class BaseVariable
{
public:
explicit BaseEntity(const std::string &name)
explicit BaseVariable(const std::string &name)
: name_(name){};
virtual ~BaseEntity(){};
virtual ~BaseVariable(){};
std::string Name() const { return name_; };

protected:
const std::string name_;
};

template <typename DataType>
class SingularVariable : public BaseEntity
class SingularVariable : public BaseVariable
{
public:
SingularVariable(const std::string &name, const DataType &value)
: BaseEntity(name), value_(new DataType(value)), delegated_(value_){};
: BaseVariable(name), value_(new DataType(value)), delegated_(value_){};
virtual ~SingularVariable() { delete value_; };

DataType *ValueAddress() { return delegated_; };
Expand All @@ -66,7 +66,7 @@ class SingularVariable : public BaseEntity
};

template <typename DataType>
class DeviceSharedSingularVariable : public BaseEntity
class DeviceSharedSingularVariable : public BaseVariable

{
public:
Expand All @@ -78,54 +78,11 @@ class DeviceSharedSingularVariable : public BaseEntity
};

template <typename DataType>
class ConstantEntity : public BaseEntity
{
public:
ConstantEntity(const std::string &name, const DataType &value)
: BaseEntity(name), value_(new DataType(value)), device_value_(nullptr){};
virtual ~ConstantEntity() { delete value_; };

bool existDeviceData() { return device_value_ != nullptr; };
void setDeviceData(DataType *data) { device_value_ = data; };
DataType *DeviceDataAddress()
{
if (!existDeviceData())
{
std::cout << "\n Error: the constant entity '" << name_ << "' not allocated on device yet!" << std::endl;
std::cout << __FILE__ << ':' << __LINE__ << std::endl;
exit(1);
}
return device_value_;
};

DataType *DataAddress() { return value_; };
template <class ExecutionPolicy>
DataType *DataAddress(const ExecutionPolicy &policy) { return DataAddress(); };
DataType *DataAddress(const execution::ParallelDevicePolicy &par_device) { return DeviceDataAddress(); }

private:
DataType *value_;
DataType *device_value_;
};

template <typename DataType>
class DeviceOnlyConstantEntity : public BaseEntity

{
public:
DeviceOnlyConstantEntity(ConstantEntity<DataType> *host_constant);
virtual ~DeviceOnlyConstantEntity();

protected:
DataType *device_only_value_;
};

template <typename DataType>
class DiscreteVariable : public BaseEntity
class DiscreteVariable : public BaseVariable
{
public:
DiscreteVariable(const std::string &name, size_t data_size)
: BaseEntity(name), data_size_(data_size),
: BaseVariable(name), data_size_(data_size),
data_field_(nullptr), device_data_field_(nullptr)
{
data_field_ = new DataType[data_size];
Expand All @@ -147,7 +104,7 @@ class DiscreteVariable : public BaseEntity
};

template <typename DataType>
class DeviceOnlyDiscreteVariable : public BaseEntity
class DeviceOnlyDiscreteVariable : public BaseVariable
{
public:
DeviceOnlyDiscreteVariable(DiscreteVariable<DataType> *host_variable);
Expand All @@ -158,12 +115,12 @@ class DeviceOnlyDiscreteVariable : public BaseEntity
};

template <typename DataType>
class MeshVariable : public BaseEntity
class MeshVariable : public BaseVariable
{
public:
using PackageData = PackageDataMatrix<DataType, 4>;
MeshVariable(const std::string &name, size_t data_size)
: BaseEntity(name), data_field_(nullptr){};
: BaseVariable(name), data_field_(nullptr){};
virtual ~MeshVariable() { delete[] data_field_; };

// void setDataField(PackageData* mesh_data){ data_field_ = mesh_data; };
Expand Down Expand Up @@ -201,4 +158,4 @@ VariableType<DataType> *addVariableToAssemble(DataContainerAddressAssemble<Varia
return new_variable;
};
} // namespace SPH
#endif // SPHINXSYS_ENTITY_H
#endif // SPHINXSYS_VARIABLE_H
8 changes: 4 additions & 4 deletions src/shared/meshes/base_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ Mesh::Mesh(Vecd mesh_lower_bound, Real grid_spacing, Arrayi all_grid_points)
all_cells_ = all_grid_points_ - Arrayi::Ones();
}
//=================================================================================================//
Vecd Mesh::CellLowerCornerPosition(const Arrayi &cell_index)
Vecd Mesh::CellLowerCornerPosition(const Arrayi &cell_index) const
{
return mesh_lower_bound_ + cell_index.cast<Real>().matrix() * grid_spacing_;
}
//=================================================================================================//
Vecd Mesh::CellPositionFromIndex(const Arrayi &cell_index)
Vecd Mesh::CellPositionFromIndex(const Arrayi &cell_index) const
{
return CellLowerCornerPosition(cell_index) + 0.5 * Vecd::Ones() * grid_spacing_;
}
//=================================================================================================//
Vecd Mesh::GridPositionFromIndex(const Arrayi &grid_index)
Vecd Mesh::GridPositionFromIndex(const Arrayi &grid_index) const
{
return mesh_lower_bound_ + grid_index.cast<Real>().matrix() * grid_spacing_;
}
//=================================================================================================//
size_t Mesh::MortonCode(const size_t &i)
size_t Mesh::MortonCode(const size_t &i) const
{
size_t x = i;
x &= 0x3ff;
Expand Down
34 changes: 17 additions & 17 deletions src/shared/meshes/base_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,41 @@ class Mesh
Mesh(Vecd mesh_lower_bound, Real grid_spacing, Arrayi all_grid_points);
~Mesh(){};

Vecd MeshLowerBound() { return mesh_lower_bound_; };
Real GridSpacing() { return grid_spacing_; };
Arrayi AllGridPoints() { return all_grid_points_; };
Arrayi AllCells() { return all_cells_; };
size_t NumberOfGridPoints() { return transferMeshIndexTo1D(all_grid_points_, all_grid_points_); };
size_t NumberOfCells() { return transferMeshIndexTo1D(all_cells_, all_cells_); };

Arrayi CellIndexFromPosition(const Vecd &position)
Vecd MeshLowerBound() const { return mesh_lower_bound_; };
Real GridSpacing() const { return grid_spacing_; };
Arrayi AllGridPoints() const { return all_grid_points_; };
Arrayi AllCells() const { return all_cells_; };
size_t NumberOfGridPoints() const { return transferMeshIndexTo1D(all_grid_points_, all_grid_points_); };
size_t NumberOfCells() const { return transferMeshIndexTo1D(all_cells_, all_cells_); };

Arrayi CellIndexFromPosition(const Vecd &position) const
{
return floor((position - mesh_lower_bound_).array() / grid_spacing_)
.cast<int>()
.max(Arrayi::Zero())
.min(all_grid_points_ - 2 * Arrayi::Ones());
};

size_t LinearCellIndexFromPosition(const Vecd &position)
size_t LinearCellIndexFromPosition(const Vecd &position) const
{
return transferMeshIndexTo1D(all_cells_, CellIndexFromPosition(position));
};

Vecd CellPositionFromIndex(const Arrayi &cell_index);
Vecd GridPositionFromIndex(const Arrayi &grid_index);
Vecd CellLowerCornerPosition(const Arrayi &cell_index);
Vecd CellPositionFromIndex(const Arrayi &cell_index) const;
Vecd GridPositionFromIndex(const Arrayi &grid_index) const;
Vecd CellLowerCornerPosition(const Arrayi &cell_index) const;
//----------------------------------------------------------------------
// Transferring between 1D mesh indexes.
// Here, mesh size can be either AllGridPoints or AllCells.
//----------------------------------------------------------------------
Arrayi transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i);
Arrayi transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) const;

size_t transferMeshIndexTo1D(const Array2i &mesh_size, const Array2i &mesh_index)
size_t transferMeshIndexTo1D(const Array2i &mesh_size, const Array2i &mesh_index) const
{
return mesh_index[0] * mesh_size[1] + mesh_index[1];
};

size_t transferMeshIndexTo1D(const Array3i &mesh_size, const Array3i &mesh_index)
size_t transferMeshIndexTo1D(const Array3i &mesh_size, const Array3i &mesh_index) const
{
return mesh_index[0] * mesh_size[1] * mesh_size[2] +
mesh_index[1] * mesh_size[2] +
Expand All @@ -105,7 +105,7 @@ class Mesh
* https://stackoverflow.com/questions/18529057/
* produce-interleaving-bit-patterns-morton-keys-for-32-bit-64-bit-and-128bit
*/
size_t transferMeshIndexToMortonOrder(const Arrayi &mesh_index);
size_t transferMeshIndexToMortonOrder(const Arrayi &mesh_index) const;

protected:
Vecd mesh_lower_bound_; /**< mesh lower bound as reference coordinate */
Expand All @@ -114,7 +114,7 @@ class Mesh
Arrayi all_grid_points_; /**< number of grid points by dimension */
Arrayi all_cells_; /**< number of cells by dimension */

size_t MortonCode(const size_t &i);
size_t MortonCode(const size_t &i) const;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/meshes/mesh_with_data_packages.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "base_mesh.h"
#include "my_memory_pool.h"
#include "sphinxsys_entity.h"
#include "sphinxsys_variable.h"

#include <algorithm>
#include <fstream>
Expand Down
20 changes: 10 additions & 10 deletions src/shared/particle_dynamics/base_local_dynamics.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Dynamic; /**< A dynamic interaction */
template <class DynamicsIdentifier>
class BaseLocalDynamics
{
UniquePtrsKeeper<BaseEntity> constant_entity_ptrs_;
UniquePtrsKeeper<BaseVariable> constant_entity_ptrs_;

public:
explicit BaseLocalDynamics(DynamicsIdentifier &identifier)
Expand All @@ -86,26 +86,26 @@ class BaseLocalDynamics
BaseParticles *particles_;

template <class DataType>
ConstantEntity<DataType> *createConstantEntity(const std::string &name, const DataType &value)
const DataType *createConstantEntity(const std::string &name, const DataType &value)
{
return constant_entity_ptrs_.createPtr<ConstantEntity<DataType>>(name, value);
return constant_entity_ptrs_.createPtr<SingularVariable<DataType>>(name, value)->ValueAddress();
};

template <class DataType, class ExecutionPolicy>
ConstantEntity<DataType> *createConstantEntity(
const DataType *createConstantEntity(
const ExecutionPolicy &policy, const std::string &name, const DataType &value)
{
return constant_entity_ptrs_.createPtr<ConstantEntity<DataType>>(name, value);
return constant_entity_ptrs_.createPtr<SingularVariable<DataType>>(name, value)->ValueAddress();
};

template <class DataType>
ConstantEntity<DataType> *createConstantEntity(
const DataType *createConstantEntity(
const execution::ParallelDevicePolicy &par_device, const std::string &name, const DataType &value)
{
ConstantEntity<DataType> *constant_entity =
constant_entity_ptrs_.createPtr<ConstantEntity<DataType>>(name, value);
constant_entity_ptrs_.createPtr<DeviceOnlyConstantEntity<DataType>>(constant_entity);
return constant_entity;
SingularVariable<DataType> *constant_entity =
constant_entity_ptrs_.createPtr<SingularVariable<DataType>>(name, value);
constant_entity_ptrs_.createPtr<DeviceSharedSingularVariable<DataType>>(constant_entity);
return constant_entity->ValueAddress();
};
};
using LocalDynamics = BaseLocalDynamics<SPHBody>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class UpdateCellLinkedList<CellLinkedListType> : public LocalDynamics
UpdateCellLinkedList(const ExecutionPolicy &execution_policy, RealBody &real_body);
virtual ~UpdateCellLinkedList(){};

UnsignedInt *setParticleOffsetListUpperBound();
template <class ExecutionPolicy>
void clearParticleOffsetList(const ExecutionPolicy &execution_policy);
void clearParticleOffsetList(const ParallelDevicePolicy &par_device);
Expand All @@ -72,12 +71,9 @@ class UpdateCellLinkedList<CellLinkedListType> : public LocalDynamics
void updateCellLists(const ParallelDevicePolicy &par_device);

protected:
ConstantEntity<Mesh> *ce_mesh_;
const Mesh *mesh_;
UnsignedInt number_of_cells_, particles_bound_;
SingularVariable<UnsignedInt> *sv_total_real_particles_;
DiscreteVariable<UnsignedInt> *dv_particle_offset_list_;

Mesh *mesh_;
Vecd *pos_;
UnsignedInt *particle_id_list_;
UnsignedInt *particle_offset_list_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,16 @@ template <class ExecutionPolicy>
UpdateCellLinkedList<CellLinkedListType>::
UpdateCellLinkedList(const ExecutionPolicy &execution_policy, RealBody &real_body)
: LocalDynamics(real_body),
ce_mesh_(createConstantEntity<Mesh>(execution_policy, "Mesh", DynamicCast<CellLinkedListType>(this, real_body.getCellLinkedList()))),
sv_total_real_particles_(particles_->getSingularVariableByName<UnsignedInt>("TotalRealParticles")),
number_of_cells_(ce_mesh_->DataAddress()->NumberOfCells()), particles_bound_(particles_->ParticlesBound()),
dv_particle_offset_list_(nullptr), mesh_(ce_mesh_->DataAddress(execution_policy)),
mesh_(createConstantEntity<Mesh>(execution_policy, "Mesh", DynamicCast<CellLinkedListType>(this, real_body.getCellLinkedList()))),
number_of_cells_(mesh_->NumberOfCells()), particles_bound_(particles_->ParticlesBound()),
pos_(particles_->getVariableDataByName<Vecd>(execution_policy, "Position")),
particle_id_list_(particles_->registerDiscreteVariable<UnsignedInt>(execution_policy, "ParticleIDList", particles_bound_)),
particle_offset_list_(particles_->registerDiscreteVariable<UnsignedInt>(execution_policy, "ParticleOffsetList", number_of_cells_ + 1)),
current_size_list_(particles_->registerDiscreteVariable<UnsignedInt>(execution_policy, "CurrentCellSize", number_of_cells_))
{
dv_particle_offset_list_ = particles_->getVariableByName<UnsignedInt>("ParticleOffsetList");
particles_->addVariableToWrite<UnsignedInt>("ParticleIDList");
}
//=================================================================================================//
template <typename CellLinkedListType>
UnsignedInt *UpdateCellLinkedList<CellLinkedListType>::setParticleOffsetListUpperBound()
{
UnsignedInt total_real_particles = particles_->TotalRealParticles();
UnsignedInt *particle_offset_list = dv_particle_offset_list_->DataField();
particle_offset_list[number_of_cells_] = total_real_particles;
return particle_offset_list;
}
//=================================================================================================//
template <class CellLinkedListType, class ExecutionPolicy>
UpdateCellLinkedList<CellLinkedListType, ExecutionPolicy>::UpdateCellLinkedList(RealBody &real_body)
: UpdateCellLinkedList<CellLinkedListType>(ExecutionPolicy{}, real_body),
Expand Down
Loading

0 comments on commit 9f5c6ca

Please sign in to comment.