1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Potential assemblers
mod double_layer;
mod single_layer;

use crate::assembly::common::{RawData2D, RlstArray};
use crate::assembly::potential::cell_assemblers::PotentialCellAssembler;
use crate::quadrature::simplex_rules::simplex_rule;
use crate::traits::{
    CellAssembler, FunctionSpace, KernelEvaluator, PotentialAssembly, PotentialIntegrand,
};
use itertools::izip;
use ndelement::traits::FiniteElement;
use ndelement::types::ReferenceCellType;
use ndgrid::traits::Grid;
use rayon::prelude::*;
use rlst::{
    rlst_dynamic_array2, rlst_dynamic_array4, DefaultIterator, MatrixInverse, RandomAccessByRef,
    RandomAccessMut, RawAccess, RawAccessMut, RlstScalar, Shape,
};
use std::collections::HashMap;

/// Assemble the contribution to the terms of a matrix for a batch of non-adjacent cells
#[allow(clippy::too_many_arguments)]
fn assemble_batch<
    T: RlstScalar + MatrixInverse,
    Space: FunctionSpace<T = T> + Sync,
    Integrand: PotentialIntegrand<T = T>,
    Kernel: KernelEvaluator<T = T>,
    Array2: RandomAccessByRef<2, Item = T::Real> + Shape<2> + RawAccess<Item = T::Real> + Sync,
>(
    assembler: &PotentialAssembler<T, Integrand, Kernel>,
    deriv_size: usize,
    output: &RawData2D<T>,
    cell_type: ReferenceCellType,
    space: &Space,
    evaluation_points: &Array2,
    cells: &[usize],
    points: &RlstArray<T::Real, 2>,
    weights: &[T::Real],
    table: &RlstArray<T, 4>,
) -> usize {
    let npts = weights.len();
    let nevalpts = evaluation_points.shape()[1];
    debug_assert!(points.shape()[1] == npts);

    let grid = space.grid();

    assert_eq!(grid.geometry_dim(), 3);
    assert_eq!(grid.topology_dim(), 2);

    let evaluator = grid.geometry_map(cell_type, points.data());
    let mut a = PotentialCellAssembler::new(
        npts,
        nevalpts,
        deriv_size,
        &assembler.integrand,
        &assembler.kernel,
        evaluator,
        table,
        evaluation_points,
        weights,
    );

    let mut local_mat = rlst_dynamic_array2!(T, [nevalpts, space.element(cell_type).dim()]);

    for cell in cells {
        a.set_cell(*cell);
        a.assemble(&mut local_mat);

        let dofs = space.cell_dofs(*cell).unwrap();
        for (dof, col) in izip!(dofs, local_mat.col_iter()) {
            for (eval_index, entry) in col.iter().enumerate() {
                unsafe {
                    *output.data.add(eval_index + output.shape[0] * *dof) += entry;
                }
            }
        }
    }
    1
}

/// Options for a potential assembler
pub struct PotentialAssemblerOptions {
    /// Number of points used in quadrature for non-singular integrals
    quadrature_degrees: HashMap<ReferenceCellType, usize>,
    /// Maximum size of each batch of cells to send to an assembly function
    batch_size: usize,
}

impl Default for PotentialAssemblerOptions {
    fn default() -> Self {
        use ReferenceCellType::{Quadrilateral, Triangle};
        Self {
            quadrature_degrees: HashMap::from([(Triangle, 37), (Quadrilateral, 37)]),
            batch_size: 128,
        }
    }
}

/// Potential assembler
///
/// Assemble potential operators by processing batches of cells in parallel
pub struct PotentialAssembler<
    T: RlstScalar + MatrixInverse,
    Integrand: PotentialIntegrand<T = T>,
    Kernel: KernelEvaluator<T = T>,
> {
    pub(crate) integrand: Integrand,
    pub(crate) kernel: Kernel,
    pub(crate) options: PotentialAssemblerOptions,
    pub(crate) deriv_size: usize,
}

unsafe impl<
        T: RlstScalar + MatrixInverse,
        Integrand: PotentialIntegrand<T = T>,
        Kernel: KernelEvaluator<T = T>,
    > Sync for PotentialAssembler<T, Integrand, Kernel>
{
}

impl<
        T: RlstScalar + MatrixInverse,
        Integrand: PotentialIntegrand<T = T>,
        Kernel: KernelEvaluator<T = T>,
    > PotentialAssembler<T, Integrand, Kernel>
{
    /// Create new
    fn new(integrand: Integrand, kernel: Kernel, deriv_size: usize) -> Self {
        Self {
            integrand,
            kernel,
            options: PotentialAssemblerOptions::default(),
            deriv_size,
        }
    }

    /// Set (non-singular) quadrature degree for a cell type
    pub fn set_quadrature_degree(&mut self, cell: ReferenceCellType, degree: usize) {
        *self.options.quadrature_degrees.get_mut(&cell).unwrap() = degree;
    }

    /// Get (non-singular) quadrature degree for a cell type
    pub fn quadrature_degree(&self, cell: ReferenceCellType) -> Option<usize> {
        self.options.quadrature_degrees.get(&cell).copied()
    }

    /// Set the maximum size of a batch of cells to send to an assembly function
    pub fn set_batch_size(&mut self, size: usize) {
        self.options.batch_size = size;
    }

    /// Get the maximum size of a batch of cells to send to an assembly function
    pub fn batch_size(&self) -> usize {
        self.options.batch_size
    }

    fn assemble<
        Space: FunctionSpace<T = T> + Sync,
        Array2: RandomAccessByRef<2, Item = T::Real> + Shape<2> + RawAccess<Item = T::Real> + Sync,
    >(
        &self,
        output: &RawData2D<T>,
        space: &Space,
        points: &Array2,
        colouring: &HashMap<ReferenceCellType, Vec<Vec<usize>>>,
    ) {
        if !space.is_serial() {
            panic!("Dense assembly can only be used for function spaces stored in serial");
        }
        if output.shape[0] != points.shape()[1] || output.shape[1] != space.global_size() {
            panic!("Matrix has wrong shape");
        }

        let batch_size = self.options.batch_size;

        for cell_type in space.grid().entity_types(2) {
            let npts = self.options.quadrature_degrees[cell_type];
            let qrule = simplex_rule(*cell_type, npts).unwrap();
            let mut qpoints = rlst_dynamic_array2!(T::Real, [2, npts]);
            for i in 0..npts {
                for j in 0..2 {
                    *qpoints.get_mut([j, i]).unwrap() =
                        num::cast::<f64, T::Real>(qrule.points[2 * i + j]).unwrap();
                }
            }
            let qweights = qrule
                .weights
                .iter()
                .map(|w| num::cast::<f64, T::Real>(*w).unwrap())
                .collect::<Vec<_>>();

            let element = space.element(*cell_type);
            let mut table = rlst_dynamic_array4!(T, element.tabulate_array_shape(0, npts));
            element.tabulate(&qpoints, 0, &mut table);

            for c in &colouring[cell_type] {
                let mut cells: Vec<&[usize]> = vec![];

                let mut start = 0;
                while start < c.len() {
                    let end = if start + batch_size < c.len() {
                        start + batch_size
                    } else {
                        c.len()
                    };

                    cells.push(&c[start..end]);
                    start = end
                }

                let numtasks = cells.len();
                let r: usize = (0..numtasks)
                    .into_par_iter()
                    .map(&|t| {
                        assemble_batch(
                            self,
                            self.deriv_size,
                            output,
                            *cell_type,
                            space,
                            points,
                            cells[t],
                            &qpoints,
                            &qweights,
                            &table,
                        )
                    })
                    .sum();
                assert_eq!(r, numtasks);
            }
        }
    }
}

impl<
        T: RlstScalar + MatrixInverse,
        Integrand: PotentialIntegrand<T = T>,
        Kernel: KernelEvaluator<T = T>,
    > PotentialAssembly for PotentialAssembler<T, Integrand, Kernel>
{
    type T = T;

    fn assemble_into_dense<
        Space: FunctionSpace<T = T> + Sync,
        Array2Mut: RandomAccessMut<2, Item = T> + Shape<2> + RawAccessMut<Item = T>,
        Array2: RandomAccessByRef<2, Item = T::Real> + Shape<2> + RawAccess<Item = T::Real> + Sync,
    >(
        &self,
        output: &mut Array2Mut,
        space: &Space,
        points: &Array2,
    ) {
        if !space.is_serial() {
            panic!("Dense assembly can only be used for function spaces stored in serial");
        }
        if output.shape()[0] != points.shape()[1] || output.shape()[1] != space.global_size() {
            panic!("Matrix has wrong shape");
        }

        let output_raw = RawData2D {
            data: output.data_mut().as_mut_ptr(),
            shape: output.shape(),
        };

        let colouring = space.cell_colouring();

        self.assemble(&output_raw, space, points, &colouring);
    }
}