PSCF v1.1
RFieldComparison.tpp
1#ifndef PSPG_R_FIELD_COMPARISON_TPP
2#define PSPG_R_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 "RFieldComparison.h"
12
13namespace Pscf {
14namespace Pspg {
15
16 // Default Constructor
17 template <int D>
19 {};
20
21 // Comparator for individual fields
22 template <int D>
24 {
25 // Copy data to host.
26 int nPoints = a.capacity();
27 cudaReal* temp_a = new cudaReal[nPoints];
28 cudaReal* temp_b = new cudaReal[nPoints];
29 cudaMemcpy(temp_a, a.cDField(), nPoints*sizeof(cudaReal),
30 cudaMemcpyDeviceToHost);
31 cudaMemcpy(temp_b, b.cDField(), nPoints*sizeof(cudaReal),
32 cudaMemcpyDeviceToHost);
33
34 DArray< cudaReal > h_a, h_b;
35 h_a.allocate(nPoints);
36 h_b.allocate(nPoints);
37 for (int j = 0; j < nPoints; j++) {
38 h_a[j] = temp_a[j];
39 h_b[j] = temp_b[j];
40 }
41 fieldComparison_.compare(h_a,h_b);
42 compared_ = true;
43
44 return fieldComparison_.maxDiff();
45 }
46
47 // Comparator for arrays of fields
48 template <int D>
50 DArray<RDField<D>> const& b)
51 {
52 int nFields = a.capacity();
53 int nPoints = a[0].capacity();
54
55 // Array on host (used for cudaMemcpy)
56 DArray< cudaReal* > temp_a, temp_b;
57 temp_a.allocate(nFields);
58 temp_b.allocate(nFields);
59
60 // DArrays on host (for compatibility with FieldComparison)
62 h_a.allocate(nFields);
63 h_b.allocate(nFields);
64
65 for (int i = 0; i < nFields; i++) {
66 temp_a[i] = new cudaReal[nPoints];
67 temp_b[i] = new cudaReal[nPoints];
68 cudaMemcpy(temp_a[i], a[i].cDField(), nPoints*sizeof(cudaReal),
69 cudaMemcpyDeviceToHost);
70 cudaMemcpy(temp_b[i], b[i].cDField(), nPoints*sizeof(cudaReal),
71 cudaMemcpyDeviceToHost);
72
73 h_a[i].allocate(nPoints);
74 h_b[i].allocate(nPoints);
75
76 for (int j = 0; j < nPoints; j++) {
77 h_a[i][j] = temp_a[i][j];
78 h_b[i][j] = temp_b[i][j];
79 }
80 }
81
82 // Perform comparison
83 fieldComparison_.compare(h_a,h_b);
84 compared_ = true;
85
86 return fieldComparison_.maxDiff();
87 }
88
89}
90}
91#endif
Data * cDField()
Return pointer to underlying C array.
Definition: DField.h:119
int capacity() const
Return allocated size.
Definition: DField.h:112
Field of real single precision values on an FFT mesh on a device.
Definition: RDField.h:34
double compare(RDField< D > const &a, RDField< D > const &b)
Comparator for individual fields.
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:199
C++ namespace for polymer self-consistent field theory (PSCF).