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
No related branches found
No related tags found
1 merge request!162Fix return type value checking for BuiltinFunctionEmbedder
......@@ -407,8 +407,8 @@ SchemeModule::SchemeModule()
std::function(
[](const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const IDiscreteFunction>& v)
-> std::shared_ptr<const IDiscreteFunction> { return shallowCopy(mesh, v); }
const std::shared_ptr<const DiscreteFunctionVariant>& v)
-> std::shared_ptr<const DiscreteFunctionVariant> { return shallowCopy(mesh, v); }
));
......
......@@ -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());
}
template <size_t Dimension>
std::shared_ptr<const IDiscreteFunction>
shallowCopy(const std::shared_ptr<const Mesh<Connectivity<Dimension>>>& mesh,
const std::shared_ptr<const IDiscreteFunction>& discrete_function)
template <typename MeshType, typename DiscreteFunctionT>
std::shared_ptr<const DiscreteFunctionVariant>
shallowCopy(const std::shared_ptr<const MeshType>& mesh, const DiscreteFunctionT& f)
{
const std::shared_ptr function_mesh =
std::dynamic_pointer_cast<const Mesh<Connectivity<Dimension>>>(discrete_function->mesh());
const std::shared_ptr function_mesh = std::dynamic_pointer_cast<const MeshType>(f.mesh());
if (mesh->shared_connectivity() != function_mesh->shared_connectivity()) {
throw NormalError("cannot shallow copy when connectivity changes");
}
switch (discrete_function->descriptor().type()) {
case DiscreteFunctionType::P0: {
switch (discrete_function->dataType()) {
case ASTNodeDataType::double_t: {
return shallowCopy(mesh,
std::dynamic_pointer_cast<const DiscreteFunctionP0<Dimension, double>>(discrete_function));
}
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");
if constexpr (std::is_same_v<MeshType, typename DiscreteFunctionT::MeshType>) {
if constexpr (is_discrete_function_P0_v<DiscreteFunctionT>) {
return std::make_shared<DiscreteFunctionVariant>(DiscreteFunctionT(mesh, f.cellValues()));
} else if constexpr (is_discrete_function_P0_vector_v<DiscreteFunctionT>) {
return std::make_shared<DiscreteFunctionVariant>(DiscreteFunctionT(mesh, f.cellArrays()));
} else {
throw UnexpectedError("invalid discrete function type");
}
// LCOV_EXCL_STOP
} else {
throw UnexpectedError("invalid mesh types");
}
}
std::shared_ptr<const IDiscreteFunction>
shallowCopy(const std::shared_ptr<const IMesh>& mesh, const std::shared_ptr<const IDiscreteFunction>& discrete_function)
std::shared_ptr<const DiscreteFunctionVariant>
shallowCopy(const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const DiscreteFunctionVariant>& discrete_function_variant)
{
if (mesh == discrete_function->mesh()) {
return discrete_function;
} else if (mesh->dimension() != discrete_function->mesh()->dimension()) {
return std::visit(
[&](auto&& f) {
if (mesh == f.mesh()) {
return discrete_function_variant;
} else if (mesh->dimension() != f.mesh()->dimension()) {
throw NormalError("incompatible mesh dimensions");
}
switch (mesh->dimension()) {
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: {
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: {
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
default: {
......@@ -175,4 +116,6 @@ shallowCopy(const std::shared_ptr<const IMesh>& mesh, const std::shared_ptr<cons
}
// LCOV_EXCL_STOP
}
},
discrete_function_variant->discreteFunction());
}
......@@ -43,7 +43,8 @@ std::shared_ptr<const IMesh> getCommonMesh(
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,
const std::shared_ptr<const IDiscreteFunction>& discrete_function);
std::shared_ptr<const DiscreteFunctionVariant> shallowCopy(
const std::shared_ptr<const IMesh>& mesh,
const std::shared_ptr<const DiscreteFunctionVariant>& discrete_function);
#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