OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
random_seeds.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
33#ifndef PFC_INITIAL_CONDITIONS_RANDOM_SEEDS_HPP
34#define PFC_INITIAL_CONDITIONS_RANDOM_SEEDS_HPP
35
36#include <random>
37
38#include "../field_modifier.hpp"
40#include "seed.hpp"
41
42namespace pfc {
43
44class RandomSeeds : public FieldModifier {
45 double m_density, m_amplitude;
46
47public:
48 void set_amplitude(double amplitude) { m_amplitude = amplitude; }
49 double get_amplitude() const { return m_amplitude; }
50 void set_density(double density) { m_density = density; }
51 double get_density() const { return m_density; }
52
53 void apply(Model &m, double) override {
54 // Functional coordinate-space implementation using field::apply
55 std::vector<Seed> seeds;
56 const int nseeds = 150;
57 const double radius = 20.0;
58 const double lower_x = -128.0 + radius;
59 const double upper_x = -128.0 + 3 * radius;
60 const double lower_y = -128.0;
61 const double upper_y = 128.0;
62 const double lower_z = -128.0;
63 const double upper_z = 128.0;
64
65 std::mt19937_64 re(42);
66 std::uniform_real_distribution<double> rx(lower_x, upper_x);
67 std::uniform_real_distribution<double> ry(lower_y, upper_y);
68 std::uniform_real_distribution<double> rz(lower_z, upper_z);
69 std::uniform_real_distribution<double> ro(0.0, 8.0 * atan(1.0));
70 using vec3 = std::array<double, 3>;
71 auto random_location = [&re, &rx, &ry, &rz]() {
72 return vec3({rx(re), ry(re), rz(re)});
73 };
74 auto random_orientation = [&re, &ro]() {
75 return vec3({ro(re), ro(re), ro(re)});
76 };
77
78 for (int i = 0; i < nseeds; i++) {
79 const std::array<double, 3> location = random_location();
80 const std::array<double, 3> orientation = random_orientation();
81 const Seed seed(location, orientation, radius, get_density(), get_amplitude());
82 seeds.push_back(seed);
83 }
84
85 pfc::field::apply(m, get_field_name(), [seeds](const pfc::Real3 &X) {
86 for (const auto &seed : seeds) {
87 if (seed.is_inside(X)) {
88 return seed.get_value(X);
89 }
90 }
91 return 0.0; // Outside seeds
92 });
93 }
94};
95
96} // namespace pfc
97
98#endif // PFC_INITIAL_CONDITIONS_RANDOM_SEEDS_HPP
Definition field_modifier.hpp:240
The Model class represents the physics model for simulations in OpenPFC.
Definition model.hpp:95
Definition random_seeds.hpp:44
void apply(Model &m, double) override
Apply the field modification to the model (pure virtual)
Definition random_seeds.hpp:53
Seed is a helper class to construct various of initial conditions.
Definition seed.hpp:39
Functional, coordinate-space field operations (header-only)
Helper class for constructing spherical seed initial conditions.
Represents the global simulation domain (the "world").
Definition world.hpp:91