File: PositionBuilders.cpp

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (308 lines) | stat: -rw-r--r-- 11,928 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Img3D/Build/PositionBuilders.cpp
//! @brief     Implements subclasses of IPositionBuilder.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Img3D/Build/PositionBuilders.h"
#include "Base/Math/Functions.h"
#include "Img3D/Build/ParacrystalLatticePositions.h"
#include "Sample/Aggregate/Interferences.h"
#include <random>

namespace {

double2d_t Generate2DLatticePoints(double l1, double l2, double alpha, double xi, unsigned n1,
                                   unsigned n2)
{
    double2d_t lattice_positions;
    std::vector<double> position;

    const unsigned nn1 = std::max(1u, n1);
    const unsigned nn2 = std::max(1u, n2);
    const int n1m = -static_cast<int>((nn1 - 1) / 2);
    const int n1M = static_cast<int>(nn1 / 2);
    const int n2m = -static_cast<int>((nn2 - 1) / 2);
    const int n2M = static_cast<int>(nn2 / 2);

    for (int i = n1m; i <= n1M; ++i) {
        for (int j = n2m; j <= n2M; ++j) {
            // For calculating lattice position vector v, we use: v = i*l1 + j*l2
            // where l1 and l2 are the lattice vectors,
            position.push_back(i * l1 * std::cos(xi)
                               + j * l2 * std::cos(alpha + xi)); // x coordinate
            position.push_back(i * l1 * std::sin(xi)
                               + j * l2 * std::sin(alpha + xi)); // y coordinate

            lattice_positions.push_back(position);
            position.clear();
        }
    }
    return lattice_positions;
}

} // namespace


//  ************************************************************************************************
//  class IPositionBuilder
//  ************************************************************************************************

IPositionBuilder::~IPositionBuilder() = default;

double2d_t IPositionBuilder::generatePositions(double layer_size, double density, int seed) const
{
    double2d_t positions = generatePositionsImpl(layer_size, density, Math::GenerateNextSeed(seed));
    const double pos_var = positionVariance();
    if (pos_var > 0.0) {
        // random generator and distribution
        std::random_device rd; // Will be used to obtain a seed for the random number engine
        std::mt19937 gen(seed < 0 ? rd()
                                  : seed); // Standard mersenne_twister_engine seeded with rd()
        std::normal_distribution<double> dis(0.0, std::sqrt(pos_var));
        for (auto& position : positions) {
            for (double& coordinate : position)
                coordinate += dis(gen);
        }
    }
    return positions;
}


//  ************************************************************************************************
//  class RandomPositionBuilder
//  ************************************************************************************************

RandomPositionBuilder::RandomPositionBuilder() = default;

RandomPositionBuilder::~RandomPositionBuilder() = default;

double2d_t RandomPositionBuilder::generatePositionsImpl(double layer_size, double density,
                                                        int seed) const
{
    double2d_t lattice_positions;
    std::vector<double> position;

    // to compute total number of particles we use the total particle density
    // and multiply by the area of the layer
    int n_particles = static_cast<int>(density * (2 * layer_size) * (2 * layer_size));

    // random generator and distribution
    std::random_device rd; // Will be used to obtain a seed for the random number engine
    std::mt19937 gen(seed < 0 ? rd() : seed); // Standard mersenne_twister_engine seeded with rd()
    std::uniform_real_distribution<double> dis(0.0, 1.0);

    for (int i = 1; i <= n_particles; ++i) {
        // generate random x and y coordinates
        position.push_back(dis(gen) * 2 * layer_size - layer_size); // x
        position.push_back(dis(gen) * 2 * layer_size - layer_size); // y

        lattice_positions.push_back(position);
        position.clear();
    }
    return lattice_positions;
}

double RandomPositionBuilder::positionVariance() const
{
    return 0.0; // no need for extra randomness here
}


//  ************************************************************************************************
//  class Lattice1DPositionBuilder
//  ************************************************************************************************

Lattice1DPositionBuilder::Lattice1DPositionBuilder(const Interference1DLattice* p_iff)
    : m_iff(p_iff->clone())
{
}

Lattice1DPositionBuilder::~Lattice1DPositionBuilder() = default;

double2d_t Lattice1DPositionBuilder::generatePositionsImpl(double layer_size, double, int) const
{
    const double length = m_iff->length();
    const double xi = m_iff->xi();

    // Take the maximum possible integer multiple of the lattice vector required
    // for populating particles correctly within the 3D model's boundaries
    unsigned n1 =
        length == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / length);

    return Generate2DLatticePoints(length, 0.0, 0.0, xi, n1, 1u);
}

double Lattice1DPositionBuilder::positionVariance() const
{
    return m_iff->positionVariance();
}


//  ************************************************************************************************
//  class Lattice2DPositionBuilder
//  ************************************************************************************************

Lattice2DPositionBuilder::Lattice2DPositionBuilder(const Interference2DLattice* p_iff)
    : m_iff(p_iff->clone())
{
}

Lattice2DPositionBuilder::~Lattice2DPositionBuilder() = default;

double2d_t Lattice2DPositionBuilder::generatePositionsImpl(double layer_size, double, int) const
{
    const Lattice2D& lattice = m_iff->lattice();
    const double l1 = lattice.length1();
    const double l2 = lattice.length2();
    const double alpha = lattice.latticeAngle();
    const double xi = lattice.rotationAngle();

    // Estimate the limits n1 and n2 of the maximum integer multiples of the lattice vectors
    // required for populating particles correctly within the 3D model's boundaries
    unsigned n1, n2;
    const double sina = std::abs(std::sin(alpha));
    if (sina <= 1e-4) {
        n1 = l1 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l1);
        n2 = l2 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l2);
    } else {
        n1 = l1 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l1 / sina);
        n2 = l2 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l2 / sina);
    }
    return Generate2DLatticePoints(l1, l2, alpha, xi, n1, n2);
}

double Lattice2DPositionBuilder::positionVariance() const
{
    return m_iff->positionVariance();
}


//  ************************************************************************************************
//  class Paracrystal2DPositionBuilder
//  ************************************************************************************************

Paracrystal2DPositionBuilder::Paracrystal2DPositionBuilder(const Interference2DParacrystal* p_iff)
    : m_iff(p_iff->clone())
{
}

Paracrystal2DPositionBuilder::~Paracrystal2DPositionBuilder() = default;

double2d_t Paracrystal2DPositionBuilder::generatePositionsImpl(double layer_size, double,
                                                               int seed) const
{
    return Paracrystal::latticePositions(m_iff.get(), layer_size, seed);
}

double Paracrystal2DPositionBuilder::positionVariance() const
{
    return m_iff->positionVariance();
}


//  ************************************************************************************************
//  class Finite2DLatticePositionBuilder
//  ************************************************************************************************

Finite2DLatticePositionBuilder::Finite2DLatticePositionBuilder(
    const InterferenceFinite2DLattice* p_iff)
    : m_iff(p_iff->clone())
{
}

Finite2DLatticePositionBuilder::~Finite2DLatticePositionBuilder() = default;

double2d_t Finite2DLatticePositionBuilder::generatePositionsImpl(double layer_size, double,
                                                                 int) const
{
    const auto& lattice = m_iff->lattice();
    const double l1 = lattice.length1();
    const double l2 = lattice.length2();
    const double alpha = lattice.latticeAngle();
    const double xi = lattice.rotationAngle();

    unsigned n1, n2;
    const double sina = std::abs(std::sin(alpha));
    if (sina <= 1e-4) {
        n1 = l1 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l1);
        n2 = l2 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l2);
    } else {
        n1 = l1 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l1 / sina);
        n2 = l2 == 0.0 ? 2 : static_cast<unsigned>(2.0 * layer_size * std::sqrt(2.0) / l2 / sina);
    }
    n1 = std::min(n1, m_iff->numberUnitCells1());
    n2 = std::min(n2, m_iff->numberUnitCells2());

    return Generate2DLatticePoints(l1, l2, alpha, xi, n1, n2);
}

double Finite2DLatticePositionBuilder::positionVariance() const
{
    return m_iff->positionVariance();
}


//  ************************************************************************************************
//  class RadialParacrystalPositionBuilder
//  ************************************************************************************************

RadialParacrystalPositionBuilder::RadialParacrystalPositionBuilder(
    const InterferenceRadialParacrystal* p_iff)
    : m_iff(p_iff->clone())
{
}

RadialParacrystalPositionBuilder::~RadialParacrystalPositionBuilder() = default;

double2d_t RadialParacrystalPositionBuilder::generatePositionsImpl(double layer_size, double,
                                                                   int seed) const
{
    double2d_t lattice_positions;

    const double distance = m_iff->peakDistance();

    // Estimate the limit n of the integer multiple i of the peakDistance required
    // for populating particles correctly within the 3D model's boundaries
    const int n = distance <= 0.0 ? 1 : static_cast<int>(layer_size * std::sqrt(2.0) / distance);

    lattice_positions.resize(2 * n + 1);
    for (auto& it : lattice_positions)
        it.resize(2);

    lattice_positions[0][0] = 0.0; // x coordinate of reference particle - at the origin
    lattice_positions[0][1] = 0.0; // y coordinate of reference particle - at the origin

    int upd_seed = Math::GenerateNextSeed(seed);
    for (int i = 1; i <= n; ++i) {
        // update seeds for each position
        upd_seed = Math::GenerateNextSeed(upd_seed);
        int upd_seed_2 = Math::GenerateNextSeed(upd_seed);

        // positions of particles located along +x (store at odd index)
        const unsigned i_left = static_cast<unsigned>(std::max(0, 2 * i - 3));

        double offset = m_iff->randomSample(upd_seed);
        lattice_positions[2 * i - 1][0] = lattice_positions[i_left][0] + distance + offset;
        lattice_positions[2 * i - 1][1] = 0.0;

        // positions of particles located along -x (store at even index)
        offset = m_iff->randomSample(upd_seed_2);
        lattice_positions[2 * i][0] = lattice_positions[2 * (i - 1)][0] - distance + offset;
        lattice_positions[2 * i][1] = 0.0;
    }
    return lattice_positions;
}

double RadialParacrystalPositionBuilder::positionVariance() const
{
    return m_iff->positionVariance();
}