-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_vec_ten.hh
executable file
·580 lines (395 loc) · 14.9 KB
/
p_vec_ten.hh
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
#ifndef P_VEC_TEN_HH_
#define P_VEC_TEN_HH_
#include <cmath>
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////
////////////// PHYSICAL VECTOR (p_vec) /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
template <class T = double> struct p_vec {
// Constructors
p_vec() {
x = 0; y = 0; z = 0;
}
p_vec(T a, T b, T c) {
x = a; y = b; z = c;
}
// Destructor
~p_vec() {}
// Components
T x, y, z;
////////////////////////////////////////////////////////////////////////////////////////////
double length() const {
// Computes the length of the vector.
return sqrt(x * x + y * y + z * z);
}
////////////////////////////////////////////////////////////////////////////////////////////
void invert() {
// Inverts the direction of the vector.
x = - x;
y = - y;
z = - z;
}
////////////////////////////////////////////////////////////////////////////////////////////
void mult_by(double a) {
// Multiplies the vector by an arbitrary number.
x *= a;
y *= a;
z *= a;
}
////////////////////////////////////////////////////////////////////////////////////////////
void norm_to(double a) {
// Normalizes the vector to a given length.
// If a is negative, the vector orientation is inverted and the length is normalized to |a|.
// If the vector has length zero, there will be no change.
double l = length();
if(l != 0) {
x *= a / l;
y *= a / l;
z *= a / l;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
void set_to(T a, T b, T c) {
// Sets the values of the vector explicitly.
x = a;
y = b;
z = c;
}
////////////////////////////////////////////////////////////////////////////////////////////
void reset() {
// Resets the vector to zero.
x = 0;
y = 0;
z = 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec operator-() const {
// Defines the operator - (unary negation).
p_vec temp(*this);
temp.x *= -1;
temp.y *= -1;
temp.z *= -1;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<double> operator+(p_vec<int> op2) const {
// Defines the operator + (difference of two vectors).
p_vec<double> temp(*this);
temp.x += op2.x;
temp.y += op2.y;
temp.z += op2.z;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<double> operator+(p_vec<double> op2) const {
// Defines the operator + (sum of two vectors).
p_vec<double> temp(*this);
temp.x += op2.x;
temp.y += op2.y;
temp.z += op2.z;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec& operator+=(const p_vec op2) {
// Defines the operator += (sum of two vectors).
x += op2.x;
y += op2.y;
z += op2.z;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<double> operator-(p_vec<int> op2) const {
// Defines the operator - (difference of two vectors).
p_vec<double> temp(*this);
temp.x -= op2.x;
temp.y -= op2.y;
temp.z -= op2.z;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<double> operator-(p_vec<double> op2) const {
// Defines the operator - (difference of two vectors).
p_vec<double> temp(*this);
temp.x -= op2.x;
temp.y -= op2.y;
temp.z -= op2.z;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec& operator-=(const p_vec op2) {
// Defines the operator += (sum of two vectors).
x -= op2.x;
y -= op2.y;
z -= op2.z;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
template <class U> double operator*(p_vec<U> op2) const {
// Defines the operator * (scalar product of two vectors).
p_vec temp(*this);
return (temp.x * op2.x + temp.y * op2.y + temp.z * op2.z);
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec operator%(p_vec op2) const {
// Defines the operatort % (cross product of two vectors).
p_vec temp(*this);
p_vec temp2(*this);
temp.x = temp2.y * op2.z - temp2.z * op2.y;
temp.y = temp2.z * op2.x - temp2.x * op2.z;
temp.z = temp2.x * op2.y - temp2.y * op2.x;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec operator/(T op) const {
// Defines the operator / (divide vector by type).
p_vec temp(*this);
temp.x = temp.x / op;
temp.y = temp.y / op;
temp.z = temp.z / op;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec& operator/=(T op) {
// Defines the operator / (divide vector by type).
x /= op;
y /= op;
z /= op;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
/*p_vec operator*(T op) const {
// Defines the operator * (multiplies vector by type)
p_vec temp(*this);
temp.x = temp.x * op;
temp.y = temp.y * op;
temp.z = temp.z * op;
return temp;
}*/
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<double> operator*(double op) const {
// Defines the operator * (multiplies vector by double)
p_vec<double> temp(this->x, this->y, this->z);
temp.x = temp.x * op;
temp.y = temp.y * op;
temp.z = temp.z * op;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec& operator*=(T op) {
// Defines the operator * (multiplies vector by type)
// Note: The syntax must be p_vec * double and NOT double * p_vec.
x *= op;
y *= op;
z *= op;
return *this;
}
};
template <class T> ostream& operator<<(ostream& os, const p_vec<T>& vec) {
// << operator
os << vec.x << " " << vec.y << " " << vec.z;
return os;
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////// PHYSICAL TENSOR RANK 2 (p_ten2) /////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
struct p_ten2 {
// Constructors
p_ten2() {
xx = 0.; xy = 0.; xz = 0.;
yx = 0.; yy = 0.; yz = 0.;
zx = 0.; zy = 0.; zz = 0.;
}
p_ten2(double a, double b, double c, double d, double e, double f, double g, double h, double i) {
xx = a; xy = b; xz = c;
yx = d; yy = e; yz = f;
zx = g; zy = h; zz = i;
}
// Destructor
~p_ten2() {}
// Components
double xx, xy, xz, yx, yy, yz, zx, zy, zz;
////////////////////////////////////////////////////////////////////////////////////////////
double tr() {
// Computes the trace of the tensor.
return xx + yy + zz;
}
////////////////////////////////////////////////////////////////////////////////////////////
double det() {
// Computes the Determinant of the tensor.
return xx * (yy * zz - yz * zy) + xy * (yz * zx - yx * zz) + xz * (yx * zy - yy * zx);
}
////////////////////////////////////////////////////////////////////////////////////////////
double mag() {
// Computes the magnitude of the tensor.
return sqrt(xx * xx + yy * yy + zz * zz + 2. * xy * xy + 2. * xz * xz + 2. * yz * yz);
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 invert() {
p_ten2 temp(*this);
p_ten2 inverse(
temp.yy * temp.zz - temp.yz * temp.zy,
temp.xz * temp.zy - temp.xy * temp.zz,
temp.xy * temp.yz - temp.xz * temp.yy,
temp.yz * temp.zx - temp.yx * temp.zz,
temp.xx * temp.zz - temp.xz * temp.zx,
temp.xz * temp.yx - temp.xx * temp.yz,
temp.yx * temp.zy - temp.yy * temp.zx,
temp.xy * temp.zx - temp.xx * temp.zy,
temp.xx * temp.yy - temp.xy * temp.yx
);
return inverse / temp.det();
}
////////////////////////////////////////////////////////////////////////////////////////////
void mult_by(double a) {
// Multiplies the tensor by an arbitrary number.
xx *= a; xy *= a; xz *= a;
yx *= a; yy *= a; yz *= a;
yx *= a; yy *= a; yz *= a;
}
////////////////////////////////////////////////////////////////////////////////////////////
void set_to(double a, double b, double c,
double d, double e, double f,
double g, double h, double i) {
// Sets the values of the tensor.
xx = a; xy = b; xz = c;
yx = d; yy = e; yz = f;
zx = g; zy = h; zz = i;
}
////////////////////////////////////////////////////////////////////////////////////////////
void reset() {
// Resets the values of the tensor.
xx = 0.; xy = 0.; xz = 0.;
yx = 0.; yy = 0.; yz = 0.;
zx = 0.; zy = 0.; zz = 0.;
}
////////////////////////////////////////////////////////////////////////////////////////////
bool is_symm() {
// Checks whether the tensor is symmetric.
if(xy == yx && xz == zx && yz == zy)
return true;
else
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////
bool is_asymm() {
// Checks whether the tensor is asymmetric.
if(xy == -yx && xz == -zx && yz == -zy && xx == 0 && yy == 0 && zz == 0)
return true;
else
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator-() const {
// Defines the operator - (unary negation).
p_ten2 temp(*this);
temp.xx *= -1; temp.xy *= -1; temp.xz *= -1;
temp.yx *= -1; temp.yy *= -1; temp.yz *= -1;
temp.zx *= -1; temp.zy *= -1; temp.zz *= -1;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator+(p_ten2 op2) const {
// Defines the operator + (sum of two tensors).
p_ten2 temp(*this);
temp.xx += op2.xx; temp.xy += op2.xy; temp.xz += op2.xz;
temp.yx += op2.yx; temp.yy += op2.yy; temp.yz += op2.yz;
temp.zx += op2.zx; temp.zy += op2.zy; temp.zz += op2.zz;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2& operator+=(p_ten2 op2) {
// Defines the operator + (sum of two tensors).
xx += op2.xx; xy += op2.xy; xz += op2.xz;
yx += op2.yx; yy += op2.yy; yz += op2.yz;
zx += op2.zx; zy += op2.zy; zz += op2.zz;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator-(p_ten2 op2) const {
// Defines the operator - (difference of two tensors).
p_ten2 temp(*this);
temp.xx -= op2.xx; temp.xy -= op2.xy; temp.xz -= op2.xz;
temp.yx -= op2.yx; temp.yy -= op2.yy; temp.yz -= op2.yz;
temp.zx -= op2.zx; temp.zy -= op2.zy; temp.zz -= op2.zz;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2& operator-=(p_ten2 op2) {
// Defines the operator + (sum of two tensors).
xx -= op2.xx; xy -= op2.xy; xz -= op2.xz;
yx -= op2.yx; yy -= op2.yy; yz -= op2.yz;
zx -= op2.zx; zy -= op2.zy; zz -= op2.zz;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator*(p_ten2 op2) const {
// Defines the operator * (tensor product: tensor * tensor => tensor)
p_ten2 temp(*this);
p_ten2 temp2(*this);
temp.xx = temp2.xx * op2.xx + temp2.xy * op2.yx + temp2.xz * op2.zx;
temp.xy = temp2.xx * op2.xy + temp2.xy * op2.yy + temp2.xz * op2.zy;
temp.xz = temp2.xx * op2.xz + temp2.xy * op2.yz + temp2.xz * op2.zz;
temp.yx = temp2.yx * op2.xx + temp2.yy * op2.yx + temp2.yz * op2.zx;
temp.yy = temp2.yx * op2.xy + temp2.yy * op2.yy + temp2.yz * op2.zy;
temp.yz = temp2.yx * op2.xz + temp2.yy * op2.yz + temp2.yz * op2.zz;
temp.zx = temp2.zx * op2.xx + temp2.zy * op2.yx + temp2.zz * op2.zx;
temp.zy = temp2.zx * op2.xy + temp2.zy * op2.yy + temp2.zz * op2.zy;
temp.zz = temp2.zx * op2.xz + temp2.zy * op2.yz + temp2.zz * op2.zz;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_vec<> operator*(p_vec<> op2) const {
// Defines the operator * (tensor product: tensor * vector => vector)
p_ten2 temp2(*this);
p_vec<> temp;
temp.x = temp2.xx * op2.x + temp2.xy * op2.y + temp2.xz * op2.z;
temp.y = temp2.yx * op2.x + temp2.yy * op2.y + temp2.yz * op2.z;
temp.z = temp2.zx * op2.x + temp2.zy * op2.y + temp2.zz * op2.z;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
double operator%(p_ten2 op2) const {
// Defines the operator % (contraction of two tensors).
p_ten2 temp(*this);
return temp.xx * op2.xx + temp.xy * op2.xy + temp.xz * op2.xz +
temp.yx * op2.yx + temp.yy * op2.yy + temp.yz * op2.yz +
temp.zx * op2.zx + temp.zy * op2.zy + temp.zz * op2.zz;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator*(double a) const {
// Defines the operator - (difference of two tensors).
p_ten2 temp(*this);
temp.xx *= a; temp.xy *= a; temp.xz *= a;
temp.yx *= a; temp.yy *= a; temp.yz *= a;
temp.zx *= a; temp.zy *= a; temp.zz *= a;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2& operator*=(double a) {
// Defines the operator + (sum of two tensors).
xx *= a; xy *= a; xz *= a;
yx *= a; yy *= a; yz *= a;
zx *= a; zy *= a; zz *= a;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2 operator/(double a) const {
// Defines the operator - (difference of two tensors).
p_ten2 temp(*this);
temp.xx /= a; temp.xy /= a; temp.xz /= a;
temp.yx /= a; temp.yy /= a; temp.yz /= a;
temp.zx /= a; temp.zy /= a; temp.zz /= a;
return temp;
}
////////////////////////////////////////////////////////////////////////////////////////////
p_ten2& operator/=(double a) {
// Defines the operator + (sum of two tensors).
xx /= a; xy /= a; xz /= a;
yx /= a; yy /= a; yz /= a;
zx /= a; zy /= a; zz /= a;
return *this;
}
};
ostream &operator<< (ostream &ostr, const p_ten2 &tensor);
#endif /* P_VEC_TEN_HH_ */