Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of div operator for Field<Vector> #256

Merged
merged 35 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fd67a4d
update CHANGELOG
greole Feb 9, 2025
57a5ea2
add basic vector traits
greole Feb 8, 2025
29e75f1
add basic scalar traits
greole Feb 8, 2025
be5df76
clean-up
greole Feb 8, 2025
816679f
wip add vector field interpolation
greole Feb 8, 2025
1a102e2
template interpolation test
greole Feb 8, 2025
c8f96e4
make it work with scalar and vector, test fails
greole Feb 9, 2025
305d332
fix failing test, bcs were missing
greole Feb 9, 2025
6f4e157
add linear benchmark
greole Feb 9, 2025
1fc3625
fixup! add linear benchmark
greole Feb 9, 2025
dac7479
wip implement upwind
greole Feb 9, 2025
cdcc44e
fixup! compilation issue, more rename
greole Feb 9, 2025
7110aa3
add upwind benchmark
greole Feb 9, 2025
990d3b4
fix formating, fix copy to host
greole Feb 11, 2025
c36931d
fix copyright year
greole Feb 11, 2025
7f00740
add templated benchmark case, extract type for json
greole Feb 11, 2025
a79c537
template also upwdind benchmark
greole Feb 11, 2025
269a3fb
slight format change
greole Feb 11, 2025
92b8e34
fixup! TestType without NeoFOAM::
greole Feb 11, 2025
059cb98
use Namespace NeoFoam to make test/benchmark less verbose
greole Feb 11, 2025
391f758
use spans() to unpack
greole Feb 15, 2025
aab61cd
get neig/own only for internal faces
greole Feb 15, 2025
a771bfc
wip
greole Jan 27, 2025
e70cd48
wip make it compile
greole Feb 3, 2025
0410558
start parameterise test
greole Feb 3, 2025
518bca3
wip
greole Feb 4, 2025
0f5714a
wip make it compile
greole Feb 7, 2025
8859c47
uncomment build div operator test
greole Feb 8, 2025
ebdf83c
use interpolation and fill functions
greole Feb 11, 2025
e97a5a0
fix failing test
greole Feb 12, 2025
dc9315c
improve interpolation tests
greole Feb 15, 2025
a47f1f3
WIP add divOperator test values
greole Feb 15, 2025
23fac66
fixup! divOperator test for CPU
greole Feb 16, 2025
7c0d396
rename test file, set correct flux value on boundary via kokkos function
greole Feb 16, 2025
112b592
clean-up
greole Feb 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Version 0.2.0 (unreleased)
## Features
- add vectorField implementation []()
- add segmentedField to represent vector of vectors [#202](https://github.com/exasim-project/NeoFOAM/pull/202)
- Adds a minimal implementation linear algebra functionality [#219](https://github.com/exasim-project/NeoFOAM/pull/219)
## Fixes
Expand Down
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ endfunction()

add_subdirectory(fields)
add_subdirectory(finiteVolume/cellCentred/operator)
add_subdirectory(finiteVolume/cellCentred/interpolation)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: Unlicense
# SPDX-FileCopyrightText: 2025 NeoFOAM authors

neofoam_benchmark(linear)
neofoam_benchmark(upwind)
47 changes: 47 additions & 0 deletions benchmarks/finiteVolume/cellCentred/interpolation/linear.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2025 NeoFOAM authors

#define CATCH_CONFIG_RUNNER // Define this before including catch.hpp to create
// a custom main

#include "NeoFOAM/NeoFOAM.hpp"
#include "../../../catch_main.hpp"

#include <catch2/catch_template_test_macros.hpp>

using NeoFOAM::finiteVolume::cellCentred::SurfaceInterpolation;
using NeoFOAM::finiteVolume::cellCentred::VolumeField;
using NeoFOAM::finiteVolume::cellCentred::SurfaceField;

namespace NeoFOAM
{

TEMPLATE_TEST_CASE("linear", "", NeoFOAM::scalar, NeoFOAM::Vector)
{
auto size = GENERATE(1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20);

NeoFOAM::Executor exec = GENERATE(
NeoFOAM::Executor(NeoFOAM::SerialExecutor {}),
NeoFOAM::Executor(NeoFOAM::CPUExecutor {}),
NeoFOAM::Executor(NeoFOAM::GPUExecutor {})
);

std::string execName = std::visit([](auto e) { return e.name(); }, exec);
UnstructuredMesh mesh = create1DUniformMesh(exec, size);
auto surfaceBCs = fvcc::createCalculatedBCs<fvcc::SurfaceBoundary<TestType>>(mesh);
Input input = TokenList({std::string("linear")});
auto linear = SurfaceInterpolation(exec, mesh, input);

auto in = VolumeField<TestType>(exec, "in", mesh, {});
auto out = SurfaceField<TestType>(exec, "out", mesh, surfaceBCs);

fill(in.internalField(), one<TestType>::value);

// capture the value of size as section name
DYNAMIC_SECTION("" << size)
{
BENCHMARK(std::string(execName)) { return (linear.interpolate(in, out)); };
}
}

}
50 changes: 50 additions & 0 deletions benchmarks/finiteVolume/cellCentred/interpolation/upwind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2025 NeoFOAM authors

#define CATCH_CONFIG_RUNNER // Define this before including catch.hpp to create
// a custom main

#include "NeoFOAM/NeoFOAM.hpp"
#include "../../../catch_main.hpp"

#include <catch2/catch_template_test_macros.hpp>

using NeoFOAM::finiteVolume::cellCentred::SurfaceInterpolation;
using NeoFOAM::finiteVolume::cellCentred::VolumeField;
using NeoFOAM::finiteVolume::cellCentred::SurfaceField;
using NeoFOAM::Input;

namespace NeoFOAM
{

TEMPLATE_TEST_CASE("upwind", "", NeoFOAM::scalar, NeoFOAM::Vector)
{
auto size = GENERATE(1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20);

NeoFOAM::Executor exec = GENERATE(
NeoFOAM::Executor(NeoFOAM::SerialExecutor {}),
NeoFOAM::Executor(NeoFOAM::CPUExecutor {}),
NeoFOAM::Executor(NeoFOAM::GPUExecutor {})
);

std::string execName = std::visit([](auto e) { return e.name(); }, exec);
UnstructuredMesh mesh = create1DUniformMesh(exec, size);
auto surfaceBCs = fvcc::createCalculatedBCs<fvcc::SurfaceBoundary<TestType>>(mesh);
Input input = TokenList({std::string("upwind")});
auto upwind = SurfaceInterpolation(exec, mesh, input);

auto in = VolumeField<TestType>(exec, "in", mesh, {});
auto flux = SurfaceField<scalar>(exec, "flux", mesh, {});
auto out = SurfaceField<TestType>(exec, "out", mesh, surfaceBCs);

fill(flux.internalField(), one<scalar>::value);
fill(in.internalField(), one<TestType>::value);

// capture the value of size as section name
DYNAMIC_SECTION("" << size)
{
BENCHMARK(std::string(execName)) { return (upwind.interpolate(flux, in, out)); };
}
}

}
41 changes: 41 additions & 0 deletions benchmarks/finiteVolume/cellCentred/operator/gaussGreenGrad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 NeoFOAM authors

#define CATCH_CONFIG_RUNNER // Define this before including catch.hpp to create
// a custom main

#include "NeoFOAM/NeoFOAM.hpp"
#include "../../../catch_main.hpp"

using Operator = NeoFOAM::dsl::Operator;

TEST_CASE("DivOperator::div", "[bench]")
{
auto size = GENERATE(1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20);

NeoFOAM::Executor exec = GENERATE(
NeoFOAM::Executor(NeoFOAM::SerialExecutor {}),
NeoFOAM::Executor(NeoFOAM::CPUExecutor {}),
NeoFOAM::Executor(NeoFOAM::GPUExecutor {})
);

std::string execName = std::visit([](auto e) { return e.name(); }, exec);
NeoFOAM::UnstructuredMesh mesh = NeoFOAM::create1DUniformMesh(exec, size);
auto surfaceBCs = fvcc::createCalculatedBCs<fvcc::SurfaceBoundary<NeoFOAM::scalar>>(mesh);
fvcc::SurfaceField<NeoFOAM::scalar> faceFlux(exec, "sf", mesh, surfaceBCs);
NeoFOAM::fill(faceFlux.internalField(), 1.0);

auto volumeBCs = fvcc::createCalculatedBCs<fvcc::VolumeBoundary<NeoFOAM::scalar>>(mesh);
fvcc::VolumeField<NeoFOAM::scalar> phi(exec, "vf", mesh, volumeBCs);
fvcc::VolumeField<NeoFOAM::scalar> divPhi(exec, "divPhi", mesh, volumeBCs);
NeoFOAM::fill(phi.internalField(), 1.0);

// capture the value of size as section name
DYNAMIC_SECTION("" << size)
{
NeoFOAM::Input input = NeoFOAM::TokenList({std::string("Gauss"), std::string("linear")});
auto op = fvcc::DivOperator(Operator::Type::Explicit, faceFlux, phi, input);

BENCHMARK(std::string(execName)) { return (op.div(divPhi)); };
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 NeoFOAM authors
// SPDX-FileCopyrightText: 2023 NeoFOAM authors

#define CATCH_CONFIG_RUNNER // Define this before including catch.hpp to create
// a custom main
#include <catch2/catch_session.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators_all.hpp>

#include "NeoFOAM/NeoFOAM.hpp"

namespace fvcc = NeoFOAM::finiteVolume::cellCentred;
#include "../../../catch_main.hpp"

using Operator = NeoFOAM::dsl::Operator;

TEST_CASE("DivOperator")

TEST_CASE("DivOperator::grad", "[bench]")
{
auto size = GENERATE(1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20);

NeoFOAM::Executor exec = GENERATE(
NeoFOAM::Executor(NeoFOAM::SerialExecutor {}),
NeoFOAM::Executor(NeoFOAM::CPUExecutor {}),
NeoFOAM::Executor(NeoFOAM::GPUExecutor {})
);

std::string execName = std::visit([](auto e) { return e.name(); }, exec);
// TODO take 1d mesh
NeoFOAM::UnstructuredMesh mesh = NeoFOAM::createSingleCellMesh(exec);
NeoFOAM::UnstructuredMesh mesh = NeoFOAM::create1DUniformMesh(exec, size);
auto surfaceBCs = fvcc::createCalculatedBCs<fvcc::SurfaceBoundary<NeoFOAM::scalar>>(mesh);

fvcc::SurfaceField<NeoFOAM::scalar> faceFlux(exec, "sf", mesh, surfaceBCs);
NeoFOAM::fill(faceFlux.internalField(), 1.0);

auto volumeBCs = fvcc::createCalculatedBCs<fvcc::VolumeBoundary<NeoFOAM::scalar>>(mesh);
fvcc::VolumeField<NeoFOAM::scalar> phi(exec, "sf", mesh, volumeBCs);
fvcc::VolumeField<NeoFOAM::scalar> phi(exec, "vf", mesh, volumeBCs);
fvcc::VolumeField<NeoFOAM::scalar> divPhi(exec, "divPhi", mesh, volumeBCs);
NeoFOAM::fill(phi.internalField(), 1.0);

SECTION("Construct from Token" + execName)
// capture the value of size as section name
DYNAMIC_SECTION("" << size)
{
NeoFOAM::Input input = NeoFOAM::TokenList({std::string("Gauss"), std::string("linear")});
fvcc::DivOperator(Operator::Type::Explicit, faceFlux, phi, input);
}
auto op = fvcc::GradOperator(Operator::Type::Explicit, faceFlux, phi, input);

SECTION("Construct from Dictionary" + execName)
{
NeoFOAM::Input input = NeoFOAM::Dictionary(
{{std::string("DivOperator"), std::string("Gauss")},
{std::string("surfaceInterpolation"), std::string("linear")}}
);
fvcc::DivOperator(Operator::Type::Explicit, faceFlux, phi, input);
BENCHMARK(std::string(execName)) { return (op.grad(divPhi)); };
}
}
18 changes: 18 additions & 0 deletions include/NeoFOAM/core/primitives/label.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include <cstdint>

#include "NeoFOAM/core/primitives/traits.hpp"


namespace NeoFOAM
{
#ifdef NEOFOAM_DP_LABEL
Expand All @@ -16,4 +19,19 @@ using localIdx = uint32_t;
using globalIdx = uint64_t;
using size_t = std::size_t;
using mpi_label_t = int;

// traits for vector
template<>
struct one<localIdx>
{
static const inline localIdx value = 1;
};


template<>
struct zero<localIdx>
{
static const inline localIdx value = 0;
};

}
16 changes: 16 additions & 0 deletions include/NeoFOAM/core/primitives/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-FileCopyrightText: 2023 NeoFOAM authors
#pragma once

#include "NeoFOAM/core/primitives/traits.hpp"

// TODO this needs to be implemented in the corresponding cmake file
namespace NeoFOAM
{
Expand All @@ -13,4 +15,18 @@ typedef float scalar;

constexpr scalar ROOTVSMALL = 1e-18;

// traits for vector
template<>
struct one<scalar>
{
static const inline scalar value = 1.0;
};


template<>
struct zero<scalar>
{
static const inline scalar value = 0.0;
};

} // namespace NeoFOAM
21 changes: 21 additions & 0 deletions include/NeoFOAM/core/primitives/traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 NeoFOAM authors

#pragma once

namespace NeoFOAM
{

template<typename T>
struct one
{
static const T value;
};

template<typename T>
struct zero
{
static const T value;
};

}
17 changes: 17 additions & 0 deletions include/NeoFOAM/core/primitives/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "NeoFOAM/core/primitives/scalar.hpp"
#include "NeoFOAM/core/primitives/label.hpp"
#include "NeoFOAM/core/primitives/traits.hpp"


namespace NeoFOAM
{
Expand Down Expand Up @@ -153,4 +155,19 @@ scalar mag(const Vector& vec) { return sqrt(vec[0] * vec[0] + vec[1] * vec[1] +

std::ostream& operator<<(std::ostream& out, const Vector& vec);


// traits for vector
template<>
struct one<Vector>
{
static const inline Vector value = {1.0, 1.0, 1.0};
};


template<>
struct zero<Vector>
{
static const inline Vector value = {0.0, 0.0, 0.0};
};

} // namespace NeoFOAM
5 changes: 2 additions & 3 deletions include/NeoFOAM/dsl/explicit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ namespace fvcc = NeoFOAM::finiteVolume::cellCentred;
namespace NeoFOAM::dsl::exp
{

Operator
div(const fvcc::SurfaceField<NeoFOAM::scalar>& faceFlux, fvcc::VolumeField<NeoFOAM::scalar>& phi)
Operator div(const fvcc::SurfaceField<scalar>& faceFlux, fvcc::VolumeField<scalar>& phi)
{
return Operator(fvcc::DivOperator(dsl::Operator::Type::Explicit, faceFlux, phi));
return Operator(fvcc::DivOperator<scalar>(dsl::Operator::Type::Explicit, faceFlux, phi));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ class Linear : public SurfaceInterpolationFactory::Register<Linear>

static std::string schema() { return "none"; }

void interpolate(const VolumeField<scalar>& volField, SurfaceField<scalar>& surfaceField)
const override;
void interpolate(const VolumeField<scalar>& src, SurfaceField<scalar>& dst) const override;

void interpolate(const VolumeField<Vector>& src, SurfaceField<Vector>& dst) const override;

void interpolate(
const SurfaceField<scalar>& faceFlux,
const VolumeField<scalar>& volField,
SurfaceField<scalar>& surfaceField
const SurfaceField<scalar>& flux, const VolumeField<scalar>& src, SurfaceField<scalar>& dst
) const override;

void interpolate(
const SurfaceField<scalar>& flux, const VolumeField<Vector>& src, SurfaceField<Vector>& dst
) const override;


std::unique_ptr<SurfaceInterpolationFactory> clone() const override;

private:
Expand Down
Loading
Loading