-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_CH.cpp
702 lines (454 loc) · 14.2 KB
/
main_CH.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// 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
// projected to and from mesh
#include <CGAL/Timer.h>
// write out matrices
//#define WRITE
//#define EXPLICIT
#include"main.h"
#include"CH_FFT.h"
#include"sim_pars.h"
#include"linear.h"
#include"fields.h"
// Init global stuff.-
#include"periodic.h"
const FT LL=128; // length of original domain
const FT Db=0.04; // diffusion constant
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;
//#define FULL
#define FULL_FULL
//#define FULL_LUMPED
//#define FLIP
#ifdef FULL_FULL
#define FULL
#endif
#ifdef FULL_LUMPED
#define FULL
#endif
#include"onto_from_mesh.h"
//const Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "[", "];");
Triangulation Tp(domain); // particles
Triangulation Tm(domain); // mesh
void load_alpha_on_fft(const Triangulation& T , CH_FFT& fft );
void load_fields_from_fft(const CH_FFT& fft , Triangulation& T );
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( Tm ) ;
//set_alpha_cos( Tm );
cout << "Numbering particles " << endl;
number(Tp);
number(Tm);
}
int Nb=sqrt( simu.no_of_particles() + 1e-12);
// Set up fft, and calculate initial velocities:
move_info( Tm );
CH_FFT fft( LL , Nb );
load_alpha_on_fft( Tm , fft );
fft.all_fields();
fft.draw( "phi", 0, fft.field_f() );
fft.draw( "mu", 0, fft.field_mu() );
fft.draw( "grad_mu_x", 0, fft.field_grad_mu_x() );
fft.draw( "grad_mu_y", 0, fft.field_grad_mu_y() );
fft.draw( "force_x", 0, fft.field_force_x() );
fft.draw( "force_y", 0, fft.field_force_y() );
fft.draw( "vel_x", 0, fft.field_vel_x() );
fft.draw( "vel_y", 0, fft.field_vel_y() );
load_fields_from_fft( fft, Tm );
// every step
areas(Tp);
quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
// just once!
linear algebra(Tm);
areas(Tm);
quad_coeffs(Tm , simu.FEMm() ); volumes(Tm, simu.FEMm() );
cout << "Setting up diff ops " << endl;
// TODO: Are these two needed at all?
// if(simu.create_points()) {
// nabla(Tm);
// TODO, they is, too clear why
Delta(Tm);
// }
const std::string mesh_file("mesh.dat");
const std::string particle_file("particles.dat");
// // step 0 draw.-
// draw(Tm, mesh_file , true);
// draw(Tp, particle_file , true);
cout << "Assigning alpha to particles " << endl;
#if defined FULL_FULL
{
Delta(Tp);
linear algebra_p(Tp);
from_mesh_full( Tm , Tp , algebra_p,kind::ALPHA);
}
#elif defined FULL_LUMPED
from_mesh_lumped( Tm , Tp , kind::ALPHA);
#elif defined FLIP
from_mesh(Tm , Tp , kind::ALPHA);
#else
from_mesh(Tm , Tp , kind::ALPHA);
#endif
// #if defined FULL_FULL
// {
// Delta(Tp);
// linear algebra_p(Tp);
// from_mesh_full( Tm , Tp , algebra_p,kind::ALPHA);
// }
// #elif defined FULL_LUMPED
// from_mesh_lumped( Tm , Tp , kind::ALPHA);
// #elif defined FLIP
// from_mesh(Tm , Tp , kind::ALPHA);
// #else
// from_mesh(Tm , Tp , kind::ALPHA);
// #endif
cout << "Moving info" << endl;
move_info( Tm );
move_info( Tp );
draw(Tm, mesh_file , true);
draw(Tp, particle_file , true);
// return 1;
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=5; //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(Tm);
move_info(Tp);
// iter loop
for( ; iter<max_iter ; iter++) {
// comment for no move.-
displ = move( Tp , dt2 );
cout << "Iter " << iter << " , moved avg " << displ << " to half point" << endl;
if( displ < min_displ) {
min_displ=displ;
min_iter=iter;
}
if( (displ < max_displ) && (iter !=0) ) break;
areas(Tp);
quad_coeffs(Tp , simu.FEMp() ); volumes(Tp, simu.FEMp() );
cout << "Proj U0, alpha0 onto mesh " << endl;
#if defined FULL
onto_mesh_full (Tp,Tm,algebra,kind::ALPHA);
#elif defined FLIP
flip_volumes(Tp , Tm , simu.FEMm() );
onto_mesh_flip (Tp,Tm,simu.FEMm(),kind::ALPHA);
#else
onto_mesh_delta (Tp,Tm,kind::ALPHA);
#endif
load_alpha_on_fft( Tm , fft );
fft.all_fields();
FT b = Db*dt2;
fft.evolve( b );
load_fields_from_fft( fft, Tm );
cout << "Proj U, alpha from mesh " << endl;
#if defined FULL_FULL
{
Delta(Tp);
linear algebra_p(Tp);
from_mesh_full_v(Tm, Tp, algebra_p , kind::U);
from_mesh_full( Tm , Tp , algebra_p,kind::ALPHA);
}
#elif defined FULL_LUMPED
from_mesh_lumped( Tm , Tp , kind::ALPHA);
from_mesh_lumped_v(Tm, Tp, kind::U);
#elif defined FLIP
from_mesh(Tm , Tp , kind::ALPHA);
from_mesh_v(Tm, Tp, kind::U);
#else
from_mesh(Tm , Tp , kind::ALPHA);
from_mesh_v(Tm, Tp, kind::U);
#endif
// // substract spurious overall movement.-
// zero_mean_v( Tm , kind::FORCE);
} // 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() );
// this, for the looks basically .-
cout << "Proj U_t+1 , alpha_t+1 onto mesh " << endl;
#if defined FULL
onto_mesh_full_v(Tp,Tm,algebra,kind::U);
onto_mesh_full (Tp,Tm,algebra,kind::ALPHA);
#elif defined FLIP
flip_volumes(Tp , Tm , simu.FEMm() );
onto_mesh_flip_v(Tp,Tm,simu.FEMm(),kind::U);
onto_mesh_flip (Tp,Tm,simu.FEMm(),kind::ALPHA);
#else
onto_mesh_delta_v(Tp,Tm,kind::U);
onto_mesh_delta (Tp,Tm,kind::ALPHA);
#endif
if(simu.current_step()%simu.every()==0) {
draw(Tm, mesh_file , true);
draw(Tp, particle_file , true);
fft.histogram( "phi", simu.current_step() , fft.field_fq() );
}
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::cpp11::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());;
// for(int i = 0 ; i < Nb ; ++i )
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());
points.clear();
// int Nb = sqrt(N + 1e-12);
// int nm = Nb* simu.mesh_factor() + 1 ;
// int Nm = nm * nm;
int Nm=simu.no_of_nodes();
int nm=sqrt(Nm + 1e-12);
Nm= nm * nm;
simu.set_no_of_nodes(Nm);
points.reserve(Nm);
cout << Nm << " mesh on square lattice" << endl;
FT spacing=LL/FT( nm +0);
FT side=LL-1*spacing;
points_on_square_grid_2(side/2.0, Nm , std::back_inserter(points),Creator());;
// // TODO: perfectly regular square grids are not too good, in fact
// CGAL::perturb_points_2(
// points.begin(), points.end(),
// 0.001* spacing );
Tm.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 );
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();
char mesh_file[]="mesh.dat";
cout << "reading from file : " << mesh_file << endl;
main_data.open(mesh_file );
int Nm=simu.no_of_nodes();
for(int i=0;i<Nm;i++) {
FT x,y;
main_data >> x;
main_data >> y;
// cout << x << " " << y << endl;
Vertex_handle vh=Tm.insert(Point(x,y));
#include"readin.h"
}
cout << "mesh 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 load_alpha_on_fft( const Triangulation& T , CH_FFT& fft ) {
int Nb = fft.Nx();
size_t align=fft.alignment();
c_array al( Nb , Nb , align );
for(F_v_it vit=T.vertices_begin();
vit != T.vertices_end();
vit++) {
int nx = vit->nx.val();
int ny = vit->ny.val();
// "right" ordering
int i = ( Nb - 1 ) - ny ;
int j = nx;
// "wrong" ordering
// int i = nx;
// int j = ny;
FT val = vit->alpha0.val();
//FT val = vit->alpha.val();
al(i,j) = val;
}
fft.set_f( al );
return;
}
void load_fields_from_fft(const CH_FFT& fft , Triangulation& T ) {
int Nb = fft.Nx();
c_array vx = fft.field_vel_x();
c_array vy = fft.field_vel_y();
c_array al = fft.field_f();
for(F_v_it vit=T.vertices_begin();
vit != T.vertices_end();
vit++) {
int nx = vit->nx.val();
int ny = vit->ny.val();
// "right" ordering
int i = ( Nb - 1 ) - ny ;
int j = nx;
// "wrong" ordering
// int i = nx;
// int j = ny;
vit->U.set( Vector_2( real(vx(i,j)) , real(vy(i,j)) ) );
vit->alpha.set( real( al(i,j) ) );
// TODO: return more fields (chem pot, pressure, force, etc)
}
return;
}
void number(Triangulation& T) {
int idx=0;
int N=simu.no_of_particles();
int Nb=sqrt(N + 1e-12);
FT spacing=LL/FT(Nb+0);
FT side=LL-1*spacing;
for(F_v_it vit=T.vertices_begin();
vit != T.vertices_end();
vit++) {
// vit->indx.set(i); //or
vit->idx = idx;
FT x = vit->point().x() + side/2.0;
FT y = vit->point().y() + side/2.0;
int i = rint( FT(Nb) * x / LL );//+ 0.5);
int j = rint( FT(Nb) * y / LL );//+ 0.5);
// --i; --j;
vit->nx = i;
vit->ny = j;
// cout << idx
// << " " << i
// << " " << j
// << " " << x
// << " " << y
// << endl;
++idx;
}
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;