File: SampleItem.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 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 (265 lines) | stat: -rw-r--r-- 8,873 bytes parent folder | download
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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Sample/SampleItem.cpp
//! @brief     Implements class SampleItem.
//!
//! @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 "GUI/Model/Sample/SampleItem.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/LayerStackItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Type/PredefinedColors.h"
#include "GUI/Model/Util/Backup.h"
#include "GUI/Model/Util/UtilXML.h"
#include <random>
#include <set>

namespace {
namespace Tag {

const QString Name("Name");
const QString Description("Description");
const QString MaterialsSet("MaterialsSet");
const QString OuterStack("OuterStack");
const QString ExternalField("ExternalField");
const QString ExpandInfoGroupbox("ExpandInfoGroupbox");
const QString ShowRoughness("ShowRoughness");

} // namespace Tag
} // namespace

SampleItem::SampleItem()
    : NamedItem("Sample")
    , m_outer_stack(std::make_unique<LayerStackItem>(&m_materials, 1))
{
    m_external_field.init("External field", "A/m", "External field (A/m)", "extField");
}

SampleItem::~SampleItem() = default;

SampleItem* SampleItem::clone() const
{
    auto* result = new SampleItem;
    GUI::Util::copyContents(this, result);
    return result;
}

std::vector<ItemWithMaterial*> SampleItem::itemsWithMaterial() const
{
    std::vector<ItemWithMaterial*> result;
    std::vector<LayerItem*> layers = uniqueLayerItems();
    for (LayerItem* l : layers)
        Vec::concat(result, l->itemsWithMaterial());
    return result;
}

void SampleItem::addStandardMaterials()
{
    // add only non-existing materials
    QString name = materialMap.key(DefaultMaterials::Default);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 1e-3, 1e-5);

    name = materialMap.key(DefaultMaterials::Vacuum);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 0.0, 0.0);

    name = materialMap.key(DefaultMaterials::Particle);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 6e-4, 2e-8);

    name = materialMap.key(DefaultMaterials::Core);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 2e-4, 1e-8);

    name = materialMap.key(DefaultMaterials::Substrate);
    if (!m_materials.materialItemFromName(name))
        m_materials.addRefractiveMaterialItem(name, 6e-6, 2e-8);
}

void SampleItem::setOuterStackItem(LayerStackItem* outer_stack)
{
    m_outer_stack.reset(outer_stack); // gets ownership
    updateTopBottom();
}

std::vector<LayerItem*> SampleItem::uniqueLayerItems() const
{
    return m_outer_stack->uniqueLayerItems();
}

std::vector<ItemWithLayers*> SampleItem::componentItems() const
{
    return m_outer_stack->componentItems();
}

int SampleItem::indexOfComponent(const ItemWithLayers* item) const
{
    return m_outer_stack->indexOfComponent(item);
}

LayerStackItem* SampleItem::parentOfComponent(const ItemWithLayers* searchedItem)
{
    return m_outer_stack->parentOfComponent(searchedItem);
}

LayerItem* SampleItem::createLayerItemAt(LayerStackItem& parentStack, int index)
{
    LayerItem* layer = parentStack.createLayerItemAt(index);
    updateTopBottom();
    return layer;
}

LayerStackItem* SampleItem::createLayerStackItemAt(LayerStackItem& parentStack, int index)
{
    return parentStack.createLayerStackItemAt(index);
}

void SampleItem::updateTopBottom()
{
    std::vector<ItemWithLayers*> non_empty_outer_components = componentItems();
    // erase empty stacks
    for (int i = non_empty_outer_components.size() - 1; i >= 0; i--) {
        if (const auto* stack = dynamic_cast<const LayerStackItem*>(non_empty_outer_components[i]))
            if (stack->uniqueLayerItems().empty() || (stack->numberOfPeriods() == 0))
                non_empty_outer_components.erase(non_empty_outer_components.begin() + i);
    }

    std::vector<LayerItem*> layers = uniqueLayerItems();
    if (layers.empty())
        return;

    // Only layers, belonging to the outer stack can be considered as ambient or substrate
    auto* ambient = dynamic_cast<LayerItem*>(non_empty_outer_components.front());
    auto* substrate = dynamic_cast<LayerItem*>(non_empty_outer_components.back());

    for (LayerItem* l : layers) {
        l->setIsAmbient(l == ambient);
        l->setIsSubstrate(l == substrate);
    }
}

void SampleItem::removeComponent(const ItemWithLayers* component)
{
    LayerStackItem* containingStack = parentOfComponent(component);
    if (!containingStack)
        return;

    containingStack->removeComponent(component);
    updateTopBottom();
}

void SampleItem::moveComponent(ItemWithLayers* component, ItemWithLayers* aboveThisComponent)
{
    LayerStackItem* containingStack = parentOfComponent(component);
    ASSERT(containingStack);

    containingStack->moveComponent(component, aboveThisComponent);
    updateTopBottom();
}

void SampleItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeTaggedValue(w, Tag::Name, name());
    XML::writeTaggedValue(w, Tag::Description, description());
    XML::writeTaggedElement(w, Tag::ExternalField, m_external_field);
    XML::writeTaggedElement(w, Tag::MaterialsSet, m_materials);
    XML::writeTaggedElement(w, Tag::OuterStack, *m_outer_stack);
    XML::writeTaggedValue(w, Tag::ExpandInfoGroupbox, expandInfo);
    XML::writeTaggedValue(w, Tag::ShowRoughness, showRoughness);
}

void SampleItem::readFrom(QXmlStreamReader* r)
{
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();
        if (tag == Tag::Name)
            setName(XML::readTaggedString(r, tag));
        else if (tag == Tag::Description)
            setDescription(XML::readTaggedString(r, tag));
        else if (tag == Tag::ExternalField)
            XML::readTaggedElement(r, tag, m_external_field);
        else if (tag == Tag::MaterialsSet)
            XML::readTaggedElement(r, tag, m_materials);
        else if (tag == Tag::OuterStack)
            XML::readTaggedElement(r, tag, *m_outer_stack);
        else if (tag == Tag::ExpandInfoGroupbox)
            expandInfo = XML::readTaggedBool(r, tag);
        else if (tag == Tag::ShowRoughness)
            showRoughness = XML::readTaggedBool(r, tag);
        else
            r->skipCurrentElement();
    }
    updateTopBottom();
}

void SampleItem::updateDefaultLayerColors()
{
    const auto& colors = GUI::Colors::layerDefaults();

    int col = 0;
    std::vector<LayerItem*> layers = uniqueLayerItems();
    for (auto* l : layers) {
        if (!l->color().isValid())
            l->setColor(colors[col]);
        col = (col + 1) % colors.size();
    }
}

void SampleItem::adjustLayerSeeds(bool update_all) const
{
    unsigned rng_seed =
        static_cast<unsigned>(std::chrono::system_clock::now().time_since_epoch().count());
    std::mt19937 gen(rng_seed);
    std::uniform_int_distribution<int> distr(0, std::numeric_limits<int>::max());

    std::vector<int> old_seeds = seeds;

    std::vector<LayerItem*> layers = m_outer_stack->unwrappedLayerItems();
    if (old_seeds.size() != layers.size()) {
        update_all = true;
        seeds.clear();
        seeds.resize(layers.size(), 0);
    }
    std::set<int> used_seeds;

    const int i_bottom = layers.size() - 1;

    seeds[i_bottom] = update_all ? distr(gen) : old_seeds[i_bottom];
    used_seeds.insert(seeds[i_bottom]);

    for (int i = i_bottom - 1; i >= 0; i--) {
        const RoughnessItem* roughness = layers[i]->roughnessSelection().certainItem();
        const bool hasCrosscorr = roughness
                                  && (roughness->certainCrosscorrModel()
                                      || dynamic_cast<const LinearGrowthRoughnessItem*>(roughness));

        // preliminary assign already existing seed
        seeds[i] = update_all ? 0 : old_seeds[i];

        // change the seed to the proper one if needed
        if (hasCrosscorr)
            seeds[i] = seeds[i + 1];
        else if (update_all || used_seeds.count(seeds[i])) {
            seeds[i] = distr(gen);
            used_seeds.insert(seeds[i]);
        }
    }
}

void SampleItem::adjustLayoutSeeds() const
{
    std::vector<LayerItem*> layers = m_outer_stack->unwrappedLayerItems();
    for (const auto* lr : layers)
        for (const auto* layout : lr->layoutItems())
            layout->updateSeed();
}