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
//! FMM tools
use crate::assembly::common::SparseMatrixData;
use crate::function::SerialFunctionSpace;
use crate::quadrature::simplex_rules::simplex_rule;
use crate::traits::FunctionSpace;
use ndelement::traits::FiniteElement;
use ndelement::types::ReferenceCellType;
use ndgrid::traits::{GeometryMap, Grid};
use rlst::CsrMatrix;
use rlst::{
    rlst_dynamic_array2, rlst_dynamic_array4, Array, BaseArray, MatrixInverse, RandomAccessByRef,
    RandomAccessMut, RawAccess, RlstScalar, Shape, VectorContainer,
};

/// Generate an array of all the quadrature points
pub fn get_all_quadrature_points<
    T: RlstScalar<Real = T> + MatrixInverse,
    G: Grid<T = T, EntityDescriptor = ReferenceCellType> + Sync,
>(
    npts: usize,
    grid: &G,
) -> Array<T, BaseArray<T, VectorContainer<T>, 2>, 2> {
    // TODO: remove hardcoding of triangle in this function
    let qrule = simplex_rule(ReferenceCellType::Triangle, npts).unwrap();
    let mut qpoints = rlst_dynamic_array2!(T, [npts, 2]);
    for i in 0..npts {
        for j in 0..2 {
            *qpoints.get_mut([i, j]).unwrap() =
                num::cast::<f64, T>(qrule.points[2 * i + j]).unwrap();
        }
    }

    let evaluator = grid.geometry_map(ReferenceCellType::Triangle, qpoints.data());

    let mut all_points = rlst_dynamic_array2!(
        T,
        [
            npts * grid.entity_count(ReferenceCellType::Triangle),
            grid.geometry_dim()
        ]
    );
    let mut points = vec![num::cast::<f64, T>(0.0).unwrap(); npts * grid.geometry_dim()];

    for cell in 0..grid.entity_count(ReferenceCellType::Triangle) {
        evaluator.points(cell, &mut points);
        for j in 0..grid.geometry_dim() {
            for i in 0..npts {
                *all_points.get_mut([cell * npts + i, j]).unwrap() = points[j * npts + i];
            }
        }
    }
    all_points
}

/// Generate a dense matrix mapping between basis functions and quadrature points
pub fn basis_to_quadrature_into_dense<
    const BLOCKSIZE: usize,
    T: RlstScalar + MatrixInverse,
    G: Grid<T = T::Real, EntityDescriptor = ReferenceCellType> + Sync,
>(
    output: &mut Array<T, BaseArray<T, VectorContainer<T>, 2>, 2>,
    npts: usize,
    space: &SerialFunctionSpace<'_, T, G>,
) {
    let sparse_matrix = basis_to_quadrature::<BLOCKSIZE, T, G>(output.shape(), npts, space);
    let data = sparse_matrix.data;
    let rows = sparse_matrix.rows;
    let cols = sparse_matrix.cols;
    for ((i, j), value) in rows.iter().zip(cols.iter()).zip(data.iter()) {
        *output.get_mut([*i, *j]).unwrap() += *value;
    }
}

/// Generate a CSR matrix mapping between basis functions and quadrature points
pub fn basis_to_quadrature_into_csr<
    const BLOCKSIZE: usize,
    T: RlstScalar + MatrixInverse,
    G: Grid<T = T::Real, EntityDescriptor = ReferenceCellType> + Sync,
>(
    npts: usize,
    space: &SerialFunctionSpace<'_, T, G>,
) -> CsrMatrix<T> {
    let grid = space.grid();
    let ncells = grid
        .entity_types(2)
        .iter()
        .map(|&i| grid.entity_count(i))
        .sum::<usize>();
    let shape = [ncells * npts, space.global_size()];
    let sparse_matrix = basis_to_quadrature::<BLOCKSIZE, T, G>(shape, npts, space);

    CsrMatrix::<T>::from_aij(
        sparse_matrix.shape,
        &sparse_matrix.rows,
        &sparse_matrix.cols,
        &sparse_matrix.data,
    )
    .unwrap()
}

/// Generate a dense transpose matrix mapping between basis functions and quadrature points
pub fn transpose_basis_to_quadrature_into_dense<
    const BLOCKSIZE: usize,
    T: RlstScalar + MatrixInverse,
    G: Grid<T = T::Real, EntityDescriptor = ReferenceCellType> + Sync,
>(
    output: &mut Array<T, BaseArray<T, VectorContainer<T>, 2>, 2>,
    npts: usize,
    space: &SerialFunctionSpace<'_, T, G>,
) {
    let shape = [output.shape()[1], output.shape()[0]];
    let sparse_matrix = basis_to_quadrature::<BLOCKSIZE, T, G>(shape, npts, space);
    let data = sparse_matrix.data;
    let rows = sparse_matrix.rows;
    let cols = sparse_matrix.cols;
    for ((i, j), value) in rows.iter().zip(cols.iter()).zip(data.iter()) {
        *output.get_mut([*j, *i]).unwrap() += *value;
    }
}

/// Generate a CSR transpose matrix mapping between basis functions and quadrature points
pub fn transpose_basis_to_quadrature_into_csr<
    const BLOCKSIZE: usize,
    T: RlstScalar + MatrixInverse,
    G: Grid<T = T::Real, EntityDescriptor = ReferenceCellType> + Sync,
>(
    npts: usize,
    space: &SerialFunctionSpace<'_, T, G>,
) -> CsrMatrix<T> {
    let grid = space.grid();
    let ncells = grid
        .entity_types(2)
        .iter()
        .map(|&i| grid.entity_count(i))
        .sum::<usize>();
    let shape = [ncells * npts, space.global_size()];
    let sparse_matrix = basis_to_quadrature::<BLOCKSIZE, T, G>(shape, npts, space);

    CsrMatrix::<T>::from_aij(
        [space.global_size(), ncells * npts],
        &sparse_matrix.cols,
        &sparse_matrix.rows,
        &sparse_matrix.data,
    )
    .unwrap()
}

fn basis_to_quadrature<
    const BLOCKSIZE: usize,
    T: RlstScalar + MatrixInverse,
    G: Grid<T = T::Real, EntityDescriptor = ReferenceCellType> + Sync,
>(
    shape: [usize; 2],
    npts: usize,
    space: &SerialFunctionSpace<'_, T, G>,
) -> SparseMatrixData<T> {
    if !space.is_serial() {
        panic!("Dense assembly can only be used for function spaces stored in serial");
    }
    let grid = space.grid();
    let ncells = grid
        .entity_types(2)
        .iter()
        .map(|&i| grid.entity_count(i))
        .sum::<usize>();
    if shape[0] != ncells * npts || shape[1] != space.global_size() {
        panic!("Matrix has wrong shape");
    }

    // TODO: pass cell types into this function
    let qrule = simplex_rule(ReferenceCellType::Triangle, npts).unwrap();
    let mut qpoints = rlst_dynamic_array2!(T::Real, [npts, 2]);
    for i in 0..npts {
        for j in 0..2 {
            *qpoints.get_mut([i, j]).unwrap() =
                num::cast::<f64, T::Real>(qrule.points[2 * i + j]).unwrap();
        }
    }
    let qweights = qrule
        .weights
        .iter()
        .map(|w| num::cast::<f64, T>(*w).unwrap())
        .collect::<Vec<_>>();

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

    let mut output = SparseMatrixData::<T>::new_known_size(
        shape,
        ncells * space.element(ReferenceCellType::Triangle).dim() * npts,
    );
    debug_assert!(qpoints.shape()[0] == npts);

    let evaluator = grid.geometry_map(ReferenceCellType::Triangle, qpoints.data());
    let npts = qweights.len();

    let mut jacobians = vec![
        num::cast::<f64, T::Real>(0.0).unwrap();
        grid.geometry_dim() * grid.topology_dim() * npts
    ];
    let mut jdets = vec![num::cast::<f64, T::Real>(0.0).unwrap(); npts];
    let mut normals = vec![num::cast::<f64, T::Real>(0.0).unwrap(); grid.geometry_dim() * npts];

    // TODO: batch this?
    for cell in 0..ncells {
        let cell_dofs = space.cell_dofs(cell).unwrap();
        evaluator.jacobians_dets_normals(cell, &mut jacobians, &mut jdets, &mut normals);
        for (qindex, w) in qweights.iter().enumerate() {
            for (i, dof) in cell_dofs.iter().enumerate() {
                output.rows.push(cell * npts + qindex);
                output.cols.push(*dof);
                output.data.push(
                    num::cast::<T::Real, T>(jdets[qindex]).unwrap()
                        * *w
                        * *table.get([0, qindex, i, 0]).unwrap(),
                );
            }
        }
    }
    output
}