File: SPHEngine.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 (300 lines) | stat: -rw-r--r-- 8,910 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
#ifdef YADE_SPH
#include "SPHEngine.hpp"
#include <core/Scene.hpp>
#include <pkg/common/Sphere.hpp>
#include <pkg/dem/ViscoelasticPM.hpp>

#include <core/Omega.hpp>
#include <core/State.hpp>

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

void SPHEngine::action()
{
	{
		YADE_PARALLEL_FOREACH_BODY_BEGIN(const shared_ptr<Body>& b, scene->bodies)
		{
			if (mask > 0 && (b->groupMask & mask) == 0) continue;
			this->calculateSPHRho(b);
			b->state->press = math::max(0.0, k * (b->state->rho - b->state->rho0));
		}
		YADE_PARALLEL_FOREACH_BODY_END();
	}
}

void SPHEngine::calculateSPHRho(const shared_ptr<Body>& b)
{
	if (b->state->rho0 < 0) { b->state->rho0 = rho0; }
	if (not b->isClump()) {
		Real rho = 0;

		// Pointer to kernel function
		KernelFunction kernelFunctionCurDensity = returnKernelFunction(KernFunctionDensity, Norm);

		// Calculate rho for every particle
		for (Body::MapId2IntrT::iterator it = b->intrs.begin(), end = b->intrs.end(); it != end; ++it) {
			const shared_ptr<Body> b2 = Body::byId((*it).first, scene);
			Sphere*                s  = dynamic_cast<Sphere*>(b->shape.get());
			if (!s) continue;

			if (((*it).second)->geom and ((*it).second)->phys) {
				const ScGeom     geom = *(YADE_PTR_CAST<ScGeom>(((*it).second)->geom));
				const ViscElPhys phys = *(YADE_PTR_CAST<ViscElPhys>(((*it).second)->phys));

				Real Mass = b2->state->mass;
				if (Mass == 0) Mass = b->state->mass;

				const Real SmoothDist = (b2->state->pos - b->state->pos).norm();

				// [Monaghan1992], (2.7) (3.8)
				rho += b2->state->mass * kernelFunctionCurDensity(SmoothDist, h);
			}
		}
		// Self mass contribution
		rho += b->state->mass * kernelFunctionCurDensity(0.0, h);
		b->state->rho = rho;
	}
}

Real smoothkernelLucy(const double& r, const double& h)
{
	if (r <= h && h > 0) {
		// Lucy Kernel function, [Lucy1977] (27)
		const Real r_h = r / h;
		return 105. / (16. * M_PI * h * h * h) * (1. + 3. * r_h) * math::pow((1. - r_h), 3);
	} else {
		return 0;
	}
}

Real smoothkernelLucyGrad(const double& r, const double& h)
{
	if (r <= h && h > 0) {
		// 1st derivative of Lucy Kernel function, [Lucy1977] (27)
		return 105. / (16. * M_PI * h * h * h) * (-12. * r) * math::pow((h - r), 2) / (h * h * h * h);
	} else {
		return 0;
	}
}

Real smoothkernelLucyLapl(const double& r, const double& h)
{
	if (r <= h && h > 0) {
		// 2nd derivative of Lucy Kernel function, [Lucy1977] (27)
		return 105. / (16. * M_PI * h * h * h) * (-12.) / (h * h * h * h) * (h * h - 2. * r * h + 3. * r * r);
	} else {
		return 0;
	}
}
//=========================================================================

Real smoothkernelBSpline1(const double& r, const double& h)
{
	// BSpline Kernel function, [Monaghan1985] (21)
	if (r <= 2.0 * h && h > 0) {
		const Real coefA = 3. / (2. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (2. / 3. - r_h * r_h + 1. / 2. * r_h * r_h * r_h);
		} else {
			return coefA / 6. * math::pow((2. - r_h), 3);
		}
	} else {
		return 0;
	}
}

Real smoothkernelBSpline1Grad(const double& r, const double& h)
{
	// 1st derivative of BSpline Kernel function, [Monaghan1985] (21)
	if (r <= 2. * h && h > 0) {
		const Real coefA = 3. / (2. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (-r_h) * (2. - 3. / 2. * r_h);
		} else {
			return coefA * (-1. / 2.) * math::pow((2. - r_h), 2);
		}
	} else {
		return 0;
	}
}

Real smoothkernelBSpline1Lapl(const double& r, const double& h)
{
	// 2nd derivative of BSpline Kernel function, [Monaghan1985] (21)
	if (r <= 2.0 * h && h > 0) {
		const Real coefA = 3. / (2. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (-2. + 3. * r_h);
		} else {
			return coefA * (2. - r_h);
		}
	} else {
		return 0;
	}
}

//=========================================================================

Real smoothkernelBSpline2(const double& r, const double& h)
{
	// BSpline Kernel function, [Monaghan1985] (22)
	if (r <= 2.0 * h && h > 0) {
		const Real coefA = 3. / (4. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (10. / 3. - 7. * r_h * r_h + 4 * r_h * r_h * r_h);
		} else {
			return coefA * math::pow((2. - r_h), 2) * ((5. - 4. * r_h) / 3.);
		}
	} else {
		return 0;
	}
}

Real smoothkernelBSpline2Grad(const double& r, const double& h)
{
	// 1st derivative of BSpline Kernel function, [Monaghan1985] (22)
	if (r <= 2.0 * h && h > 0) {
		const Real coefA = 3. / (4. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (-2.) / (h * h) * (7. * r - 6. * r * r_h);
		} else {
			return coefA * 2. / h * (-6. + 7. * r_h - 2. * math::pow(r_h, 2));
		}
	} else {
		return 0;
	}
}

Real smoothkernelBSpline2Lapl(const double& r, const double& h)
{
	// 2nd derivative of BSpline Kernel function, [Monaghan1985] (22)
	if (r <= 2.0 * h && h > 0) {
		const Real coefA = 3. / (4. * M_PI * h * h * h);
		const Real r_h   = r / h;
		if (r <= h) {
			return coefA * (-2.) / (h * h) * (7. - 12. * r_h);
		} else {
			return coefA * 2. / (h * h) * (7. - 4. * r_h);
		}
	} else {
		return 0;
	}
}

//=========================================================================
KernelFunction returnKernelFunction(const int a, const typeKernFunctions typeF) { return returnKernelFunction(a, a, typeF); }

KernelFunction returnKernelFunction(const int a, const int b, const typeKernFunctions typeF)
{
	if (a != b) { throw runtime_error("Kernel types should be equal!"); }
	if (a == Lucy) {
		if (typeF == Norm) {
			return smoothkernelLucy;
		} else if (typeF == Grad) {
			return smoothkernelLucyGrad;
		} else if (typeF == Lapl) {
			return smoothkernelLucyLapl;
		} else {
			KERNELFUNCDESCR
		}
	} else if (a == BSpline1) {
		if (typeF == Norm) {
			return smoothkernelBSpline1;
		} else if (typeF == Grad) {
			return smoothkernelBSpline1Grad;
		} else if (typeF == Lapl) {
			return smoothkernelBSpline1Lapl;
		} else {
			KERNELFUNCDESCR
		}
	} else if (a == BSpline2) {
		if (typeF == Norm) {
			return smoothkernelBSpline2;
		} else if (typeF == Grad) {
			return smoothkernelBSpline2Grad;
		} else if (typeF == Lapl) {
			return smoothkernelBSpline2Lapl;
		} else {
			KERNELFUNCDESCR
		}
	} else {
		KERNELFUNCDESCR
	}
}

bool computeForceSPH(shared_ptr<IGeom>& _geom, shared_ptr<IPhys>& _phys, Interaction* I, Vector3r& force)
{
	const ScGeom& geom  = *static_cast<ScGeom*>(_geom.get());
	Scene*        scene = Omega::instance().getScene().get();
	ViscElPhys&   phys  = *static_cast<ViscElPhys*>(_phys.get());

	const int id1 = I->getId1();
	const int id2 = I->getId2();


	const BodyContainer& bodies = *scene->bodies;

	if (bodies[id1]->isClumpMember() and bodies[id2]->isClumpMember() and bodies[id1]->clumpId == bodies[id2]->clumpId) {
		//If 2 bodies belong to the same clump, do not calculate forces
		force = Vector3r::Zero();
		return true;
	}

	//////////////////////////////////////////////////////////////////
	// Copy-paste
	// Handle periodicity.
	const Vector3r shift2   = scene->isPeriodic ? scene->cell->intrShiftPos(I->cellDist) : Vector3r::Zero();
	const Vector3r shiftVel = scene->isPeriodic ? scene->cell->intrShiftVel(I->cellDist) : Vector3r::Zero();

	const State& de1 = *static_cast<State*>(bodies[id1]->state.get());
	const State& de2 = *static_cast<State*>(bodies[id2]->state.get());

	const Vector3r c1x = (geom.contactPoint - de1.pos);
	const Vector3r c2x = (geom.contactPoint - de2.pos - shift2);

	const Vector3r relativeVelocity = (de1.vel + de1.angVel.cross(c1x)) - (de2.vel + de2.angVel.cross(c2x)) + shiftVel;
	const Real     normalVelocity   = geom.normal.dot(relativeVelocity);

	// Copy-paste
	//////////////////////////////////////////////////////////////////


	const Real Mass1 = bodies[id1]->state->mass;
	const Real Mass2 = bodies[id2]->state->mass;

	const Real Rho1 = bodies[id1]->state->rho;
	const Real Rho2 = bodies[id2]->state->rho;

	const Vector3r xixj = de2.pos - de1.pos;

	if (phys.kernelFunctionCurrentPressure(xixj.norm(), phys.h)) {
		Real fpressure = 0.0;
		if (Rho1 != 0.0 and Rho2 != 0.0) {
			// from [Monaghan1992], (3.3), multiply by Mass2, because we need a force, not du/dt
			fpressure = -Mass1 * Mass2 * (bodies[id1]->state->press / (Rho1 * Rho1) + bodies[id2]->state->press / (Rho2 * Rho2))
			        * phys.kernelFunctionCurrentPressure(xixj.norm(), phys.h);
		}

		Vector3r fvisc = Vector3r::Zero();
		if (Rho1 != 0.0 and Rho2 != 0.0) {
			// from [Morris1997], (22), multiply by Mass2, because we need a force, not du/dt
			fvisc = phys.mu * Mass1 * Mass2 * (-normalVelocity * geom.normal) / (Rho1 * Rho2) * 1 / (xixj.norm())
			        * phys.kernelFunctionCurrentPressure(xixj.norm(), phys.h);
			//phys.kernelFunctionCurrentVisco(xixj.norm(), phys.h);
		}
		force = fpressure * geom.normal + fvisc;
		return true;
	} else {
		return false;
	}
}
YADE_PLUGIN((SPHEngine));

} // namespace yade

#endif