PSCF v1.3.2
Reduce.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Reduce.h"
9#include <util/containers/Array.h>
10#include <cmath>
11
12namespace Pscf {
13namespace Prdc {
14namespace Cpu {
15namespace Reduce {
16
17 /*
18 * Get maximum of array elements.
19 */
20 double max(Array<double> const & in)
21 {
22 int n = in.capacity();
23 UTIL_CHECK(n > 0);
24 double max = in[0];
25 for (int i = 1; i < n; i++) {
26 if (in[i] > max) max = in[i];
27 }
28 return max;
29 }
30
31 /*
32 * Get maximum absolute magnitude of array elements.
33 */
34 double maxAbs(Array<double> const & in)
35 {
36 int n = in.capacity();
37 UTIL_CHECK(n > 0);
38 double val;
39 double max = std::abs(in[0]);
40 for (int i = 1; i < n; i++) {
41 val = std::abs(in[i]);
42 if (val > max) max = val;
43 }
44 return max;
45 }
46
47 /*
48 * Get minimum of array elements.
49 */
50 double min(Array<double> const & in)
51 {
52 int n = in.capacity();
53 UTIL_CHECK(n > 0);
54 double min = in[0];
55 for (int i = 1; i < n; i++) {
56 if (in[i] < min) min = in[i];
57 }
58 return min;
59 }
60
61 /*
62 * Get minimum absolute magnitude of array elements.
63 */
64 double minAbs(Array<double> const & in)
65 {
66 int n = in.capacity();
67 UTIL_CHECK(n > 0);
68 double val;
69 double min = std::abs(in[0]);
70 for (int i = 1; i < n; i++) {
71 val = std::abs(in[i]);
72 if (val < min) min = val;
73 }
74 return min;
75 }
76
77 /*
78 * Compute sum of array elements (GPU kernel wrapper).
79 */
80 double sum(Array<double> const & in)
81 {
82 int n = in.capacity();
83 UTIL_CHECK(n > 0);
84 double sum = 0.0;
85 for (int i = 0; i < n; i++) {
86 sum += in[i];
87 }
88 return sum;
89 }
90
91 /*
92 * Compute inner product of two real arrays.
93 */
94 double innerProduct(Array<double> const & a,
95 Array<double> const & b)
96 {
97 int n = a.capacity();
98 UTIL_CHECK(n > 0);
99 UTIL_CHECK(b.capacity() == n);
100 double sum = 0.0;
101 for (int i = 0; i < n; i++) {
102 sum += a[i]*b[i];
103 }
104 return sum;
105 }
106
107} // Reduce
108} // Cpu
109} // Prdc
110} // Pscf
Array container class template.
Definition Array.h:34
int capacity() const
Return allocated size.
Definition Array.h:159
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
Functions that perform array reductions on the Cpu.
Definition Reduce.cpp:15
double minAbs(Array< double > const &in)
Get minimum absolute magnitude of array elements .
Definition Reduce.cpp:64
double sum(Array< double > const &in)
Compute sum of array elements .
Definition Reduce.cpp:80
double maxAbs(Array< double > const &in)
Get maximum absolute magnitude of array elements .
Definition Reduce.cpp:34
double innerProduct(Array< double > const &a, Array< double > const &b)
Compute inner product of two real arrays .
Definition Reduce.cpp:94
double max(Array< double > const &in)
Get maximum of array elements .
Definition Reduce.cpp:20
double min(Array< double > const &in)
Get minimum of array elements .
Definition Reduce.cpp:50
Fields and FFTs for periodic boundary conditions (CPU)
Definition CField.cpp:12
Periodic fields and crystallography.
Definition CField.cpp:11
PSCF package top-level namespace.