PSCF v1.5.0
cpp/RFieldDftComparison.tpp
1#ifndef PSCF_CPP_R_FIELD_DFT_COMPARISON_TPP
2#define PSCF_CPP_R_FIELD_DFT_COMPARISON_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "RFieldDftComparison.h"
12#include <pscf/field/cpp/RFieldDft.h>
13#include <util/containers/DArray.h>
14#include <cmath>
15
16namespace Pscf {
17
18 /*
19 * Default constructor.
20 */
21 template <int D>
23 : maxDiff_(0.0),
24 rmsDiff_(0.0)
25 {};
26
27 /*
28 * Comparator for individual fields.
29 */
30 template <int D>
32 RFieldDft<D,CPT> const& b)
33 {
34 UTIL_CHECK(a.capacity() > 0);
35 UTIL_CHECK(a.capacity() == b.capacity());
36 int n = a.capacity();
37 double diffSq, diff, d0, d1;
38 maxDiff_ = 0.0;
39 rmsDiff_ = 0.0;
40 for (int i = 0; i < n; ++i) {
41 d0 = a[i][0] - b[i][0];
42 d1 = a[i][1] - b[i][1];
43 diffSq = d0*d0 + d1*d1;
44 diff = sqrt(diffSq);
45 if (std::isnan(diff)) {
46 // If either field has a NaN component, set error to very
47 // high value and exit the function
48 maxDiff_ = 1e8;
49 rmsDiff_ = 1e8;
50 return maxDiff_;
51 } else if (diff > maxDiff_) {
52 maxDiff_ = diff;
53 }
54 rmsDiff_ += diffSq;
55 //std::cout << i
56 // << " " << a[i][0] << " " << a[i][1]
57 // << " " << b[i][0] << " " << b[i][1]
58 // << " " << diff << std::endl;
59 }
60 rmsDiff_ = rmsDiff_/double(n);
61 rmsDiff_ = sqrt(rmsDiff_);
62 return maxDiff_;
63 }
64
65 /*
66 * Comparator for arrays of fields.
67 */
68 template <int D>
69 double
71 DArray< RFieldDft<D,CPT> > const & b)
72 {
73 UTIL_CHECK(a.capacity() > 0);
74 UTIL_CHECK(a.capacity() == b.capacity());
75 UTIL_CHECK(a[0].capacity() > 0);
76 int m = a.capacity();
77 double diffSq, diff, d0, d1;
78 maxDiff_ = 0.0;
79 rmsDiff_ = 0.0;
80 int i, j, n;
81 for (i = 0; i < m; ++i) {
82 n = a[i].capacity();
83 UTIL_CHECK(n > 0);
84 UTIL_CHECK(n == b[i].capacity());
85 for (j = 0; j < n; ++j) {
86 d0 = a[i][j][0] - b[i][j][0];
87 d1 = a[i][j][1] - b[i][j][1];
88 diffSq = d0*d0 + d1*d1;
89 diff = sqrt(diffSq);
90 if (std::isnan(diff)) {
91 // If either field has a NaN component, set error to very
92 // high value and exit the function
93 maxDiff_ = 1e8;
94 rmsDiff_ = 1e8;
95 return maxDiff_;
96 } else if (diff > maxDiff_) {
97 maxDiff_ = diff;
98 }
99 rmsDiff_ += diffSq;
100 }
101 }
102 rmsDiff_ = rmsDiff_/double(m*n);
103 rmsDiff_ = sqrt(rmsDiff_);
104 return maxDiff_;
105 }
106
107} // namespace Pscf
108#endif
Dynamically allocatable contiguous array template.
Definition DArray.h:32
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.