File: TriaxialState.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 (324 lines) | stat: -rw-r--r-- 11,070 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
/*************************************************************************
*  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. *
*************************************************************************/

/// The info types that will be used and the namespace in which the types will be defined (a unique namespace with different info types would give name conflicts)

#include "TriaxialState.h"
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>

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

namespace CGT {

	using std::ifstream;
	using std::string; // inside .cpp file it does not leak.

	TriaxialState::TriaxialState(void)
	        : NO_ZERO_ID(false)
	        , filter_distance(-0.1)
	        , maxId(-1)
	        , tesselated(false)
	{
	}

	TriaxialState::~TriaxialState(void)
	{
		ContactIterator last = contacts_end();
		for (ContactIterator it = contacts_begin(); it != last; ++it) {
			if (*it) delete *it;
		}
	}

	Real TriaxialState::find_parameter(const char* parameter_name, ifstream& file)
	{
		string buffer;
		Real   value;
		file >> buffer;
		bool test = (buffer == string(parameter_name));
		while (!test) {
			buffer.clear();
			file >> buffer;
			test = (buffer == string(parameter_name) || file.eof());
		}
		if (!file.eof()) file >> value;
		else
			value = 0;
		// 	cout << string(parameter_name) << value << endl;
		return value;
	}

	Real TriaxialState::find_parameter(const char* parameter_name, boost::iostreams::filtering_istream& file)
	{
		string buffer;
		Real   value;
		file >> buffer;
		bool test = (buffer == string(parameter_name));
		while (!test) {
			buffer.clear();
			file >> buffer;
			test = (buffer == string(parameter_name) || file.eof());
		}
		if (!file.eof()) file >> value;
		else
			value = 0;
		// 	cout << string(parameter_name) << value << endl;
		return value;
	}

	Real TriaxialState::find_parameter(const char* parameter_name, const char* filename)
	{
		ifstream statefile(filename);
		return find_parameter(parameter_name, statefile);
	}


	void TriaxialState::reset(void)
	{
		tesselated = false;
		Tes.Clear();
		mean_radius = 0;
		grains.clear();
		ContactIterator contacts_end = contacts.end();
		for (ContactIterator it = contacts.begin(); it != contacts_end; ++it)
			delete *it;
		contacts.clear();
		contacts.resize(0);
		box.base = Point(1.0e10, 1.0e10, 1.0e10);
		box.sommet = Point(-1.0e10, -1.0e10, -1.0e10);
	}

	TriaxialState::GrainIterator TriaxialState::grains_begin(void)
	{
		GrainIterator git = grains.begin();
		if (NO_ZERO_ID) return ++git;
		else
			return git;
	}

	TriaxialState::ContactIterator TriaxialState::contacts_begin(void) { return contacts.begin(); }

	TriaxialState::GrainIterator TriaxialState::grains_end(void) { return grains.end(); }

	TriaxialState::ContactIterator TriaxialState::contacts_end(void) { return contacts.end(); }

	TriaxialState::Grain& TriaxialState::grain(unsigned int id) { return grains[id]; }

	TriaxialState::Tesselation& TriaxialState::Tesselate(void)
	{
		if (!tesselated) {
			Tes.Clear();
			GrainIterator git = grains_begin();
			GrainIterator last = grains_end();
			Tes.vertexHandles.resize(grains.size() + (NO_ZERO_ID ? 1 : 0));
			for (; git != last; ++git) {
				if (git->id != -1 /*&& git->isSphere*/)
					Tes.vertexHandles[git->id]
					        = Tes.insert(git->sphere.x(), git->sphere.y(), git->sphere.z(), git->sphere.weight(), git->id, !git->isSphere);
				//vh->->info() = git->translation; FIXME : this could define displacements in the triangulation itself
				//			cerr << "Tes.insert(git->sphere.x(), git->sphere.y(), git->sphere.z(), git->sphere.weight(), git->id);" << endl;
			}
			tesselated = true;
		}
		return Tes;
	}

	TriaxialState::Tesselation& TriaxialState::tesselation(void) { return Tesselate(); }

	bool TriaxialState::inside(Real x, Real y, Real z)
	{
		return (x >= (box.base.x() + filter_distance * mean_radius) && x <= (box.sommet.x() - filter_distance * mean_radius)
		        && y >= (box.base.y() + filter_distance * mean_radius) && y <= (box.sommet.y() - filter_distance * mean_radius)
		        && z >= (box.base.z() + filter_distance * mean_radius) && z <= (box.sommet.z() - filter_distance * mean_radius));
	}

	bool TriaxialState::inside(CVector v) { return TriaxialState::inside(v.x(), v.y(), v.z()); }

	bool TriaxialState::inside(Point p) { return TriaxialState::inside(p.x(), p.y(), p.z()); }

	bool TriaxialState::from_file(const char* filename, bool bz2)
	{
		reset();

		//	// Don't use bzipped files
		//	// 	cout << filename << endl;
		//	ifstream Statefile(filename);
		// 	if (!Statefile.is_open()) {
		// 		cout << "Error opening files";
		// 		return false;
		// 	}

		//	// Use bzipped files

		boost::iostreams::filtering_istream Statefile;
		if (bz2) {
			Statefile.push(boost::iostreams::bzip2_decompressor());
			Statefile.push(boost::iostreams::file_source(string(filename) + ".bz2"));
		} else
			Statefile.push(boost::iostreams::file_source(string(filename)));
		if (!Statefile.good()) {
			cerr << "Error opening files";
			return false;
		}

		long Idg;
		long Ns = 0; //number of spheres (excluding fictious ones))
		Statefile >> Ng;
		//Real x, y, z, rad; //coordonn�es/rayon
		//Real tx, ty, tz;
		Point pos(CGAL::ORIGIN);
		mean_radius = 0;
		CVector trans, rot;
		Real    rad; //coordonn�es/rayon
		bool    isSphere;


		grains.resize(Ng + 1);
		//cout << "Ngrains =" << Ng << endl;
		if (NO_ZERO_ID) {
			GrainIterator git = grains.begin();
			git->id = 0;
			git->sphere = Sphere(CGAL::ORIGIN, 0);
			git->translation = CGAL::NULL_VECTOR;
			git->rotation = CGAL::NULL_VECTOR;
		}

		long i = NO_ZERO_ID ? 1 : 0;

		using math::max; // when used inside function it does not leak - it is safe.
		using math::min;

		for (; i <= Ng; ++i) {
			Statefile >> Idg >> pos >> rad >> trans >> rot >> isSphere;
			maxId = max(maxId, Idg);
			grains[Idg].id = Idg;
			grains[Idg].sphere = Sphere(pos, rad);
			grains[Idg].translation = trans;
			grains[Idg].rotation = rot;
			grains[Idg].isSphere = isSphere;
			box.base = Point(min(box.base.x(), pos.x() - rad), min(box.base.y(), pos.y() - rad), min(box.base.z(), pos.z() - rad));
			box.sommet = Point(max(box.sommet.x(), pos.x() + rad), max(box.sommet.y(), pos.y() + rad), max(box.sommet.z(), pos.z() + rad));
			if (isSphere) {
				mean_radius += grains[Idg].sphere.weight();
				++Ns;
			}
			//cout << "Idg: "<< Idg << " sphere: " << grains[Idg].sphere << " trans: " << grains[Idg].translation << endl;
		}
		mean_radius /= Ns; //rayon moyen
		//cout << filename << " loaded : " << Ng << " grains with mean radius = " << mean_radius << endl;

		long    id1, id2;
		int     stat;
		CVector c_pos, normal, old_fs, fs;
		Real    old_fn, fn, frictional_work;
		Statefile >> Nc;
		contacts.resize(Nc);

		for (long j = 0; j < Nc; ++j) {
			Contact* c = new Contact;
			Statefile >> id1 >> id2 >> normal >> c_pos >> old_fn >> old_fs >> fn >> fs >> frictional_work >> stat;

			normal = (grains[id2].sphere.point() - grains[id1].sphere.point());
			normal = normal / sqrt(pow(normal.x(), 2) + pow(normal.y(), 2) + pow(normal.z(), 2));
			c->grain1 = &(grains[id1]);
			c->grain2 = &(grains[id2]);
			grains[id1].contacts.push_back(c);
			grains[id2].contacts.push_back(c);
			c->normal = normal;
			c->position = c_pos;
			c->old_fn = old_fn;
			c->old_fs = old_fs;
			c->fn = fn;
			c->fs = fs;
			c->frictional_work = frictional_work;
			c->status = (Contact::Status)stat;
			if (contacts[j]) delete contacts[j];
			contacts[j] = c;
		}

		//cout << "c_pos=" << contacts[10]->position << " old_fn=" << contacts[10]->old_fn << " normal=" << contacts[10]->normal << endl;
		//rfric = find_parameter("rfric=", Statefile);// � remettre quand les fichiers n'auront plus l'espace de trop...
		Eyn = find_parameter("Eyn", Statefile);
		Eys = find_parameter("Eys", Statefile);
		wszzh = find_parameter("wszzh", Statefile);
		wsxxd = find_parameter("wsxxd", Statefile);
		wsyyfa = find_parameter("wsyyfa", Statefile);
		eps3 = find_parameter("eps3", Statefile);
		eps1 = find_parameter("eps1", Statefile);
		eps2 = find_parameter("eps2", Statefile);
		porom = find_parameter("porom", Statefile);
		haut = find_parameter("haut", Statefile);
		larg = find_parameter("larg", Statefile);
		prof = find_parameter("prof", Statefile);
		ratio_f = find_parameter("ratio_f", Statefile);
		vit = find_parameter("vit", Statefile);

		// 	//Don't use bzipped files
		// 	Statefile.close();


		//cout << endl << "wszzh= " << wszzh << endl;

		/*GrainIterator grains_end = grains.end();
	for (GrainIterator it=grains.begin(); it!=grains_end; ++it)
	{
	 if (it==grains.begin()) ++it;
	 Vue1.Dessine_Sphere(it->sphere.x(), it->sphere.y(), it->sphere.z(), it->sphere.weight(), 10);
	}*/
		//Vue1.Affiche();

		return true;
	}

	bool TriaxialState::to_file(const char* filename, bool bz2)
	{
		//string fname (filename);

		// use bzipped files
		boost::iostreams::filtering_ostream Statefile;

		//if(boost::algorithm::ends_with(filename,".bz2")) Statefile.push(iostreams::bzip2_compressor());
		if (bz2) {
			Statefile.push(boost::iostreams::bzip2_compressor());
			Statefile.push(boost::iostreams::file_sink(string(filename) + ".bz2"));
		} else
			Statefile.push(boost::iostreams::file_sink(string(filename)));
		// Don't use bzipped files
		//ofstream Statefile (filename);
		//if (!Statefile.is_open())	{
		if (!Statefile.good()) {
			cerr << "Error opening files";
			return false;
		}

		long Id_max = grains.size() - 1;
		Statefile << Id_max << endl;
		for (long Idg = 0; Idg <= Id_max; ++Idg) {
			Statefile << grains[Idg].id << " " << grains[Idg].sphere.point() << " " << grains[Idg].sphere.weight() << " " << grains[Idg].translation
			          << " " << grains[Idg].rotation << " " << grains[Idg].isSphere << endl;
		}
		long Nc2 = contacts.size();
		Statefile << Nc2 << endl;
		for (long i = 0; i < Nc2; ++i) {
			Statefile << contacts[i]->grain1->id << " " << contacts[i]->grain2->id << " " << contacts[i]->normal << " " << contacts[i]->position
			          << " " << contacts[i]->old_fn << " " << contacts[i]->old_fs << " " << contacts[i]->fn << " " << contacts[i]->fs << " "
			          << contacts[i]->frictional_work << " " << contacts[i]->status << endl;
		}

		Statefile << "Eyn " << Eyn << " Eys " << Eys << " wszzh " << wszzh << " wsxxd " << wsxxd << " wsyyfa " << wsyyfa << " eps3 " << eps3 << " eps1 "
		          << eps1 << " eps2 " << eps2 << " porom " << porom << " haut " << haut << " larg " << larg << " prof " << prof << " ratio_f "
		          << ratio_f << " vit " << vit << endl;
		//	 	//Don't use bzipped files
		//		Statefile.close();
		return true;
	}

} // namespace CGT

} // namespace yade