PLaSK library

Loading...
Searching...
No Matches
regular1d.cpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#include "regular1d.hpp"
15
16#include "../log/log.hpp"
17
18 namespace plask {
19
20 RegularAxis &RegularAxis::operator=(const RegularAxis &src) {
21 bool resized = points_count != src.points_count;
22 lo = src.lo; _step = src._step; points_count = src.points_count;
23 if (resized) fireResized(); else fireChanged();
24 return *this;
25}
26
27 void RegularAxis::reset(double first, double last, std::size_t points_count) {
28 lo = first;
29 _step = (last - first) / ((points_count>1)?double(points_count-1):1.);
30 bool resized = this->points_count != points_count;
31 this->points_count = points_count;
32 if (resized) fireResized(); else fireChanged();
33}
34
35std::size_t RegularAxis::findUpIndex(double to_find) const {
36 const double index_before_cail = (to_find - lo) / _step;
37 if (index_before_cail < 0.0) return 0;
38 const double index_after_ceil = std::ceil(index_before_cail);
39 std::size_t index = std::size_t(index_after_ceil);
40 if (index_before_cail == index_after_ceil) ++index;
41 if (index > points_count) return points_count;
42 return index;
43}
44
45 void RegularAxis::writeXML(XMLElement &object) const {
46 object.attr("type", "regular").attr("start", first()).attr("stop", last()).attr("num", size());
47}
48
49 bool RegularAxis::isIncreasing() const
50{
51 return step() >= 0;
52}
53
54shared_ptr<MeshAxis> RegularAxis::getMidpointAxis() const
55{
56 beforeCalcMidpointMesh();
57 auto result = plask::make_shared<RegularMesh1D>(*this);
58 //if (this->points_count > 0) { //beforeCalcMidpointMesh() throws exception if this is not true
59 --result->points_count;
60 result->lo += _step * 0.5;
61 //}
62 return result;
63 //return plask::make_shared<RegularMesh1D>(this->first() + this->step() * 0.5, this->last() - this->step() * 0.5, this->points_count - 1);
64}
65
66 bool RegularAxis::hasSameNodes(const MeshD<1> &to_compare) const {
67 if (const RegularAxis* c = dynamic_cast<const RegularAxis*>(&to_compare))
68 return *this == *c; // this will call == operator from RegularAxis
69 return MeshAxis::hasSameNodes(to_compare);
70}
71
72shared_ptr<RegularMesh1D> readRegularMeshAxis(XMLReader& reader) {
73 double start = reader.requireAttribute<double>("start");
74 double stop = reader.requireAttribute<double>("stop");
75 size_t count = reader.requireAttribute<size_t>("num");
76 reader.requireTagEnd();
77 return plask::make_shared<RegularMesh1D>(start, stop, count);
78}
79
80shared_ptr<RegularMesh1D> readRegularMesh1D(XMLReader& reader) {
81 reader.requireTag("axis");
82 auto result = readRegularMeshAxis(reader);
83 reader.requireTagEnd();
84 return result;
85}
86
87 RegisterMeshReader regularmesh_reader("regular", readRegularMesh1D);
88
89
90shared_ptr<RegularMesh1D> readRegularMesh1D_obsolete(XMLReader& reader) {
91 writelog(LOG_WARNING, "Mesh type \"{0}\" is obsolete, use \"regular\" instead.", reader.requireAttribute("type"));
92 return readRegularMesh1D(reader);
93}
94 RegisterMeshReader regularmesh1d_reader("regular1d", readRegularMesh1D_obsolete);
95
96}