-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte_carlo_wall.cc
1578 lines (1291 loc) · 54 KB
/
monte_carlo_wall.cc
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This is terrible design, each MC move has its with-wall equivalent.
The reason is that I didn't want to affect standard MC moves performance in any way.
Wall is regarded to interact with vertices; maybe should ineract with excluders?
The idea is that bonds should not break solely because of the wall (produces extra vertices at the same position)
*/
#include <iostream>
#include <stdlib.h>
#include <random>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Utils/PropertyManager.hh>
#include <OpenMesh/Apps/Assembly/topology.hh>
#include <OpenMesh/Apps/Assembly/energy.hh>
#include <OpenMesh/Apps/Assembly/geometry.hh>
#include <OpenMesh/Apps/Assembly/monte_carlo.hh>
#include <OpenMesh/Apps/Assembly/monte_carlo_wall.hh>
#include <OpenMesh/Apps/Assembly/excluders.hh>
#include <OpenMesh/Apps/Assembly/custom_mesh_props.hh>
#include <OpenMesh/Apps/Assembly/random.hh>
/*
Attempts insertion of a new face of type face_type with rotation rotational_config=0,1,2 compared to the
anchor edge of the corresponding prototype face.
Position (the boundary edge) where the new subunit is inserted is chosen randomly.
Positions (boundary edges) are currently searched twice, in this function and when computing p_propose.
This could be optimized by searching only outside and passing boundary_halfedges to this function.
Doesn't seem to make much of a speedup now.
- test if randvec() works as expected
*/
bool attempt_insertion_wall_not_implemented(MyMesh & mesh, int face_type, int rotational_config, UmbrellaWindow & uw, std::mt19937 & eng){
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
auto edge_props = OpenMesh::HProp<HalfedgeProp>(mesh, "edge_props");
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
//get a prototype face of kind face_type for properties
MyMesh::FaceHandle proto_face = (*mesh_props).prototype_faces[face_type];
std::vector<MyMesh::HalfedgeHandle> boundary_halfedges = find_boundary_halfedges(mesh);
//int which = int(rand() % boundary_halfedges.size());
int which = randint(boundary_halfedges.size(), eng);
MyMesh::VertexHandle v_from, v_to, new_vertex;
std::vector<MyMesh::VertexHandle> vhandles;
std::vector<MyMesh::EdgeHandle> affected_edges;
MyMesh::FaceHandle new_face;
MyMesh::Point new_vertex_point;
double E_before, E_after, dE, p, r, kT, d_max, R_add, v_add, l_AC, l_BC, l_AB, xC, yC, D;
MyMesh::HalfedgeHandle he = boundary_halfedges[which];
MyMesh::HalfedgeHandle anchor_halfedge, hedge1, hedge2;
//should be up to date, after garbage collection!!
int n_faces_before = mesh.n_faces();
int n_faces_after = n_faces_before + 1;
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
kT = (*mesh_props).kT;
R_add = (*mesh_props).R_add;
v_add = 4.0*M_PI*R_add*R_add*R_add/3.0;
D = (*mesh_props).DOS;
//d_max = (*mesh_props).d_max;
//affected edges before: edge(he)
affected_edges.push_back( mesh.edge_handle(he) );
E_before = energy(mesh, affected_edges);
E_before+=uw.bias_potential(n_faces_before);
v_from = mesh.from_vertex_handle(he);
v_to = mesh.to_vertex_handle(he);
//need to compute new vertex position: find eq. position then rotate
//for now use some crap; needs to be repositioned after rotation is decided, according to theta0 between old hedge and new hedge
new_vertex_point = 0.5*(mesh.point( v_from ) + mesh.point( v_to ));
new_vertex = mesh.add_vertex(new_vertex_point);
vhandles.push_back(v_from);
vhandles.push_back(v_to);
vhandles.push_back(new_vertex);
new_face = mesh.add_face(vhandles);
//need to set new_face 1) face properties, 2) edge properties from prototype
//adjust edge rotation/permutation
anchor_halfedge = mesh.find_halfedge(v_from, v_to);
for (int ir=0; ir<rotational_config; ir++){
anchor_halfedge = mesh.next_halfedge_handle(anchor_halfedge);
}
clone_properties_from_prototype(mesh, face_type, new_face, anchor_halfedge);
//get edge lengths and in-plane position of new vertex
hedge1 = mesh.find_halfedge(v_to, new_vertex);
hedge2 = mesh.find_halfedge( new_vertex, v_from);
if (mesh.is_boundary(hedge1) || mesh.is_boundary(hedge2)){
std::cout<<"Warning! attempt_insertion; not OK, one of them is boundary"<<std::endl;
}
l_AC = edge_props[hedge1].l0;
l_BC = edge_props[hedge2].l0;
l_AB = mesh.calc_edge_length( mesh.edge_handle(he));
//get in-plane position of new_vertex, assuming the general case of non-equilateral triangles, l01!=l02 necessarily
std::tie(xC, yC) = calculate_triangle_third_vertex_coordinate(l_AB, l_BC, l_AC);
//need a Point to std::vector<double> converter
//or just use Point for r vectors
MyMesh::VertexHandle v0 = mesh.to_vertex_handle(mesh.next_halfedge_handle( mesh.opposite_halfedge_handle(he) ) );
MyMesh::Point r0 = mesh.point(v0);
MyMesh::Point ex = (mesh.point(v_from) - mesh.point(v_to)).normalize();
MyMesh::Point b = r0 - mesh.point(v_from);
MyMesh::Point ey = -(b - (b.dot(ex)) * ex).normalize();
MyMesh::Point rC = xC*ex + yC*ey;
hedge1 = mesh.find_halfedge(v_to, v_from);
hedge2 = mesh.opposite_halfedge_handle(hedge1);
int edge_type1 = edge_props[hedge1].edge_type;
int edge_type2 = edge_props[hedge2].edge_type;
double theta0 = 0.5*(edge_props[hedge1].theta0[edge_type2] + edge_props[hedge2].theta0[edge_type1] );
//now need to pick a rotation
rotatevec(rC, ex, theta0);
//REPLACE WITH PROPER VOLUME RAND
rC+=mesh.point(v_to);
//std::cout<<"rC "<<rC<<" "<<(rC - mesh.point(v_to)).norm()<<" "<< (rC - mesh.point(v_from)).norm() <<std::endl;
rC+=randvec(eng)*R_add;
mesh.set_point(new_vertex, rC);
//========= excluder =========================================
//update new face's COM
mesh.calc_face_centroid(new_face, face_props[new_face].COM );
//update new face's neighbor list
update_neighbor_list(mesh, new_face);
//check new face's overlap with its neighbors
bool overlap = check_neighbor_overlap(mesh, new_face);
//if (overlap) std::cout<<"OVERLAP "<<std::endl;
//============================================================
//affected_edges_after: of get_affected_edges(new_vertex)
affected_edges = get_affected_edges(mesh, new_vertex);
E_after = energy(mesh, affected_edges);
E_after+=uw.bias_potential(n_faces_after);
dE = E_after-E_before;
//std::cout<<"dE "<<dE<<std::endl;
float mu=face_props[new_face].mu;
p = D*v_add*exp(-(dE-mu)/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if ( (r<p) && (!overlap) ){
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
//std::cout<<"INSERTION acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//------------------------
//========= excluder =========================================
//if accepted, add the new face to neighbors' neighbor lists
for (auto & neighbor: face_props[new_face].neighbor_list){
face_props[neighbor].neighbor_list.push_back(new_face);
}
//============================================================
return true;
}
else{
mesh.delete_face(new_face);
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//std::cout<<"INSERTION rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts insertion of a new face of type face_type with rotation rotational_config=0,1,2 compared to the
anchor edge of the corresponding prototype face.
Position (the open wedge) where the new subunit is inserted is chosen randomly.
Positions (wedges) are currently searched twice, in this function and when computing p_propose.
This could be optimized by searching only outside and passing wedges to this function.
Doesn't seem to make much of a speedup now.
*/
bool attempt_wedge_insertion_wall_not_implemented(MyMesh & mesh, int face_type, int rotational_config, UmbrellaWindow & uw, std::mt19937 & eng){
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
auto edge_props = OpenMesh::HProp<HalfedgeProp>(mesh, "edge_props");
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
//get_wedges
std::vector <MyMesh::VertexHandle> w1, w2, w3;
std::vector<MyMesh::EdgeHandle> affected_edges1, affected_edges2;
double kT, E_before, E_after, dE, r, p;
//should be up to date, after garbage collection!!
int n_faces_before = mesh.n_faces();
int n_faces_after = n_faces_before + 1;
auto wedges = get_open_wedge_triplets(mesh);
w1 = std::get<0>(wedges);
w2 = std::get<1>(wedges);
w3 = std::get<2>(wedges);
if (w1.size()==0){
return false;
}
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
//int which = int(rand() % w1.size());
int which = randint( w1.size(), eng );
affected_edges1 = get_affected_edges(mesh, w1[which]);
affected_edges2 = get_affected_edges(mesh, w3[which]);
E_before = energy(mesh, affected_edges1) + energy(mesh, affected_edges2);
E_before+=uw.bias_potential(n_faces_before);
std::vector<MyMesh::VertexHandle> vhandles;
vhandles.push_back(w1[which]);
vhandles.push_back(w2[which]);
vhandles.push_back(w3[which]);
MyMesh::FaceHandle new_face = mesh.add_face(vhandles);
kT = (*mesh_props).kT;
//anchor halfedge belongs to newly added face
MyMesh::HalfedgeHandle anchor_halfedge = mesh.find_halfedge(w1[which], w2[which]);
for (int ir=0; ir<rotational_config; ir++){
anchor_halfedge = mesh.next_halfedge_handle(anchor_halfedge);
}
clone_properties_from_prototype(mesh, face_type, new_face, anchor_halfedge);
//========= excluder =========================================
//update new face's COM
mesh.calc_face_centroid(new_face, face_props[new_face].COM );
//update new face's neighbor list
update_neighbor_list(mesh, new_face);
//check new face's overlap with its neighbors
bool overlap = check_neighbor_overlap(mesh, new_face);
//if (overlap) std::cout<<"OVERLAP "<<std::endl;
//============================================================
//affected_edges1 = get_affected_edges(mesh, w1[which]);
//affected_edges2 = get_affected_edges(mesh, w3[which]);
MyMesh::EdgeHandle new_edge = find_edge(mesh, w1[which], w3[which]);
E_after = energy(mesh, affected_edges1) + energy(mesh, affected_edges2) + edge_stretch_energy(mesh, new_edge, edge_props );
E_after+=uw.bias_potential(n_faces_after);
dE = E_after-E_before;
//std::cout<<"wedge dE "<<dE<<std::endl;
float mu=face_props[new_face].mu;
p = exp(-(dE-mu)/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if ( (r<p) && (!overlap)){
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
//std::cout<<"WEDGE INSERTION acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//------------------------
//========= excluder =========================================
//if accepted, add the new face to neighbors' neighbor lists
for (auto & neighbor: face_props[new_face].neighbor_list){
face_props[neighbor].neighbor_list.push_back(new_face);
}
//============================================================
return true;
}
else{
mesh.delete_face(new_face);
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//std::cout<<"WEDGE INSERTION rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<" "<<overlap<<std::endl;
//std::cout<<"OL? "<<overlap<<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts simple (non-wedge) removal of a new face of type face_type
Removable face is chosen randomly.
Removable faces are currently searched twice, in this function and when computing p_propose.
This could be optimized by searching only outside and passing removable_faces to this function.
Doesn't seem to make much of a speedup now.
*/
bool attempt_removal_wall_not_implemented(MyMesh & mesh, int face_type, UmbrellaWindow & uw, std::mt19937 & eng){
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
auto edge_props = OpenMesh::HProp<HalfedgeProp>(mesh, "edge_props");
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
double E_before, E_after, dE, kT, mu, r, p, R_add, v_add, D;
//should be up to date, after garbage collection!!
int n_faces_before = mesh.n_faces();
int n_faces_after = n_faces_before - 1;
//find removable subunits geometrically
/* std::vector<MyMesh::FaceHandle> removable_faces_geom = get_simply_removable_faces(mesh, face_type);
//filter them for the current subunit type only
std::vector<MyMesh::FaceHandle> removable_faces;
for (auto & rface : removable_faces_geom){
if (face_props[rface].face_type == face_type){
removable_faces.push_back(rface);
}
}
*/
std::vector<MyMesh::FaceHandle> removable_faces = get_simply_removable_faces(mesh, face_type);
if (removable_faces.size()==0){
//std::cout<<"nothing to remove"<<std::endl;
return false;
}
kT = (*mesh_props).kT;
R_add = (*mesh_props).R_add;
v_add = 4.0*M_PI*R_add*R_add*R_add/3.0;
D = (*mesh_props).DOS;
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
//int which = int(rand() % removable_faces.size());
int which = randint( removable_faces.size(), eng);
mu=face_props[ removable_faces[which] ].mu;
//need to find hanging vertex to find affected edges; or just use the faces' edges
MyMesh::VertexHandle v0, v1, v2;
MyMesh::HalfedgeHandle he;
for (MyMesh::FaceVertexIter fv = mesh.fv_iter(removable_faces[which]); fv.is_valid(); ++fv ){
//std::cout<<"valence "<<mesh.valence(*fv)<<std::endl;
if (mesh.valence(*fv)==2){
v0 = *fv;
}
}
he = mesh.halfedge_handle(v0);
if (mesh.is_boundary(he)){
he = mesh.opposite_halfedge_handle(he);
}
if (mesh.from_vertex_handle(he)==v0){
he = mesh.next_halfedge_handle(he);
v1 = mesh.from_vertex_handle(he);
v2 = mesh.to_vertex_handle(he);
}
if (mesh.to_vertex_handle(he)==v0){
he = mesh.prev_halfedge_handle(he);
v1 = mesh.from_vertex_handle(he);
v2 = mesh.to_vertex_handle(he);
}
std::vector<MyMesh::EdgeHandle> affected_edges = get_affected_edges(mesh, v0);
E_before = energy(mesh, affected_edges);
E_before+=uw.bias_potential(n_faces_before);
E_after = halfedge_stretch_energy(mesh, mesh.opposite_halfedge_handle(he), edge_props);
E_after+=uw.bias_potential(n_faces_after);
dE = E_after - E_before;
p = (1.0/ (D*v_add) )*exp(-(dE+mu)/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if (r<p){
//============== excluder ============================
//remove the to-be-removed face from its neighbors neighbor lists
//going through neighbors of the removable face
std::vector<MyMesh::FaceHandle> * vec;
for (auto & neighbor: face_props[ removable_faces[which] ].neighbor_list){
vec = &(face_props[neighbor].neighbor_list);
//this line supposedly removes from vector
(*vec).erase(std::remove((*vec).begin(), (*vec).end(), removable_faces[which] ), (*vec).end());
}
//====================================================
//std::cout<<"REMOVED"<<std::endl;
mesh.delete_face(removable_faces[which]);
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
// std::cout<<"REMOVAL acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//------------------------
return true;
}
else{
//std::cout<<"NOT REMOVED"<<std::endl;
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//std::cout<<"REMOVAL rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts wedge removal of a new face of type face_type
Removable wedge face is chosen randomly.
Removable faces are currently searched twice, in this function and when computing p_propose.
This could be optimized by searching only outside and passing removable_faces to this function.
Doesn't seem to make much of a speedup now.
*/
bool attempt_wedge_removal_wall_not_implemented(MyMesh & mesh, int face_type, UmbrellaWindow & uw, std::mt19937 & eng){
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
auto edge_props = OpenMesh::HProp<HalfedgeProp>(mesh, "edge_props");
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
double E_before, E_after, dE, kT, mu, r, p;
std::vector<MyMesh::EdgeHandle> affected_edges_before;
std::vector<MyMesh::HalfedgeHandle> affected_halfedges_after;
MyMesh::EdgeHandle edge;
//should be up to date, after garbage collection!!
int n_faces_before = mesh.n_faces();
int n_faces_after = n_faces_before - 1;
//find removable subunits geometrically
/* std::vector<MyMesh::FaceHandle> removable_faces_geom = get_wedge_removable_faces(mesh);
//filter them for the current subunit type only
std::vector<MyMesh::FaceHandle> removable_faces;
for (auto & rface : removable_faces_geom){
if (face_props[rface].face_type == face_type){
removable_faces.push_back(rface);
}
}
*/
std::vector<MyMesh::FaceHandle> removable_faces = get_wedge_removable_faces(mesh, face_type);
if (removable_faces.size()==0){
//std::cout<<"W nothing to remove"<<std::endl;
return false;
}
kT = (*mesh_props).kT;
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
//int which = int(rand() % removable_faces.size());
int which = randint( removable_faces.size(), eng);
mu=face_props[ removable_faces[which] ].mu;
for (MyMesh::FaceHalfedgeIter fh = mesh.fh_iter(removable_faces[which]); fh.is_valid(); ++fh ){
edge = mesh.edge_handle(*fh);
affected_edges_before.push_back( edge );
if (!mesh.is_boundary(edge)){
affected_halfedges_after.push_back( mesh.opposite_halfedge_handle(*fh) );
}
}
E_before = energy(mesh, affected_edges_before);
E_before+=uw.bias_potential(n_faces_before);
E_after = halfedge_stretch_energy(mesh, affected_halfedges_after[0], edge_props) + halfedge_stretch_energy(mesh, affected_halfedges_after[1], edge_props);
E_after+=uw.bias_potential(n_faces_after);
dE = E_after - E_before;
p = exp(-(dE+mu)/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if (r<p){
//============== excluder ============================
//remove the to-be-removed face from its neighbors neighbor lists
//going through neighbors of the removable face
std::vector<MyMesh::FaceHandle> * vec;
for (auto & neighbor: face_props[ removable_faces[which] ].neighbor_list){
vec = &(face_props[neighbor].neighbor_list);
//this line supposedly removes from vector
(*vec).erase(std::remove((*vec).begin(), (*vec).end(), removable_faces[which] ), (*vec).end());
}
//====================================================
//std::cout<<"W REMOVED"<<std::endl;
mesh.delete_face(removable_faces[which]);
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
// std::cout<<"WEDGE REMOVAL acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//------------------------
return true;
}
else{
//std::cout<<"W NOT REMOVED"<<std::endl;
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//std::cout<<"WEDGE REMOVAL rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts simple move of a vertex.
*/
bool attempt_move_wall(MyMesh & mesh, std::mt19937 & eng, Wall & wall){
//std::cout<<"attempt_move_tmp.."<<std::endl;
//int which = int(rand() % mesh.n_vertices());
int which = randint( mesh.n_vertices(), eng);
MyMesh::VertexHandle v = mesh.vertex_handle(which);
std::vector<MyMesh::EdgeHandle> affected_edges = get_affected_edges(mesh, v);
double E_before = elastic_energy(mesh, affected_edges);
//double E_after_ae, dE_ae;
//double E_before = full_elastic_energy(mesh);
double E_after, dE, p, r, kT, d_max, dx, dy, dz;
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
kT = (*mesh_props).kT;
d_max = (*mesh_props).d_max;
/*dx = d_max*((2.0*rand()/(RAND_MAX + 1.0))-1.0) ;
dy = d_max*((2.0*rand()/(RAND_MAX + 1.0))-1.0) ;
dz = d_max*((2.0*rand()/(RAND_MAX + 1.0))-1.0) ;*/
dx = randdouble(-d_max, d_max, eng);
dy = randdouble(-d_max, d_max, eng);
dz = randdouble(-d_max, d_max, eng);
//std::cout<<"dz "<<dz<<std::endl;
//======= wall =====================
//std::vector<MyMesh::FaceHandle> affected_faces = get_affected_faces(mesh, v);
//auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
E_before+=wall.wall_energy(mesh, v);
//===================================
mesh.set_point(v, mesh.point(v)+MyMesh::Point(dx, dy, dz));
//dE_ae = E_after_ae- E_before_ae;
//std::cout<<dE<<" "<<dE_ae<<std::endl;
//========= excluder =========================================
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
bool overlap=false;
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(v); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
//check affected face's overlap with its neighbors
overlap = check_neighbor_overlap(mesh, *vf, face_props);
if (overlap) break;
}
//============================================================
//E_after = full_elastic_energy(mesh);
E_after = elastic_energy(mesh, affected_edges);
//========= wall ==============
E_after+=wall.wall_energy(mesh, v);
//=============================
dE = E_after - E_before;
p = exp(-dE/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if ( (r<p) && (!overlap)){
return true;
}
else{
mesh.set_point(v, mesh.point(v)-MyMesh::Point(dx, dy, dz));
//========= excluder =========================================
//reset affected faces' COMs back
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(v); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
}
//============================================================
return false;
}
//return;
}
/*
Attempts type1 (wedge) fusion. Fusable pairs are searched here as well, they could be passed as a variable.
*/
bool attempt_type1_fusion_wall(MyMesh & mesh, std::mt19937 & eng, Wall & wall){
//for now, find fusion pairs here every time an attempt is made
//but this will be changed:
//1. wedges will be found outside and passed as argument to get_type1_fusion_triplets
//2. type1_fusion_triplets are also found outside and filtered for distance between pairs
//3. eligible fusion triplets will be passed as arguments to this function
//this will be problematic because of either garbage collection or vertex replacements upon attempts
//wedges can't really be preserved in between (rejected) attempts unless the involved wedges are updated.
//in that case, garbage collection is only done upon accepted attempts
int which;
MyMesh::VertexHandle vA, vB, vC, vp, vp2;
MyMesh::Point vAinit, vBinit;
double p, r, E_before, E_after, dE, v_fuse, D;
std::vector<MyMesh::EdgeHandle> affected_edges, affected_edges1, affected_edges2;
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
float kT = (*mesh_props).kT;
double l_fuse = (*mesh_props).l_fuse;
//float l_fuse2 = (*mesh_props).l_fuse * (*mesh_props).l_fuse;
v_fuse = 4.0*M_PI*l_fuse*l_fuse*l_fuse*0.125/3.0;
D = (*mesh_props).DOS;
/*std::vector<MyMesh::VertexHandle> v1, v2, v3, v1f, v2f, v3f;
auto fusion_vectors = get_type1_fusion_triplets(mesh);
v1 = std::get<0>(fusion_vectors);
v2 = std::get<1>(fusion_vectors);
v3 = std::get<2>(fusion_vectors);
*/
std::vector<MyMesh::VertexHandle> v1f, v2f, v3f;
auto fusion_vectors = get_type1_fusion_triplets(mesh, l_fuse);
v1f = std::get<0>(fusion_vectors);
v2f = std::get<1>(fusion_vectors);
v3f = std::get<2>(fusion_vectors);
/*
for (int i=0; i<v1.size(); i++){
if ( (mesh.point(v1[i]) - mesh.point(v3[i])).sqrnorm() < l_fuse2){
v1f.push_back(v1[i]);
v3f.push_back(v3[i]);
v2f.push_back(v2[i]);
}
}
*/
if (v1f.size()==0){
return false;
}
//which = int(rand() % v1f.size());
which = randint( v1f.size(), eng);
vC = v2f[which];
//no need for this, v1f-v2f-v3f are ordered
/* if (mesh.is_boundary(mesh.find_halfedge(v2f[which], v1f[which]))){
vA = v1f[which];
vB = v3f[which];
}
else{
*/
vB = v1f[which];
vA = v3f[which];
// }
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
//std::cout<<vA<<" "<<vB<<" "<<vC<<std::endl;
affected_edges1 = get_affected_edges(mesh, vA);
affected_edges2 = get_affected_edges(mesh, vB);
E_before = energy(mesh, affected_edges1) + energy(mesh, affected_edges2);
//======= wall =====================
E_before+=wall.wall_energy(mesh, vA);
E_before+=wall.wall_energy(mesh, vB);
//===================================
vAinit = mesh.point(vA);
vBinit = mesh.point(vB);
vp = merge_vertices(mesh, vB, vA);
if ( (vp!=vA) && (vp!=vB)){
std::cout<<"both vertices lost in fusion"<<std::endl;
}
mesh.set_point(vp, 0.5*(vAinit + vBinit ));
//========= excluder =========================================
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
bool overlap=false;
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
//check affected face's overlap with its neighbors
overlap = check_neighbor_overlap(mesh, *vf);
if (overlap) break;
}
//============================================================
affected_edges = get_affected_edges(mesh, vp);
E_after = energy(mesh, affected_edges);
//====== wall ===============
E_after+=wall.wall_energy(mesh, vp);
//==========================
dE = E_after - E_before;
p = (1.0/ (D*v_fuse) )*exp(-dE/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if ( (r<p) && (!overlap)){
//mesh.garbage_collection();
//mesh.garbage_collection<std::vector<MyMesh::VertexHandle*>, std::vector<MyMesh::HalfedgeHandle*>, std::vector<MyMesh::FaceHandle*> >(handle_tracking_v, handle_tracking_h, handle_tracking_f);
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
//std::cout<<"TYPE1 FUSION acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//---------------------------
return true;
}
else{
vp2 = split_vertices(mesh, vp, vC);
if (mesh.is_boundary(mesh.find_halfedge(vC, vp))){
mesh.set_point(vp, vAinit);
mesh.set_point(vp2, vBinit);
}
else{
mesh.set_point(vp, vBinit);
mesh.set_point(vp2, vAinit);
}
//========= excluder =========================================
//reset affected faces' COMs back
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
}
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp2); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
}
//============================================================
//mesh.garbage_collection();
//mesh.garbage_collection<std::vector<MyMesh::VertexHandle*>, std::vector<MyMesh::HalfedgeHandle*>, std::vector<MyMesh::FaceHandle*> >(handle_tracking_v, handle_tracking_h, handle_tracking_f);
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//std::cout<<"TYPE1 FUSION rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts type2 (cracks) fusion. Fusable triplets are searched here as well, they could be passed as a variable.
*/
bool attempt_type2_fusion_wall(MyMesh & mesh, std::mt19937 & eng, Wall & wall){
/* std::vector<MyMesh::VertexHandle> v1, v2, v3, v1f, v2f, v3f;
auto fusion_vectors = get_type2_fusion_triplets(mesh);
v1 = std::get<0>(fusion_vectors);
v2 = std::get<1>(fusion_vectors);
v3 = std::get<2>(fusion_vectors);
*/
int which;
MyMesh::VertexHandle vA, vB, vC, vp, vp2, vD;
MyMesh::Point vAinit, vBinit;
double p, r, E_before, E_after, dE, v_fuse, D;
std::vector<MyMesh::EdgeHandle> affected_edges, affected_edges1, affected_edges2;
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
float kT = (*mesh_props).kT;
double l_fuse = (*mesh_props).l_fuse;
//float l_fuse2 = (*mesh_props).l_fuse * (*mesh_props).l_fuse;
v_fuse = 4.0*M_PI*l_fuse*l_fuse*l_fuse*0.125/3.0;
D = (*mesh_props).DOS;
std::vector<MyMesh::VertexHandle> v1f, v2f, v3f;
auto fusion_vectors = get_type2_fusion_triplets(mesh, l_fuse);
v1f = std::get<0>(fusion_vectors);
v2f = std::get<1>(fusion_vectors);
v3f = std::get<2>(fusion_vectors);
/*
for (int i=0; i<v1.size(); i++){
// if (sqr_dist(mesh, v1[i], v3[i]) < l_fuse2){
if ( (mesh.point(v1[i]) - mesh.point(v3[i])).sqrnorm() < l_fuse2){
v1f.push_back(v1[i]);
v3f.push_back(v3[i]);
v2f.push_back(v2[i]);
}
}
*/
if (v1f.size()==0){
return false;
}
//which = int(rand() % v1f.size());
which = randint( v1f.size(), eng);
vC = v2f[which];
//no need for this, v1f-v2f-v3f are ordered
/* if (mesh.is_boundary(mesh.find_halfedge(v2f[which], v1f[which]))){
vA = v1f[which];
vB = v3f[which];
}
else{
*/
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
vB = v1f[which];
vA = v3f[which];
// }
vD = mesh.to_vertex_handle( mesh.next_halfedge_handle(mesh.find_halfedge(vC, vA)) );
//std::cout<<vA<<" "<<vB<<" "<<vC<<std::endl;
affected_edges1 = get_affected_edges(mesh, vA);
affected_edges2 = get_affected_edges(mesh, vB);
E_before = energy(mesh, affected_edges1) + energy(mesh, affected_edges2);
//======= wall =====================
E_before+=wall.wall_energy(mesh, vA);
E_before+=wall.wall_energy(mesh, vB);
//===================================
vAinit = mesh.point(vA);
vBinit = mesh.point(vB);
vp = merge_vertices(mesh, vB, vA);
//========= excluder =========================================
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
bool overlap=false;
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
//check affected face's overlap with its neighbors
overlap = check_neighbor_overlap(mesh, *vf);
if (overlap) break;
}
//============================================================
if ( (vp!=vA) && (vp!=vB)){
std::cout<<"both vertices lost in fusion"<<std::endl;
}
mesh.set_point(vp, 0.5*(vAinit + vBinit ));
affected_edges = get_affected_edges(mesh, vp);
E_after = energy(mesh, affected_edges);
//====== wall ===============
E_after+=wall.wall_energy(mesh, vp);
//==========================
dE = E_after - E_before;
p = (1.0/ (D*v_fuse) )*exp(-dE/kT);
//r = rand()/(RAND_MAX + 1.0);
r = randdouble(eng);
if ( (r<p) && (!overlap) ){
//--------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t -dE) > 1e-8)
//std::cout<<"TYPE2 FUSION acc, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//---------------------------
return true;
}
else{
vp2 = split_vertices(mesh, vp, vC, vD);
if (mesh.is_boundary(mesh.find_halfedge(vC, vp))){
mesh.set_point(vp, vAinit);
mesh.set_point(vp2, vBinit);
}
else{
mesh.set_point(vp, vBinit);
mesh.set_point(vp2, vAinit);
}
//========= excluder =========================================
//reset affected faces' COMs back
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
}
for (MyMesh::VertexFaceIter vf = mesh.vf_iter(vp2); vf.is_valid(); ++vf ){
//update affected faces' COMs
mesh.calc_face_centroid(*vf, face_props[*vf].COM );
}
//============================================================
//------------------------------------
//Ea_t = full_energy(mesh);
//dE_t = Ea_t - Eb_t;
//if (std::abs(dE_t) > 1e-8)
//std::cout<<"TYPE2 FUSION rej, dE, dE_t, dE-dE_t "<<dE<<" "<<dE_t<<" "<<dE-dE_t <<std::endl;
//--------------------------------------
return false;
}
}
/*
Attempts type2 (crack) fission. Fission triplets are searched here as well, they could be passed as a variable.
*/
bool attempt_type2_fission_wall(MyMesh & mesh, std::mt19937 & eng, Wall & wall){
std::vector<MyMesh::VertexHandle> v1, v2, v3;
MyMesh::VertexHandle vp, vback;
MyMesh::Point v2init;
int which;
double p, r, E_before, E_after, dE, v_fuse, D;
std::vector<MyMesh::EdgeHandle> affected_edges, affected_edges1, affected_edges2;
auto mesh_props = OpenMesh::MProp<MeshProp>(mesh, "mesh_props");
float kT = (*mesh_props).kT;
float l_fuse = (*mesh_props).l_fuse;
float l_fuse2 = l_fuse * l_fuse;
v_fuse = 4.0*M_PI*l_fuse*l_fuse*l_fuse*0.125/3.0;
D = (*mesh_props).DOS;
auto fission_vectors = get_type2_fission_triplets(mesh);
v1 = std::get<0>(fission_vectors);
v2 = std::get<1>(fission_vectors);
v3 = std::get<2>(fission_vectors);
if (v1.size()==0){
return false;
}
//which = int(rand() % v1.size());
which = randint( v1.size(), eng);
v2init = mesh.point(v2[which]);
//---------------------
//double Eb_t, Ea_t, dE_t;
//Eb_t = full_energy(mesh);
//----------------------
affected_edges = get_affected_edges(mesh, v2[which]);
E_before = energy(mesh, affected_edges);
//======= wall =====================
E_before+=wall.wall_energy(mesh, v2[which]);
//===================================
vp = split_vertices(mesh, v2[which], v1[which], v3[which]);
//!!! Need to set to random from sphere
MyMesh::Point dr = randvec(eng)*0.5*l_fuse;
mesh.set_point(vp, mesh.point(vp)+dr);
mesh.set_point(v2[which], mesh.point(v2[which])-dr);
//========= excluder =========================================