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

Commit

Permalink
ids for single element grid
Browse files Browse the repository at this point in the history
  • Loading branch information
mscroggs committed Mar 7, 2024
1 parent db5f849 commit 1015f7f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/grid/single_element_grid/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub struct SerialSingleElementGridBuilder<const GDIM: usize, T: Float + Scalar<R
points: Vec<T>,
cells: Vec<usize>,
point_indices_to_ids: Vec<usize>,
cell_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>,
}

Expand Down Expand Up @@ -51,8 +51,8 @@ where
points: vec![],
cells: vec![],
point_indices_to_ids: vec![],
cell_indices_to_ids: vec![],
point_ids_to_indices: HashMap::new(),
cell_indices_to_ids: vec![],
cell_ids_to_indices: HashMap::new(),
}
}
Expand All @@ -71,8 +71,8 @@ where
points: Vec::with_capacity(npoints * Self::GDIM),
cells: Vec::with_capacity(ncells * points_per_cell),
point_indices_to_ids: Vec::with_capacity(npoints),
cell_indices_to_ids: Vec::with_capacity(ncells),
point_ids_to_indices: HashMap::new(),
cell_indices_to_ids: Vec::with_capacity(ncells),
cell_ids_to_indices: HashMap::new(),
}
}
Expand Down Expand Up @@ -109,6 +109,10 @@ where
&self.cells,
self.element_data.0,
self.element_data.1,
self.point_indices_to_ids,
self.point_ids_to_indices,
self.cell_indices_to_ids,
self.cell_ids_to_indices,
)
}
}
53 changes: 45 additions & 8 deletions src/grid/single_element_grid/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rlst_dense::{
rlst_array_from_slice2, rlst_dynamic_array4,
traits::{RandomAccessByRef, Shape, UnsafeRandomAccessByRef},
};
use std::collections::HashMap;

/// Geometry of a serial grid
pub struct SerialSingleElementGeometry<T: Float + Scalar> {
Expand All @@ -26,6 +27,10 @@ pub struct SerialSingleElementGeometry<T: Float + Scalar> {
diameters: Vec<T>,
volumes: Vec<T>,
cell_indices: 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>,
}

unsafe impl<T: Float + Scalar> Sync for SerialSingleElementGeometry<T> {}
Expand All @@ -35,6 +40,10 @@ impl<T: Float + Scalar> SerialSingleElementGeometry<T> {
coordinates: Array<T, BaseArray<T, VectorContainer<T>, 2>, 2>,
cells_input: &[usize],
element: CiarletElement<T>,
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>,
) -> Self {
let dim = coordinates.shape()[1];
let size = element.dim();
Expand Down Expand Up @@ -86,6 +95,10 @@ impl<T: Float + Scalar> SerialSingleElementGeometry<T> {
diameters,
volumes,
cell_indices,
point_indices_to_ids,
point_ids_to_indices,
cell_indices_to_ids,
cell_ids_to_indices,
}
}
}
Expand Down Expand Up @@ -167,18 +180,16 @@ impl<T: Float + Scalar> Geometry for SerialSingleElementGeometry<T> {
}

fn point_index_to_id(&self, index: usize) -> usize {
panic!();
self.point_indices_to_ids[index]
}

fn cell_index_to_id(&self, index: usize) -> usize {
panic!();
self.cell_indices_to_ids[index]
}

fn point_id_to_index(&self, id: usize) -> usize {
panic!();
self.point_ids_to_indices[&id]
}
fn cell_id_to_index(&self, id: usize) -> usize {
panic!();
self.cell_ids_to_indices[&id]
}
}

Expand Down Expand Up @@ -273,7 +284,15 @@ mod test {
*points.get_mut([2, 1]).unwrap() = 1.0;
*points.get_mut([3, 0]).unwrap() = 0.0;
*points.get_mut([3, 1]).unwrap() = 1.0;
SerialSingleElementGeometry::new(points, &[0, 1, 2, 0, 2, 3], p1triangle)
SerialSingleElementGeometry::new(
points,
&[0, 1, 2, 0, 2, 3],
p1triangle,
vec![0, 1, 2, 3],
HashMap::from([(0, 0), (1, 1), (2, 2), (3, 3)]),
vec![0, 1],
HashMap::from([(0, 0), (1, 1)]),
)
}

fn example_geometry_3d() -> SerialSingleElementGeometry<f64> {
Expand Down Expand Up @@ -311,7 +330,25 @@ mod test {
*points.get_mut([8, 0]).unwrap() = 1.0;
*points.get_mut([8, 1]).unwrap() = 1.0;
*points.get_mut([8, 2]).unwrap() = 0.0;
SerialSingleElementGeometry::new(points, &[0, 2, 8, 5, 4, 1, 0, 8, 6, 7, 3, 4], p2triangle)
SerialSingleElementGeometry::new(
points,
&[0, 2, 8, 5, 4, 1, 0, 8, 6, 7, 3, 4],
p2triangle,
vec![0, 1, 2, 3, 4, 5, 6, 7, 8],
HashMap::from([
(0, 0),
(1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 6),
(7, 7),
(8, 8),
]),
vec![0, 1],
HashMap::from([(0, 0), (1, 1)]),
)
}

#[test]
Expand Down
24 changes: 22 additions & 2 deletions src/grid/single_element_grid/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rlst_dense::{
data_container::VectorContainer,
traits::MatrixInverse,
};
use std::collections::HashMap;

/// A serial grid
pub struct SerialSingleElementGrid<T: Float + Scalar> {
Expand All @@ -28,11 +29,17 @@ impl<T: Float + Scalar> SerialSingleElementGrid<T>
where

Check warning on line 29 in src/grid/single_element_grid/grid.rs

View workflow job for this annotation

GitHub Actions / Rust style checks

Diff in /home/runner/work/grid-rs/grid-rs/src/grid/single_element_grid/grid.rs

Check warning on line 29 in src/grid/single_element_grid/grid.rs

View workflow job for this annotation

GitHub Actions / Rust style checks

Diff in /home/runner/work/grid-rs/grid-rs/src/grid/single_element_grid/grid.rs
for<'a> Array<T, ArrayViewMut<'a, T, BaseArray<T, VectorContainer<T>, 2>, 2>, 2>: MatrixInverse,
{

#[allow(clippy::too_many_arguments)]
pub fn new(
points: Array<T, BaseArray<T, VectorContainer<T>, 2>, 2>,
cells: &[usize],
cell_type: ReferenceCellType,
cell_degree: 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>,
) -> Self {
if cell_type == ReferenceCellType::Triangle && cell_degree == 1 {
warn!("Creating a single element grid with a P1 triangle. Using a FlatTriangleGrid would be faster.");
Expand All @@ -54,8 +61,21 @@ where
start += npoints;
}

let topology = SerialSingleElementTopology::new(&cell_vertices, cell_type);
let geometry = SerialSingleElementGeometry::<T>::new(points, cells, element);
let topology = SerialSingleElementTopology::new(
&cell_vertices,
cell_type,
&point_indices_to_ids,
&cell_indices_to_ids,
);
let geometry = SerialSingleElementGeometry::<T>::new(
points,
cells,
element,
point_indices_to_ids,
point_ids_to_indices,
cell_indices_to_ids,
cell_ids_to_indices,
);
Self { topology, geometry }
}
}
Expand Down
48 changes: 38 additions & 10 deletions src/grid/single_element_grid/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::grid::traits::{Ownership, Topology};
use crate::reference_cell;
use crate::reference_cell::ReferenceCellType;
use crate::types::CellLocalIndexPair;
use std::collections::HashMap;

fn all_equal<T: Eq>(a: &[T], b: &[T]) -> bool {
if a.len() != b.len() {
Expand All @@ -30,15 +31,29 @@ pub struct SerialSingleElementTopology {
cells_to_entities: Vec<Vec<Vec<usize>>>,
entities_to_cells: Vec<Vec<Vec<CellLocalIndexPair<usize>>>>,
entity_types: Vec<ReferenceCellType>,
vertex_indices_to_ids: Vec<usize>,
vertex_ids_to_indices: HashMap<usize, usize>,
cell_indices_to_ids: Vec<usize>,
cell_ids_to_indices: HashMap<usize, usize>,
}

unsafe impl Sync for SerialSingleElementTopology {}

impl SerialSingleElementTopology {
pub fn new(cells_input: &[usize], cell_type: ReferenceCellType) -> Self {
pub fn new(
cells_input: &[usize],
cell_type: ReferenceCellType,
point_indices_to_ids: &[usize],
grid_cell_indices_to_ids: &[usize],
) -> Self {
let size = reference_cell::entity_counts(cell_type)[0];
let ncells = cells_input.len() / size;

let mut vertex_indices_to_ids = vec![];
let mut vertex_ids_to_indices = HashMap::new();
let mut cell_indices_to_ids = vec![];
let mut cell_ids_to_indices = HashMap::new();

let mut index_map = vec![0; ncells];
let mut vertices = vec![];
let dim = reference_cell::dim(cell_type);
Expand All @@ -59,11 +74,15 @@ impl SerialSingleElementTopology {
for (cell_i, i) in index_map.iter_mut().enumerate() {
let cell = &cells_input[start..start + size];
*i = cell_i;
cell_indices_to_ids.push(grid_cell_indices_to_ids[cell_i]);
cell_ids_to_indices.insert(grid_cell_indices_to_ids[cell_i], cell_i);
let mut row = vec![];
for v in cell {
if !vertices.contains(v) {
entities_to_cells[0].push(vec![]);
vertices.push(*v);
vertex_indices_to_ids.push(point_indices_to_ids[*v]);
vertex_ids_to_indices.insert(point_indices_to_ids[*v], *v);
}
row.push(vertices.iter().position(|&r| r == *v).unwrap());
}
Expand Down Expand Up @@ -118,6 +137,10 @@ impl SerialSingleElementTopology {
cells_to_entities,
entities_to_cells,
entity_types,
vertex_indices_to_ids,
vertex_ids_to_indices,
cell_indices_to_ids,
cell_ids_to_indices,
}
}
}
Expand Down Expand Up @@ -192,17 +215,17 @@ impl Topology for SerialSingleElementTopology {
}
}

fn vertex_index_to_id(&self, index: Self::IndexType) -> usize {
panic!();
fn vertex_index_to_id(&self, index: usize) -> usize {
self.vertex_indices_to_ids[index]
}
fn cell_index_to_id(&self, index: Self::IndexType) -> usize {
panic!();
fn cell_index_to_id(&self, index: usize) -> usize {
self.cell_indices_to_ids[index]
}
fn vertex_id_to_index(&self, id: usize) -> Self::IndexType {
panic!();
fn vertex_id_to_index(&self, id: usize) -> usize {
self.vertex_ids_to_indices[&id]
}
fn cell_id_to_index(&self, id: usize) -> Self::IndexType {
panic!();
fn cell_id_to_index(&self, id: usize) -> usize {
self.cell_ids_to_indices[&id]
}
}

Expand All @@ -211,7 +234,12 @@ mod test {
use super::*;

fn example_topology() -> SerialSingleElementTopology {
SerialSingleElementTopology::new(&[0, 1, 2, 2, 1, 3], ReferenceCellType::Triangle)
SerialSingleElementTopology::new(
&[0, 1, 2, 2, 1, 3],
ReferenceCellType::Triangle,
&[0, 1, 2, 3],
&[0, 1],
)
}

#[test]
Expand Down
4 changes: 1 addition & 3 deletions src/grid/traits_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ where
type Geometry<'a> = CellGeometry<'a, T, GridImpl> where Self: 'a;

fn id(&self) -> usize {
panic!();

//self.grid.geometry().point_index_to_id(self.index)
self.grid.geometry().point_index_to_id(self.index)
}
fn index(&self) -> usize {
self.index
Expand Down

0 comments on commit 1015f7f

Please sign in to comment.