OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
seed_grid.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
34#ifndef PFC_INITIAL_CONDITIONS_SEED_GRID_HPP
35#define PFC_INITIAL_CONDITIONS_SEED_GRID_HPP
36
37#include <random>
38
39#include "../field_modifier.hpp"
41#include "seed.hpp"
42
43namespace pfc {
44
45class SeedGrid : public FieldModifier {
46private:
47 int m_Nx = 1, m_Ny = 2, m_Nz = 2;
48 double m_X0, m_radius;
49 double m_rho, m_amplitude;
50
51public:
52 // Setters
53 void set_Nx(int Nx) { m_Nx = Nx; }
54 void set_Ny(int Ny) { m_Ny = Ny; }
55 void set_Nz(int Nz) { m_Nz = Nz; }
56 void set_X0(double X0) { m_X0 = X0; }
57 void set_radius(double radius) { m_radius = radius; }
58 void set_density(double rho) { m_rho = rho; }
59 void set_amplitude(double amplitude) { m_amplitude = amplitude; }
60
61 // Getters
62 int get_Nx() const { return m_Nx; }
63 int get_Ny() const { return m_Ny; }
64 int get_Nz() const { return m_Nz; }
65 double get_X0() const { return m_X0; }
66 double get_radius() const { return m_radius; }
67 double get_density() const { return m_rho; }
68 double get_amplitude() const { return m_amplitude; }
69
70 SeedGrid() = default;
71
72 SeedGrid(int Ny, int Nz, double X0, double radius)
73 : m_Nx(1), m_Ny(Ny), m_Nz(Nz), m_X0(X0), m_radius(radius) {}
74
75 void apply(Model &m, double) override {
76 // Functional coordinate-space implementation using field::apply
77 const World &w = m.get_world();
78 const auto size = get_size(w);
79 const auto spacing = get_spacing(w);
80
81 std::vector<Seed> seeds;
82 const int Nx = m_Nx;
83 const int Ny = m_Ny;
84 const int Nz = m_Nz;
85 const double radius = get_radius();
86
87 const double Dy = spacing[1] * size[1] / Ny;
88 const double Dz = spacing[2] * size[2] / Nz;
89 const double X0 = m_X0;
90 const double Y0 = Dy / 2.0;
91 const double Z0 = Dz / 2.0;
92 const int nseeds = Nx * Ny * Nz;
93
94 std::cout << "Generating " << nseeds << " regular seeds with radius " << radius
95 << "\n";
96
97 std::mt19937_64 re(42);
98 std::uniform_real_distribution<double> rt(-0.2 * radius, 0.2 * radius);
99 std::uniform_real_distribution<double> rr(0.0, 8.0 * atan(1.0));
100
101 for (int j = 0; j < Ny; j++) {
102 for (int k = 0; k < Nz; k++) {
103 const std::array<double, 3> location = {X0 + rt(re), Y0 + Dy * j + rt(re),
104 Z0 + Dz * k + rt(re)};
105 const std::array<double, 3> orientation = {rr(re), rr(re), rr(re)};
106 const Seed seed(location, orientation, get_radius(), get_density(),
107 get_amplitude());
108 seeds.push_back(seed);
109 }
110 }
111
112 pfc::field::apply(m, get_field_name(), [seeds](const pfc::Real3 &X) {
113 for (const auto &seed : seeds) {
114 if (seed.is_inside(X)) {
115 return seed.get_value(X);
116 }
117 }
118 return 0.0; // Outside all seeds
119 });
120 }
121};
122
123} // namespace pfc
124
125#endif // PFC_INITIAL_CONDITIONS_SEED_GRID_HPP
Definition field_modifier.hpp:240
The Model class represents the physics model for simulations in OpenPFC.
Definition model.hpp:95
Definition seed_grid.hpp:45
void apply(Model &m, double) override
Apply the field modification to the model (pure virtual)
Definition seed_grid.hpp:75
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