-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_CH_1.cpp
515 lines (317 loc) · 10.4 KB
/
main_CH_1.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// polyFEM
// with mass matrix, and all
// plus, correction to quadratic consistency
// periodic boundary conditions
// Cahn-Hilliard solver, overdamped regime, reduced units
// check "D" in linear::chempot_inv for (I think) the only
// reduced parameter
#include <CGAL/Timer.h>
// write out matrices
//#define WRITE
//#define EXPLICIT
#include"main.h"
#include"sim_pars.h"
#include"linear.h"
#include"fields.h"
// Init global stuff.-
#include"periodic.h"
const FT LL=10; // length of original domain
Iso_rectangle domain(-LL/2, -LL/2, LL/2, LL/2);
// TODO: the two triangulations store different things.
// specific bases and faces should be implemented for each
sim_pars simu;
//const Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "[", "];");
Triangulation Tp(domain); // particles
int main() {
// CGAL::Timer time;
//
// time.start();
cout << "Creating point cloud" << endl;
simu.read();
create();
if(simu.create_points()) {
// set_alpha_circle( Tp , 2);
// set_alpha_under_cos( Tp ) ;
cout << "Creating alpha field " << endl;
set_alpha_random( Tp ) ;
cout << "Numbering particles " << endl;
number(Tp);
}
// areas(Tp);
// quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
// volumes(Tp, simu.FEMp() );
// Delta(Tp);
// linear algebra(Tp);
// if(simu.create_points()) {
// nabla(Tp);
// Delta(Tp);
// }
move_info( Tp );
// /// Prev test begin
//cout << "Calculating Lapl U" << endl;
//algebra.laplacian_v(kind::UOLD,kind::LAPLU);
//FT dt=simu.dt();
//cout << "Calculating Ustar implicitely" << endl;
//algebra.ustar_inv(kind::USTAR, dt , kind::UOLD, false);
//cout << "Solving PPE" << endl;
//algebra.PPE( kind::USTAR, dt, kind:: P );
//cout << "Calculating grad p" << endl;
//algebra.gradient(kind::P, kind::GRADP);
//algebra.mass_s(kind::DIVU);
//draw();
// return 1;
/// Prev test end
#ifdef WRITE
algebra.save_matrices();
#endif
// areas(Tp);
// quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
// nabla(Tp);
// Delta(Tp);
// linear algebra(Tp);
// cout << "Calculating grad alpha" << endl;
// algebra.gradient(kind::ALPHA, kind::GRADALPHA);
const std::string particle_file("particles.dat");
draw(Tp, particle_file , true);
simu.advance_time();
simu.next_step();
// bool first_iter=true;
CGAL::Timer time;
time.start();
std::ofstream log_file;
log_file.open("main.log");
bool is_overdamped = ( simu.mu() > 1 ) ; // high or low Re
for(;
simu.current_step() <= simu.Nsteps();
simu.next_step()) {
cout
<< "Step " << simu.current_step()
<< " . Time " << simu.time()
<< " ; t step " << simu.dt()
<< endl;
FT dt=simu.dt();
FT dt2 = dt / 2.0 ;
int iter=0;
FT displ=1e10;
FT min_displ=1e10;
int min_iter=0;
const int max_iter=1; //10;
const FT max_displ= 1e-8; // < 0 : disable
// leapfrog, special first step.-
// if(simu.current_step() == 1) dt2 *= 0.5;
// dt2 *= 0.5;
move_info(Tp);
// iter loop
for( ; iter<max_iter ; iter++) {
cout << "Move iteration " << iter << " of " << max_iter << " " << endl;
// comment for no move.-
displ=move( Tp , dt2 );
cout << "Iter " << iter << " , moved avg " << displ << " to half point" << endl;
if( (displ < max_displ) && (iter !=0) ) break;
areas(Tp);
quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
nabla(Tp);
Delta(Tp);
linear algebra(Tp);
if( displ < min_displ) {
min_displ=displ;
min_iter=iter;
}
// set_forces_Kolmo(Tp);
// Reynolds number discrimination
#ifdef EXPLICIT
cout << "Calculating Ustar explicitely" << endl;
algebra.laplacian_v(kind::UOLD,kind::LAPLU);
u_star(Tp, dt2 , false );
#else
// cout << "Calculating chem pot" << endl;
// algebra.chempot(kind::ALPHA, kind::CHEMPOT);
cout << "Calculating alpha implicitely" << endl;
// partly explicit ( unstable ? ):
cout << "Calculating chem pot explicitely" << endl;
if (iter==0)
algebra.chempot( kind::ALPHA0, kind::CHEMPOT );
else
algebra.chempot( kind::ALPHA , kind::CHEMPOT );
// inner iter loop
for( int alpha_it=0 ; alpha_it < 1 ; alpha_it++) { // max_iter ; alpha_it++) {
cout << "Alpha loop iter " << alpha_it << endl;
algebra.chempot( kind::ALPHA , kind::CHEMPOT );
algebra.alpha_inv_cp(kind::ALPHA, dt2 , kind::ALPHA0 );
}
// algebra.gradient(kind::ALPHA, kind::ALPHA0); // ???
// // iterative, fully implicit (does not converge):
// int alpha_it=0;
// // inner iter loop
// for( ; alpha_it < 10 ; alpha_it++) { // max_iter ; alpha_it++) {
// cout << "Alpha loop iter " << alpha_it << endl;
// algebra.alpha_inv_cp2(kind::ALPHA, dt2 , kind::ALPHA0 );
// cout << "Calculating chem pot implicitely" << endl;
// algebra.chempot_inv(kind::ALPHA, dt2 , kind::ALPHA0 );
// }
// // draw(Tp, particle_file , true);
cout << "Settinf Ustar = force" << endl;
// algebra.ustar_is_force(kind::USTAR);
algebra.chem_pot_force();
// substract spurious overall movement.-
zero_mean_v( Tp , kind::FORCE);
#endif
cout << "Solving PPE" << endl;
// comment for no move.-
algebra.PPE( kind::FORCE , 1 , kind:: P ); // Dt set to 1
// cout << "Calculating grad p" << endl;
// // comment for no move.-
// algebra.gradient(kind::P, kind::GRADP);
algebra.u_inv_od(kind::U);
cout << "Evolving U " << endl;
// comment for no move.-
u_new( Tp , dt2 );
cout << "U evolved " << endl;
} // iter loop
// comment for no move.-
displ=move( Tp , dt );
// update_half_velocity( Tp , false );
// comment for no move.-
update_half_velocity( Tp , is_overdamped );
update_half_alpha( Tp );
areas(Tp);
quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
if(simu.current_step()%simu.every()==0)
draw(Tp, particle_file , true);
log_file
<< simu.current_step() << " "
<< simu.time() << " " ;
integrals( Tp , log_file); log_file << " ";
fidelity( Tp , log_file ); log_file << endl;
simu.advance_time();
} // time loop
time.stop();
log_file.close();
cout << "Total runtime: " << time.time() << endl;
return 0;
}
void create(void) {
int N=simu.no_of_particles();
std::vector<Point> points;
// points.reserve(N);
if(simu.create_points()) {
if(simu.at_random()) {
points.reserve(N);
CGAL::Random_points_in_square_2<Point,Creator> g(LL/2.0-0.0001);
CGAL::copy_n( g, N, std::back_inserter(points));
cout << N << " particles placed at random" << endl;
} else {
// if((plotting)&&(Nin%2==0)&&(spike)) {
// cout << "Please enter an odd number of particles" << endl;
// std::abort();
// }
int Nb=sqrt(N + 1e-12);
N=Nb*Nb;
simu.set_no_of_particles(N);
points.reserve(N);
cout << N << " particles placed on square lattice" << endl;
FT spacing=LL/FT(Nb+0);
FT side=LL-1*spacing;
points_on_square_grid_2(side/2.0, N, std::back_inserter(points),Creator());;
if(simu.perturb()) {
CGAL::perturb_points_2(
points.begin(), points.end(),
simu.pert_rel()* spacing );//,Creator());
cout << "each particle perturbed about " << simu.pert_rel()* spacing << endl;
}
}
cout << "Inserting" << endl;
Tp.insert(points.begin(), points.end());
} else {
int N=simu.no_of_particles();
char part_file[]="particles.dat";
cout << "reading from file : " << part_file << endl;
std::ifstream main_data;
main_data.open(part_file );
if(main_data.fail()){
cout << "part file not found " << endl;
abort();
}
for(int i=0;i<N;i++) {
FT x,y;
main_data >> x;
main_data >> y;
// cout << x << " " << y << endl;
Vertex_handle vh=Tp.insert(Point(x,y));
#include"readin.h"
}
cout << "particles' data read" << endl;
main_data.close();
}
// straight from the manual.-
Triangulation::Covering_sheets cs = Tp.number_of_sheets();
cout << "Original covering (particles): " << cs[0] << ' ' << cs[1] << endl;
// return ;
Tp.convert_to_1_sheeted_covering();
cs = Tp.number_of_sheets();
cout << "Current covering (particles): " << cs[0] << ' ' << cs[1] << endl;
return ;
if ( Tp.is_triangulation_in_1_sheet() ) // = true
{
bool is_extensible = Tp.is_extensible_triangulation_in_1_sheet_h1()
|| Tp.is_extensible_triangulation_in_1_sheet_h2(); // = false
Tp.convert_to_1_sheeted_covering();
cs = Tp.number_of_sheets();
cout << "Current covering: " << cs[0] << ' ' << cs[1] << endl;
if ( is_extensible ) // = false
cout << "It is safe to change the triangulation here." << endl;
else {
cout << "It is NOT safe to change the triangulation here!" << endl;
abort();
}
// T.convert_to_9_sheeted_covering();
// cs = T.number_of_sheets();
// cout << "Current covering: " << cs[0] << ' ' << cs[1] << endl;
} else {
cout << "Triangulation not on one sheet!" << endl;
abort();
}
// cout << "It is (again) safe to modify the triangulation." << endl;
return ;
}
void number(Triangulation& T) {
int i=0;
for(F_v_it vit=T.vertices_begin();
vit != T.vertices_end();
vit++) {
// vit->indx.set(i); //or
vit->idx=i;
++i;
}
return;
}
// for (
// Periodic_point_iterator pit=
// T.periodic_points_begin(stored_cover);
// pit != T.periodic_points_end(stored_cover);
// ++pit)
// {
// // pt = *ptit;
// // if (! (pt[0].second.is_null() && pt[1].second.is_null() && pt[2].second.is_null()) )
// // {
// // Convert the current Periodic_triangle to a Triangle if it is
// // not strictly contained inside the original domain.
// // Note that this requires EXACT constructions to be exact!
// // t_bd = T.triangle(pt);
// //}
// Point p=pit->first;
// Offset os=pit->second;
// interior
// << p.x()+os[0]*LL << " "
// << p.y()+os[1]*LL << " "
// // << vit->indx()
// << endl;
// }
// for(F_v_it vit=T.vertices_begin();
// vit != T.vertices_end();
// vit++)
// interior
// << vit->point().x() << " "
// << vit->point().y() << " "
// << vit->indx() << endl;
// return;