PSCF v1.1
pspc/field/KFieldComparison.tpp
1#ifndef PSPC_K_FIELD_COMPARISON_TPP
2#define PSPC_K_FIELD_COMPARISON_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "KFieldComparison.h"
12#include <cmath>
13
14namespace Pscf {
15namespace Pspc {
16
17 // Default Constructor
18 template <int D>
20 : maxDiff_(0.0),
21 rmsDiff_(0.0)
22 {};
23
24 // Comparator for individual fields.
25 template <int D>
27 {
28 UTIL_CHECK(a.capacity() > 0);
29 UTIL_CHECK(a.capacity() == b.capacity());
30 int n = a.capacity();
31 double diffSq, diff, d0, d1;
32 maxDiff_ = 0.0;
33 rmsDiff_ = 0.0;
34 for (int i = 0; i < n; ++i) {
35 d0 = a[i][0] - b[i][0];
36 d1 = a[i][1] - b[i][1];
37 diffSq = d0*d0 + d1*d1;
38 diff = sqrt(diffSq);
39 if (std::isnan(diff)) {
40 // If either field has a NaN component, set error to very
41 // high value and exit the function
42 maxDiff_ = 1e8;
43 rmsDiff_ = 1e8;
44 return maxDiff_;
45 } else if (diff > maxDiff_) {
46 maxDiff_ = diff;
47 }
48 rmsDiff_ += diffSq;
49 //std::cout << i
50 // << " " << a[i][0] << " " << a[i][1]
51 // << " " << b[i][0] << " " << b[i][1]
52 // << " " << diff << std::endl;
53 }
54 rmsDiff_ = rmsDiff_/double(n);
55 rmsDiff_ = sqrt(rmsDiff_);
56 return maxDiff_;
57 }
58
59 // Comparator for arrays of fields
60 template <int D>
62 DArray< RFieldDft<D> > const & b)
63 {
64 UTIL_CHECK(a.capacity() > 0);
65 UTIL_CHECK(a.capacity() == b.capacity());
66 UTIL_CHECK(a[0].capacity() > 0);
67 int m = a.capacity();
68 double diffSq, diff, d0, d1;
69 maxDiff_ = 0.0;
70 rmsDiff_ = 0.0;
71 int i, j, n;
72 for (i = 0; i < m; ++i) {
73 n = a[i].capacity();
74 UTIL_CHECK(n > 0);
75 UTIL_CHECK(n == b[i].capacity());
76 for (j = 0; j < n; ++j) {
77 d0 = a[i][j][0] - b[i][j][0];
78 d1 = a[i][j][1] - b[i][j][1];
79 diffSq = d0*d0 + d1*d1;
80 diff = sqrt(diffSq);
81 if (std::isnan(diff)) {
82 // If either field has a NaN component, set error to very
83 // high value and exit the function
84 maxDiff_ = 1e8;
85 rmsDiff_ = 1e8;
86 return maxDiff_;
87 } else if (diff > maxDiff_) {
88 maxDiff_ = diff;
89 }
90 rmsDiff_ += diffSq;
91 }
92 }
93 rmsDiff_ = rmsDiff_/double(m*n);
94 rmsDiff_ = sqrt(rmsDiff_);
95 return maxDiff_;
96 }
97
98}
99}
100#endif
double compare(RFieldDft< D > const &a, RFieldDft< D > const &b)
Compare individual fields.
Fourier transform of a real field on an FFT mesh.
Definition: RFieldDft.h:31
int capacity() const
Return allocated size.
Definition: Array.h:159
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
C++ namespace for polymer self-consistent field theory (PSCF).