Skip to content
Snippets Groups Projects
Commit 6bd810c6 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Fix constness management in binary operator and add unary minus

Also performed a few clean-up
parent 055a2ba5
No related branches found
No related tags found
1 merge request!89Add missing compatibility check when affecting lists to R^d or R^dxd
...@@ -31,6 +31,7 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -31,6 +31,7 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
DiscreteFunctionDescriptorP0Vector m_discrete_function_descriptor; DiscreteFunctionDescriptorP0Vector m_discrete_function_descriptor;
public: public:
PUGS_INLINE
ASTNodeDataType ASTNodeDataType
dataType() const final dataType() const final
{ {
...@@ -44,18 +45,21 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -44,18 +45,21 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
return m_cell_arrays.sizeOfArrays(); return m_cell_arrays.sizeOfArrays();
} }
PUGS_INLINE
const CellArray<DataType>& const CellArray<DataType>&
cellArrays() const cellArrays() const
{ {
return m_cell_arrays; return m_cell_arrays;
} }
PUGS_INLINE
std::shared_ptr<const IMesh> std::shared_ptr<const IMesh>
mesh() const mesh() const
{ {
return m_mesh; return m_mesh;
} }
PUGS_INLINE
const IDiscreteFunctionDescriptor& const IDiscreteFunctionDescriptor&
descriptor() const final descriptor() const final
{ {
...@@ -63,21 +67,75 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -63,21 +67,75 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
} }
PUGS_FORCEINLINE PUGS_FORCEINLINE
Array<double> operator DiscreteFunctionP0Vector<Dimension, const DataType>() const
{
return DiscreteFunctionP0Vector<Dimension, const DataType>(m_mesh, m_cell_arrays);
}
PUGS_INLINE
void
fill(const DataType& data) const noexcept
{
static_assert(not std::is_const_v<DataType>, "Cannot modify ItemValue of const");
m_cell_arrays.fill(data);
}
PUGS_INLINE DiscreteFunctionP0Vector
operator=(const DiscreteFunctionP0Vector& f)
{
Assert(m_mesh == f.m_mesh);
Assert(this->size() == f.size());
m_cell_arrays = f.m_cell_arrays;
return *this;
}
friend PUGS_INLINE DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
copy(const DiscreteFunctionP0Vector& source)
{
return DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>{source.m_mesh, copy(source.cellArrays())};
}
friend PUGS_INLINE void
copy_to(const DiscreteFunctionP0Vector<Dimension, DataType>& source,
DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>& destination)
{
Assert(source.m_mesh == destination.m_mesh);
copy_to(source.m_cell_arrays, destination.m_cell_arrays);
}
PUGS_FORCEINLINE
Array<DataType>
operator[](const CellId cell_id) const noexcept(NO_ASSERT) operator[](const CellId cell_id) const noexcept(NO_ASSERT)
{ {
return m_cell_arrays[cell_id]; return m_cell_arrays[cell_id];
} }
friend DiscreteFunctionP0Vector PUGS_INLINE DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
operator-() const
{
Assert(m_cell_arrays.isBuilt());
DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>> opposite = copy(*this);
const size_t size_of_arrays = this->size();
parallel_for(
m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) {
auto array = opposite[cell_id];
for (size_t i = 0; i < size_of_arrays; ++i) {
array[i] = -array[i];
}
});
return opposite;
}
friend DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
operator+(const DiscreteFunctionP0Vector& f, const DiscreteFunctionP0Vector& g) operator+(const DiscreteFunctionP0Vector& f, const DiscreteFunctionP0Vector& g)
{ {
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.m_mesh == g.m_mesh, "functions are nor defined on the same mesh");
Assert(f.size() == g.size(), "P0 vectors have different sizes"); Assert(f.size() == g.size(), "P0 vectors have different sizes");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>> sum(f.m_mesh, f.size());
DiscreteFunctionP0Vector sum(mesh, f.size());
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) {
for (size_t i = 0; i < f.size(); ++i) { for (size_t i = 0; i < f.size(); ++i) {
sum[cell_id][i] = f[cell_id][i] + g[cell_id][i]; sum[cell_id][i] = f[cell_id][i] + g[cell_id][i];
} }
...@@ -85,15 +143,14 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -85,15 +143,14 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
return sum; return sum;
} }
friend DiscreteFunctionP0Vector friend DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
operator-(const DiscreteFunctionP0Vector& f, const DiscreteFunctionP0Vector& g) operator-(const DiscreteFunctionP0Vector& f, const DiscreteFunctionP0Vector& g)
{ {
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh");
Assert(f.size() == g.size(), "P0 vectors have different sizes"); Assert(f.size() == g.size(), "P0 vectors have different sizes");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>> difference(f.m_mesh, f.size());
DiscreteFunctionP0Vector difference(mesh, f.size());
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) {
for (size_t i = 0; i < f.size(); ++i) { for (size_t i = 0; i < f.size(); ++i) {
difference[cell_id][i] = f[cell_id][i] - g[cell_id][i]; difference[cell_id][i] = f[cell_id][i] - g[cell_id][i];
} }
...@@ -102,32 +159,33 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -102,32 +159,33 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
} }
template <typename DataType2> template <typename DataType2>
friend DiscreteFunctionP0Vector friend DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
operator*(const DiscreteFunctionP0<Dimension, DataType2>& f, const DiscreteFunctionP0Vector& g) operator*(const DiscreteFunctionP0<Dimension, DataType2>& f, const DiscreteFunctionP0Vector& g)
{ {
static_assert(std::is_arithmetic_v<DataType2>, "left operand must be arithmetic"); static_assert(std::is_arithmetic_v<DataType2>, "left operand must be arithmetic");
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>> product(g.m_mesh, g.size());
DiscreteFunctionP0Vector product(mesh, g.size()); const size_t size_of_arrays = g.size();
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { g.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) {
const double f_value = f[cell_id]; const DataType2& f_value = f[cell_id];
for (size_t i = 0; i < g.size(); ++i) { for (size_t i = 0; i < size_of_arrays; ++i) {
product[cell_id][i] = f_value * g[cell_id][i]; product[cell_id][i] = f_value * g[cell_id][i];
} }
}); });
return product; return product;
} }
friend DiscreteFunctionP0Vector template <typename DataType2>
operator*(double a, const DiscreteFunctionP0Vector& f) friend DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>>
operator*(const DataType2& a, const DiscreteFunctionP0Vector& f)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0Vector<Dimension, std::remove_const_t<DataType>> product(f.m_mesh, f.size());
DiscreteFunctionP0Vector product(mesh, f.size()); const size_t size_of_arrays = f.size();
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) {
for (size_t i = 0; i < f.size(); ++i) { for (size_t i = 0; i < size_of_arrays; ++i) {
product[cell_id][i] = a * f[cell_id][i]; product[cell_id][i] = a * f[cell_id][i];
} }
}); });
...@@ -149,16 +207,10 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction ...@@ -149,16 +207,10 @@ class DiscreteFunctionP0Vector : public IDiscreteFunction
: m_mesh{mesh}, m_cell_arrays{mesh->connectivity(), size} : m_mesh{mesh}, m_cell_arrays{mesh->connectivity(), size}
{} {}
template <typename DataType2> DiscreteFunctionP0Vector(const std::shared_ptr<const MeshType>& mesh, const CellArray<DataType>& cell_arrays)
DiscreteFunctionP0Vector(const std::shared_ptr<const MeshType>& mesh, const CellArray<DataType2>& cell_arrays) : m_mesh{mesh}, m_cell_arrays{cell_arrays}
: m_mesh{mesh}
{ {
static_assert(std::is_same_v<std::remove_const_t<DataType>, std::remove_const_t<DataType2>>); Assert(mesh->shared_connectivity() == cell_arrays.connectivity_ptr());
static_assert(std::is_const_v<DataType> or not std::is_const_v<DataType2>);
Assert(mesh->connectivity_ptr() == cell_arrays.connectivity_ptr());
m_cell_arrays = cell_arrays;
} }
DiscreteFunctionP0Vector(const DiscreteFunctionP0Vector&) noexcept = default; DiscreteFunctionP0Vector(const DiscreteFunctionP0Vector&) noexcept = default;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment