-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasolve.c
343 lines (276 loc) · 10.1 KB
/
asolve.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*----------------------------------------------------------------------------*/
/* asolve.c */
/* Author: Godwin Duan */
/*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_eigen.h>
#include <gsl/gsl_linalg.h>
#include "asolve.h"
/* Prints gsl_matrix m. Used for debugging. */
#ifndef NDEBUG
static void print_matrix(const gsl_matrix *m)
{
int i, j;
int rows, cols;
assert(m != NULL);
rows = m->size1;
cols = m->size2;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%.2lf\t", gsl_matrix_get(m, i, j));
}
printf("\n");
}
}
#endif
/* Creates and returns a diagonal square matrix of size num_beads x num_beads.
* Diagonal entries correspond to masses of beads. Caller responsible for
* freeing matrix. */
static gsl_matrix *create_mass_matrix(const Bead *beads, int num_beads)
{
int i;
gsl_matrix *m;
assert(beads != NULL);
assert(num_beads > 0);
m = gsl_matrix_calloc(num_beads, num_beads);
/* Skipping error checking */
for (i = 0; i < num_beads; i++)
gsl_matrix_set(m, i, i, beads[i].mass);
return m;
}
/* Creates and returns a tridiagonal square matrix of size num_beads x
* num_beads. Entries correspond to the spring constants that connect beads.
* Caller responsible for freeing matrix. */
static gsl_matrix *create_spring_k_matrix(const double *connections,
int num_beads)
{
int i, j;
gsl_matrix *m;
assert(connections != NULL);
assert(num_beads > 0);
m = gsl_matrix_calloc(num_beads, num_beads);
/* Skipping error checking */
/* i is row number, j is col number */
for (i = 0; i < num_beads; i++)
{
for (j = 0; j < num_beads; j++)
{
if (i == j)
gsl_matrix_set(m, i, j, connections[i] + connections[i + 1]);
if (i == j - 1)
gsl_matrix_set(m, i, j, -1 * connections[i + 1]);
if (i == j + 1)
gsl_matrix_set(m, i, j, -1 * connections[i]);
}
}
return m;
}
/* Creates and returns a tridiagonal square matrix of size num_beads x
* num_beads. Entries correspond to the string tensions and lengths that
* connect beads. Caller responsible for freeing matrix. */
static gsl_matrix *create_string_k_matrix(const double *connections,
double tension, int num_beads)
{
int i, j;
gsl_matrix *m;
assert(connections != NULL);
assert(num_beads > 0);
m = gsl_matrix_calloc(num_beads, num_beads);
/* Skipping error checking */
for (i = 0; i < num_beads; i++)
{
for (j = 0; j < num_beads; j++)
{
if (i == j)
gsl_matrix_set(m, i, j, tension * (1 / connections[i] +
1 / connections[i + 1]));
if (i == j - 1)
gsl_matrix_set(m, i, j, -1 * tension * (1 / connections[i + 1]));
if (i == j + 1)
gsl_matrix_set(m, i, j, -1 * tension * (1 / connections[i]));
}
}
return m;
}
/* Creates and returns the inverse square root of mass_matrix. Caller
* responsible for freeing matrix. */
static gsl_matrix *create_invsqrt_mass_matrix(const gsl_matrix *mass_matrix)
{
gsl_matrix *invsqrtm;
int i, j;
assert(mass_matrix != NULL);
invsqrtm = gsl_matrix_alloc(mass_matrix->size1, mass_matrix->size2);
gsl_matrix_memcpy(invsqrtm, mass_matrix);
for (i = 0; i < invsqrtm->size1; i++)
for (j = 0; j < invsqrtm->size2; j++)
if (i == j)
gsl_matrix_set(invsqrtm, i, j,
pow(gsl_matrix_get(invsqrtm, i, j), -0.5));
return invsqrtm;
}
/* Creates and returns the dynamical D matrix given by
* (mass_matrix)^-1/2(k_matrix)(mass_matrix)^-1/2. Caller responsible for
* freeing matrix. */
static gsl_matrix *create_d_matrix(const gsl_matrix *mass_matrix,
const gsl_matrix *k_matrix)
{
gsl_matrix *d_matrix, *invsqrtm, *tempm;
assert(mass_matrix != NULL);
assert(k_matrix != NULL);
/* Both matrices must be square and the same size */
assert(mass_matrix->size1 == mass_matrix->size2);
assert(k_matrix->size1 == k_matrix->size2);
assert(mass_matrix->size1 == k_matrix->size1);
tempm = gsl_matrix_alloc(mass_matrix->size1, mass_matrix->size2);
d_matrix = gsl_matrix_alloc(mass_matrix->size1, mass_matrix->size2);
/* Skipping error checking */
invsqrtm = create_invsqrt_mass_matrix(mass_matrix);
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, k_matrix, invsqrtm, 0.0,
tempm);
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, invsqrtm, tempm, 0.0,
d_matrix);
gsl_matrix_free(invsqrtm);
gsl_matrix_free(tempm);
return d_matrix;
}
/* Calculates the normal modes of the system defined by mass_matrix and k_matrix
* and saves resultant eigenfrequencies and eigenvectors in the Result it
* returns. Caller responsible for freeing eigenfrequencies and eigenvectors. */
static Result find_normal_modes(const gsl_matrix *mass_matrix,
const gsl_matrix *k_matrix)
{
Result result;
gsl_eigen_symmv_workspace *w;
gsl_vector *eval;
gsl_matrix *evec;
gsl_matrix *d_matrix;
gsl_matrix *invsqrtm;
int i, j;
assert(mass_matrix != NULL);
assert(k_matrix != NULL);
d_matrix = create_d_matrix(mass_matrix, k_matrix);
w = gsl_eigen_symmv_alloc(d_matrix->size1);
eval = gsl_vector_alloc(d_matrix->size1);
evec = gsl_matrix_alloc(d_matrix->size1, d_matrix->size2);
/* Destroys d_matrix */
gsl_eigen_symmv(d_matrix, eval, evec, w);
gsl_eigen_symmv_free(w);
gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_ASC);
result.num_modes = d_matrix->size1;
/* Load in eigenfrequencies */
/* Eigenfrequency = sqrt(eigenvalue) */
result.eigenfrequencies = malloc(result.num_modes * sizeof(double));
for (i = 0; i < result.num_modes; i++)
result.eigenfrequencies[i] = sqrt(gsl_vector_get(eval, i));
/* Translate eigenvectors back to regular coordinates */
/* Then load in the translated eigenvectors */
result.eigenvectors = malloc(result.num_modes * sizeof(double*));
invsqrtm = create_invsqrt_mass_matrix(mass_matrix);
for (i = 0; i < result.num_modes; i++)
{
result.eigenvectors[i] = malloc(result.num_modes * sizeof(double));
double mscalar = gsl_matrix_get(invsqrtm, i, i);
for (j = 0; j < result.num_modes; j++)
result.eigenvectors[i][j] = mscalar * gsl_matrix_get(evec, i, j);
}
/* Normalize the translated eigenvectors */
for (i = 0; i < result.num_modes; i++)
{
double mag = 0;
for (j = 0; j < result.num_modes; j++)
mag += pow(result.eigenvectors[j][i], 2);
mag = sqrt(mag);
for (j = 0; j < result.num_modes; j++)
result.eigenvectors[j][i] /= mag;
}
gsl_vector_free(eval);
gsl_matrix_free(evec);
gsl_matrix_free(invsqrtm);
gsl_matrix_free(d_matrix);
return result;
}
/* Given result containing eigenfrequencies and eigenvectors, finds and returns
* coefficients that satisfy the initial conditions given in beads. a
* corresponds to the cosine term and b corresponds to the sine term. Caller
* responsible for freeing coefficient array. */
static Coefficient *apply_ics(Bead *beads, Result result)
{
Coefficient *coeffs;
gsl_vector *ix, *iv, *a, *b;
gsl_matrix *eigv;
gsl_permutation *p;
int i, j;
assert(beads != NULL);
assert(result.eigenfrequencies != NULL);
assert(result.eigenvectors != NULL);
coeffs = malloc(result.num_modes * sizeof(Coefficient));
/* Skipping error checking */
a = gsl_vector_alloc(result.num_modes);
b = gsl_vector_alloc(result.num_modes);
ix = gsl_vector_alloc(result.num_modes);
iv = gsl_vector_alloc(result.num_modes);
for (i = 0; i < result.num_modes; i++)
{
gsl_vector_set(ix, i, beads[i].x0);
gsl_vector_set(iv, i, beads[i].v0);
}
eigv = gsl_matrix_alloc(result.num_modes, result.num_modes);
for (i = 0; i < result.num_modes; i++)
for (j = 0; j < result.num_modes; j++)
gsl_matrix_set(eigv, i, j, result.eigenvectors[i][j]);
/* Initial positions calculation */
/* Solved using LU decomposition */
p = gsl_permutation_alloc(result.num_modes);
gsl_linalg_LU_decomp (eigv, p, &i);
gsl_linalg_LU_solve (eigv, p, ix, a);
/* Initial velocities calculation */
gsl_linalg_LU_solve (eigv, p, iv, b);
/* Load results into coefficients array */
for (i = 0; i < result.num_modes; i++)
{
coeffs[i].a = gsl_vector_get(a, i);
/* For velocity terms, we divide by the eigenfrequency since we took a
* derivative */
coeffs[i].b = gsl_vector_get(b, i) / result.eigenfrequencies[i];
}
gsl_vector_free(a);
gsl_vector_free(b);
gsl_vector_free(ix);
gsl_vector_free(iv);
gsl_matrix_free(eigv);
gsl_permutation_free(p);
return coeffs;
}
Result asolve(Simulation sim)
{
Result result;
gsl_matrix *mass_matrix, *k_matrix;
assert(sim.beads != NULL);
assert(sim.connections != NULL);
printf("Performing an analytical solution for a ");
if (sim.sim_type == STRING)
printf("string with ");
else if (sim.sim_type == SPRING)
printf("spring with ");
printf("%d beads.\n", sim.num_beads);
printf("\n");
mass_matrix = create_mass_matrix(sim.beads, sim.num_beads);
if (sim.sim_type == SPRING)
k_matrix = create_spring_k_matrix(sim.connections, sim.num_beads);
if (sim.sim_type == STRING)
k_matrix = create_string_k_matrix(sim.connections, sim.tension,
sim.num_beads);
result = find_normal_modes(mass_matrix, k_matrix);
result.coefficients = apply_ics(sim.beads, result);
gsl_matrix_free(mass_matrix);
gsl_matrix_free(k_matrix);
return result;
}