PSCF v1.4.0
cpu/RFieldDftComparison.tpp
1#ifndef PRDC_CPU_R_FIELD_DFT_COMPARISON_TPP
2#define PRDC_CPU_R_FIELD_DFT_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 "RFieldDftComparison.h"
12#include <prdc/cpu/RFieldDft.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>
34 RFieldDft<D> const& b)
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>
71 double
73 DArray< RFieldDft<D> > const & b)
74 {
75 UTIL_CHECK(a.capacity() > 0);
76 UTIL_CHECK(a.capacity() == b.capacity());
77 UTIL_CHECK(a[0].capacity() > 0);
78 int m = a.capacity();
79 double diffSq, diff, d0, d1;
80 maxDiff_ = 0.0;
81 rmsDiff_ = 0.0;
82 int i, j, n;
83 for (i = 0; i < m; ++i) {
84 n = a[i].capacity();
85 UTIL_CHECK(n > 0);
86 UTIL_CHECK(n == b[i].capacity());
87 for (j = 0; j < n; ++j) {
88 d0 = a[i][j][0] - b[i][j][0];
89 d1 = a[i][j][1] - b[i][j][1];
90 diffSq = d0*d0 + d1*d1;
91 diff = sqrt(diffSq);
92 if (std::isnan(diff)) {
93 // If either field has a NaN component, set error to very
94 // high value and exit the function
95 maxDiff_ = 1e8;
96 rmsDiff_ = 1e8;
97 return maxDiff_;
98 } else if (diff > maxDiff_) {
99 maxDiff_ = diff;
100 }
101 rmsDiff_ += diffSq;
102 }
103 }
104 rmsDiff_ = rmsDiff_/double(m*n);
105 rmsDiff_ = sqrt(rmsDiff_);
106 return maxDiff_;
107 }
108
109}
110}
111}
112#endif
double compare(RFieldDft< D > const &a, RFieldDft< D > const &b)
Compare individual fields.
Fourier transform of a real field on an FFT mesh.
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.