File: DFNFlow.cpp

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (400 lines) | stat: -rw-r--r-- 20,014 bytes parent folder | download | duplicates (3)
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

/*************************************************************************
*  Copyright (C) 2014 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. *
*************************************************************************/
#ifdef FLOW_ENGINE

#include <pkg/dem/JointedCohesiveFrictionalPM.hpp>
// #include<pkg/dem/ScGeom.hpp>

//keep this #ifdef for commited versions unless you really have stable version that should be compiled by default
//it will save compilation time for everyone else
//when you want it compiled, you can pass -DDFNFLOW to cmake, or just uncomment the following line

//#define DFNFLOW

#ifdef DFNFLOW
#include "FlowEngine_DFNFlowEngineT.hpp"

namespace yade { // Cannot have #include directive inside.

class DFNCellInfo : public FlowCellInfo_DFNFlowEngineT {
public:
	bool crack;
	Real crackArea;
	DFNCellInfo()
	        : crack(false)
	{
	} /// enable to visualize cracked cells in Paraview
	  // 	Real anotherVariable;
	  // 	void anotherFunction() {};

	// 	/// ROBERT
	// 	std::vector<int> faceBreakCount;
	// 	std::vector<int> facetAperture;
	// 	DFNCellInfo(void)
	// 	{
	// 		faceBreakCount.resize(4,0);
	// 		facetAperture.resize(4,0);
	// 	}
	// 	inline std::vector<int>& count(void) {return faceBreakCount;}
	// 	inline std::vector<int>& aperture(void) {return facetAperture;}
};

class DFNVertexInfo : public FlowVertexInfo_DFNFlowEngineT {
public:
	//same here if needed
};

typedef CGT::_Tesselation<CGT::TriangulationTypes<DFNVertexInfo, DFNCellInfo>> DFNTesselation;
#ifdef LINSOLV
class DFNBoundingSphere : public CGT::FlowBoundingSphereLinSolv<DFNTesselation>
#else
class DFNBoundingSphere : public CGT::FlowBoundingSphere<DFNTesselation>
#endif
{
public:
	void saveVtk(const char* folder, bool withBoundaries) //FIXME: withBoundaries does nothing in DFNFlow right now. Needed for consistent templating.
	{
		RTriangulation&     Tri = T[noCache ? (!currentTes) : currentTes].Triangulation();
		static unsigned int number = 0;
		char                filename[250];
		mkdir(folder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		sprintf(filename, "%s/out_%d.vtk", folder, number++);
		int firstReal = -1;

		/// count fictious vertices and cells
		vtkInfiniteVertices = vtkInfiniteCells = 0;
		FiniteCellsIterator cellEnd = Tri.finite_cells_end();
		for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != cellEnd; cell++) {
			bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
			        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
			if (!isDrawable) vtkInfiniteCells += 1;
		}
		for (FiniteVerticesIterator v = Tri.finite_vertices_begin(); v != Tri.finite_vertices_end(); ++v) {
			if (!v->info().isReal()) vtkInfiniteVertices += 1;
			else if (firstReal == -1)
				firstReal = vtkInfiniteVertices;
		}

		basicVTKwritter vtkfile(
		        (unsigned int)Tri.number_of_vertices() - vtkInfiniteVertices, (unsigned int)Tri.number_of_finite_cells() - vtkInfiniteCells);

		vtkfile.open(filename, "test");

		vtkfile.begin_vertices();
		double x, y, z;
		for (FiniteVerticesIterator v = Tri.finite_vertices_begin(); v != Tri.finite_vertices_end(); ++v) {
			if (v->info().isReal()) {
				x = (double)(v->point().point()[0]);
				y = (double)(v->point().point()[1]);
				z = (double)(v->point().point()[2]);
				vtkfile.write_point(x, y, z);
			}
		}
		vtkfile.end_vertices();

		vtkfile.begin_cells();
		for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != Tri.finite_cells_end(); ++cell) {
			bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
			        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
			if (isDrawable) {
				vtkfile.write_cell(
				        cell->vertex(0)->info().id() - firstReal,
				        cell->vertex(1)->info().id() - firstReal,
				        cell->vertex(2)->info().id() - firstReal,
				        cell->vertex(3)->info().id() - firstReal);
			}
		}
		vtkfile.end_cells();

		if (permeabilityMap) {
			vtkfile.begin_data("Permeability", CELL_DATA, SCALARS, FLOAT);
			for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != Tri.finite_cells_end(); ++cell) {
				bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
				        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
				if (isDrawable) { vtkfile.write_data(cell->info().s); }
			}
			vtkfile.end_data();
		} else {
			vtkfile.begin_data("Pressure", CELL_DATA, SCALARS, FLOAT);
			for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != Tri.finite_cells_end(); ++cell) {
				bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
				        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
				if (isDrawable) { vtkfile.write_data(cell->info().p()); }
			}
			vtkfile.end_data();
		}

		if (1) {
			averageRelativeCellVelocity();
			vtkfile.begin_data("Velocity", CELL_DATA, VECTORS, FLOAT);
			for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != Tri.finite_cells_end(); ++cell) {
				bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
				        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
				if (isDrawable) {
					vtkfile.write_data(
					        cell->info().averageVelocity()[0], cell->info().averageVelocity()[1], cell->info().averageVelocity()[2]);
				}
			}
			vtkfile.end_data();
		}

		if (1) {
			vtkfile.begin_data("fracturedCells", CELL_DATA, SCALARS, FLOAT);
			for (FiniteCellsIterator cell = Tri.finite_cells_begin(); cell != Tri.finite_cells_end(); ++cell) {
				bool isDrawable = cell->info().isReal() && cell->vertex(0)->info().isReal() && cell->vertex(1)->info().isReal()
				        && cell->vertex(2)->info().isReal() && cell->vertex(3)->info().isReal();
				if (isDrawable) { vtkfile.write_data(cell->info().crack); }
			}
			vtkfile.end_data();
		}
	}
};

typedef TemplateFlowEngine_DFNFlowEngineT<DFNCellInfo, DFNVertexInfo, DFNTesselation, DFNBoundingSphere> DFNFlowEngineT;
REGISTER_SERIALIZABLE(DFNFlowEngineT);
YADE_PLUGIN((DFNFlowEngineT));
class DFNFlowEngine : public DFNFlowEngineT {
public:
	void trickPermeability(Solver* flow);
	void interpolateCrack(Tesselation& Tes, Tesselation& NewTes);
	void trickPermeability(RTriangulation::Facet_circulator& facet, Real aperture, RTriangulation::Finite_edges_iterator& edge);
	void trickPermeability(RTriangulation::Finite_edges_iterator& edge, Real aperture);
	//	void setPositionsBuffer(bool current);
	Real leakOffRate;
	Real averageAperture;
	Real averageFracturePermeability;
	Real maxAperture;
	Real crackArea;
	bool edgeOnJoint;
	Real getCrackArea() { return crackArea; }
	Real getLeakOffRate() { return leakOffRate; }
	Real getAverageAperture() { return averageAperture; }
	Real getMaxAperture() { return maxAperture; }
	// 	void computeTotalFractureArea(Real totalFractureArea,bool printFractureTotalArea); /// Trying to get fracture's surface
	Real totalFractureArea; /// Trying to get fracture's surface
	                        //	CELL_SCALAR_GETTER(double,.crackArea,crackArea)

	// clang-format off
	YADE_CLASS_BASE_DOC_ATTRS_INIT_CTOR_PY(DFNFlowEngine,DFNFlowEngineT,"This is an enhancement of the FlowEngine for intact and fractured rocks that takes into acount pre-existing discontinuities and bond breakage between particles. The local conductivity around the broken link is calculated according to parallel plates model",
	((Real,jointsResidualAperture,1.e-6,,"residual aperture of joints"))
// 	/// ROBERT
// 	((Real,facetEdgeBreakThreshold,1,,"minimum number of edges that need to be broken for a facet to be tricked"))
	((Real,slotInitialAperture,1.e-6,,"initial aperture of injection slots"))
	((Real,inducedCracksResidualAperture,0.,,"residual aperture of induced cracks"))
	((Real,apertureFactor,1.,,"calibration parameter for fracture conductance to deal with tortuosity"))
	((bool,updatePositions,false,,"update particles positions when rebuilding the mesh (experimental)"))
 	((bool,printFractureTotalArea,false,,"The final fracture area computed through the network")) /// Trying to get fracture's surface
	((bool,calcCrackArea,true,,"The amount of crack per pore is updated if calcCrackArea=True")) /// Trying to get fracture's surface
	,,,
	.def("getCrackArea",&DFNFlowEngine::getCrackArea,(boost::python::arg("id")),"get the cracked area within cell 'id'.")
	.def("getLeakOffRate",&DFNFlowEngine::getLeakOffRate,"report leak-off rate")
    	.def("getAverageAperture",&DFNFlowEngine::getAverageAperture,"report the current average aperture")
    	.def("getMaxAperture",&DFNFlowEngine::getMaxAperture,"report the max aperture")
// 	.def("computeTotalFractureArea",&DFNFlowEngineT::computeTotalFractureArea,"Compute and print the total fracture area of the network") /// Trying to get fracture's surface
// 	.def("trickPermeability",&DFNFlowEngineT::trickPermeability,"measure the mean trickPermeability in the period")
	)
	// clang-format on
	DECLARE_LOGGER;
};
REGISTER_SERIALIZABLE(DFNFlowEngine);
YADE_PLUGIN((DFNFlowEngine));

/// In this version, we never update positions when !updatePositions, i.e. keep triangulating the same positions
//void DFNFlowEngine::setPositionsBuffer(bool current)
//{
//	vector<posData>& buffer = current? positionBufferCurrent : positionBufferParallel;
//	if ( !updatePositions && buffer.size()>0 ) return;
//	buffer.clear();
//	buffer.resize(scene->bodies->size());
//	shared_ptr<Sphere> sph(new Sphere);
//        const int Sph_Index = sph->getClassIndexStatic();
//	FOREACH ( const shared_ptr<Body>& b, *scene->bodies ) {
//                if ( !b || ignoredBody==b->getId() ) continue;
//                posData& dat = buffer[b->getId()];
//		dat.id = b->getId();
//		dat.pos = b->state->pos;
//		dat.isSphere = (b->shape->getClassIndex()==Sph_Index);
//		if (dat.isSphere) dat.radius = YADE_CAST<Sphere*>(b->shape.get())->radius;
//		dat.exists=true;
//	}
//}

/// function allows us to interpolate information about fractured/non fractured cells so we can identify newly fractured cells, monitor half width, and identify fracture tip. We also use the loop to compute leakoff rate
void DFNFlowEngine::interpolateCrack(Tesselation& Tes, Tesselation& NewTes)
{
	RTriangulation&     Tri = Tes.Triangulation();
	RTriangulation&     newTri = NewTes.Triangulation();
	FiniteCellsIterator cellEnd = newTri.finite_cells_end();
#ifdef YADE_OPENMP
	const long size = NewTes.cellHandles.size();
#pragma omp parallel for num_threads(ompThreads > 0 ? ompThreads : 1)
	for (long i = 0; i < size; i++) {
		CellHandle& newCell = NewTes.cellHandles[i];
#else
	FOREACH(CellHandle & newCell, NewTes.cellHandles)
	{
#endif
		CVector center(0, 0, 0);
		if (newCell->info().fictious() == 0)
			for (int k = 0; k < 4; k++)
				center = center + 0.25 * (Tes.vertex(newCell->vertex(k)->info().id())->point().point() - CGAL::ORIGIN);

		CellHandle oldCell = Tri.locate(CGT::Sphere(center[0], center[1], center[2]));
		newCell->info().crack = oldCell->info().crack;
		//		For later commit newCell->info().fractureTip = oldCell->info().fractureTip;
		//		For later commit newCell->info().cellHalfWidth = oldCell->info().cellHalfWidth;

		/// compute leakoff rate by summing the flow through facets abutting non-cracked neighbors
		if (oldCell->info().crack && !oldCell->info().fictious()) {
			Real facetFlowRate = 0;
			facetFlowRate -= oldCell->info().dv();
			for (int k = 0; k < 4; k++) {
				if (!oldCell->neighbor(k)->info().crack) {
					facetFlowRate = oldCell->info().kNorm()[k] * (oldCell->info().shiftedP() - oldCell->neighbor(k)->info().shiftedP());
					leakOffRate += facetFlowRate;
				}
			}
		}
	}
}

void DFNFlowEngine::trickPermeability(RTriangulation::Facet_circulator& facet, Real aperture, RTriangulation::Finite_edges_iterator& ed_it)
{
	const RTriangulation::Facet& currentFacet
	        = *facet; /// seems verbose but facet->first was declaring a junk cell and crashing program (old site, fixed bug https://bugs.launchpad.net/yade/+bug/1666339)
	const RTriangulation& Tri = solver->T[solver->currentTes].Triangulation();
	const CellHandle&     cell1 = currentFacet.first;
	const CellHandle&     cell2 = currentFacet.first->neighbor(facet->second);
	if (Tri.is_infinite(cell1) || Tri.is_infinite(cell2)) cerr << "Infinite cell found in trickPermeability, should be handled somehow, maybe" << endl;

	// 	/// ROBERT
	// 	if (cell1->info().count()[currentFacet.second] < 3){
	// 		cell1->info().count()[currentFacet.second] += 1;
	// 		//cell1->info().aperture()[currentFacet.second] += aperture // used with avgAperture below if desired
	// 	}
	// 	if (!edgeOnJoint && cell1->info().count()[currentFacet.second] < facetEdgeBreakThreshold) return; // only allow facets with 2 or 3 broken edges to be tricked
	// Need to decide aperture criteria! Otherwise it selects the most recent broken edge aperture for facet's perm calc. Here is one possibility:
	//Real avgAperture = cell1->info().aperture()[currentFacet.second]/Real(cell1->info().count()[currentFacet.second]);

	cell1->info().kNorm()[currentFacet.second] = cell2->info().kNorm()[Tri.mirror_index(cell1, currentFacet.second)]
	        = apertureFactor * pow((aperture), 3) / (12 * viscosity);
	/// For vtk recorder:
	cell1->info().crack = 1;
	cell2->info().crack = 1;
	cell2->info().blocked = cell1->info().blocked = cell2->info().Pcondition = cell1->info().Pcondition
	        = false;                                                                /// those ones will be included in the flow problem
	Point&  CellCentre1 = cell1->info();                                            /// Trying to get fracture's surface
	Point&  CellCentre2 = cell2->info();                                            /// Trying to get fracture's surface
	CVector networkFractureLength = CellCentre1 - CellCentre2;                      /// Trying to get fracture's surface
	double  networkFractureDistance = sqrt(networkFractureLength.squared_length()); /// Trying to get fracture's surface
	Real    networkFractureArea = pow(networkFractureDistance, 2);                  /// Trying to get fracture's surface
	totalFractureArea += networkFractureArea;                                       /// Trying to get fracture's surface
	// 	cout <<" ------------------ The total surface area up to here is --------------------" << totalFractureArea << endl;
	// 	printFractureTotalArea = totalFractureArea; /// Trying to get fracture's surface
	if (calcCrackArea) {
		CVector edge = ed_it->first->vertex(ed_it->second)->point().point() - ed_it->first->vertex(ed_it->third)->point().point();
		CVector unitV = edge * (1. / sqrt(edge.squared_length()));
		Point   p3 = ed_it->first->vertex(ed_it->third)->point().point()
		        + unitV * (cell1->info() - ed_it->first->vertex(ed_it->third)->point().point()) * unitV;
		Real halfCrackArea = 0.25 * sqrt(std::abs(cross_product(CellCentre1 - p3, CellCentre2 - p3).squared_length())); //
		cell1->info().crackArea += halfCrackArea;
		cell2->info().crackArea += halfCrackArea;
		crackArea += 2 * halfCrackArea;
	}
}

void DFNFlowEngine::trickPermeability(RTriangulation::Finite_edges_iterator& edge, Real aperture)
{
	const RTriangulation&            Tri = solver->T[solver->currentTes].Triangulation();
	RTriangulation::Facet_circulator facet1 = Tri.incident_facets(*edge);
	RTriangulation::Facet_circulator facet0 = facet1++;
	trickPermeability(facet0, aperture, edge);
	while (facet1 != facet0) {
		trickPermeability(facet1, aperture, edge);
		facet1++;
	}
	/// Needs the fracture surface for this edge?
	// 	double edgeArea = solver->T[solver->currentTes].computeVFacetArea(edge); cout<<"edge area="<<edgeArea<<endl;
}

void DFNFlowEngine::trickPermeability(Solver* flow)
{
	leakOffRate = 0;
	const RTriangulation& Tri = flow->T[solver->currentTes].Triangulation();
	if (!first) interpolateCrack(solver->T[solver->currentTes], flow->T[flow->currentTes]);
	const JCFpmPhys*                       jcfpmphys;
	const shared_ptr<InteractionContainer> interactions = scene->interactions;
	int                                    numberOfCrackedOrJointedInteractions = 0;
	Real                                   SumOfApertures = 0.;
	averageAperture = 0;
	maxAperture = 0;
	crackArea = 0;
	//Real totalFractureArea=0; /// Trying to get fracture's surface
	// 	const shared_ptr<IGeom>& ig;
	// 	const ScGeom* geom; // = static_cast<ScGeom*>(ig.get());
	FiniteEdgesIterator edge = Tri.finite_edges_begin();
	for (; edge != Tri.finite_edges_end(); ++edge) {
		const VertexInfo&              vi1 = (edge->first)->vertex(edge->second)->info();
		const VertexInfo&              vi2 = (edge->first)->vertex(edge->third)->info();
		const shared_ptr<Interaction>& interaction = interactions->find(vi1.id(), vi2.id());

		if (interaction && interaction->isReal()) {
			if (edge->first->info().isFictious) continue; /// avoid trick permeability for fictitious
			jcfpmphys = YADE_CAST<JCFpmPhys*>(interaction->phys.get());

			if (jcfpmphys->isOnJoint || jcfpmphys->isBroken) {
				numberOfCrackedOrJointedInteractions += 1;

				/// here are some workarounds
				Real residualAperture = jcfpmphys->isOnJoint ? jointsResidualAperture
				                                             : inducedCracksResidualAperture; /// inducedCracksResidualAperture=0. by default
				                                                                              // 				/// ROBERT
				                                                                              // 				Real residualAperture;
				                                                                              // 				if (jcfpmphys->isOnJoint){
				                                                                              // 					residualAperture = jointsResidualAperture;
				                                                                              // 					edgeOnJoint = true;
				                                                                              // 				}else{
				// 					residualAperture = inducedCracksResidualAperture;
				// 					edgeOnJoint = false;
				// 				}
				/// for injection slot (different from pre-existing fractures if needed)
				if (jcfpmphys->isOnSlot) { residualAperture = slotInitialAperture; }

				/// avoid trick permeability (permeability=matrix permeability)
				// 				if ( !jcfpmphys->isOnJoint && (residualAperture <= 0.) ) continue;
				if ((jcfpmphys->crackJointAperture <= 0.) && (residualAperture <= 0.)) continue;

				// 				Real aperture = jcfpmphys->crackJointAperture;
				// 				if (aperture < residualAperture) aperture = residualAperture;
				Real aperture = (jcfpmphys->crackJointAperture <= residualAperture) ? residualAperture : jcfpmphys->crackJointAperture;

				// 				cout<<"aperture = " << aperture <<endl;
				// 				if (aperture > valueToDefine) aperture = valueToDefine; /// to avoid problem for large deformations?
				if (aperture <= residualAperture) aperture = residualAperture;
				if (aperture > maxAperture) maxAperture = aperture;
				SumOfApertures += aperture;

				trickPermeability(edge, aperture);
			};
		}
	}
	averageAperture = SumOfApertures / numberOfCrackedOrJointedInteractions; /// DEBUG
	// 	cout << " Average aperture in joint ( -D ) = " << AverageAperture << endl; /// DEBUG
}

// Real DFNFlowEngine::computeTotalFractureArea(totalFractureArea,printFractureTotalArea) /// Trying to get fracture's surface
// {
// 	 if (printFractureTotalArea >0) {
// 		cout<< " The total fracture area computed from the Network is: " << totalFractureArea <<endl;
// 	 }
// }

} // namespace yade

#endif //DFNFLOW
#endif //FLOWENGINE