Skip to content

Commit 3fa5ec0

Browse files
authored
feat(core, kernels): add util method to set a vertex value (#436)
* feat(core): add util method to set a vertex value * fix: wrap in option to fix mismatched type * refactor(gridgen): use `set_vertex` to init from GPU data * fix(core): remove unused const generic from `set_vertex` * refactor(gridgen): use `set_vertex` to init in CPU routine * refactor(core): use `set_vertex` method in IO routines
1 parent 3f06a69 commit 3fa5ec0

6 files changed

Lines changed: 44 additions & 31 deletions

File tree

applications/generate_grid/gpu.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn build_2d<T: CoordsFloat>(
8181

8282
let vertices = vertices.as_slice()?;
8383
map.par_iter_vertices().for_each(|d| {
84-
map.write_vertex(d as VertexIdType, vertices[d as usize]);
84+
map.set_vertex(d as VertexIdType, vertices[d as usize]);
8585
});
8686
Ok(map)
8787
}
@@ -164,7 +164,7 @@ pub fn build_3d<T: CoordsFloat>(
164164

165165
let vertices = vertices.as_slice()?;
166166
map.par_iter_vertices().for_each(|d| {
167-
map.write_vertex(d as VertexIdType, vertices[d as usize]);
167+
map.set_vertex(d as VertexIdType, vertices[d as usize]);
168168
});
169169
Ok(map)
170170
}

honeycomb-core/src/attributes/collections.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub struct AttrSparseVec<T: AttributeBind + AttributeUpdate> {
3030
data: Vec<TVar<Option<T>>>,
3131
}
3232

33+
impl<A: AttributeBind + AttributeUpdate> AttrSparseVec<A> {
34+
pub(crate) fn set_atomic(&self, id: usize, val: A) {
35+
self.data[id].write_atomic(Some(val));
36+
}
37+
}
38+
3339
unsafe impl<A: AttributeBind + AttributeUpdate> Send for AttrSparseVec<A> {}
3440
unsafe impl<A: AttributeBind + AttributeUpdate> Sync for AttrSparseVec<A> {}
3541

honeycomb-core/src/cmap/builder/io.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use vtkio::{IOBuffer, Vtk};
99

1010
use crate::attributes::AttrStorageManager;
1111
use crate::cmap::{BuilderError, CMap2, CMap3, DartIdType, VertexIdType};
12-
use crate::geometry::{CoordsFloat, Vertex2};
12+
use crate::geometry::{CoordsFloat, Vertex2, Vertex3};
1313

1414
// --- Custom
1515

@@ -198,7 +198,7 @@ pub fn build_2d_from_cmap_file<T: CoordsFloat>(
198198
if it.next().is_some() {
199199
return Err(BuilderError::BadValue("incorrect vertex line format"));
200200
}
201-
map.write_vertex(id, (T::from(x).unwrap(), T::from(y).unwrap()));
201+
map.set_vertex(id, Vertex2(T::from(x).unwrap(), T::from(y).unwrap()));
202202
}
203203
}
204204

@@ -315,9 +315,9 @@ pub fn build_3d_from_cmap_file<T: CoordsFloat>(
315315
if it.next().is_some() {
316316
return Err(BuilderError::BadValue("incorrect vertex line format"));
317317
}
318-
map.write_vertex(
318+
map.set_vertex(
319319
id,
320-
(
320+
Vertex3(
321321
T::from(x).unwrap(),
322322
T::from(y).unwrap(),
323323
T::from(z).unwrap(),
@@ -484,9 +484,9 @@ pub fn build_2d_from_vtk<T: CoordsFloat>(
484484
// build the triangle
485485
let d0 = cmap.allocate_used_darts(3);
486486
let (d1, d2) = (d0 + 1, d0 + 2);
487-
cmap.write_vertex(d0 as VertexIdType, vertices[vids[0]]);
488-
cmap.write_vertex(d1 as VertexIdType, vertices[vids[1]]);
489-
cmap.write_vertex(d2 as VertexIdType, vertices[vids[2]]);
487+
cmap.set_vertex(d0 as VertexIdType, vertices[vids[0]]);
488+
cmap.set_vertex(d1 as VertexIdType, vertices[vids[1]]);
489+
cmap.set_vertex(d2 as VertexIdType, vertices[vids[2]]);
490490
cmap.link::<1>(d0, d1).unwrap(); // edge d0 links vertices vids[0] & vids[1]
491491
cmap.link::<1>(d1, d2).unwrap(); // edge d1 links vertices vids[1] & vids[2]
492492
cmap.link::<1>(d2, d0).unwrap(); // edge d2 links vertices vids[2] & vids[0]
@@ -508,10 +508,7 @@ pub fn build_2d_from_vtk<T: CoordsFloat>(
508508
let di = d0 + i as DartIdType;
509509
let dip1 =
510510
if i == n_vertices - 1 { d0 } else { di + 1 };
511-
cmap.write_vertex(
512-
di as VertexIdType,
513-
vertices[vids[i]],
514-
);
511+
cmap.set_vertex(di as VertexIdType, vertices[vids[i]]);
515512
cmap.link::<1>(di, dip1).unwrap();
516513
sew_buffer
517514
.insert((vids[i], vids[(i + 1) % n_vertices]), di);
@@ -531,10 +528,10 @@ pub fn build_2d_from_vtk<T: CoordsFloat>(
531528
// build the quad
532529
let d0 = cmap.allocate_used_darts(4);
533530
let (d1, d2, d3) = (d0 + 1, d0 + 2, d0 + 3);
534-
cmap.write_vertex(d0 as VertexIdType, vertices[vids[0]]);
535-
cmap.write_vertex(d1 as VertexIdType, vertices[vids[1]]);
536-
cmap.write_vertex(d2 as VertexIdType, vertices[vids[2]]);
537-
cmap.write_vertex(d3 as VertexIdType, vertices[vids[3]]);
531+
cmap.set_vertex(d0 as VertexIdType, vertices[vids[0]]);
532+
cmap.set_vertex(d1 as VertexIdType, vertices[vids[1]]);
533+
cmap.set_vertex(d2 as VertexIdType, vertices[vids[2]]);
534+
cmap.set_vertex(d3 as VertexIdType, vertices[vids[3]]);
538535
cmap.link::<1>(d0, d1).unwrap(); // edge d0 links vertices vids[0] & vids[1]
539536
cmap.link::<1>(d1, d2).unwrap(); // edge d1 links vertices vids[1] & vids[2]
540537
cmap.link::<1>(d2, d3).unwrap(); // edge d2 links vertices vids[2] & vids[3]

honeycomb-core/src/cmap/dim2/utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! [`CMap2`] utilities implementations
22
3-
use crate::cmap::{CMap2, DartIdType};
4-
use crate::geometry::CoordsFloat;
3+
use crate::cmap::{CMap2, DartIdType, VertexIdType};
4+
use crate::geometry::{CoordsFloat, Vertex2};
55

66
use super::CMAP2_BETA;
77

@@ -25,4 +25,9 @@ impl<T: CoordsFloat> CMap2<T> {
2525
self.betas[(1, dart_id)].write_atomic(b1);
2626
self.betas[(2, dart_id)].write_atomic(b2);
2727
}
28+
29+
/// Set a vertex value for the corresponding ID.
30+
pub fn set_vertex(&self, vid: VertexIdType, v: Vertex2<T>) {
31+
self.vertices.set_atomic(vid as usize, v);
32+
}
2833
}

honeycomb-core/src/cmap/dim3/utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//!
33
//! This module contains utility code for the [`CMap3`] structure.
44
5-
use crate::cmap::{CMap3, DartIdType};
6-
use crate::geometry::CoordsFloat;
5+
use crate::cmap::{CMap3, DartIdType, VertexIdType};
6+
use crate::geometry::{CoordsFloat, Vertex3};
77

88
use super::CMAP3_BETA;
99

@@ -35,4 +35,9 @@ impl<T: CoordsFloat> CMap3<T> {
3535
self.betas[(2, dart_id)].write_atomic(b2);
3636
self.betas[(3, dart_id)].write_atomic(b3);
3737
}
38+
39+
/// Set a vertex value for the corresponding ID.
40+
pub fn set_vertex(&self, vid: VertexIdType, v: Vertex3<T>) {
41+
self.vertices.set_atomic(vid as usize, v);
42+
}
3843
}

honeycomb-kernels/src/grid_generation/internals.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn build_2d_grid<T: CoordsFloat>(
3434
.par_bridge()
3535
.for_each(|(y_idx, x_idx)| {
3636
let vertex_id = map.vertex_id((1 + x_idx * 4 + y_idx * 4 * n_square_x) as DartIdType);
37-
map.write_vertex(
37+
map.set_vertex(
3838
vertex_id,
3939
origin
4040
+ Vector2(
@@ -48,7 +48,7 @@ pub(crate) fn build_2d_grid<T: CoordsFloat>(
4848
(0..n_square_x).into_par_iter().for_each(|x_idx| {
4949
let y_idx = n_square_y - 1;
5050
let vertex_id = map.vertex_id((4 + x_idx * 4 + y_idx * 4 * n_square_x) as DartIdType);
51-
map.write_vertex(
51+
map.set_vertex(
5252
vertex_id,
5353
origin
5454
+ Vector2(
@@ -62,7 +62,7 @@ pub(crate) fn build_2d_grid<T: CoordsFloat>(
6262
(0..n_square_y).into_par_iter().for_each(|y_idx| {
6363
let x_idx = n_square_x - 1;
6464
let vertex_id = map.vertex_id((2 + x_idx * 4 + y_idx * 4 * n_square_x) as DartIdType);
65-
map.write_vertex(
65+
map.set_vertex(
6666
vertex_id,
6767
origin
6868
+ Vector2(
@@ -76,7 +76,7 @@ pub(crate) fn build_2d_grid<T: CoordsFloat>(
7676
{
7777
let (x_idx, y_idx) = (n_square_x - 1, n_square_y - 1);
7878
let vertex_id = map.vertex_id((3 + x_idx * 4 + y_idx * 4 * n_square_x) as DartIdType); // top right
79-
map.write_vertex(
79+
map.set_vertex(
8080
vertex_id,
8181
origin
8282
+ Vector2(
@@ -146,7 +146,7 @@ pub(crate) fn build_2d_splitgrid<T: CoordsFloat>(
146146
.par_bridge()
147147
.for_each(|(y_idx, x_idx)| {
148148
let vertex_id = map.vertex_id((1 + x_idx * 6 + y_idx * 6 * n_square_x) as DartIdType);
149-
map.write_vertex(
149+
map.set_vertex(
150150
vertex_id,
151151
origin
152152
+ Vector2(
@@ -160,7 +160,7 @@ pub(crate) fn build_2d_splitgrid<T: CoordsFloat>(
160160
(0..n_square_x).into_par_iter().for_each(|x_idx| {
161161
let y_idx = n_square_y - 1;
162162
let vertex_id = map.vertex_id((4 + x_idx * 6 + y_idx * 6 * n_square_x) as DartIdType);
163-
map.write_vertex(
163+
map.set_vertex(
164164
vertex_id,
165165
origin
166166
+ Vector2(
@@ -174,7 +174,7 @@ pub(crate) fn build_2d_splitgrid<T: CoordsFloat>(
174174
(0..n_square_y).into_par_iter().for_each(|y_idx| {
175175
let x_idx = n_square_x - 1;
176176
let vertex_id = map.vertex_id((2 + x_idx * 6 + y_idx * 6 * n_square_x) as DartIdType);
177-
map.write_vertex(
177+
map.set_vertex(
178178
vertex_id,
179179
origin
180180
+ Vector2(
@@ -188,7 +188,7 @@ pub(crate) fn build_2d_splitgrid<T: CoordsFloat>(
188188
{
189189
let (x_idx, y_idx) = (n_square_x - 1, n_square_y - 1);
190190
let vertex_id = map.vertex_id((6 + x_idx * 6 + y_idx * 6 * n_square_x) as DartIdType); // top right
191-
map.write_vertex(
191+
map.set_vertex(
192192
vertex_id,
193193
origin
194194
+ Vector2(
@@ -261,7 +261,7 @@ pub(crate) fn build_3d_grid<T: CoordsFloat>(
261261
.filter(|d| *d as VertexIdType == map.vertex_id(*d))
262262
.for_each(|d| {
263263
let v = origin + generate_hex_offset(d, n_cells_per_axis, lengths);
264-
map.write_vertex(d as VertexIdType, v);
264+
map.set_vertex(d as VertexIdType, v);
265265
});
266266

267267
// check the number of built volumes
@@ -452,7 +452,7 @@ pub(crate) fn build_3d_tetgrid<T: CoordsFloat>(
452452
.filter(|d| *d as VertexIdType == map.vertex_id(*d))
453453
.for_each(|d| {
454454
let v = origin + generate_tet_offset(d, n_cells_per_axis, lengths);
455-
map.write_vertex(d as VertexIdType, v);
455+
map.set_vertex(d as VertexIdType, v);
456456
});
457457

458458
// check the number of built volumes

0 commit comments

Comments
 (0)