File: KinematicLocalisationAnalyser.cpp

package info (click to toggle)
yade 2025.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,308 kB
  • sloc: cpp: 93,298; python: 50,409; sh: 577; makefile: 162
file content (819 lines) | stat: -rw-r--r-- 30,988 bytes parent folder | download | duplicates (2)
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
/*************************************************************************
*  Copyright (C) 2006 by Bruno Chareyre                                  *
*  bruno.chareyre@grenoble-inp.fr                                        *
*                                                                        *
*  This program is free software; it is licensed under the terms of the  *
*  GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/

//This class computes statistics of micro-variables assuming axi-symetry
// #define		VERTEX_INFO_TYPE	Vertex_Info;
// #define		CELL_INFO_TYPE		Cell_Info;

#include "KinematicLocalisationAnalyser.hpp" //This one first, because it defines the info types
#include <lib/high-precision/Constants.hpp>
// #include "Tesselation.h"
// #include "TriaxialState.h"
#include "basicVTKwritter.hpp"
#include <sstream>
//#include <utility>

namespace yade { // Cannot have #include directive inside.
namespace CGT {
	using std::ofstream;
	using ::yade::math::max;
	using ::yade::math::min; // using inside .cpp file is ok.

	int         n_debug = 0;
	std::string _itoa(int i)
	{
		std::ostringstream buffer;
		buffer << i;
		return buffer.str();
	}

	KinematicLocalisationAnalyser::KinematicLocalisationAnalyser()
	{
		sphere_discretisation = SPHERE_DISCRETISATION;
		linear_discretisation = LINEAR_DISCRETISATION;
		consecutive = false;
		bz2 = true;
		TS1 = &ts1;
		TS0 = &ts0;
	}


	KinematicLocalisationAnalyser::~KinematicLocalisationAnalyser() { }

	KinematicLocalisationAnalyser::KinematicLocalisationAnalyser(const char* state_file1, bool /*usebz2*/)
	{
		sphere_discretisation = SPHERE_DISCRETISATION;
		linear_discretisation = LINEAR_DISCRETISATION;
		consecutive = false;
		bz2 = true;
		TS1 = &ts1;
		TS0 = &ts0;
		TS1->from_file(state_file1, /*use bz2?*/ bz2);
	}

	KinematicLocalisationAnalyser::KinematicLocalisationAnalyser(const char* state_file1, const char* state_file0, bool consecutive_files, bool usebz2)
	{
		consecutive = consecutive_files;
		bz2 = usebz2;
		sphere_discretisation = SPHERE_DISCRETISATION;
		linear_discretisation = LINEAR_DISCRETISATION;
		TS1 = &ts1;
		TS0 = &ts0;
		TS1->from_file(state_file1, /*use bz2?*/ bz2);
		TS0->from_file(state_file0, /*use bz2?*/ bz2);

		Delta_epsilon(3, 3) = TS1->eps3 - TS0->eps3;
		Delta_epsilon(1, 1) = TS1->eps1 - TS0->eps1;
		Delta_epsilon(2, 2) = TS1->eps2 - TS0->eps2;
	}

	const vector<Tenseur3>& KinematicLocalisationAnalyser::computeParticlesDeformation(const char* state_file1, const char* state_file0, bool usebz2)
	{
		consecutive = false;
		bz2 = usebz2;
		TS1->from_file(state_file1, /*use bz2?*/ bz2);
		TS0->from_file(state_file0, /*use bz2?*/ bz2);
		//FIXME: redundant?
		Delta_epsilon(3, 3) = TS1->eps3 - TS0->eps3;
		Delta_epsilon(1, 1) = TS1->eps1 - TS0->eps1;
		Delta_epsilon(2, 2) = TS1->eps2 - TS0->eps2;
		return computeParticlesDeformation();
	}


	KinematicLocalisationAnalyser::KinematicLocalisationAnalyser(const char* base_name, int n0, int n1, bool usebz2)
	{
		file_number_1 = n1;
		file_number_0 = n0;
		base_file_name = string(base_name);
		consecutive = ((n1 - n0) == 1);
		bz2 = usebz2;
		sphere_discretisation = SPHERE_DISCRETISATION;
		linear_discretisation = LINEAR_DISCRETISATION;
		TS1 = &ts1;
		TS0 = &ts0;
		std::ostringstream file_name1, file_name0;
		file_name1 << (string)(base_file_name) << n1;
		file_name0 << (string)(base_file_name) << n0;
		TS1->from_file(file_name1.str().c_str(), bz2);
		TS0->from_file(file_name0.str().c_str(), bz2);
		Delta_epsilon(3, 3) = TS1->eps3 - TS0->eps3;
		Delta_epsilon(1, 1) = TS1->eps1 - TS0->eps1;
		Delta_epsilon(2, 2) = TS1->eps2 - TS0->eps2;
	}

	void KinematicLocalisationAnalyser::SetBaseFileName(string name) { base_file_name = name; }

	bool KinematicLocalisationAnalyser::SetFileNumbers(int n0, int n1)
	{
		bool bf0 = false;
		bool bf1 = false;
		if (file_number_0 != n0) {
			if (file_number_1 != n0) {
				//file_name = base_file_name + n0;
				bf0 = TS0->from_file((base_file_name + _itoa(file_number_0)).c_str(), bz2);
			} else {
				TS0 = TS1;
				bf0 = true;
				TS1 = &ts0;
				//file_name = base_file_name + string(n1);
				bf1 = TS1->from_file((base_file_name + _itoa(file_number_1)).c_str(), bz2);
			}
		} else if (n1 != file_number_1) {
			//file_name = base_file_name + string(n1);
			bf0 = true;
			bf1 = TS1->from_file((base_file_name + _itoa(file_number_1)).c_str(), bz2);
		}
		file_number_1 = n1;
		file_number_0 = n0;
		consecutive = ((n1 - n0) == 1);
		Delta_epsilon(3, 3) = TS1->eps3 - TS0->eps3;
		Delta_epsilon(1, 1) = TS1->eps1 - TS0->eps1;
		Delta_epsilon(2, 2) = TS1->eps2 - TS0->eps2;
		return (bf0 && bf1);
	}

	void KinematicLocalisationAnalyser::SetConsecutive(bool t) { consecutive = t; }

	void KinematicLocalisationAnalyser::SetNO_ZERO_ID(bool t)
	{
		TS0->NO_ZERO_ID = t;
		TS1->NO_ZERO_ID = t;
	}

	void KinematicLocalisationAnalyser::SwitchStates(void)
	{
		TriaxialState* TStemp = TS0;
		TS0 = TS1;
		TS1 = TStemp;
	}

	vector<KinematicLocalisationAnalyser::Edge_iterator>&
	KinematicLocalisationAnalyser::Oriented_Filtered_edges(Real Nymin, Real Nymax, vector<Edge_iterator>& filteredList)
	{
		RTriangulation& T = TS1->tesselation().Triangulation();
		filteredList.clear();
		Edge_iterator ed_end = T.edges_end();
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it) && TS1->inside(T.segment(*ed_it).source()) && TS1->inside(T.segment(*ed_it).target())) {
				Segment s = T.segment(*ed_it);
				CVector v = s.to_vector();
				Real    ny = math::abs(v.y() / sqrt(s.squared_length()));

				if (Nymin < ny && ny <= Nymax) filteredList.push_back(ed_it);
			}
		}
		return filteredList;
	}

	bool KinematicLocalisationAnalyser::DefToFile(const char* state_file1, const char* state_file0, const char* output_file_name, bool usebz2)
	{
		consecutive = false;
		bz2 = usebz2;
		TS1->from_file(state_file1, /*use bz2?*/ bz2);
		TS0->from_file(state_file0, /*use bz2?*/ bz2);
		DefToFile(output_file_name);
		return 0;
	}

	bool KinematicLocalisationAnalyser::DefToFile(const char* output_file_name)
	{
		computeParticlesDeformation();
		Tesselation&    Tes = TS0->tesselation();
		RTriangulation& Tri = Tes.Triangulation();
		basicVTKwritter vtk(n_real_vertices, n_real_cells);
		vtk.open(output_file_name, "Output file generated by Yade's KinematicLocalisationAnalyser");
		// renumber vertices to handle fictious or erased bodies
		vector<unsigned> altIds(TS0->maxId + 1, 0);
		unsigned         newId = 0;

		vtk.begin_vertices();
		RTriangulation::Finite_vertices_iterator V_it = Tri.finite_vertices_begin();
		for (; V_it != Tri.finite_vertices_end(); ++V_it) {
			if (V_it->info().isFictious) continue;
			vtk.file << TS1->grain(V_it->info().id()).sphere.point() << endl; // get final positions in order to display with deformed shape
			altIds[V_it->info().id()] = newId++;
		}
		vtk.end_vertices();

		vtk.begin_cells();
		Finite_cells_iterator cell = Tri.finite_cells_begin();
		for (; cell != Tri.finite_cells_end(); ++cell) {
			if (!cell->info().isFictious)
				vtk.write_cell(
				        altIds[cell->vertex(0)->info().id()],
				        altIds[cell->vertex(1)->info().id()],
				        altIds[cell->vertex(2)->info().id()],
				        altIds[cell->vertex(3)->info().id()]);
		}
		vtk.end_cells();
		vtk.begin_data("Strain_matrix", POINT_DATA, TENSORS, FLOAT);
		V_it = Tri.finite_vertices_begin();
		for (; V_it != Tri.finite_vertices_end(); ++V_it) {
			if (!V_it->info().isFictious) {
				Tenseur_sym3 epsilon(ParticleDeformation[V_it->info().id()]);
				vtk.file << ParticleDeformation[V_it->info().id()] << endl;
			}
		}
		vtk.end_data();
		vtk.begin_data("Strain_deviator", POINT_DATA, SCALARS, FLOAT);
		V_it = Tri.finite_vertices_begin();
		for (; V_it != Tri.finite_vertices_end(); ++V_it) {
			if (!V_it->info().isFictious) {
				Tenseur_sym3 epsilon(ParticleDeformation[V_it->info().id()]);

				vtk.write_data((float)epsilon.Deviatoric().Norme());
			}
			//vtk.write_data((float) epsilon.Deviatoric()(1,1)-epsilon.Deviatoric()(0,0));}
		}
		vtk.end_data();
		vtk.close();
		return true;
	}

	bool KinematicLocalisationAnalyser::DistribsToFile(const char* output_file_name)
	{
		ofstream output_file(output_file_name);
		if (!output_file.is_open()) {
			cerr << "Error opening files";
			return false;
		}

		output_file << "sym_grad_u_total_g (wrong averaged strain):" << endl << Tenseur_sym3(grad_u_total_g) << endl;
		output_file << "Total volume = " << v_total << ", grad_u = " << endl
		            << grad_u_total << endl
		            << "sym_grad_u (true average strain): " << endl
		            << Tenseur_sym3(grad_u_total) << endl;
		output_file << "Macro strain = " << Delta_epsilon << endl;

		ContactDistributionToFile(output_file);
		AllNeighborDistributionToFile(output_file);

		TS1->filter_distance = 2.0;
		ContactDistributionToFile(output_file);
		AllNeighborDistributionToFile(output_file);

		TS1->filter_distance = 4.0;
		ContactDistributionToFile(output_file);
		AllNeighborDistributionToFile(output_file);

		output_file << "Contact_fabric : ";
		output_file << (Tenseur_sym3)Contact_fabric(*TS1); // << endl;
		output_file << "Contact_anisotropy : " << Contact_anisotropy(*TS1) << endl << endl;
		output_file << "Neighbor_fabric : " << Neighbor_fabric(*TS1) << endl;
		output_file << "Neighbor_anisotropy : " << Neighbor_anisotropy(*TS1) << endl << endl;

		RTriangulation&       T = TS1->tesselation().Triangulation();
		Edge_iterator         ed_end = T.edges_end();
		vector<Edge_iterator> edges;
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it)) {
				Segment s = T.segment(*ed_it);
				CVector v = s.to_vector();
				Real    xx = math::abs(v.z() / sqrt(s.squared_length()));

				if (xx > 0.95) edges.push_back(ed_it);
			}
		}
		NormalDisplacementDistributionToFile(edges, output_file);

		edges.clear();
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it)) {
				Segment s = T.segment(*ed_it);
				CVector v = s.to_vector();
				Real    xx = math::abs(v.z() / sqrt(s.squared_length()));

				if (xx < 0.05) edges.push_back(ed_it);
			}
		}
		NormalDisplacementDistributionToFile(edges, output_file);

		edges.clear();
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it)) {
				Segment s = T.segment(*ed_it);
				CVector v = s.to_vector();
				Real    xx = math::abs(v.z() / sqrt(s.squared_length()));

				if (xx > 0.65 && xx < 0.75) edges.push_back(ed_it);
			}
		}
		NormalDisplacementDistributionToFile(edges, output_file);
		output_file.close();
		return true;
	}

	long KinematicLocalisationAnalyser::Filtered_contacts(TriaxialState& state)
	{
		long                           nc1 = 0;
		TriaxialState::ContactIterator cend = state.contacts_end();
		for (TriaxialState::ContactIterator cit = state.contacts_begin(); cit != cend; ++cit) {
			if (state.inside((*cit)->grain1->sphere.point()) && state.inside((*cit)->grain2->sphere.point())) nc1 += 2;
			else if (state.inside((*cit)->grain1->sphere.point()) || state.inside((*cit)->grain2->sphere.point()))
				++nc1;
		}
		return nc1;
	}

	long KinematicLocalisationAnalyser::Filtered_neighbors(TriaxialState& state)
	{
		long            nv1 = 0;
		RTriangulation& T = state.tesselation().Triangulation();
		Edge_iterator   ed_end = T.edges_end();
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it)) {
				Segment s(T.segment(*ed_it));
				if (state.inside(s.source()) && state.inside(s.target())) nv1 += 2;
				else if (state.inside(s.source()) || state.inside(s.target()))
					++nv1;
			}
		}
		return nv1;
	}

	long KinematicLocalisationAnalyser::Filtered_grains(TriaxialState& state)
	{
		long                         ng1 = 0;
		TriaxialState::GrainIterator gend = state.grains_end();
		for (TriaxialState::GrainIterator git = state.grains_begin(); git != gend; ++git) {
			if (state.inside(git->sphere.point())) ++ng1;
		}
		return ng1;
	}

	Real KinematicLocalisationAnalyser::Filtered_volume(TriaxialState& /*state*/) { return 0; }

	Real KinematicLocalisationAnalyser::Contact_coordination(TriaxialState& state) { return Filtered_contacts(state) / Filtered_grains(state); }

	Real KinematicLocalisationAnalyser::Neighbor_coordination(TriaxialState& state) { return Filtered_neighbors(state) / Filtered_grains(state); }


	Tenseur_sym3 KinematicLocalisationAnalyser::Neighbor_fabric(TriaxialState& state)
	{
		RTriangulation& T = state.tesselation().Triangulation();
		Edge_iterator   ed_end = T.edges_end();
		Tenseur_sym3    Tens;
		CVector         v;
		Segment         s;
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != ed_end; ++ed_it) {
			if (!T.is_infinite(*ed_it)) {
				s = T.segment(*ed_it);
				if (state.inside(s.source()) && state.inside(s.target())) {
					v = T.segment(*ed_it).to_vector() * (1 / sqrt(T.segment(*ed_it).squared_length()));
					for (int i = 1; i < 4; i++)
						for (int j = 3; j >= i; j--)
							Tens(i, j) += 2 * v[i - 1] * v[j - 1];
				} else if (state.inside(s.source()) || state.inside(s.target())) {
					v = T.segment(*ed_it).to_vector() * (1 / sqrt(T.segment(*ed_it).squared_length()));
					for (int i = 1; i < 4; i++)
						for (int j = 3; j >= i; j--)
							Tens(i, j) += v[i - 1] * v[j - 1];
				}
			}
		}
		Tens /= Filtered_neighbors(state);
		return Tens;
	}

	Tenseur_sym3 KinematicLocalisationAnalyser::Contact_fabric(TriaxialState& state)
	{
		Tenseur_sym3                   Tens;
		CVector                        v;
		TriaxialState::ContactIterator cend = state.contacts_end();

		for (TriaxialState::ContactIterator cit = state.contacts_begin(); cit != cend; ++cit) {
			if (state.inside((*cit)->grain1->sphere.point()) && state.inside((*cit)->grain2->sphere.point())) {
				v = (*cit)->normal;
				for (int i = 1; i < 4; i++)
					for (int j = 3; j >= i; j--)
						Tens(i, j) += 2 * v[i - 1] * v[j - 1];
			} else if (state.inside((*cit)->grain1->sphere.point()) || state.inside((*cit)->grain2->sphere.point())) {
				v = (*cit)->normal;
				for (int i = 1; i < 4; i++)
					for (int j = 3; j >= i; j--)
						Tens(i, j) += v[i - 1] * v[j - 1];
			}
		}
		Tens /= Filtered_contacts(state);
		return Tens;
	}

	Real KinematicLocalisationAnalyser::Contact_anisotropy(TriaxialState& state)
	{
		Tenseur_sym3 tens(Contact_fabric(state));
		return tens.Deviatoric().Norme() / tens.Trace();
	}

	Real KinematicLocalisationAnalyser::Neighbor_anisotropy(TriaxialState& state)
	{
		Tenseur_sym3 tens(Neighbor_fabric(state));
		return tens.Deviatoric().Norme() / tens.Trace();
	}

	vector<pair<Real, Real>>& KinematicLocalisationAnalyser::NormalDisplacementDistribution(vector<Edge_iterator>& edges, vector<pair<Real, Real>>& row)
	{
		//cerr << "n_debug=" << n_debug++ << endl;   /// DEBUG LINE  ///
		row.clear();
		row.resize(linear_discretisation + 1);
		vector<Real> Un_values;
		Un_values.resize(edges.size());
		Real                            UNmin(100000), UNmax(-100000);
		CVector                         branch, U;
		Real                            Un;
		Vertex_handle                   Vh1, Vh2;
		vector<Edge_iterator>::iterator ed_end = edges.end();
		long                            val_count = 0;

		//cerr << "n_debug=" << n_debug++ << endl;   /// DEBUG LINE  ///
		for (vector<Edge_iterator>::iterator ed_it = edges.begin(); ed_it != ed_end; ++ed_it) {
			Vh1 = (*ed_it)->first->vertex((*ed_it)->second);
			Vh2 = (*ed_it)->first->vertex((*ed_it)->third);
			branch = Vh1->point().point() - Vh2->point().point();
			NORMALIZE(branch);
			if (consecutive) U = TS1->grain(Vh1->info().id()).translation - TS1->grain(Vh2->info().id()).translation;
			else {
				U = (TS1->grain(Vh1->info().id()).sphere.point() - TS0->grain(Vh1->info().id()).sphere.point())
				        - (TS1->grain(Vh2->info().id()).sphere.point() - TS0->grain(Vh2->info().id()).sphere.point());
			}
			//Un = (U - (Delta_epsilon*branch))*branch; //Diff�rence par rapport � Un moyen
			Un = U * branch;

			UNmin = min(UNmin, Un);
			UNmax = max(UNmax, Un);
			Un_values[val_count++] = Un;
			//cerr << "Un=" << Un << " U=" << U << " branch=" << branch <<  endl;
		}
		//cerr << "n_debug=" << n_debug++ << endl;   /// DEBUG LINE  ///

		Real DUN = (UNmax - UNmin) / linear_discretisation;
		for (int i = 0; i <= linear_discretisation; ++i) {
			row[i].first = UNmin + (i + 0.5) * DUN;
			row[i].second = 0;
		}
		//cerr << "n_debug=" << n_debug++ << endl;   /// DEBUG LINE  ///

		val_count = val_count - 1;
		//cerr << "nval=" << val_count << " reserved=" << edges.size() << endl;
		for (; val_count >= 0; --val_count) {
			//cerr << "n_debug0=" << n_debug << endl;   /// DEBUG LINE  ///
			row[(int)((Un_values[val_count] - UNmin) / DUN)].second += 1;
		}
		//cerr << "DUN=" << DUN << " UNmin=" << UNmin << " UNmax=" << UNmax << endl;
		return row;
		//cerr << "n_debug=" << n_debug++ << endl;   /// DEBUG LINE  ///
	}


	ofstream& KinematicLocalisationAnalyser::NormalDisplacementDistributionToFile(vector<Edge_iterator>& edges, ofstream& output_file)
	{
		vector<pair<Real, Real>> row;
		NormalDisplacementDistribution(edges, row);
		vector<pair<Real, Real>>::iterator r_end = row.end();

		//output part :
		output_file << "#Normal displacement distribution" << endl
		            << "eps3=" << Delta_epsilon(3, 3) << " eps2=" << Delta_epsilon(2, 2) << " eps1=" << Delta_epsilon(1, 1)
		            << " number of neigbors: " << edges.size() << endl
		            << "Un_min=" << 1.5 * row[0].first - 0.5 * row[1].first << " Un_max=" << row[row.size() - 1].first << endl;
		cout << "#Normal displacement distribution" << endl
		     << "eps3=" << Delta_epsilon(3, 3) << " eps2=" << Delta_epsilon(2, 2) << " eps1=" << Delta_epsilon(1, 1)
		     << " number of neigbors: " << edges.size() << endl
		     << "Un_min=" << 1.5 * row[0].first - 0.5 * row[1].first << " Un_max=" << row[row.size() - 1].first << endl;
		for (vector<pair<Real, Real>>::iterator r_it = row.begin(); r_it != r_end; ++r_it) {
			output_file << r_it->first << " " << r_it->second << endl;
			cout << r_it->first << " " << r_it->second << endl;
		}
		output_file << endl;
		return output_file;
	}

	ofstream& KinematicLocalisationAnalyser::ContactDistributionToFile(ofstream& output_file)
	{
		//cerr << "ContactDistributionToFile" << endl;
		vector<pair<Real, Real>> row;
		row.resize(sphere_discretisation + 1);
		Real DZ = 1.0 / sphere_discretisation; //interval in term of cos(teta)
		long nc1 = 0;
		long nc2 = 0;
		long ng1 = 0;
		long ng2 = 0;
		//cerr << "ContactDistributionToFile05" << endl;
		TriaxialState::ContactIterator cend = (*TS1).contacts_end();
		TriaxialState::GrainIterator   gend = (*TS1).grains_end();

		for (int i = 0; i <= sphere_discretisation; ++i) {
			row[i].first = (i + 0.5) * DZ;
			row[i].second = 0;
		}

		for (TriaxialState::GrainIterator git = (*TS1).grains_begin(); git != gend; ++git) {
			if ((*TS1).inside(git->sphere.point())) ++ng1;
			else
				++ng2;
		}

		for (TriaxialState::ContactIterator cit = (*TS1).contacts_begin(); cit != cend; ++cit) {
			if ((*TS1).inside((*cit)->grain1->sphere.point()) && (*TS1).inside((*cit)->grain2->sphere.point())) {
				row[(int)(math::abs((*cit)->normal.z()) / DZ)].second += 2;
				nc1 += 2;
			} else {
				if ((*TS1).inside((*cit)->grain1->sphere.point()) || (*TS1).inside((*cit)->grain2->sphere.point())) {
					row[(int)(math::abs((*cit)->normal.z()) / DZ)].second += 1;
					++nc1;
				}
				//cerr << "(*cit)->normal.z(),DZ : " << (*cit)->normal.z() << " " << DZ << endl;}
				else
					++nc2;
			}
		}
		//normalisation :
		Real normalize = 1.0 / (ng1 * 4 * DZ * 3.141592653);
		for (int i = 0; i <= sphere_discretisation; ++i)
			row[i].second *= normalize;

		//output part :
		output_file << "#Contacts distribution" << endl
		            << "(filter dist. = " << (*TS1).filter_distance << ", " << nc1 << " contacts, " << nc2 << " excluded contacts, for " << ng1 << "/"
		            << (ng1 + ng2) << " grains)" << endl;
		output_file << "max_nz number_of_contacts" << endl;
		cerr << "#Contacts distribution (filter dist. = " << (*TS1).filter_distance << ", " << nc1 << " contacts, " << nc2 << " excluded contacts, for "
		     << ng1 << "/" << (ng1 + ng2) << " grains)" << endl;
		cerr << "mean_nz number_of_contacts" << endl;
		for (int i = 0; i <= sphere_discretisation; ++i) {
			output_file << row[i].first << " " << row[i].second << endl;
			cerr << row[i].first << " " << row[i].second << endl;
		}
		output_file << endl;
		return output_file;
	}


	ofstream& KinematicLocalisationAnalyser::AllNeighborDistributionToFile(ofstream& output_file)
	{
		vector<pair<Real, Real>> row;
		row.resize(sphere_discretisation);
		Real DZ = 1.0 / sphere_discretisation;
		long nv1 = 0;
		long nv2 = 0;
		long nv3 = 0;
		long ng1 = 0;
		long ng2 = 0;

		for (int i = 0; i < sphere_discretisation; ++i) {
			row[i].first = (i + 0.5) * DZ;
			row[i].second = 0;
		}

		TriaxialState::GrainIterator gend = (*TS1).grains_end();
		for (TriaxialState::GrainIterator git = (*TS1).grains_begin(); git != gend; ++git) {
			if ((*TS1).inside(git->sphere.point())) ++ng1;
			else
				++ng2;
		}

		RTriangulation& T = (*TS1).tesselation().Triangulation();
		Segment         s;
		CVector         v;
		for (Edge_iterator ed_it = T.edges_begin(); ed_it != T.edges_end(); ed_it++) {
			if (!T.is_infinite(*ed_it)) {
				s = T.segment(*ed_it);
				if ((*TS1).inside(s.source()) && (*TS1).inside(s.target())) {
					v = s.to_vector();
					row[(int)(math::abs(v.z() / sqrt(s.squared_length())) / DZ)].second += 2;
					nv1 += 2;
				} else {
					if ((*TS1).inside(s.source()) || (*TS1).inside(s.target())) {
						v = s.to_vector();
						row[(int)(math::abs(v.z() / sqrt(s.squared_length())) / DZ)].second += 1;
						++nv1;
					} else
						++nv2;
				}
			} else
				++nv3;
		}

		Real normalize = 1.0 / (ng1 * 4 * DZ * 3.141592653);
		for (int i = 0; i < sphere_discretisation; ++i)
			row[i].second *= normalize;

		output_file << "#Neighbors distribution" << endl
		            << "(filter dist. = " << (*TS1).filter_distance << ", " << nv1 << " neighbors + " << nv2 << " excluded + " << nv3
		            << " infinite, for " << ng1 << "/" << (ng1 + ng2) << " grains)" << endl;
		output_file << "max_nz number_of_neighbors" << endl;
		cerr << "#Neighbors distribution" << endl
		     << "(filter dist. = " << (*TS1).filter_distance << ", " << nv1 << " neighbors + " << nv2 << " excluded + " << nv3 << " infinite, for "
		     << ng1 << "/" << (ng1 + ng2) << " grains)" << endl;
		cerr << "mean_nz number_of_neighbors" << endl;
		for (int i = 0; i < sphere_discretisation; ++i) {
			output_file << row[i].first << " " << row[i].second << endl;
			cerr << row[i].first << " " << row[i].second << endl;
		}
		output_file << endl;
		return output_file;
	}

	void KinematicLocalisationAnalyser::SetForceIncrements(
	        void) //WARNING : This function will modify the contact lists : add virtual (lost)) contacts in state 1 and modify old_force and force in state 0, execute this function after all other force analysis functions if you want to avoid problems
	{
		//if (true) cerr << "SetForceIncrements"<< endl;
		//  vector< pair<Real, Real> > row;
		//  row.resize ( sphere_discretisation );
		//  Real DZ = 1.0/sphere_discretisation;
		long Nc0 = TS0->contacts.size();
		long Nc1 = TS1->contacts.size();
		n_persistent = 0;
		n_new = 0;
		n_lost = 0;
		long lost_in_state0 = 0;

		for (int i = 0; i < Nc0; ++i) {
			TS0->contacts[i]->visited = false;
			if (TS0->contacts[i]->status == TriaxialState::Contact::LOST) ++lost_in_state0;
		}
		for (int i = 0; i < Nc1; ++i)
			TS1->contacts[i]->visited = false;
		//cerr << "Nc1 "<<Nc1<<", Nc0 "<<Nc0<<" ("<<Nc0-lost_in_state0<<" real)"<<endl;
		for (int i = 0; i < Nc0; ++i) {
			// cerr << 1;
			if (TS0->contacts[i]->status != TriaxialState::Contact::LOST) {
				//  cerr << 2;
				for (int j = 0; j < Nc1; ++j) {
					if (TS0->contacts[i]->grain1->id == TS1->contacts[j]->grain1->id
					    && TS0->contacts[i]->grain2->id
					            == TS1->contacts[j]->grain2->id) { // This is a PERSISTENT contact (i.e. it is present in state 0 and 1)
						//TS0->contacts[i]->visited = true;
						TS1->contacts[j]->visited = true;
						//TS0->contacts[i]->status = TriaxialState::Contact::PERSISTENT;
						TS1->contacts[j]->status = TriaxialState::Contact::PERSISTENT;
						TS1->contacts[j]->old_fn = TS0->contacts[i]->fn;
						TS1->contacts[j]->old_fs = TS0->contacts[i]->fs;
						++n_persistent;
						break;
					} else if (j + 1 == Nc1) { //This contact was not found in state 1, add it as a LOST contact
						//    cerr << 3 << endl;
						TriaxialState::Contact* c = new TriaxialState::Contact;
						c->visited = true;
						c->status = TriaxialState::Contact::LOST;
						c->grain1 = TS0->contacts[i]->grain1;
						c->grain2 = TS0->contacts[i]->grain2;
						c->position = TS0->contacts[i]->position;
						c->normal = TS0->contacts[i]->normal;
						c->old_fn = TS0->contacts[i]->fn;
						c->fn = 0;
						c->old_fs = TS0->contacts[i]->fs;
						c->frictional_work = TS0->contacts[i]->frictional_work;
						c->fs = CGAL::NULL_VECTOR;
						TS1->contacts.push_back(c);
						++Nc1;
						++n_lost;
						break;
					}
				}
			}
		}
		//cerr << 4;
		for (int j = 0; j < Nc1; ++j) { //This contact was not visited, it is a NEW one
			//cerr << 5;
			if (!TS1->contacts[j]->visited /*&& TS1->contacts[j]->status != TriaxialState::Contact::LOST*/) {
				//cerr << 6;
				TS1->contacts[j]->status = TriaxialState::Contact::NEW;
				TS1->contacts[j]->old_fn = 0;
				TS1->contacts[j]->old_fs = CGAL::NULL_VECTOR;
				++n_new;
			}
		}
	}

	void KinematicLocalisationAnalyser::SetDisplacementIncrements(void)
	{
		TriaxialState::GrainIterator gend = TS1->grains_end();
		for (TriaxialState::GrainIterator git = TS1->grains_begin(); git != gend; ++git)
			if (git->id >= 0)
				git->translation = TS0->grain(git->id).translation = TS1->grain(git->id).sphere.point() - TS0->grain(git->id).sphere.point();
		consecutive = true;
	}

	ofstream& KinematicLocalisationAnalyser::StrictNeighborDistributionToFile(ofstream& output_file) { return output_file; }

	CVector KinematicLocalisationAnalyser::Deplacement(Finite_cells_iterator cell, int facet)
	{
		CVector v(0.f, 0.f, 0.f);
		int     id;
		for (int i = 0; i < 4; i++)
			if (i != facet) {
				id = cell->vertex(i)->info().id();
				v = v + (TS1->grain(id).sphere.point() - TS0->grain(id).sphere.point());
			}
		v = v / 3;
		return v;
	}

	void KinematicLocalisationAnalyser::Grad_u(Finite_cells_iterator cell, int facet, CVector& V, Tenseur3& T)
	{
		CVector S = cross_product(
		                    (cell->vertex(l_vertices[facet][1])->point().point()) - (cell->vertex(l_vertices[facet][0])->point().point()),
		                    (cell->vertex(l_vertices[facet][2])->point().point()) - (cell->vertex(l_vertices[facet][1])->point().point()))
		        / 2.f;

		Somme(T, V, S);
	}

	void KinematicLocalisationAnalyser::Grad_u(Finite_cells_iterator cell, Tenseur3& T, bool vol_divide) // Gradient of displacement
	{
		T.reset();
		CVector v;
		for (int facet = 0; facet < 4; facet++) {
			v = Deplacement(cell, facet);
			Grad_u(cell, facet, v, T);
		}
		if (vol_divide) T /= Tesselation::Volume(cell);
	}

	const vector<Tenseur3>& KinematicLocalisationAnalyser::computeParticlesDeformation(void)
	{
		Tesselation&    Tes = TS0->tesselation();
		RTriangulation& Tri = Tes.Triangulation();
		Tenseur3        grad_u;
		Real            v;
		v_total = 0;
		v_solid_total = 0;
		grad_u_total.reset();
		v_total_g = 0;
		grad_u_total_g.reset();
		Delta_epsilon(3, 3) = TS1->eps3 - TS0->eps3;
		Delta_epsilon(1, 1) = TS1->eps1 - TS0->eps1;
		Delta_epsilon(2, 2) = TS1->eps2 - TS0->eps2;
		vector<Real> volumeWeight(Tes.Max_id() + 1, 0);

		//compute Voronoi tesselation (i.e. voronoi center of each cell)
		if (!Tes.computed) Tes.compute();
		if (ParticleDeformation.size() != (unsigned int)(Tes.Max_id() + 1)) {
			ParticleDeformation.clear();
			ParticleDeformation.resize(Tes.Max_id() + 1);
		}
		//reset volumes and tensors of each particle
		n_real_vertices = 0;
		n_fictious_vertices = 0;
		for (RTriangulation::Finite_vertices_iterator V_it = Tri.finite_vertices_begin(); V_it != Tri.finite_vertices_end(); V_it++) {
			ParticleDeformation[V_it->info().id()].reset();
			if (!V_it->info().isFictious) ++n_real_vertices;
			else
				++n_fictious_vertices;
		}
		Finite_cells_iterator cell = Tri.finite_cells_begin();
		Finite_cells_iterator cell0 = Tri.finite_cells_end();

		//compute grad_u and volumes of all cells in the triangulation, and assign them to each of the vertices ( volume*grad_u is added here rather than grad_u, the weighted average is computed later )
		//define the number of non-fictious cells, i.e. not in contact with a boundary
		n_real_cells = 0;
		for (; cell != cell0; cell++) {
			cell->info().isFictious
			        = (cell->vertex(0)->info().isFictious || cell->vertex(1)->info().isFictious || cell->vertex(2)->info().isFictious
			           || cell->vertex(3)->info().isFictious);
			if (!cell->info().isFictious) {
				Grad_u(cell,
				       grad_u,
				       false); // false : don't divide by volume, here grad_u = volume of cell * average grad_u in cell, the final value is divided by the total volume later (see below)
				v = Tri.tetrahedron(cell).volume();
				grad_u_total += grad_u;
				v_total += v;
				++n_real_cells;
				for (unsigned int index = 0; index < 4; index++) {
					volumeWeight[cell->vertex(index)->info().id()] += v;
					ParticleDeformation[cell->vertex(index)->info().id()] += grad_u;
				}
			}
		}
		//Do we delete volume and grad_u for particles on the border?
		//  Tesselation::Vector_Vertex border_vertices;
		//  Tes.Voisins(Tri.infinite_vertex(), border_vertices);
		//  unsigned int l = border_vertices.size();
		//  for (unsigned int i=0; i<l; ++i) {
		//   //cerr << "border " << i << endl;
		//   border_vertices[i]->info().v() =0;
		//
		//   ParticleDeformation[border_vertices[i]->info().id()]=NULL_TENSEUR3;
		//  }

		//Divide sum(v*grad_u) by sum(v) to get the average grad_u on each particle
		for (RTriangulation::Finite_vertices_iterator V_it = Tri.finite_vertices_begin(); V_it != Tri.finite_vertices_end(); V_it++) {
			v_total_g += volumeWeight[V_it->info().id()];
			v_solid_total += 4.188790 * pow(V_it->point().weight(), 1.5); //4.18... = 4/3*PI; and here, weight is rad²
			grad_u_total_g += ParticleDeformation[V_it->info().id()];
			if (volumeWeight[V_it->info().id()] > 0) ParticleDeformation[V_it->info().id()] /= volumeWeight[V_it->info().id()];
		}
		grad_u_total_g /= v_total_g;
		return ParticleDeformation;
	}

	Real KinematicLocalisationAnalyser::computeMacroPorosity(void) { return (1 - v_solid_total / (TS1->haut * TS1->larg * TS1->prof)); }

} // namespace CGT
} // namespace yade