GQCP
Loading...
Searching...
No Matches
SQOperatorStorageBase.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
24
25#include <algorithm>
26
27
28namespace GQCP {
29
30
31/*
32 * MARK: Implementing SQOperatorStorageBase
33 */
34
44template <typename _MatrixRepresentation, typename _Vectorizer, typename _DerivedOperator>
46public:
47 // The type used to represent the set of parameters/matrix elements/integrals for one component of a second-quantized operator.
48 using MatrixRepresentation = _MatrixRepresentation;
49
50 // The type of the operator that derives from this class, enabling CRTP and compile-time polymorphism.
51 using DerivedOperator = _DerivedOperator;
52
53 // The type of the vectorizer that relates a one-dimensional storage of matrix representations to the tensor structure of the second-quantized operators. This allows for a distinction between scalar operators (such as the kinetic energy or Coulomb operator), vector operators (such as the spin operator) and matrix/tensor operators (such as quadrupole and multipole operators).
54 using Vectorizer = _Vectorizer;
55
56 // The type of 'this'
58
59 // The type of the final derived operator, enabling CRTP and compile-time polymorphism. VectorSpaceArithmetic (and other functionality) should be implemented on the **final** deriving class, not on intermediate classes. In the current design of the SQOperator classes, there is just one intermediate class, so finding out the final derived class is easy.
61
62 // The type that corresponds to the scalar version of the final second-quantized operator operator type.
64
65
66protected:
67 // The array that takes care of the storage of the second quantized operator's matrix elements.
69
70
71public:
72 /*
73 * MARK: Constructors
74 */
75
82 array {array} {
83
84 const auto first_dimension = array.elements()[0].dimension();
85
86 for (const auto& parameters : array.elements()) {
87 if (parameters.dimension() != first_dimension) {
88 throw std::invalid_argument("SQOperatorStorageBase(const StorageArray<MatrixRepresentation, Vectorizer>& array): The dimensions of the matrix representations must be equal.");
89 }
90 }
91 }
92
93
99 template <typename Z = Vectorizer>
101 typename std::enable_if<std::is_same<Z, ScalarVectorizer>::value>::type* = 0) :
103
104
110 template <typename Z = Vectorizer>
111 SQOperatorStorageBase(const std::vector<MatrixRepresentation>& parameters,
112 typename std::enable_if<std::is_same<Z, VectorVectorizer>::value>::type* = 0) :
114
115
121 template <typename Z = Vectorizer>
122 SQOperatorStorageBase(const std::array<MatrixRepresentation, 3>& parameters,
123 typename std::enable_if<std::is_same<Z, VectorVectorizer>::value>::type* = 0) :
124 SQOperatorStorageBase(StorageArray<MatrixRepresentation, VectorVectorizer>(std::vector<MatrixRepresentation>(parameters.begin(), parameters.end()), VectorVectorizer({3}))) {} // Convert the `std::vector` to a `std::array`.
125
126
132
133
145 static FinalOperator Zero(const size_t dim, const Vectorizer& vectorizer) {
146 return FinalOperator {StorageArray<MatrixRepresentation, Vectorizer> {MatrixRepresentation::Zero(dim), vectorizer}};
147 }
148
149
157 template <typename Z = Vectorizer>
159 return Self::Zero(dim, ScalarVectorizer {});
160 }
161
162
170 template <typename Z = Vectorizer>
172 return Self::Zero(dim, VectorVectorizer {{3}});
173 }
174
175
183 static FinalOperator Random(const size_t dim, const Vectorizer& vectorizer) {
184 return FinalOperator {StorageArray<MatrixRepresentation, Vectorizer> {MatrixRepresentation::Random(dim), vectorizer}};
185 }
186
187
195 template <typename Z = Vectorizer>
197 return Self::Random(dim, ScalarVectorizer {});
198 }
199
200
208 template <typename Z = Vectorizer>
210 return Self::Random(dim, VectorVectorizer {{3}});
211 }
212
213
214 /*
215 * MARK: Parameter access
216 */
217
221 const std::vector<MatrixRepresentation>& allParameters() const { return this->array.elements(); }
222
226 std::vector<MatrixRepresentation>& allParameters() { return this->array.elements(); }
227
235 template <typename... Indices>
236 const MatrixRepresentation& parameters(const Indices&... indices) const { return this->array(indices...); }
237
238
246 template <typename... Indices>
247 MatrixRepresentation& parameters(const Indices&... indices) { return this->array(indices...); }
248
249
257 template <typename... Indices>
258 ScalarFinalOperator operator()(const Indices&... indices) const {
259
260 // Access the underlying array's storage, and wrap the result in a scalar form of the derived second-quantized operator. The correct scalar derived operator can be instantiated since the scalar derived operator type has a special constructor that expects just one matrix.
261 return ScalarFinalOperator {this->array(indices...)};
262 }
263
264
265 /*
266 * MARK: General info
267 */
268
272 size_t numberOfComponents() const { return this->array.vectorizer().numberOfElements(); }
273
277 size_t numberOfOrbitals() const { return this->array.elements()[0].dimension(); /* all the dimensions are the same, this is checked in the constructor */ }
278
282 const Vectorizer& vectorizer() const { return this->array.vectorizer(); }
283};
284
285
286} // namespace GQCP
Definition: DenseVectorizer.hpp:49
Definition: SQOperatorStorageBase.hpp:45
static FinalOperator Random(const size_t dim, const Vectorizer &vectorizer)
Definition: SQOperatorStorageBase.hpp:183
std::vector< MatrixRepresentation > & allParameters()
Definition: SQOperatorStorageBase.hpp:226
SQOperatorStorageBase()
Definition: SQOperatorStorageBase.hpp:130
ScalarFinalOperator operator()(const Indices &... indices) const
Definition: SQOperatorStorageBase.hpp:258
const MatrixRepresentation & parameters(const Indices &... indices) const
Definition: SQOperatorStorageBase.hpp:236
size_t numberOfOrbitals() const
Definition: SQOperatorStorageBase.hpp:277
_DerivedOperator DerivedOperator
Definition: SQOperatorStorageBase.hpp:51
static enable_if_t< std::is_same< Z, ScalarVectorizer >::value, FinalOperator > Random(const size_t dim)
Definition: SQOperatorStorageBase.hpp:196
const std::vector< MatrixRepresentation > & allParameters() const
Definition: SQOperatorStorageBase.hpp:221
SQOperatorStorageBase(const std::array< MatrixRepresentation, 3 > &parameters, typename std::enable_if< std::is_same< Z, VectorVectorizer >::value >::type *=0)
Definition: SQOperatorStorageBase.hpp:122
typename OperatorTraits< DerivedOperator >::DerivedOperator FinalOperator
Definition: SQOperatorStorageBase.hpp:60
typename OperatorTraits< FinalOperator >::ScalarOperator ScalarFinalOperator
Definition: SQOperatorStorageBase.hpp:63
_MatrixRepresentation MatrixRepresentation
Definition: SQOperatorStorageBase.hpp:48
_Vectorizer Vectorizer
Definition: SQOperatorStorageBase.hpp:54
static enable_if_t< std::is_same< Z, ScalarVectorizer >::value, FinalOperator > Zero(const size_t dim)
Definition: SQOperatorStorageBase.hpp:158
static enable_if_t< std::is_same< Z, VectorVectorizer >::value, FinalOperator > Random(const size_t dim)
Definition: SQOperatorStorageBase.hpp:209
SQOperatorStorageBase(const StorageArray< MatrixRepresentation, Vectorizer > &array)
Definition: SQOperatorStorageBase.hpp:81
const Vectorizer & vectorizer() const
Definition: SQOperatorStorageBase.hpp:282
static FinalOperator Zero(const size_t dim, const Vectorizer &vectorizer)
Definition: SQOperatorStorageBase.hpp:145
SQOperatorStorageBase(const MatrixRepresentation &parameters, typename std::enable_if< std::is_same< Z, ScalarVectorizer >::value >::type *=0)
Definition: SQOperatorStorageBase.hpp:100
static enable_if_t< std::is_same< Z, VectorVectorizer >::value, FinalOperator > Zero(const size_t dim)
Definition: SQOperatorStorageBase.hpp:171
SQOperatorStorageBase(const std::vector< MatrixRepresentation > &parameters, typename std::enable_if< std::is_same< Z, VectorVectorizer >::value >::type *=0)
Definition: SQOperatorStorageBase.hpp:111
StorageArray< MatrixRepresentation, Vectorizer > array
Definition: SQOperatorStorageBase.hpp:68
size_t numberOfComponents() const
Definition: SQOperatorStorageBase.hpp:272
MatrixRepresentation & parameters(const Indices &... indices)
Definition: SQOperatorStorageBase.hpp:247
Definition: StorageArray.hpp:38
const std::vector< Element > & elements() const
Definition: StorageArray.hpp:161
const Vectorizer & vectorizer() const
Definition: StorageArray.hpp:177
Definition: BaseOneElectronIntegralBuffer.hpp:25
typename std::enable_if< B, T >::type enable_if_t
Definition: type_traits.hpp:37
DenseVectorizer< 0 > ScalarVectorizer
Definition: DenseVectorizer.hpp:203
Definition: OperatorTraits.hpp:28