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

Add unary minus, copy and copy_to utilities for DiscreteFunctionP0

Also performed few clean-up
parent de61e9df
No related branches found
No related tags found
1 merge request!89Add missing compatibility check when affecting lists to R^d or R^dxd
...@@ -26,30 +26,35 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -26,30 +26,35 @@ class DiscreteFunctionP0 : public IDiscreteFunction
DiscreteFunctionDescriptorP0 m_discrete_function_descriptor; DiscreteFunctionDescriptorP0 m_discrete_function_descriptor;
public: public:
PUGS_INLINE
ASTNodeDataType ASTNodeDataType
dataType() const final dataType() const final
{ {
return ast_node_data_type_from<DataType>; return ast_node_data_type_from<DataType>;
} }
PUGS_INLINE
const CellValue<DataType>& const CellValue<DataType>&
cellValues() const cellValues() const
{ {
return m_cell_values; return m_cell_values;
} }
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
{ {
return m_discrete_function_descriptor; return m_discrete_function_descriptor;
} }
PUGS_FORCEINLINE
operator DiscreteFunctionP0<Dimension, const DataType>() const operator DiscreteFunctionP0<Dimension, const DataType>() const
{ {
return DiscreteFunctionP0<Dimension, const DataType>(m_mesh, m_cell_values); return DiscreteFunctionP0<Dimension, const DataType>(m_mesh, m_cell_values);
...@@ -63,23 +68,46 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -63,23 +68,46 @@ class DiscreteFunctionP0 : public IDiscreteFunction
m_cell_values.fill(data); m_cell_values.fill(data);
} }
PUGS_FORCEINLINE friend PUGS_INLINE DiscreteFunctionP0<Dimension, std::remove_const_t<DataType>>
DataType& copy(const DiscreteFunctionP0& source)
{
return DiscreteFunctionP0<Dimension, std::remove_const_t<DataType>>{source.m_mesh, copy(source.cellValues())};
}
friend PUGS_INLINE void
copy_to(const DiscreteFunctionP0<Dimension, DataType>& source,
DiscreteFunctionP0<Dimension, std::remove_const_t<DataType>>& destination)
{
Assert(source.m_mesh == destination.m_mesh);
copy_to(source.m_cell_values, destination.m_cell_values);
}
PUGS_FORCEINLINE DataType&
operator[](const CellId cell_id) const noexcept(NO_ASSERT) operator[](const CellId cell_id) const noexcept(NO_ASSERT)
{ {
return m_cell_values[cell_id]; return m_cell_values[cell_id];
} }
PUGS_INLINE DiscreteFunctionP0<Dimension, std::remove_const_t<DataType>>
operator-() const
{
Assert(m_cell_values.isBuilt());
DiscreteFunctionP0<Dimension, std::remove_const_t<DataType>> opposite = copy(*this);
parallel_for(
m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { opposite[cell_id] = -opposite[cell_id]; });
return opposite;
}
template <typename DataType2T> template <typename DataType2T>
PUGS_INLINE DiscreteFunctionP0<Dimension, decltype(DataType{} + DataType2T{})> PUGS_INLINE DiscreteFunctionP0<Dimension, decltype(DataType{} + DataType2T{})>
operator+(const DiscreteFunctionP0<Dimension, DataType2T>& g) const operator+(const DiscreteFunctionP0<Dimension, DataType2T>& g) const
{ {
const DiscreteFunctionP0& f = *this; const DiscreteFunctionP0& f = *this;
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are not defined on the same mesh");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0<Dimension, decltype(DataType{} + DataType2T{})> sum(f.m_mesh);
DiscreteFunctionP0<Dimension, decltype(DataType{} + DataType2T{})> sum(mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = f[cell_id] + g[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = f[cell_id] + g[cell_id]; });
return sum; return sum;
} }
...@@ -87,11 +115,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -87,11 +115,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} + DataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} + DataType{})>
operator+(const LHSDataType& a, const DiscreteFunctionP0& g) operator+(const LHSDataType& a, const DiscreteFunctionP0& g)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(g.mesh());
using ProductDataType = decltype(LHSDataType{} + DataType{}); using ProductDataType = decltype(LHSDataType{} + DataType{});
DiscreteFunctionP0<Dimension, ProductDataType> sum(mesh); DiscreteFunctionP0<Dimension, ProductDataType> sum(g.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = a + g[cell_id]; }); g.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = a + g[cell_id]; });
return sum; return sum;
} }
...@@ -99,11 +126,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -99,11 +126,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} + RHSDataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} + RHSDataType{})>
operator+(const DiscreteFunctionP0& f, const RHSDataType& b) operator+(const DiscreteFunctionP0& f, const RHSDataType& b)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(DataType{} + RHSDataType{}); using ProductDataType = decltype(DataType{} + RHSDataType{});
DiscreteFunctionP0<Dimension, ProductDataType> sum(mesh); DiscreteFunctionP0<Dimension, ProductDataType> sum(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = f[cell_id] + b; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { sum[cell_id] = f[cell_id] + b; });
return sum; return sum;
} }
...@@ -112,11 +138,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -112,11 +138,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
operator-(const DiscreteFunctionP0<Dimension, DataType2T>& g) const operator-(const DiscreteFunctionP0<Dimension, DataType2T>& g) const
{ {
const DiscreteFunctionP0& f = *this; const DiscreteFunctionP0& f = *this;
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are not defined on the same mesh");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0<Dimension, decltype(DataType{} - DataType2T{})> difference(f.m_mesh);
DiscreteFunctionP0<Dimension, decltype(DataType{} - DataType2T{})> difference(mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = f[cell_id] - g[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = f[cell_id] - g[cell_id]; });
return difference; return difference;
} }
...@@ -124,11 +149,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -124,11 +149,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} - DataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} - DataType{})>
operator-(const LHSDataType& a, const DiscreteFunctionP0& g) operator-(const LHSDataType& a, const DiscreteFunctionP0& g)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(g.mesh());
using ProductDataType = decltype(LHSDataType{} - DataType{}); using ProductDataType = decltype(LHSDataType{} - DataType{});
DiscreteFunctionP0<Dimension, ProductDataType> difference(mesh); DiscreteFunctionP0<Dimension, ProductDataType> difference(g.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = a - g[cell_id]; }); g.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = a - g[cell_id]; });
return difference; return difference;
} }
...@@ -136,11 +160,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -136,11 +160,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} - RHSDataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} - RHSDataType{})>
operator-(const DiscreteFunctionP0& f, const RHSDataType& b) operator-(const DiscreteFunctionP0& f, const RHSDataType& b)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(DataType{} - RHSDataType{}); using ProductDataType = decltype(DataType{} - RHSDataType{});
DiscreteFunctionP0<Dimension, ProductDataType> difference(mesh); DiscreteFunctionP0<Dimension, ProductDataType> difference(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = f[cell_id] - b; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { difference[cell_id] = f[cell_id] - b; });
return difference; return difference;
} }
...@@ -149,11 +172,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -149,11 +172,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
operator*(const DiscreteFunctionP0<Dimension, DataType2T>& g) const operator*(const DiscreteFunctionP0<Dimension, DataType2T>& g) const
{ {
const DiscreteFunctionP0& f = *this; const DiscreteFunctionP0& f = *this;
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are not defined on the same mesh");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0<Dimension, decltype(DataType{} * DataType2T{})> product(f.m_mesh);
DiscreteFunctionP0<Dimension, decltype(DataType{} * DataType2T{})> product(mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = f[cell_id] * g[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = f[cell_id] * g[cell_id]; });
return product; return product;
} }
...@@ -161,11 +183,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -161,11 +183,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} * DataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} * DataType{})>
operator*(const LHSDataType& a, const DiscreteFunctionP0& f) operator*(const LHSDataType& a, const DiscreteFunctionP0& f)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(LHSDataType{} * DataType{}); using ProductDataType = decltype(LHSDataType{} * DataType{});
DiscreteFunctionP0<Dimension, ProductDataType> product(mesh); DiscreteFunctionP0<Dimension, ProductDataType> product(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = a * f[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = a * f[cell_id]; });
return product; return product;
} }
...@@ -173,11 +194,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -173,11 +194,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} * RHSDataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} * RHSDataType{})>
operator*(const DiscreteFunctionP0& f, const RHSDataType& b) operator*(const DiscreteFunctionP0& f, const RHSDataType& b)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(DataType{} * RHSDataType{}); using ProductDataType = decltype(DataType{} * RHSDataType{});
DiscreteFunctionP0<Dimension, ProductDataType> product(mesh); DiscreteFunctionP0<Dimension, ProductDataType> product(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = f[cell_id] * b; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { product[cell_id] = f[cell_id] * b; });
return product; return product;
} }
...@@ -186,11 +206,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -186,11 +206,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
operator/(const DiscreteFunctionP0<Dimension, DataType2T>& g) const operator/(const DiscreteFunctionP0<Dimension, DataType2T>& g) const
{ {
const DiscreteFunctionP0& f = *this; const DiscreteFunctionP0& f = *this;
Assert(f.mesh() == g.mesh(), "functions are nor defined on the same mesh"); Assert(f.mesh() == g.mesh(), "functions are not defined on the same mesh");
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh()); DiscreteFunctionP0<Dimension, decltype(DataType{} / DataType2T{})> ratio(f.m_mesh);
DiscreteFunctionP0<Dimension, decltype(DataType{} / DataType2T{})> ratio(mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = f[cell_id] / g[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = f[cell_id] / g[cell_id]; });
return ratio; return ratio;
} }
...@@ -198,11 +217,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -198,11 +217,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} / DataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(LHSDataType{} / DataType{})>
operator/(const LHSDataType& a, const DiscreteFunctionP0& f) operator/(const LHSDataType& a, const DiscreteFunctionP0& f)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(LHSDataType{} / DataType{}); using ProductDataType = decltype(LHSDataType{} / DataType{});
DiscreteFunctionP0<Dimension, ProductDataType> ratio(mesh); DiscreteFunctionP0<Dimension, ProductDataType> ratio(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = a / f[cell_id]; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = a / f[cell_id]; });
return ratio; return ratio;
} }
...@@ -210,11 +228,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction ...@@ -210,11 +228,10 @@ class DiscreteFunctionP0 : public IDiscreteFunction
PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} / RHSDataType{})> PUGS_INLINE friend DiscreteFunctionP0<Dimension, decltype(DataType{} / RHSDataType{})>
operator/(const DiscreteFunctionP0& f, const RHSDataType& b) operator/(const DiscreteFunctionP0& f, const RHSDataType& b)
{ {
std::shared_ptr mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
using ProductDataType = decltype(DataType{} / RHSDataType{}); using ProductDataType = decltype(DataType{} / RHSDataType{});
DiscreteFunctionP0<Dimension, ProductDataType> ratio(mesh); DiscreteFunctionP0<Dimension, ProductDataType> ratio(f.m_mesh);
parallel_for( parallel_for(
mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = f[cell_id] / b; }); f.m_mesh->numberOfCells(), PUGS_LAMBDA(CellId cell_id) { ratio[cell_id] = f[cell_id] / b; });
return ratio; return ratio;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment