Skip to content

Commit 5ebfb82

Browse files
committed
Extend quadrature API
1 parent 62e431e commit 5ebfb82

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/assembly/buffers.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::nalgebra::allocator::Allocator;
44
use crate::nalgebra::{
55
DMatrix, DefaultAllocator, DimName, Dynamic, MatrixSlice, MatrixSliceMut, OPoint, RealField, Scalar,
66
};
7+
use crate::quadrature::Quadrature;
78
use crate::space::FiniteElementSpace;
89
use crate::SmallDim;
910
use itertools::izip;
@@ -104,6 +105,27 @@ where
104105
quad_data: Vec<Data>,
105106
}
106107

108+
impl<T, D, Data> Quadrature<T, D> for QuadratureBuffer<T, D, Data>
109+
where
110+
T: Scalar,
111+
D: DimName,
112+
DefaultAllocator: Allocator<T, D>,
113+
{
114+
type Data = Data;
115+
116+
fn weights(&self) -> &[T] {
117+
&self.quad_weights
118+
}
119+
120+
fn points(&self) -> &[OPoint<T, D>] {
121+
&self.quad_points
122+
}
123+
124+
fn data(&self) -> &[Self::Data] {
125+
&self.quad_data
126+
}
127+
}
128+
107129
impl<T, D, Data> Default for QuadratureBuffer<T, D, Data>
108130
where
109131
T: Scalar,

src/quadrature.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::{Add, AddAssign, Mul};
1+
use std::ops::{Add, AddAssign, Deref, Mul};
22

33
use nalgebra::allocator::Allocator;
44
use nalgebra::{DefaultAllocator, DimName, OPoint, Point1, Scalar, U2, U3};
@@ -47,6 +47,14 @@ where
4747
}
4848
integral
4949
}
50+
51+
fn to_parts(&self) -> QuadratureParts<&[T], &[OPoint<T, D>], &[Self::Data]> {
52+
QuadratureParts {
53+
weights: self.weights(),
54+
points: self.points(),
55+
data: self.data(),
56+
}
57+
}
5058
}
5159

5260
/// Trait alias for 1D quadrature rules.
@@ -139,6 +147,77 @@ where
139147
}
140148
}
141149

150+
/// Marker to indicate that a quadrature rule stored in [`QuadratureParts`] has no associated data.
151+
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
152+
pub struct NoData;
153+
154+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
155+
pub struct QuadratureParts<WeightsArray, PointsArray, DataArray> {
156+
pub weights: WeightsArray,
157+
pub points: PointsArray,
158+
pub data: DataArray,
159+
}
160+
161+
impl<WeightsArray, PointsArray, DataArray> QuadratureParts<WeightsArray, PointsArray, DataArray> {
162+
pub fn with_data<DataArray2>(self, data: DataArray2) -> QuadratureParts<WeightsArray, PointsArray, DataArray2> {
163+
QuadratureParts {
164+
weights: self.weights,
165+
points: self.points,
166+
data,
167+
}
168+
}
169+
}
170+
171+
impl<T, D, WeightsArray, PointsArray, DataArray, Data> Quadrature<T, D>
172+
for QuadratureParts<WeightsArray, PointsArray, DataArray>
173+
where
174+
T: Scalar,
175+
D: DimName,
176+
WeightsArray: AsRef<[T]>,
177+
PointsArray: AsRef<[OPoint<T, D>]>,
178+
DataArray: Deref<Target = [Data]>,
179+
DefaultAllocator: Allocator<T, D>,
180+
{
181+
type Data = Data;
182+
183+
fn weights(&self) -> &[T] {
184+
self.weights.as_ref()
185+
}
186+
187+
fn points(&self) -> &[OPoint<T, D>] {
188+
self.points.as_ref()
189+
}
190+
191+
fn data(&self) -> &[Self::Data] {
192+
self.data.deref()
193+
}
194+
}
195+
196+
impl<T, D, WeightsArray, PointsArray> Quadrature<T, D> for QuadratureParts<WeightsArray, PointsArray, NoData>
197+
where
198+
T: Scalar,
199+
D: DimName,
200+
WeightsArray: AsRef<[T]>,
201+
PointsArray: AsRef<[OPoint<T, D>]>,
202+
DefaultAllocator: Allocator<T, D>,
203+
{
204+
type Data = ();
205+
206+
fn weights(&self) -> &[T] {
207+
self.weights.as_ref()
208+
}
209+
210+
fn points(&self) -> &[OPoint<T, D>] {
211+
self.points.as_ref()
212+
}
213+
214+
fn data(&self) -> &[()] {
215+
// This is a "sound" way of constructing a unit type slice of arbitrary size.
216+
// Since it's zero-sized, it won't actually allocate any memory and the leak is elided
217+
vec![(); self.weights().len()].leak()
218+
}
219+
}
220+
142221
fn convert_quadrature_rule_from_1d_f64<T>(quadrature: fenris_quadrature::Rule<1>) -> QuadraturePair1d<T>
143222
where
144223
T: RealField,

0 commit comments

Comments
 (0)