-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmxMicroRefinementStudy.cpp
More file actions
383 lines (340 loc) · 10.4 KB
/
Copy pathmxMicroRefinementStudy.cpp
File metadata and controls
383 lines (340 loc) · 10.4 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* mxDrawGrid.cpp
*
* Created on: Sep 28, 2011
* Author: fogelson
*/
#include "MexTools/MexTools.h"
#include "Geo/Geometry.h"
#include "Ops/Operators.h"
#include "Ops/OperatorFactory.h"
#include "Smoothers/Smoothers.h"
#include "TransferOperators/TransferOperators.h"
#include "Multigrid/Multigrid.h"
#include <vector>
using namespace std;
using namespace blitzmatlab;
using namespace CFD::OOGeometry;
using namespace CFD::OOOps;
using namespace CFD::OOMexTools;
using namespace CFD::OOMultigrid;
/* nlhs: number of outputs to Matlab function
* plhs: array of pointers to the memory locations of the outputs
* nrhs: number of inputs to Matlab function
* prhs: array of pointers to the memory locations of the inputs
*/
/* The following command should be invoked from MATLAB:
*
* mxMicroRefinementStudy(deltaT,h,r,t0,steps,H,D,v1,v2,vCycles)
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
// Desired number of inputs and outputs
int outputs = 0;
int inputs = 10;
if(nrhs != inputs){
mexErrMsgTxt("Wrong number of inputs.");
}
if(nlhs > outputs){
mexErrMsgTxt("Too many outputs.");
}
MexPlotTool plotter;
double deltaT, h, r, t0, H, D;
int v1, v2, vCycles, steps;
deltaT = getMxDouble(prhs[0]);
h = getMxDouble(prhs[1]);
r = getMxDouble(prhs[2]);
t0 = getMxDouble(prhs[3]);
steps = getMxInt(prhs[4]);
H = getMxDouble(prhs[5]);
D = getMxDouble(prhs[6]);
v1 = getMxInt(prhs[7]);
v2 = getMxInt(prhs[8]);
vCycles = getMxInt(prhs[9]);
double pi = acos(-1);
double offset = h/2;
Grid * g = new Circle(h,r,offset);
CellDoubleArray xCentroid = g->getCellCentroidX();
CellDoubleArray yCentroid = g->getCellCentroidY();
CellDoubleArray xCell = g->getCellX();
CellDoubleArray yCell = g->getCellY();
CellDoubleArray u = g->makeCellDoubleArray();
//u = pow2(xCentroid) + pow2(yCentroid);
DiffusionBackwardEulerFactory * factory = new DiffusionBackwardEulerFactory(D,deltaT);
//FENEBackwardEulerFactory * factory = new FENEBackwardEulerFactory(deltaT);
//factory->setD(D);
//factory->setH(H);
//factory->setQmax(r);
Interpolator * interpolator = new PiecewiseConstantInterpolator();
Restrictor * restrictor = new VolumeWeightedRestrictor();
StenciledSmoother * smoother = new GSFourPoint();
MultigridSolver * solver = new MultigridSolver(smoother, interpolator, restrictor);
CellDoubleArray zed = g->makeCellDoubleArray();
CellDoubleArray u0 = g->makeCellDoubleArray();
double t = t0;
u0 = (pow2(xCell) + pow2(yCell))*exp(t);
u = u0;
CellDoubleArray rhs = g->makeCellDoubleArray();
CellDoubleArray f = g->makeCellDoubleArray();
CellDoubleArray uExact = g->makeCellDoubleArray();
for(int n = 0; n < steps; n++){
t = n*deltaT + t0;
uExact = (pow2(xCentroid) + pow2(yCentroid))*exp(t);
/*plotter.newFigure();
plotter.graphCellCentroidData(uExact,g);
stringstream titleExact;
titleExact << "uExact, t = ";
titleExact << t;
plotter.title(titleExact.str());
plotter.colorbar();
plotter.newFigure();
plotter.graphCellCentroidData(u,g);
stringstream titleU;
titleU << "u, t = ";
titleU << t;
plotter.title(titleU.str());
plotter.colorbar();
plotter.newFigure();
plotter.graphCellCentroidData(factory->getLHS(g)->constantTerm,g);
plotter.colorbar();*/
factory->setTime(t);
f = exp(t+deltaT)*(pow2(xCentroid)+pow2(yCentroid)-4);
rhs = deltaT*f + factory->getRHS(g)->apply(u);
for(int c = 0; c < vCycles; c++){
solver->vCycle(u,u0,rhs,v1,v2,g,factory);
}
u0 = u;
/*t = n*deltaT + t0;
cout << t << endl;
factory->setTime(t);
f = exp(t+deltaT)*(pow2(xCentroid)+pow2(yCentroid)-4);
rhs = deltaT*f + factory->getRHS(g)->apply(u0);
for(int c = 0; c < vCycles; c++){
solver->vCycle(u,u0,rhs,v1,v2,g,factory);
}
u0 = u;*/
}
uExact = (pow2(xCell) + pow2(yCell))*exp(t+deltaT);
CellDoubleArray err = g->makeCellDoubleArray();
err = u - uExact;
double errl2 = sum(where(g->getCellTypes()!=COVERED, pow2(err)*(g->getVolumes()), 0));
errl2 = errl2/sum(where(g->getCellTypes()!=COVERED, g->getVolumes(), 0));
errl2 = sqrt(errl2);
double maxErr = max(where(g->getCellTypes() != COVERED, abs(err), 0));
cout << errl2 << endl;
/*
plotter.newFigure();
plotter.graphCellCentroidData(u,g);
plotter.colorbar();
plotter.newFigure();
plotter.graphCellCentroidData(uExact,g);
plotter.colorbar();
*/
plotter.newFigure();
plotter.graphCellCentroidData(err,g);
plotter.colorbar();
/*plotter.newFigure();
plotter.graphCellCentroidData(u,g);
plotter.colorbar();
plotter.newFigure();
plotter.graphCellCentroidData(uExact,g);
plotter.colorbar();*/
delete interpolator;
delete restrictor;
delete smoother;
delete solver;
delete factory;
delete g;
// CellDoubleArray xCC = g->getCellX();
// CellDoubleArray yCC = g->getCellY();
// CellDoubleArray rCC = g->makeCellDoubleArray();
// rCC = sqrt(pow2(xCC) + pow2(yCC));
//
// CellDoubleArray u = g->makeCellDoubleArray();
// u = cos(2*pi*rCC);
//
// CellDoubleArray xCT = g->getCellCentroidX();
// CellDoubleArray yCT = g->getCellCentroidY();
// CellDoubleArray rCT = g->makeCellDoubleArray();
// rCT = sqrt(pow2(xCT) + pow2(yCT));
//
// /*FaceDoubleArray xFace = g->getFaceX();
// FaceDoubleArray yFace = g->getFaceY();
// FaceDoubleArray theta = g->makeFaceDoubleArray();
// theta = atan2(yFace,xFace);
//
// FaceDoubleArray beta = g->makeFaceDoubleArray();
// beta = 1;//where(abs(theta) >= pi/2, 3, 1);
//
// CellToFaceOperator flux;
// flux = beta*grad;
//
// LaplacianFactory LFac;
// L = *(LFac.get(g));
// //CellToCellOperator L;
// //L = LFac.get(g);*/
//
// CellDoubleArray LuExact = g->makeCellDoubleArray();
// LuExact = -2*pi*sin(2*pi*rCT)/rCT - 4*pow2(pi)*cos(2*pi*rCT);
//
// double deltaT = .05;
// double D = .1;
//
// //OperatorFactory<CellToCellOperator> * factory = new AdvectionDiffusionBackwardEulerFactory(3,0,D,deltaT);
// //AdvectionDiffusionBackwardEulerFactory * factory = new AdvectionDiffusionBackwardEulerFactory(8,0,D,deltaT);
// //FENEBackwardEulerFactory * factory = new FENEBackwardEulerFactory(deltaT);
// FENESteadyFactory * factory = new FENESteadyFactory();
// factory->setD(1);
// factory->setH(1);
// factory->setQmax(1);
// factory->setGradU(0,10,0,0);
// StenciledSmoother * smoother = new GSFourPoint();
// Interpolator * interpolator = new PiecewiseConstantInterpolator();
// Restrictor * restrictor = new VolumeWeightedRestrictor();
//
// MultigridSolver * solver = new MultigridSolver(smoother,interpolator,restrictor);
//
// CellToCellOperator * lhs, * rhs;
//
// u = 1;//where(xCC <= .01, 1, 0);
//
//
// plotter.newFigure();
// CellDoubleArray uNew = g->makeCellDoubleArray();
// CellDoubleArray f = g->makeCellDoubleArray();
//
// lhs = factory->getLHS(g);
// rhs = factory->getRHS(g);
//
//
// for(int n = 0; n <= 0; n++){
// cout << n << endl;
//// CellDoubleArray s = g->makeCellDoubleArray();
//// s = (g->getVolumes())*u;
//// double total = sum(where(g->getCellTypes() != COVERED, s, 0));
//// cout << total << endl;
//
// lhs = factory->getLHS(g);
// rhs = factory->getRHS(g);
// f = (*rhs)(u);
// //smoother->smooth(uNew,u,f,*lhs,400);
// for(int k = 0; k < 30; k++){
// solver->vCycle(uNew,u,f,2,2,g,factory);
// //factory->clearMap(factory->lhs);
// //factory->clearMap(factory->rhs);
// //lhs = factory->getLHS(g);
// //rhs = factory->getRHS(g);
// }
// u = uNew;
// /*if(n % 1 == 0){
// plotter.graphCellCentroidData(u,g);
//// plotter.graphCellDoubleArray(u,g,"mesh");
// plotter.colorbar();
// plotter.drawNow();
// }*/
// }
// plotter.graphCellCentroidData(u,g);
// plotter.colorbar();
// plotter.drawNow();
//
// /*OperatorFactory<CellToCellOperator> * upwind = new UpwindFactory(*solidBody);
// CellToCellOperator * uw = upwind->get(g);
//
// u = where(abs(xCC) <= .2 || abs(yCC) <= .2 , 0,1);
//
// mxArray * nullplhs[0], * nullprhs[0];
//
// CellDoubleArray uNew = g->makeCellDoubleArray();
// plotter.initializeMovie();
// plotter.newFigure();
//
// for(int n = 0; n <= 6000; n++){
// uNew = u + deltaT*((*uw)(u));
// u = uNew;
// if(n % 200 == 0){
// plotter.graphCellCentroidData(u,g);
// mexCallMATLAB(0,nullplhs,0,nullprhs,"colorbar");
// //plotter.captureFrame();
// plotter.drawNow();
// }
// }
// //plotter.playMovie();*/
//
//
//
///* SplitOperatorFactory<CellToCellOperator> * factory = new CrankNicholsonDiffusionFactory(deltaT);
// CellToCellOperator * P = factory->getRHS(g);
//
// StenciledSmoother * smoother = new GSFourPoint();
// Interpolator * interpolator = new PiecewiseConstantInterpolator();
// Restrictor * restrictor = new VolumeWeightedRestrictor();
//
// MultigridSolver solver(smoother,interpolator,restrictor);
//
//
// u = 0;
//
// mxArray * nullplhs[0], * nullprhs[0];
//
// Grid * fine = g;
//
// //CellDoubleArray uNew = g->makeCellDoubleArray();
// CellDoubleArray rhs = g->makeCellDoubleArray();
//
// for(int n = 0; n < 20; n++){
// rhs = (*P)(u) - deltaT*LuExact;
// for(int k = 0; k < 3; k++){
// //smoother->smooth(u,u,rhs,*(factory->getLHS(g)),100);
// solver.vCycle(u,u,rhs,3,3,fine,factory);
// }
// //plotter.graphCellCentroidData(u,fine);
// //mexCallMATLAB(0,nullplhs,0,nullprhs,"colorbar");
// //plotter.drawNow();
// }
// //plotter.graphCellCentroidData(u,fine);
// //mexCallMATLAB(0,nullplhs,0,nullprhs,"colorbar");*/
//
///* for(int k = 0; k < 1; k++){
// CellToCellOperator * Afine = factory->getLHS(g);
// smoother->smooth(uNew,u,rhs,*Afine,3);
// CellDoubleArray Lu = g->makeCellDoubleArray();
// Lu = (*Afine)(u);
// CellDoubleArray rF = g->makeCellDoubleArray();
// rF = rhs - Lu;
// Grid * coarse = g->getCoarse();
// CellDoubleArray rC = coarse->makeCellDoubleArray();
// restrictor->doRestrict(rC,rF,coarse,fine);
// CellDoubleArray eC = coarse->makeCellDoubleArray();
// CellDoubleArray zC = coarse->makeCellDoubleArray();
// CellToCellOperator * Acoarse = factory->getLHS(coarse);
// smoother->smooth(eC,zC,rC,(*Acoarse),500);
// CellDoubleArray eF = fine->makeCellDoubleArray();
// interpolator->doInterpolate(eC,eF,coarse,fine);
// CellDoubleArray uCorrected = fine->makeCellDoubleArray();
// uCorrected = uNew + eF;
// smoother->smooth(uNew,uCorrected,rhs,(*Afine),3);
// u = uNew;
// }*/
//
//
//
//
// /*plotter.newFigure();
// for(int k = 0; k < 50; k++){
// plotter.graphCellCentroidData(u,g);
// mexCallMATLAB(0,nullplhs,0,nullprhs,"colorbar");
// plotter.drawNow();
// CellDoubleArray rhs = g->makeCellDoubleArray();
// rhs = (*P)(u) - deltaT*LuExact;
// solver.vCycle(uNew,u,rhs,4,4,g,factory);
// u = uNew;
// }*/
//
//
//
// delete g;
// delete factory;
//delete interpolator;
//delete restrictor;
//delete smoother;
//delete factory;
}