OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
seed.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
27#ifndef PFC_INITIAL_CONDITIONS_SEED_HPP
28#define PFC_INITIAL_CONDITIONS_SEED_HPP
29
30#include <array>
31#include <cmath>
32
33namespace pfc {
34
39class Seed {
40private:
41 using vec3 = std::array<double, 3>;
42 using mat3 = std::array<vec3, 3>;
43 using vec36 = std::array<vec3, 6>;
44 using vec32 = std::array<vec3, 2>;
45
46 const vec3 location_;
47 const vec3 orientation_;
48 const vec36 q_;
49 const vec32 bbox_;
50 const double rho_;
51 const double radius_;
52 const double amplitude_;
53
54 mat3 yaw(double a) {
55 double ca = cos(a);
56 double sa = sin(a);
57 return {vec3({ca, -sa, 0.0}), vec3({sa, ca, 0.0}), vec3({0.0, 0.0, 1.0})};
58 }
59
60 mat3 pitch(double b) {
61 double cb = cos(b);
62 double sb = sin(b);
63 return {vec3({cb, 0.0, sb}), vec3({0.0, 1.0, 0.0}), vec3({-sb, 0.0, cb})};
64 }
65
66 mat3 roll(double c) {
67 double cc = cos(c);
68 double sc = sin(c);
69 return {vec3({1.0, 0.0, 0.0}), vec3({0.0, cc, -sc}), vec3({0.0, sc, cc})};
70 }
71
72 mat3 mult3(const mat3 &A, const mat3 &B) {
73 mat3 C = {vec3{0.0}};
74 for (int i = 0; i < 3; i++) {
75 for (int j = 0; j < 3; j++) {
76 for (int k = 0; k < 3; k++) {
77 C[i][j] += A[i][k] * B[k][j];
78 }
79 }
80 }
81 return C;
82 }
83
84 vec3 mult3(const mat3 &A, const vec3 &b) {
85 vec3 c = {};
86 for (int i = 0; i < 3; i++) {
87 for (int j = 0; j < 3; j++) {
88 c[i] += A[i][j] * b[j];
89 }
90 }
91 return c;
92 }
93
94 vec36 rotate(const vec3 &orientation) {
95 const double s = 1.0 / sqrt(2.0);
96 const vec3 q1 = {s, s, 0};
97 const vec3 q2 = {s, 0, s};
98 const vec3 q3 = {0, s, s};
99 const vec3 q4 = {s, 0, -s};
100 const vec3 q5 = {s, -s, 0};
101 const vec3 q6 = {0, s, -s};
102 mat3 Ra = yaw(orientation[0]);
103 mat3 Rb = pitch(orientation[1]);
104 mat3 Rc = roll(orientation[2]);
105 mat3 R = mult3(Ra, mult3(Rb, Rc));
106 const vec36 q = {mult3(R, q1), mult3(R, q2), mult3(R, q3),
107 mult3(R, q4), mult3(R, q5), mult3(R, q6)};
108 return q;
109 }
110
111 vec32 bounding_box(const vec3 &location, double radius) {
112 const vec3 low = {location[0] - radius, location[1] - radius,
113 location[2] - radius};
114 const vec3 high = {location[0] + radius, location[1] + radius,
115 location[2] + radius};
116 const vec32 bbox = {low, high};
117 return bbox;
118 }
119
120 inline bool is_inside_bbox(const vec3 &location) const {
121 const vec32 bbox = get_bbox();
122 return (location[0] > bbox[0][0]) && (location[0] < bbox[1][0]) &&
123 (location[1] > bbox[0][1]) && (location[1] < bbox[1][1]) &&
124 (location[2] > bbox[0][2]) && (location[2] < bbox[1][2]);
125 }
126
127 double get_radius() const { return radius_; }
128 double get_rho() const { return rho_; }
129 double get_amplitude() const { return amplitude_; }
130 vec3 get_location() const { return location_; }
131 vec36 get_q() const { return q_; }
132 vec32 get_bbox() const { return bbox_; }
133
134public:
135 Seed(const vec3 &location, const vec3 &orientation, const double radius,
136 const double rho, const double amplitude)
137 : location_(location), orientation_(orientation), q_(rotate(orientation_)),
138 bbox_(bounding_box(location, radius)), rho_(rho), radius_(radius),
139 amplitude_(amplitude) {}
140
141 bool is_inside(const vec3 &X) const {
142 /*
143 if (!is_inside_bbox(X)) {
144 return false;
145 }
146 */
147 const vec3 Y = get_location();
148 double x = X[0] - Y[0];
149 double y = X[1] - Y[1];
150 double z = X[2] - Y[2];
151 double r = get_radius();
152
153 return x * x + y * y + z * z < r * r;
154 }
155
156 double get_value(const vec3 &location) const {
157 double x = location[0];
158 double y = location[1];
159 double z = location[2];
160 double u = get_rho();
161 double a = get_amplitude();
162 vec36 q = get_q();
163 for (int i = 0; i < 6; i++) {
164 u += 2.0 * a * cos(q[i][0] * x + q[i][1] * y + q[i][2] * z);
165 }
166 return u;
167 }
168};
169} // namespace pfc
170
171#endif // PFC_INITIAL_CONDITIONS_SEED_HPP
Seed is a helper class to construct various of initial conditions.
Definition seed.hpp:39
Represents the global simulation domain (the "world").
Definition world.hpp:91