summaryrefslogtreecommitdiff
path: root/tangle/LLBM/materials.h
diff options
context:
space:
mode:
authorAdrian Kummerlaender2021-05-17 00:15:33 +0200
committerAdrian Kummerlaender2021-05-17 00:15:33 +0200
commit4ec94c97879aafef15f7663135745e4ba61e62cf (patch)
tree322ae3f003892513f529842ff0b3fd100573b680 /tangle/LLBM/materials.h
downloadLiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar.gz
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar.bz2
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar.lz
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar.xz
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.tar.zst
LiterateLB-4ec94c97879aafef15f7663135745e4ba61e62cf.zip
Extract first public LiterateLB version
Diffstat (limited to 'tangle/LLBM/materials.h')
-rw-r--r--tangle/LLBM/materials.h108
1 files changed, 108 insertions, 0 deletions
diff --git a/tangle/LLBM/materials.h b/tangle/LLBM/materials.h
new file mode 100644
index 0000000..d782d8d
--- /dev/null
+++ b/tangle/LLBM/materials.h
@@ -0,0 +1,108 @@
+#pragma once
+
+#include "memory.h"
+#include "sdf.h"
+
+template <typename DESCRIPTOR>
+class CellMaterials : public SharedVector<int> {
+private:
+ const descriptor::Cuboid<DESCRIPTOR> _cuboid;
+ int* const _materials;
+
+public:
+ CellMaterials(descriptor::Cuboid<DESCRIPTOR> cuboid):
+ SharedVector<int>(cuboid.volume),
+ _cuboid(cuboid),
+ _materials(this->host()) { }
+
+ template <typename F>
+ CellMaterials(descriptor::Cuboid<DESCRIPTOR> cuboid, F f):
+ CellMaterials(cuboid) {
+ set(f);
+ }
+
+ descriptor::Cuboid<DESCRIPTOR> cuboid() const {
+ return _cuboid;
+ };
+
+ int get(std::size_t iCell) const {
+ return _materials[iCell];
+ }
+
+ void set(std::size_t iCell, int material) {
+ _materials[iCell] = material;
+ }
+ template <typename F>
+ void set(F f) {
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ set(iCell, f(gidInverse(_cuboid, iCell)));
+ }
+ }
+
+ template <typename S>
+ void sdf(S distance, int material, float eps=1e-2) {
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ auto p = gidInverseSmooth(_cuboid, iCell);
+ if (distance(p) < eps) {
+ set(iCell, material);
+ }
+ }
+ }
+
+ void clean(int material) {
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ if (get(iCell) == material) {
+ if (_cuboid.isInside(iCell)) {
+ bool surrounded = true;
+ for (unsigned iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
+ int m = get(descriptor::neighbor<DESCRIPTOR>(_cuboid, iCell, iPop));
+ surrounded &= m == material || m == 0;
+ }
+ if (surrounded) {
+ set(iCell, 0);
+ }
+ }
+ }
+ }
+ }
+ DeviceBuffer<std::size_t> list_of_material(int material) {
+ std::vector<std::size_t> cells;
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ if (_materials[iCell] == material) {
+ cells.emplace_back(iCell);
+ }
+ }
+ return DeviceBuffer<std::size_t>(cells);
+ }
+ DeviceBuffer<bool> mask_of_material(int material) {
+ std::unique_ptr<bool[]> mask(new bool[_cuboid.volume]{});
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ mask[iCell] = (_materials[iCell] == material);
+ }
+ return DeviceBuffer<bool>(mask.get(), _cuboid.volume);
+ }
+ std::size_t get_link_count(int bulk, int solid) {
+ std::size_t count = 0;
+ for (pop_index_t iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ std::size_t jCell = descriptor::neighbor<DESCRIPTOR>(_cuboid, iCell, iPop);
+ if (get(iCell) == bulk && get(jCell) == solid) {
+ count++;
+ }
+ }
+ }
+ return count;
+ }
+
+ template <typename F>
+ void for_links(int bulk, int solid, F f) {
+ for (pop_index_t iPop=0; iPop < DESCRIPTOR::q; ++iPop) {
+ for (std::size_t iCell=0; iCell < _cuboid.volume; ++iCell) {
+ std::size_t jCell = descriptor::neighbor<DESCRIPTOR>(_cuboid, iCell, iPop);
+ if (get(iCell) == bulk && get(jCell) == solid) {
+ f(iCell, iPop);
+ }
+ }
+ }
+ }
+};