PSCF v1.4.0
cpu/CFieldComparison.tpp
1#ifndef PRDC_CPU_C_FIELD_COMPARISON_TPP
2#define PRDC_CPU_C_FIELD_COMPARISON_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "CFieldComparison.h"
12#include <prdc/cpu/CField.h>
13#include <util/containers/DArray.h>
14#include <cmath>
15
16namespace Pscf {
17namespace Prdc {
18namespace Cpu {
19
20 /*
21 * Default constructor.
22 */
23 template <int D>
25 : maxDiff_(0.0),
26 rmsDiff_(0.0)
27 {};
28
29 /*
30 * Comparator for individual fields.
31 */
32 template <int D>
33 double
35 {
36 UTIL_CHECK(a.capacity() > 0);
37 UTIL_CHECK(a.capacity() == b.capacity());
38 int n = a.capacity();
39 double diffSq, diff, d0, d1;
40 maxDiff_ = 0.0;
41 rmsDiff_ = 0.0;
42 for (int i = 0; i < n; ++i) {
43 d0 = a[i][0] - b[i][0];
44 d1 = a[i][1] - b[i][1];
45 diffSq = d0*d0 + d1*d1;
46 diff = sqrt(diffSq);
47 if (std::isnan(diff)) {
48 // If either field has a NaN component, set error to very
49 // high value and exit the function
50 maxDiff_ = 1e8;
51 rmsDiff_ = 1e8;
52 return maxDiff_;
53 } else if (diff > maxDiff_) {
54 maxDiff_ = diff;
55 }
56 rmsDiff_ += diffSq;
57 //std::cout << i
58 // << " " << a[i][0] << " " << a[i][1]
59 // << " " << b[i][0] << " " << b[i][1]
60 // << " " << diff << std::endl;
61 }
62 rmsDiff_ = rmsDiff_/double(n);
63 rmsDiff_ = sqrt(rmsDiff_);
64 return maxDiff_;
65 }
66
67 /*
68 * Comparator for arrays of fields.
69 */
70 template <int D>
72 DArray< CField<D> > const & b)
73 {
74 UTIL_CHECK(a.capacity() > 0);
75 UTIL_CHECK(a.capacity() == b.capacity());
76 UTIL_CHECK(a[0].capacity() > 0);
77 int m = a.capacity();
78 double diffSq, diff, d0, d1;
79 maxDiff_ = 0.0;
80 rmsDiff_ = 0.0;
81 int i, j, n;
82 for (i = 0; i < m; ++i) {
83 n = a[i].capacity();
84 UTIL_CHECK(n > 0);
85 UTIL_CHECK(n == b[i].capacity());
86 for (j = 0; j < n; ++j) {
87 d0 = a[i][j][0] - b[i][j][0];
88 d1 = a[i][j][1] - b[i][j][1];
89 diffSq = d0*d0 + d1*d1;
90 diff = sqrt(diffSq);
91 if (std::isnan(diff)) {
92 // If either field has a NaN component, set error to very
93 // high value and exit the function
94 maxDiff_ = 1e8;
95 rmsDiff_ = 1e8;
96 return maxDiff_;
97 } else if (diff > maxDiff_) {
98 maxDiff_ = diff;
99 }
100 rmsDiff_ += diffSq;
101 }
102 }
103 rmsDiff_ = rmsDiff_/double(m*n);
104 rmsDiff_ = sqrt(rmsDiff_);
105 return maxDiff_;
106 }
107
108}
109}
110}
111#endif
double compare(CField< D > const &a, CField< D > const &b)
Compare individual fields.
Field of complex double precision values on an FFT mesh.
Definition cpu/CField.h:29
int capacity() const
Return allocated size.
Definition Array.h:144
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
Fields and FFTs for periodic boundary conditions (CPU)
Definition complex.cpp:12
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.