SIRIUS 7.5.0
Electronic structure library and applications
field4d.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file field4d.hpp
21 *
22 * \brief Base class for sirius::Density and sirius::Potential.
23 */
24
25#ifndef __FIELD4D_HPP__
26#define __FIELD4D_HPP__
27
28#include <memory>
29#include <array>
30#include <stdexcept>
31#include "SDDK/memory.hpp"
33#include "core/typedefs.hpp"
34
35namespace sirius {
36
37/// Four-component function consisting of scalar and vector parts.
38/** This class is used to represents density/magnetisation and potential/magentic filed of the system. */
40{
41 private:
42 /// Four components of the field: scalar, vector_z, vector_x, vector_y
43 std::array<std::unique_ptr<Periodic_function<double>>, 4> components_;
44
45 protected:
47
48 public:
49 /// Constructor.
51 std::array<periodic_function_ptr_t<double> const*, 4> ptr__ = {nullptr, nullptr, nullptr, nullptr})
52 : ctx_{ctx__}
53 {
54 for (int i = 0; i < ctx_.num_mag_dims() + 1; i++) {
55 smooth_periodic_function_ptr_t<double> const* ptr_rg{nullptr};
56 spheric_function_set_ptr_t<double> const* ptr_mt{nullptr};
57 if (ptr__[i] && ptr__[i]->rg.ptr) {
58 ptr_rg = &ptr__[i]->rg;
59 }
60 if (ptr__[i] && ptr__[i]->mt.ptr) {
61 ptr_mt = &ptr__[i]->mt;
62 }
63 if (ctx_.full_potential()) {
64 /* allocate with global MT part */
65 components_[i] = std::make_unique<Periodic_function<double>>(ctx_, [&](int ia){return lmax__;}, nullptr,
66 ptr_rg, ptr_mt);
67 } else {
68 components_[i] = std::make_unique<Periodic_function<double>>(ctx_, ptr_rg);
69 }
70 }
71 }
72
73 /// Return scalar part of the field.
74 inline auto& scalar()
75 {
76 return *(components_[0]);
77 }
78
79 /// Return scalar part of the field.
80 inline auto const& scalar() const
81 {
82 return *(components_[0]);
83 }
84
85 /// Return component of the vector part of the field.
86 inline auto& vector(int i)
87 {
88 RTE_ASSERT(i >= 0 && i <= 2);
89 return *(components_[i + 1]);
90 }
91
92 /// Return component of the vector part of the field.
93 inline auto const& vector(int i) const
94 {
95 RTE_ASSERT(i >= 0 && i <= 2);
96 return *(components_[i + 1]);
97 }
98
99 inline auto& component(int i)
100 {
101 RTE_ASSERT(i >= 0 && i <= 3);
102 return *(components_[i]);
103 }
104
105 /// Throws error in case of invalid access.
106 inline auto& component_raise(int i)
107 {
108 if (components_[i] == nullptr) {
109 throw std::runtime_error("invalid access");
110 }
111 return *(components_[i]);
112 }
113
114 inline auto const& component(int i) const
115 {
116 RTE_ASSERT(i >= 0 && i <= 3);
117 return *(components_[i]);
118 }
119
120 inline void zero()
121 {
122 for (int i = 0; i < ctx_.num_mag_dims() + 1; i++) {
123 component(i).zero();
124 }
125 }
126
127 inline void fft_transform(int direction__)
128 {
129 for (int i = 0; i < ctx_.num_mag_dims() + 1; i++) {
130 component(i).rg().fft_transform(direction__);
131 }
132 }
133
134 inline auto& ctx()
135 {
136 return ctx_;
137 }
138
139 inline auto const& ctx() const
140 {
141 return ctx_;
142 }
143
144 auto mt_components()
145 {
146 std::vector<Spheric_function_set<double, atom_index_t>*> result;
147 result.push_back(&components_[0]->mt());
148 switch (ctx_.num_mag_dims()) {
149 case 1: {
150 /* z-component */
151 result.push_back(&components_[1]->mt());
152 break;
153 }
154 case 3: {
155 /* x-component */
156 result.push_back(&components_[2]->mt());
157 /* y-component */
158 result.push_back(&components_[3]->mt());
159 /* z-component */
160 result.push_back(&components_[1]->mt());
161 break;
162 }
163 }
164 return result;
165 }
166
167 auto pw_components()
168 {
169 std::vector<Smooth_periodic_function<double>*> result;
170 result.push_back(&components_[0]->rg());
171 switch (ctx_.num_mag_dims()) {
172 case 1: {
173 /* z-component */
174 result.push_back(&components_[1]->rg());
175 break;
176 }
177 case 3: {
178 /* x-component */
179 result.push_back(&components_[2]->rg());
180 /* y-component */
181 result.push_back(&components_[3]->rg());
182 /* z-component */
183 result.push_back(&components_[1]->rg());
184 break;
185 }
186 }
187 return result;
188 }
189};
190
191} // namespace sirius
192
193#endif
Four-component function consisting of scalar and vector parts.
Definition: field4d.hpp:40
auto & scalar()
Return scalar part of the field.
Definition: field4d.hpp:74
std::array< std::unique_ptr< Periodic_function< double > >, 4 > components_
Four components of the field: scalar, vector_z, vector_x, vector_y.
Definition: field4d.hpp:43
auto & component_raise(int i)
Throws error in case of invalid access.
Definition: field4d.hpp:106
auto const & vector(int i) const
Return component of the vector part of the field.
Definition: field4d.hpp:93
auto const & scalar() const
Return scalar part of the field.
Definition: field4d.hpp:80
Field4D(Simulation_context &ctx__, lmax_t lmax__, std::array< periodic_function_ptr_t< double > const *, 4 > ptr__={nullptr, nullptr, nullptr, nullptr})
Constructor.
Definition: field4d.hpp:50
auto & vector(int i)
Return component of the vector part of the field.
Definition: field4d.hpp:86
Simulation context is a set of parameters and objects describing a single simulation.
int num_mag_dims() const
Number of dimensions in the magnetization vector.
Memory management functions and classes.
Namespace of the SIRIUS library.
Definition: sirius.f90:5
Contains declaration and partial implementation of sirius::Periodic_function class.
Describe external pointers to periodic function.
Definition: typedefs.hpp:256
Contains typedefs, enums and simple descriptors.