-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers.cpp
259 lines (230 loc) · 9.02 KB
/
solvers.cpp
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
#include <iostream>
#include <string>
#include "myfunctions.h"
using namespace std;
#include <mpi.h>
#define F77NAME(x) x##_
extern "C"
{
void F77NAME(dgbsv)(const int& n, const int& kl, const int& ku,
const int& nrhs, const double * A, const int& ldab,
int * ipiv, double * B, const int& ldb, int& info);
void F77NAME(dcopy)(const int& n, const double * x, const int& incx,
const double * y, const int& incy);
void F77NAME(dscal) (const int& n, const double& alpha, const double * x,
const int& incx);
void F77NAME(daxpy) (const int& n, const double& alpha, const double * x,
const int& incx, const double * y, const int& incy);
void F77NAME(dgemv) (const char& trans, const int& m,
const int& n, const double& alpha,
const double* a, const int& lda,
const double* x, const int& incx,
const double& beta, double* y, const int& incy);
double F77NAME(dnrm2) (const int& n, const double* a, const int& incx);
void F77NAME(dgbmv)(const char& trans, const int& m, const int& n,
const int& kl, const int& ku,
const double& alpha, const double* a, const int& lda,
const double* x, const int& incx, const double& beta,
double* y, const int& incy);
}
void Static_Solver(double A, double E, double I, double L, double l, double qx, double qy, double Fy, int Nx, int ku, int kl, int N)
{
const int lda = 1 + 2*kl + ku;
double * Kb = new double[lda*N]();
double * F = new double[N]();
Build_K_global_banded(Kb, A, E, l, L, I, Nx, N, kl, ku, lda, kl);
Build_F_global(F, Fy, qx, qy, 1.0, l, Nx, N);
Banded_Matrix_Solver(Kb, F, N, lda, kl, ku);
Write_Vector(F,N, l, L, "Task1");
delete[] Kb;
delete[] F;
}
void Dynamic_Solver_1(double A, double E, double I, double L, double l, double qx, double qy, double Fy, double rho, double T, int Nt, int Nx, int ku, int kl, int N)
{
const int lda = 1 + kl + ku;
const double del_t = T/(double)Nt;
double * Mb = new double[N]();
double * u0 = new double[N]();
double * u1 = new double[N]();
double * F = new double[N]();
double * Kb = new double[lda*N]();
double * d = new double[Nt]();
Build_M_global_banded(Mb, rho, A, l, Nx, N);
Build_K_global_banded_altered(Kb, Mb, A, E, l, L, I, del_t, Nx, N, kl, ku, lda, 0);
for (int i = 0; i<Nt; ++i)
{
Build_F_global(F, Fy, qx, qy, (double)i*del_t/T, l, Nx, N);
Build_Multiplier1(F, Kb, Mb, u0, u1, del_t, N, lda, kl, ku);
F77NAME(dcopy)(N, u1, 1, u0, 1);
for (int j = 0; j<N; ++j)
{
u1[j] = F[j]/Mb[j];
}
d[i] = u1[(N-1)/2];
}
Write_Point_Displacement(d, Nt, "Task2");
delete[] Mb;
delete[] u1;
delete[] F;
delete[] Kb;
delete[] d;
}
void Dynamic_Solver_2(double A, double E, double I, double L, double l, double qx, double qy, double Fy, double rho, double T, int Nt, int Nx, int ku, int kl, int N)
{
const int lda = 1 + 2*kl + ku;
const double del_t = T/(double)Nt;
const double beta = 0.25;
const double gamma = 0.5;
const double coeff1 = 1.0/(beta*del_t*del_t);
const double coeff2 = 1.0/(beta*del_t);
const double coeff3 = (1.0-2.0*beta)/(2.0*beta);
const double coeff4 = (1.0-gamma)*del_t;
const double coeff5 = gamma*del_t;
double * Mb = new double[N]();
double * Keff = new double[lda*N]();
double * F = new double[N]();
double * u0 = new double[N]();
double * u1 = new double[N]();
double * udot = new double[N]();
double * udotdot0 = new double[N]();
double * udotdot1 = new double[N]();
Build_M_global_banded(Mb, rho, A, l, Nx, N);
Build_Keff(Keff, Mb, coeff1, A, E, l, L, I, Nx, N, kl, ku, lda, kl);
for (int i = 0; i<Nt; ++i)
{
Build_F_global(F, Fy, qx, qy, (double)(i+1)*del_t/T, l, Nx, N);
Build_Multiplier2(u1, F, Mb, u0, udot, udotdot0, coeff1, coeff2, coeff3, N);
Banded_Matrix_Solver(Keff, u1, N, lda, kl, ku);
Build_udotdot(udotdot1, u1, u0, udot, udotdot0, coeff1, coeff2, coeff3, N);
Build_udot(udot, udotdot0, udotdot1, coeff4, coeff5, N);
F77NAME(dcopy) (N, u1, 1, u0, 1);
F77NAME(dcopy) (N, udotdot1, 1, udotdot0, 1);
}
Write_Vector(u0, N, l, L, "Task1");
delete[] Keff;
delete[] u0;
delete[] u1;
delete[] udot;
delete[] udotdot0;
delete[] udotdot1;
}
void Dynamic_Solver_MPI_1(double A, double E, double I, double L, double l, double qx, double qy, double Fy, double rho, double T, int Nt, int Nx, int ku, int kl, int N, int size, int rank)
{
const int lda = 1 + kl + ku;
const double del_t = T/(double)Nt;
const int Nxloc = Nx/2 + 2;
const int Nloc = (Nxloc-1)*3 ;
double * Mbloc = new double[Nloc]();
double * u0loc = new double[Nloc]();
double * u1loc = new double[Nloc]();
double * Kbloc = new double[Nloc*lda]();
double * Floc = new double[Nloc]();
Build_M_global_banded_MPI(Mbloc, rho, A, l, Nx, Nxloc, Nloc, size, rank);
Build_K_global_banded_altered_MPI(Kbloc, Mbloc, A, E, l, L, I, del_t, Nx, Nxloc, N, Nloc, kl, ku, lda, 0, size, rank);
for (int i = 0; i<Nt; ++i)
{
Build_F_global_MPI(Floc, Fy, qx, qy, (double)i*del_t/T, l, Nx, Nxloc, N, Nloc, size, rank);
Build_Multiplier1_MPI(Floc, Kbloc, Mbloc, u0loc, u1loc, del_t, N, Nloc, lda, kl, ku, size, rank);
F77NAME(dcopy)(Nloc, u1loc, 1, u0loc, 1);
for (int j = 0; j<Nloc; ++j)
{
u1loc[j] = Floc[j]/Mbloc[j];
}
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0)
{
MPI_Send(&u1loc[(Nx/2 - 2)*3], 3, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&u1loc[3*Nx/2], 3, MPI_DOUBLE, 1, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
else if (rank == 1)
{
MPI_Recv(&u1loc[0], 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Send(&u1loc[6], 3, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
}
// Print_Vector_Parallel(u0loc, Nloc, size, rank);
if (rank == 0)
{
double * u0 = new double[N]();
for (int i = 0; i < Nloc; ++i)
{
u0[i] = u0loc[i];
}
MPI_Recv(&u0[Nloc], Nloc-9, MPI_DOUBLE, 1, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
Write_Vector(u0, N, l, L, "Task1");
// double * Mb = new double[N]();
// double * u1 = new double[N]();
// double * F = new double[N]();
// double * Kb = new double[lda*N]();
// Build_M_global_banded(Mb, rho, A, l, Nx, N);
// Build_K_global_banded_altered(Kb, Mb, A, E, l, L, I, del_t, Nx, N, kl, ku, lda, 0);
// for (int i = 0; i<Nt; ++i)
// {
// Build_F_global(F, Fy, qx, qy, (double)i*del_t/T, l, Nx, N);
// Build_Multiplier1(F, Kb, Mb, u0, u1, del_t, N, lda, kl, ku);
// F77NAME(dcopy)(N, u1, 1, u0, 1);
// for (int j = 0; j<N; ++j)
// {
// u1[j] = F[j]/Mb[j];
// }
// }
// cout << endl;
// cout << "True Solution" << endl;
// Print_Vector(u1, N);
}
else if (rank == 1)
{
MPI_Send(&u0loc[9], Nloc-9 , MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
}
delete[] Mbloc;
delete[] u0loc;
delete[] u1loc;
delete[] Kbloc;
delete[] Floc;
}
void Dynamic_Solver_MPI_2(double A, double E, double I, double L, double l, double qx, double qy, double Fy, double rho, double T, int Nt, int Nx, int ku, int kl, int N, int size, int rank)
{
const int nb = (int)(N/size) + 1;
const int lda = 1 + 2*kl + 2*ku;
const double del_t = T/(double)Nt;
const double beta = 0.25;
const double gamma = 0.5;
const double coeff1 = 1.0/(beta*del_t*del_t);
const double coeff2 = 1.0/(beta*del_t);
const double coeff3 = (1.0-2.0*beta)/(2.0*beta);
const double coeff4 = (1.0-gamma)*del_t;
const double coeff5 = gamma*del_t;
double * Mbloc = new double[nb]();
double * Kbloc = new double [nb*lda]();
double * Floc = new double[nb]();
double * u0loc = new double[nb]();
double * u1loc = new double[nb]();
double * udotloc = new double[nb]();
double * udotdot0loc = new double[nb]();
double * udotdot1loc = new double[nb]();
double * Mb = new double[N]();
double * Keff = new double[lda*N]();
double * F = new double[N]();
Build_M_global_banded(Mb, rho, A, l, Nx, N);
Build_Keff(Keff, Mb, coeff1, A, E, l, L, I, Nx, N, kl, ku, lda, kl+ku);
Build_Block_Array(Mbloc, Mb, N, N, nb, nb, 1, 0, 0, 1.0, size, rank);
Build_Block_Array(Kbloc, Keff, N, lda*N, nb, nb*lda, lda, kl+ku, ku, 1.0, size, rank);
for (int i = 0; i < Nt; ++i)
{
Build_F_global(F, Fy, qx, qy, (double)(i+1)*del_t/T, l, Nx, N);
Build_Block_Array(Floc, F, N, N, nb, nb, 1, 0, 0, 0.0, size, rank);
Build_Multiplier2(u1loc, Floc, Mbloc, u0loc, udotloc, udotdot0loc, coeff1, coeff2, coeff3, nb);
Banded_Matrix_Solver_Parallel(Kbloc, u1loc, size*nb, nb, lda, kl, ku, size);
Build_udotdot(udotdot1loc, u1loc, u0loc, udotloc, udotdot0loc, coeff1, coeff2, coeff3, nb);
Build_udot(udotloc, udotdot0loc, udotdot1loc, coeff4, coeff5, nb);
F77NAME(dcopy) (nb, u1loc, 1, u0loc, 1);
F77NAME(dcopy) (nb, udotdot1loc, 1, udotdot0loc, 1);
}
double * u0 = new double[nb*size]();
MPI_Gather(u0loc, nb, MPI_DOUBLE, u0, nb, MPI_DOUBLE, 0,MPI_COMM_WORLD);
if (rank == 0)
{
Write_Vector(u0, N, l, L, "Task1");
}
}