PSCF v1.1
FieldComparison.tpp
1#ifndef PSCF_FIELD_COMPARISON_TPP
2#define PSCF_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 "FieldComparison.h"
12#include <cmath>
13
14namespace Pscf {
15
16 using namespace Util;
17
18 // Default Constructor
19 template <class FT>
21 : maxDiff_(0.0),
22 rmsDiff_(0.0),
23 begin_(begin)
24 {};
25
26 // Comparator for individual fields.
27 template <class FT>
28 double FieldComparison<FT>::compare(FT const& a, FT const& b)
29 {
30 UTIL_CHECK(a.capacity() > 0);
31 UTIL_CHECK(a.capacity() == b.capacity());
32 int n = a.capacity();
33 double diff;
34 maxDiff_ = 0.0;
35 rmsDiff_ = 0.0;
36 for (int i = begin_; i < n; ++i) {
37 diff = std::abs(a[i] - b[i]);
38 if (std::isnan(diff)) {
39 // If either field has a NaN component, set error to very
40 // high value and exit the function
41 maxDiff_ = 1e8;
42 rmsDiff_ = 1e8;
43 return maxDiff_;
44 } else if (diff > maxDiff_) {
45 maxDiff_ = diff;
46 }
47 rmsDiff_ += diff*diff;
48 }
49 rmsDiff_ = rmsDiff_/double(n);
50 rmsDiff_ = sqrt(rmsDiff_);
51 return maxDiff_;
52 }
53
54 // Comparator for arrays of fields
55 template <class FT>
57 DArray<FT> const & b)
58 {
59 UTIL_CHECK(a.capacity() > 0);
60 UTIL_CHECK(a.capacity() == b.capacity());
61 UTIL_CHECK(a[0].capacity() > 0);
62 int m = a.capacity();
63 double diff;
64 maxDiff_ = 0.0;
65 rmsDiff_ = 0.0;
66 int i, j, n;
67 for (i = 0; i < m; ++i) {
68 n = a[i].capacity();
69 UTIL_CHECK(n > 0);
70 UTIL_CHECK(n == b[i].capacity());
71 for (j = begin_; j < n; ++j) {
72 diff = std::abs(a[i][j] - b[i][j]);
73 if (std::isnan(diff)) {
74 // If either field has a NaN component, set error to very
75 // high value and exit the function
76 maxDiff_ = 1e8;
77 rmsDiff_ = 1e8;
78 return maxDiff_;
79 } else if (diff > maxDiff_) {
80 maxDiff_ = diff;
81 }
82 rmsDiff_ += diff*diff;
83 }
84 }
85 rmsDiff_ = rmsDiff_/double(m*n);
86 rmsDiff_ = sqrt(rmsDiff_);
87 return maxDiff_;
88 }
89
90}
91#endif
FieldComparison(int begin=0)
Default constructor.
double compare(FT const &a, FT const &b)
Compare individual fields.
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).
Utility classes for scientific computation.
Definition: accumulators.mod:1