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

Adapt shallow copy to DiscreteFunctionVariant

parent bc97cc4d
Branches
Tags
1 merge request!162Fix return type value checking for BuiltinFunctionEmbedder
...@@ -407,8 +407,8 @@ SchemeModule::SchemeModule() ...@@ -407,8 +407,8 @@ SchemeModule::SchemeModule()
std::function( std::function(
[](const std::shared_ptr<const IMesh>& mesh, [](const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const IDiscreteFunction>& v) const std::shared_ptr<const DiscreteFunctionVariant>& v)
-> std::shared_ptr<const IDiscreteFunction> { return shallowCopy(mesh, v); } -> std::shared_ptr<const DiscreteFunctionVariant> { return shallowCopy(mesh, v); }
)); ));
......
...@@ -65,109 +65,50 @@ shallowCopy(const std::shared_ptr<const Mesh<Connectivity<Dimension>>>& mesh, ...@@ -65,109 +65,50 @@ shallowCopy(const std::shared_ptr<const Mesh<Connectivity<Dimension>>>& mesh,
return std::make_shared<DiscreteFunctionP0<Dimension, DataType>>(mesh, discrete_function->cellValues()); return std::make_shared<DiscreteFunctionP0<Dimension, DataType>>(mesh, discrete_function->cellValues());
} }
template <size_t Dimension> template <typename MeshType, typename DiscreteFunctionT>
std::shared_ptr<const IDiscreteFunction> std::shared_ptr<const DiscreteFunctionVariant>
shallowCopy(const std::shared_ptr<const Mesh<Connectivity<Dimension>>>& mesh, shallowCopy(const std::shared_ptr<const MeshType>& mesh, const DiscreteFunctionT& f)
const std::shared_ptr<const IDiscreteFunction>& discrete_function)
{ {
const std::shared_ptr function_mesh = const std::shared_ptr function_mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
std::dynamic_pointer_cast<const Mesh<Connectivity<Dimension>>>(discrete_function->mesh());
if (mesh->shared_connectivity() != function_mesh->shared_connectivity()) { if (mesh->shared_connectivity() != function_mesh->shared_connectivity()) {
throw NormalError("cannot shallow copy when connectivity changes"); throw NormalError("cannot shallow copy when connectivity changes");
} }
switch (discrete_function->descriptor().type()) { if constexpr (std::is_same_v<MeshType, typename DiscreteFunctionT::MeshType>) {
case DiscreteFunctionType::P0: { if constexpr (is_discrete_function_P0_v<DiscreteFunctionT>) {
switch (discrete_function->dataType()) { return std::make_shared<DiscreteFunctionVariant>(DiscreteFunctionT(mesh, f.cellValues()));
case ASTNodeDataType::double_t: { } else if constexpr (is_discrete_function_P0_vector_v<DiscreteFunctionT>) {
return shallowCopy(mesh, return std::make_shared<DiscreteFunctionVariant>(DiscreteFunctionT(mesh, f.cellArrays()));
std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, double>>(discrete_function)); } else {
} throw UnexpectedError("invalid discrete function type");
case ASTNodeDataType::vector_t: {
switch (discrete_function->dataType().dimension()) {
case 1: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyVector<1>>>(
discrete_function));
}
case 2: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyVector<2>>>(
discrete_function));
}
case 3: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyVector<3>>>(
discrete_function));
}
// LCOV_EXCL_START
default: {
throw UnexpectedError("invalid data vector dimension: " + stringify(discrete_function->dataType().dimension()));
}
// LCOV_EXCL_STOP
}
}
case ASTNodeDataType::matrix_t: {
if (discrete_function->dataType().numberOfRows() != discrete_function->dataType().numberOfColumns()) {
// LCOV_EXCL_START
throw UnexpectedError(
"invalid data matrix dimensions: " + stringify(discrete_function->dataType().numberOfRows()) + "x" +
stringify(discrete_function->dataType().numberOfColumns()));
// LCOV_EXCL_STOP
}
switch (discrete_function->dataType().numberOfRows()) {
case 1: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyMatrix<1>>>(
discrete_function));
}
case 2: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyMatrix<2>>>(
discrete_function));
}
case 3: {
return shallowCopy(mesh, std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, TinyMatrix<3>>>(
discrete_function));
}
// LCOV_EXCL_START
default: {
throw UnexpectedError(
"invalid data matrix dimensions: " + stringify(discrete_function->dataType().numberOfRows()) + "x" +
stringify(discrete_function->dataType().numberOfColumns()));
}
// LCOV_EXCL_STOP
}
}
// LCOV_EXCL_START
default: {
throw UnexpectedError("invalid kind of P0 function: invalid data type");
}
// LCOV_EXCL_STOP
}
}
// LCOV_EXCL_START
default: {
throw UnexpectedError("invalid discretization type");
} }
// LCOV_EXCL_STOP } else {
throw UnexpectedError("invalid mesh types");
} }
} }
std::shared_ptr<const IDiscreteFunction> std::shared_ptr<const DiscreteFunctionVariant>
shallowCopy(const std::shared_ptr<const IMesh>& mesh, const std::shared_ptr<const IDiscreteFunction>& discrete_function) shallowCopy(const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const DiscreteFunctionVariant>& discrete_function_variant)
{ {
if (mesh == discrete_function->mesh()) { return std::visit(
return discrete_function; [&](auto&& f) {
} else if (mesh->dimension() != discrete_function->mesh()->dimension()) { if (mesh == f.mesh()) {
return discrete_function_variant;
} else if (mesh->dimension() != f.mesh()->dimension()) {
throw NormalError("incompatible mesh dimensions"); throw NormalError("incompatible mesh dimensions");
} }
switch (mesh->dimension()) { switch (mesh->dimension()) {
case 1: { case 1: {
return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<1>>>(mesh), discrete_function); return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<1>>>(mesh), f);
} }
case 2: { case 2: {
return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<2>>>(mesh), discrete_function); return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<2>>>(mesh), f);
} }
case 3: { case 3: {
return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<3>>>(mesh), discrete_function); return shallowCopy(std::dynamic_pointer_cast<const Mesh<Connectivity<3>>>(mesh), f);
} }
// LCOV_EXCL_START // LCOV_EXCL_START
default: { default: {
...@@ -175,4 +116,6 @@ shallowCopy(const std::shared_ptr<const IMesh>& mesh, const std::shared_ptr<cons ...@@ -175,4 +116,6 @@ shallowCopy(const std::shared_ptr<const IMesh>& mesh, const std::shared_ptr<cons
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
} }
},
discrete_function_variant->discreteFunction());
} }
...@@ -43,7 +43,8 @@ std::shared_ptr<const IMesh> getCommonMesh( ...@@ -43,7 +43,8 @@ std::shared_ptr<const IMesh> getCommonMesh(
bool hasSameMesh(const std::vector<std::shared_ptr<const DiscreteFunctionVariant>>& discrete_function_variant_list); bool hasSameMesh(const std::vector<std::shared_ptr<const DiscreteFunctionVariant>>& discrete_function_variant_list);
std::shared_ptr<const IDiscreteFunction> shallowCopy(const std::shared_ptr<const IMesh>& mesh, std::shared_ptr<const DiscreteFunctionVariant> shallowCopy(
const std::shared_ptr<const IDiscreteFunction>& discrete_function); const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const DiscreteFunctionVariant>& discrete_function);
#endif // DISCRETE_FUNCTION_UTILS_HPP #endif // DISCRETE_FUNCTION_UTILS_HPP
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment