Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Add implementation of grid topology and geometry #3

Merged
merged 110 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
2078a1c
Start adding grid implementation
mscroggs Feb 23, 2024
08bb6fa
Merge branch 'main' into mscroggs/implementation
mscroggs Feb 23, 2024
cd08b48
copy and update topology implementation
mscroggs Feb 23, 2024
eaa7ee6
implement and test connectivity
mscroggs Feb 23, 2024
2daf0a9
implement and test connectivity
mscroggs Feb 23, 2024
00cf3be
install mpi on workflow
mscroggs Feb 23, 2024
0ce94ff
remove adjacency list from storage of connectivity
mscroggs Feb 23, 2024
025ddd2
remove adjacency list from input
mscroggs Feb 23, 2024
544e14c
Add mixed cell type tests (that currently fail)
mscroggs Feb 23, 2024
28f8404
start working on connectivity refactor
mscroggs Feb 23, 2024
c78899e
attempting to split connectivity by entity type
mscroggs Feb 26, 2024
aa22a50
Revert "attempting to split connectivity by entity type"
mscroggs Feb 26, 2024
bf6aab1
store connectivity by entity type
mscroggs Feb 26, 2024
0b19ac4
update implementation to use _neww
mscroggs Feb 26, 2024
c518b22
make mixed mesh work
mscroggs Feb 26, 2024
b9c5f46
tidying
mscroggs Feb 27, 2024
ee7bd86
remove old implementation
mscroggs Feb 27, 2024
b7ec892
clippy
mscroggs Feb 27, 2024
bc440e0
remove use of Bempp reference cell
mscroggs Feb 27, 2024
0d774e8
add test that cell_entities and connectivity are the same
mscroggs Feb 27, 2024
2b28e56
fmt
mscroggs Feb 27, 2024
544dde9
clippy
mscroggs Feb 27, 2024
24dffb5
make cell_connectivity as we go
mscroggs Feb 27, 2024
21cd207
index cells by type and index
mscroggs Feb 27, 2024
ac24d0a
look up cell types instead of recomputing every time
mscroggs Feb 27, 2024
8de2348
use cell_connectivity when we can
mscroggs Feb 27, 2024
4c4e2ac
add geometry template
mscroggs Feb 27, 2024
8317467
Revert "use cell_connectivity when we can"
mscroggs Feb 27, 2024
a9d789f
tidy
mscroggs Feb 27, 2024
100008b
working on geometry
mscroggs Feb 27, 2024
2a763ef
remove prints
mscroggs Feb 28, 2024
eed5c17
small simplifying
mscroggs Feb 28, 2024
31286c6
Merge branch 'main' into mscroggs/implementation
mscroggs Feb 28, 2024
bd57bfc
add new files
mscroggs Feb 28, 2024
6199d29
fmt
mscroggs Feb 28, 2024
832304a
Implement new traits for grid_impl
mscroggs Feb 29, 2024
925de7c
clippy
mscroggs Feb 29, 2024
a4cfe86
implment new() for mixed cell type mesh
mscroggs Feb 29, 2024
da90cda
remove comments
mscroggs Feb 29, 2024
a6a924e
f64::consts::FRAC_1_SQRT_2
mscroggs Feb 29, 2024
03e8c3e
fmt
mscroggs Feb 29, 2024
334550b
std::
mscroggs Feb 29, 2024
0fd8587
move files to mixed_grid/
mscroggs Feb 29, 2024
e2a9977
fmt
mscroggs Feb 29, 2024
2e03387
add some TODOs
mscroggs Feb 29, 2024
44aedf2
Merge branch 'main' into mscroggs/implementation
mscroggs Feb 29, 2024
4543f24
fmt
mscroggs Feb 29, 2024
62de714
Add IndexType to Topology
mscroggs Feb 29, 2024
5cc1a0b
add indextype to geometry
mscroggs Feb 29, 2024
8494345
add single element grid
mscroggs Feb 29, 2024
3bd59ef
vertex -> point
mscroggs Feb 29, 2024
febef7a
Merge branch 'main' into mscroggs/implementation
mscroggs Feb 29, 2024
66422f5
Partially merge branch 'main' into mscroggs/implementation
mscroggs Feb 29, 2024
ea47e44
fix lifetime error
mscroggs Feb 29, 2024
bc30908
clippy
mscroggs Feb 29, 2024
4c4398d
fmt
mscroggs Feb 29, 2024
ceba375
working on evaluator
mscroggs Mar 1, 2024
ff00eff
compute points on physical cell
mscroggs Mar 1, 2024
c41b22d
use rlst array in place of flat vector
mscroggs Mar 4, 2024
0bc931b
remove prints
mscroggs Mar 4, 2024
e7bf21f
compute jacobian
mscroggs Mar 4, 2024
7cb8626
compute_normal
mscroggs Mar 4, 2024
3a5fad5
use rlst for coordinates
mscroggs Mar 4, 2024
c1624f8
start implementing map iterators
mscroggs Mar 4, 2024
126b487
store table, not iterator
mscroggs Mar 5, 2024
ea3759f
new file
mscroggs Mar 5, 2024
1b07a34
simpler reference_to_physical_map (no iterator)
mscroggs Mar 5, 2024
d64f699
implement map for mixed grid
mscroggs Mar 5, 2024
524e22b
implement point and vertex iterators
mscroggs Mar 5, 2024
82b4a0a
use ReferenceCellType from bempp, remove matches
mscroggs Mar 5, 2024
af1fe12
add cell_to_entities and entity_to_cells to trait
mscroggs Mar 5, 2024
8ee73f0
update tests, simplify
mscroggs Mar 5, 2024
02510e6
New connectivity format for single element grid
mscroggs Mar 5, 2024
5c70ba5
remove prints
mscroggs Mar 5, 2024
2746a68
_
mscroggs Mar 5, 2024
de9bcd3
fmt
mscroggs Mar 5, 2024
19bd2c6
update connectivity for mixed grid
mscroggs Mar 6, 2024
646713c
clippy
mscroggs Mar 6, 2024
8cd9666
store &geometry and &topology directly rather than full grid
mscroggs Mar 6, 2024
53f655d
implement *_to_cells
mscroggs Mar 6, 2024
4b404b6
implement midpoint for single element grid
mscroggs Mar 6, 2024
db26e03
midpoints for mixed grid
mscroggs Mar 6, 2024
288094c
tidy
mscroggs Mar 6, 2024
32b20ce
Working on flat triangle grid
mscroggs Mar 6, 2024
e6f2b02
working on flat triangle grid
mscroggs Mar 6, 2024
2a0efd9
remove std::marker::
mscroggs Mar 7, 2024
1b78e27
Merge branch 'mscroggs/implementation' of github.com:bempp/grid-rs in…
mscroggs Mar 7, 2024
79c60c4
implement builder for flat triangle grid
mscroggs Mar 7, 2024
8f012a1
tidy
mscroggs Mar 7, 2024
2e46d6d
tidy imports
mscroggs Mar 7, 2024
ba5bde1
Add builder for single element grid
mscroggs Mar 7, 2024
07b4f21
Add builder for mixed grid
mscroggs Mar 7, 2024
9ebf14a
move grid/grid to grid/traits_impl
mscroggs Mar 7, 2024
fb2e257
update Inverse trait
mscroggs Mar 7, 2024
57dbeb7
reset branch
mscroggs Mar 7, 2024
797645a
create_grid should consume the builder
mscroggs Mar 7, 2024
361ff38
define cells using ids not indices
mscroggs Mar 7, 2024
8c0dd3b
merge geometry and topology for flat_triangle_grid into grid.rs
mscroggs Mar 7, 2024
6a97cc1
clippy
mscroggs Mar 7, 2024
db5f849
add topology id functions
mscroggs Mar 7, 2024
1015f7f
ids for single element grid
mscroggs Mar 7, 2024
4e20832
ids for mixed element grid
mscroggs Mar 8, 2024
d6c7cc9
panic instead of failing to find non-vertex point
mscroggs Mar 8, 2024
829c151
Add documentation
mscroggs Mar 8, 2024
d440ee8
implement diameters
mscroggs Mar 8, 2024
61b0337
add diameter test
mscroggs Mar 8, 2024
5db7ea1
more diameter tests
mscroggs Mar 8, 2024
7bdb0fe
cancel 2s in diameter formula
mscroggs Mar 8, 2024
7a9e944
volume for single element grid
mscroggs Mar 8, 2024
044278d
volume for mixed grid
mscroggs Mar 8, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/style-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: mpi4py/setup-mpi@v1
with:
mpi: mpich
- uses: actions/checkout@v3
- uses: actions/checkout@v3
- name: Install LAPACK & OpenBLAS
run:
sudo apt-get install libopenblas-dev liblapack-dev
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
approx = "0.5"
bempp-traits = { git = "https://github.com/bempp/bempp-rs.git" }
bempp-tools = { git = "https://github.com/bempp/bempp-rs.git" }
bempp-element = { git = "https://github.com/bempp/bempp-rs.git" }
bempp-quadrature = { git = "https://github.com/bempp/bempp-rs.git" }
num = "0.4"
rlst = { git = "https://github.com/linalg-rs/rlst.git" }
rlst-dense = { git = "https://github.com/linalg-rs/rlst.git" }
rlst-proc-macro = { git = "https://github.com/linalg-rs/rlst.git" }
rlst-common = { git = "https://github.com/linalg-rs/rlst.git" }

log = "0.4"
13 changes: 12 additions & 1 deletion src/grid.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
pub mod surface_triangle_grid;
//! Grid implementation

pub mod common;
pub mod flat_triangle_grid;
pub mod mixed_grid;
pub mod single_element_grid;
pub mod traits;
pub mod traits_impl;

pub use self::traits::Geometry;
pub use self::traits::Grid;
pub use self::traits::Topology;
148 changes: 148 additions & 0 deletions src/grid/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//! Functionality common to multiple grid implementations

use crate::grid::traits::Geometry;
use num::Float;
use rlst_common::types::Scalar;
use rlst_dense::{
array::Array,
traits::{Shape, UnsafeRandomAccessByRef, UnsafeRandomAccessByValue},
};

/// Compute a physical point
pub fn compute_point<T: Scalar, Table: UnsafeRandomAccessByRef<4, Item = T> + Shape<4>>(
geometry: &impl Geometry<T = T>,
table: Table,
cell_index: usize,
point_index: usize,
point: &mut [T],
) {
assert_eq!(geometry.dim(), point.len());

let cell = geometry.index_map()[cell_index];

for component in point.iter_mut() {
*component = T::from(0.0).unwrap();
}
for (i, v) in geometry.cell_points(cell).unwrap().iter().enumerate() {
let t = unsafe { *table.get_unchecked([0, point_index, i, 0]) };
for (j, component) in point.iter_mut().enumerate() {
*component += *geometry.coordinate(*v, j).unwrap() * t;
}
}
}

/// Compute a Jacobian
pub fn compute_jacobian<T: Scalar, Table: UnsafeRandomAccessByRef<4, Item = T> + Shape<4>>(
geometry: &impl Geometry<T = T>,
table: Table,
tdim: usize,
cell_index: usize,
point_index: usize,
jacobian: &mut [T],
) {
let gdim = geometry.dim();
assert_eq!(jacobian.len(), gdim * tdim);

let cell = geometry.index_map()[cell_index];

for component in jacobian.iter_mut() {
*component = T::from(0.0).unwrap();
}
for (i, v) in geometry.cell_points(cell).unwrap().iter().enumerate() {
for gd in 0..gdim {
for td in 0..tdim {
jacobian[td * gdim + gd] += *geometry.coordinate(*v, gd).unwrap()
* unsafe { *table.get_unchecked([1 + td, point_index, i, 0]) };
}
}
}
}

/// Compute a normal from a Jacobian of a cell with topological dimension 2 and geometric dimension 3
pub fn compute_normal_from_jacobian23<T: Scalar>(jacobian: &[T], normal: &mut [T]) {
assert_eq!(jacobian.len(), 6);
assert_eq!(normal.len(), 3);

for (i, j, k) in [(0, 1, 2), (1, 2, 0), (2, 0, 1)] {
normal[i] = jacobian[j] * jacobian[3 + k] - jacobian[k] * jacobian[3 + j];
}
let size = Scalar::sqrt(normal.iter().map(|&x| Scalar::powi(x, 2)).sum::<T>());
for i in normal.iter_mut() {
*i /= size;
}
}

/// Compute the determinant of a matrix
pub fn compute_det<T: Scalar<Real = T>>(jacobian: &[T], tdim: usize, gdim: usize) -> T {
assert_eq!(jacobian.len(), tdim * gdim);
match tdim {
1 => match gdim {
1 => T::abs(jacobian[0]),
2 => T::sqrt(jacobian.iter().map(|x| x.powi(2)).sum()),
3 => T::sqrt(jacobian.iter().map(|x| x.powi(2)).sum()),
_ => {
unimplemented!("compute_det() not implemented for topological dimension {tdim} and geometric dimension: {gdim}");
}
},
2 => match gdim {
2 => T::abs(jacobian[0] * jacobian[3] - jacobian[1] * jacobian[2]),
3 => T::sqrt(
[(1, 2), (2, 0), (0, 1)]
.iter()
.map(|(j, k)| {
(jacobian[*j] * jacobian[3 + *k] - jacobian[*k] * jacobian[3 + *j]).powi(2)
})
.sum(),
),
_ => {
unimplemented!("compute_det() not implemented for topological dimension {tdim} and geometric dimension: {gdim}");
}
},
3 => match gdim {
3 => T::abs(
[(0, 1, 2), (1, 2, 0), (2, 0, 1)]
.iter()
.map(|(i, j, k)| {
jacobian[*i]
* (jacobian[3 + *j] * jacobian[6 + *k]
- jacobian[3 + *k] * jacobian[6 + *j])
})
.sum(),
),
_ => {
unimplemented!("compute_det() not implemented for topological dimension {tdim} and geometric dimension: {gdim}");
}
},
_ => {
unimplemented!("compute_det() not implemented for topological dimension {tdim}");
}
}
}

/// Compute the diameter of a triangle
pub fn compute_diameter_triangle<
T: Scalar<Real = T> + Float,
ArrayImpl: UnsafeRandomAccessByValue<1, Item = T> + Shape<1>,
>(
v0: Array<T, ArrayImpl, 1>,
v1: Array<T, ArrayImpl, 1>,
v2: Array<T, ArrayImpl, 1>,
) -> T {
let a = (v0.view() - v1.view()).norm_2();
let b = (v0 - v2.view()).norm_2();
let c = (v1 - v2).norm_2();
Scalar::sqrt((b + c - a) * (a + c - b) * (a + b - c) / (a + b + c))
}

/// Compute the diameter of a quadrilateral
pub fn compute_diameter_quadrilateral<
T: Scalar<Real = T> + Float,
ArrayImpl: UnsafeRandomAccessByValue<1, Item = T> + Shape<1>,
>(
v0: Array<T, ArrayImpl, 1>,
v1: Array<T, ArrayImpl, 1>,
v2: Array<T, ArrayImpl, 1>,
v3: Array<T, ArrayImpl, 1>,
) -> T {
T::max((v0 - v3).norm_2(), (v1 - v2).norm_2())
}
9 changes: 9 additions & 0 deletions src/grid/flat_triangle_grid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! A flat triangle grid
//!
//! In this grid, every cell is a flat triangle

mod builder;
mod grid;

pub use self::builder::SerialFlatTriangleGridBuilder;
pub use self::grid::SerialFlatTriangleGrid;
92 changes: 92 additions & 0 deletions src/grid/flat_triangle_grid/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! Grid builder

use crate::grid::flat_triangle_grid::grid::SerialFlatTriangleGrid;
use crate::traits::builder::Builder;
use num::Float;
use rlst_common::types::Scalar;
use rlst_dense::{
array::{views::ArrayViewMut, Array},
base_array::BaseArray,
data_container::VectorContainer,
rlst_array_from_slice2, rlst_dynamic_array2,
traits::MatrixInverse,
};
use std::collections::HashMap;

/// Grid builder for a flat triangle grid
pub struct SerialFlatTriangleGridBuilder<T: Float + Scalar<Real = T>> {
points: Vec<T>,
cells: Vec<usize>,
point_indices_to_ids: Vec<usize>,
point_ids_to_indices: HashMap<usize, usize>,
cell_indices_to_ids: Vec<usize>,
cell_ids_to_indices: HashMap<usize, usize>,
}

impl<T: Float + Scalar<Real = T>> Builder<3> for SerialFlatTriangleGridBuilder<T>
where
for<'a> Array<T, ArrayViewMut<'a, T, BaseArray<T, VectorContainer<T>, 2>, 2>, 2>: MatrixInverse,
{
type GridType = SerialFlatTriangleGrid<T>;
type T = T;
type CellData = [usize; 3];
type GridMetadata = ();

fn new(_data: ()) -> Self {
Self {
points: vec![],
cells: vec![],
point_indices_to_ids: vec![],
point_ids_to_indices: HashMap::new(),
cell_indices_to_ids: vec![],
cell_ids_to_indices: HashMap::new(),
}
}

fn new_with_capacity(npoints: usize, ncells: usize, _data: ()) -> Self {
Self {
points: Vec::with_capacity(npoints * Self::GDIM),
cells: Vec::with_capacity(ncells * 3),
point_indices_to_ids: Vec::with_capacity(npoints),
point_ids_to_indices: HashMap::new(),
cell_indices_to_ids: Vec::with_capacity(ncells),
cell_ids_to_indices: HashMap::new(),
}
}

fn add_point(&mut self, id: usize, data: [T; 3]) {
self.point_ids_to_indices
.insert(id, self.point_indices_to_ids.len());
self.point_indices_to_ids.push(id);
self.points.extend_from_slice(&data);
}

fn add_cell(&mut self, id: usize, cell_data: [usize; 3]) {
self.cell_ids_to_indices
.insert(id, self.cell_indices_to_ids.len());
self.cell_indices_to_ids.push(id);
for id in &cell_data {
self.cells.push(self.point_ids_to_indices[id]);
}
}

fn create_grid(self) -> Self::GridType {
// TODO: remove this transposing
let npts = self.point_indices_to_ids.len();
let mut points = rlst_dynamic_array2!(T, [npts, 3]);
points.fill_from(rlst_array_from_slice2!(
T,
&self.points,
[npts, 3],
[1, npts]
));
SerialFlatTriangleGrid::new(
points,
&self.cells,
self.point_indices_to_ids,
self.point_ids_to_indices,
self.cell_indices_to_ids,
self.cell_ids_to_indices,
)
}
}
Loading
Loading