PSCF v1.5.0
cpp/CFieldComparison.tpp
1#ifndef PSCF_C_FIELD_COMPARISON_CP_TPP
2#define PSCF_C_FIELD_COMPARISON_CP_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 "CFieldComparison.h"
12#include <pscf/field/cpp/CField.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>
31 double
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 }
56 rmsDiff_ = rmsDiff_/double(n);
57 rmsDiff_ = sqrt(rmsDiff_);
58 return maxDiff_;
59 }
60
61 /*
62 * Comparator for arrays of fields.
63 */
64 template <int D>
66 DArray< CField<D,CPT> > const & b)
67 {
68 UTIL_CHECK(a.capacity() > 0);
69 UTIL_CHECK(a.capacity() == b.capacity());
70 UTIL_CHECK(a[0].capacity() > 0);
71 int m = a.capacity();
72 double diffSq, diff, d0, d1;
73 maxDiff_ = 0.0;
74 rmsDiff_ = 0.0;
75 int i, j, n;
76 for (i = 0; i < m; ++i) {
77 n = a[i].capacity();
78 UTIL_CHECK(n > 0);
79 UTIL_CHECK(n == b[i].capacity());
80 for (j = 0; j < n; ++j) {
81 d0 = a[i][j][0] - b[i][j][0];
82 d1 = a[i][j][1] - b[i][j][1];
83 diffSq = d0*d0 + d1*d1;
84 diff = sqrt(diffSq);
85 if (std::isnan(diff)) {
86 // If either field has a NaN component, set error to very
87 // high value and exit the function
88 maxDiff_ = 1e8;
89 rmsDiff_ = 1e8;
90 return maxDiff_;
91 } else if (diff > maxDiff_) {
92 maxDiff_ = diff;
93 }
94 rmsDiff_ += diffSq;
95 }
96 }
97 rmsDiff_ = rmsDiff_/double(m*n);
98 rmsDiff_ = sqrt(rmsDiff_);
99 return maxDiff_;
100 }
101
102}
103#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.