-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocal_matrices.c
289 lines (264 loc) · 12 KB
/
local_matrices.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/** @file */
/**
* @brief Calculate the local mass matrix.
*
* @param[in] ref_data Pointer to the struct containing information about the
* finite element space.
* @param[in] local_element Pointer to the struct containing information about
* the current element.
* @param[in] convection_function Function pointer for calculating the
* convection field. No SUPG stabilization will be done if this is NULL.
* @param[out] mass_matrix Pointer to the array which will be filled in with
* mass matrix data. The array must be of size (at least)
* num_basis_functions**2.
*/
int cf_local_mass(const cf_ref_arrays_s* ref_data,
const cf_local_element_s* local_element,
cf_convection_f convection_function,
double mass_matrix[
static ipow(ref_data->num_basis_functions, 2)])
{
int i;
int num_points = ref_data->num_points;
int num_basis_functions = ref_data->num_basis_functions;
int length = num_points*num_basis_functions;
double supg_test_function_values[length];
double values[length];
double weights_scaled[num_points];
int status = 0;
memcpy(values, ref_data->values, sizeof(double)*length);
for (i = 0; i < num_points; i++) {
weights_scaled[i] = ref_data->weights[i]*local_element->jacobian;
}
cf_diagonal_multiply(num_basis_functions, num_points, values,
weights_scaled);
if (convection_function == NULL
|| local_element->supg_stabilization_constant == 0.0) {
DGEMM_WRAPPER_NT(num_basis_functions, num_basis_functions,
num_points, ref_data->values, values,
mass_matrix);
}
else {
cf_supg_test_function_value(ref_data, local_element,
convection_function,
supg_test_function_values);
DGEMM_WRAPPER_NT(num_basis_functions, num_basis_functions,
num_points, supg_test_function_values, values,
mass_matrix);
}
return status;
}
/**
* @brief Calculate the local convection matrix.
*
* @param[in] ref_data Pointer to the struct containing information about the
* finite element space.
* @param[in] local_element Pointer to the struct containing information about
* the current element.
* @param[in] convection_function Function pointer for calculating the
* convection field. No SUPG stabilization will be done if this is NULL.
* @param[out] convection_matrix Pointer to the array which will be filled in
* with convection matrix data. The array must be of size (at least)
* num_basis_functions**2.
*/
int cf_local_convection(const cf_ref_arrays_s* ref_data,
const cf_local_element_s* local_element,
cf_convection_f convection_function,
double convection_matrix[
static ipow(ref_data->num_basis_functions, 2)])
{
int i;
int num_points = ref_data->num_points;
int num_basis_functions = ref_data->num_basis_functions;
int length = num_points*num_basis_functions;
int point_num, basis_num, index;
double dx[length];
double dy[length];
double xs[length];
double ys[length];
double test_function_values[length];
double weights_scaled[num_points];
cf_convection_s convection;
int status = 0;
cf_affine_transformation(ref_data, local_element, xs, ys);
cf_physical_gradients(ref_data, local_element, dx, dy);
for (i = 0; i < num_points; i++) {
weights_scaled[i] = ref_data->weights[i]*local_element->jacobian;
}
if (convection_function == NULL
|| local_element->supg_stabilization_constant == 0.0) {
memcpy(test_function_values, ref_data->values,
sizeof(double)*length);
}
else {
cf_supg_test_function_value(ref_data, local_element,
convection_function,
test_function_values);
}
for (point_num = 0; point_num < num_points; point_num++) {
convection = convection_function(xs[point_num], ys[point_num]);
for (basis_num = 0; basis_num < num_basis_functions;
basis_num++) {
index = CF_INDEX(basis_num, point_num,
num_basis_functions, num_points);
dx[index] *= convection.value[0];
dx[index] += convection.value[1]*dy[index];
}
}
cf_diagonal_multiply(num_basis_functions, num_points, dx,
weights_scaled);
DGEMM_WRAPPER_NT(num_basis_functions, num_basis_functions,
num_points, test_function_values, dx,
convection_matrix);
return status;
}
/**
* @brief Calculate the local stiffness matrix.
*
* @param[in] ref_data Pointer to the struct containing information about the
* finite element space.
* @param[in] local_element Pointer to the struct containing information about
* the current element.
* @param[in] convection_function Function pointer for calculating the
* convection field. No SUPG stabilization will be done if this is NULL.
* @param[out] stiffness_matrix Pointer to the array which will be filled in
* with stiffness matrix data. The array must be of size (at least)
* num_basis_functions**2.
*/
int cf_local_stiffness(const cf_ref_arrays_s* ref_data,
const cf_local_element_s* local_element,
cf_convection_f convection_function,
double stiffness_matrix[
static ipow(ref_data->num_basis_functions, 2)])
{
int i;
int num_points = ref_data->num_points;
int num_basis_functions = ref_data->num_basis_functions;
int length = num_points*num_basis_functions;
double dx[length];
double dy[length];
double test_function_dx[length];
double test_function_dy[length];
double weights_scaled[num_points];
int status = 0;
cf_physical_gradients(ref_data, local_element, dx, dy);
for (i = 0; i < num_points; i++) {
weights_scaled[i] = ref_data->weights[i]*local_element->jacobian;
}
if (convection_function == NULL
|| local_element->supg_stabilization_constant == 0.0) {
memcpy(test_function_dx, dx, sizeof(double)*length);
memcpy(test_function_dy, dy, sizeof(double)*length);
}
else {
cf_supg_test_function_gradient(ref_data, local_element,
convection_function,
test_function_dx,
test_function_dy);
}
cf_diagonal_multiply(num_basis_functions, num_points, dx,
weights_scaled);
cf_diagonal_multiply(num_basis_functions, num_points, dy,
weights_scaled);
DGEMM_WRAPPER_NT(num_basis_functions, num_basis_functions,
num_points, test_function_dx, dx, stiffness_matrix);
DGEMM_WRAPPER_NT_ADD_C(num_basis_functions, num_basis_functions,
num_points, test_function_dy, dy,
stiffness_matrix);
return status;
}
/**
* @brief Calculate the local hessian matrix.
*
* @param[in] ref_data Pointer to the struct containing information about the
* finite element space.
* @param[in] local_element Pointer to the struct containing information about
* the current element.
* @param[in] convection_function Function pointer for calculating the
* convection field. This is not actually used in this function, but the
* argument is here to keep a consistent interface.
* @param[out] stiffness_matrix Pointer to the array which will be filled in
* with stiffness matrix data. The array must be of size (at least)
* num_basis_functions**2.
*/
int cf_local_hessian(const cf_ref_arrays_s* ref_data,
const cf_local_element_s* local_element,
cf_convection_f convection_function,
double hessian_matrix[
static ipow(ref_data->num_basis_functions, 2)])
{
int i;
int num_points = ref_data->num_points;
int num_basis_functions = ref_data->num_basis_functions;
double dxx[num_basis_functions*num_points];
double dxy[num_basis_functions*num_points];
double dyy[num_basis_functions*num_points];
double weights_scaled[num_points];
int status = 0;
cf_physical_hessian(ref_data, local_element, dxx, dxy, dyy);
for (i = 0; i < num_points; i++) {
weights_scaled[i] = ref_data->weights[i]*local_element->jacobian;
}
/* Reassign dxx and dyy to be values of the laplacian. */
for (i = 0; i < num_basis_functions*num_points; i++) {
dxx[i] += dyy[i];
}
memcpy(dyy, dxx, sizeof(double)*(num_basis_functions*num_points));
/*
* Scale one set of laplacian values by the weights (themselves scaled
* by the Jacobian) and then calculate the matrix of inner products.
*/
cf_diagonal_multiply(num_basis_functions, num_points, dxx,
weights_scaled);
DGEMM_WRAPPER_NT(num_basis_functions, num_basis_functions, num_points,
dxx, dyy, hessian_matrix);
return status;
}
/**
* @brief Calculate the local load vector.
*
* @param[in] ref_data Pointer to the struct containing information about the
* finite element space.
* @param[in] local_element Pointer to the struct containing information about
* the current element.
* @param[in] convection_function Function pointer for calculating the
* convection field. No SUPG stabilization will be done if this is NULL.
* @param[in] forcing_function Function pointer for calculating the
* forcing field.
* @param[in] time Time value at which to evaluate forcing_function.
* @param[out] local_load_vector Pointer to the array which will be filled in
* with the load vector. Must be of length at least num_points.
*/
int cf_local_load(const cf_ref_arrays_s* ref_data,
const cf_local_element_s* local_element,
cf_convection_f convection_function,
cf_forcing_f forcing_function, double time,
double local_load_vector[static ref_data->num_points])
{
int i;
int i_one = 1;
int num_points = ref_data->num_points;
int num_basis_functions = ref_data->num_basis_functions;
int length = num_points*num_basis_functions;
double force_values[num_points];
double test_function[num_points*num_basis_functions];
int status = 0;
double xs[num_points], ys[num_points];
cf_affine_transformation(ref_data, local_element, xs, ys);
for (i = 0; i < num_points; i++) {
force_values[i] = local_element->jacobian*ref_data->weights[i]
*forcing_function(time, xs[i], ys[i]);
}
if (convection_function == NULL
|| local_element->supg_stabilization_constant == 0.0) {
memcpy(test_function, ref_data->values, sizeof(double)*length);
}
else {
cf_supg_test_function_value(ref_data, local_element,
convection_function,
test_function);
}
DGEMM_WRAPPER_NT(num_basis_functions, i_one, num_points,
test_function, force_values, local_load_vector);
return status;
}