GRINS-0.8.0
regression_helper.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2017 Paul T. Bauman, Roy H. Stogner
7 // Copyright (C) 2010-2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #ifndef GRINS_REGRESSION_HELPER_H
26 #define GRINS_REGRESSION_HELPER_H
27 
28 // C++
29 #include <limits>
30 
31 // libMesh
32 #include "libmesh/libmesh_common.h"
33 
34 #ifdef GRINS_HAVE_CPPUNIT
35 
36 #include <cppunit/extensions/HelperMacros.h>
37 
38 #endif // GRINS_HAVE_CPPUNIT
39 
40 
41 namespace GRINSTesting
42 {
47  {
48  protected:
49 
51  libMesh::Real linear_slope(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y, unsigned int start_index, unsigned int end_index)
52  {
53  libmesh_assert(x.size() == y.size());
54  libmesh_assert(start_index < end_index);
55 
56  unsigned int n = x.size();
57  libmesh_assert(end_index < n);
58 
59  libMesh::Real sum_y = 0.0,
60  sum_x2 = 0.0,
61  sum_x = 0.0,
62  sum_xy = 0.0;
63 
64  for (unsigned int i=start_index; i<=end_index; i++)
65  {
66  libMesh::Real xi = x[i];
67  libMesh::Real yi = y[i];
68 
69  sum_y += yi;
70  sum_x2 += xi*xi;
71  sum_x += xi;
72  sum_xy += xi*yi;
73  }
74 
75 
76  libMesh::Real slope = ( n*sum_xy - sum_x*sum_y )/( n*sum_x2 - (sum_x*sum_x) );
77  return slope;
78  }
79 
81  libMesh::Real linear_intercept(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y, unsigned int start_index, unsigned int end_index)
82  {
83  libmesh_assert(x.size() == y.size());
84  libmesh_assert(start_index < end_index);
85 
86  unsigned int n = x.size();
87  libmesh_assert(end_index < n);
88 
89  libMesh::Real sum_y = 0.0,
90  sum_x2 = 0.0,
91  sum_x = 0.0,
92  sum_xy = 0.0;
93 
94  for (unsigned int i=start_index; i<=end_index; i++)
95  {
96  libMesh::Real xi = x[i];
97  libMesh::Real yi = y[i];
98 
99  sum_y += yi;
100  sum_x2 += xi*xi;
101  sum_x += xi;
102  sum_xy += xi*yi;
103  }
104 
105  libMesh::Real intercept = ( sum_y*sum_x2 - sum_x*sum_xy )/( n*sum_x2 - (sum_x*sum_x) );
106  return intercept;
107  }
108 
110  void check_convergence_rate(std::vector<libMesh::Real> x, std::vector<libMesh::Real> y, unsigned int start_index, unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
111  {
112  libmesh_assert(tol > 0.0);
113  libmesh_assert(x.size() == y.size());
114  libmesh_assert(start_index < end_index);
115  libmesh_assert(end_index < x.size());
116 
117  libMesh::Real calculated_rate = linear_slope(x,y,start_index,end_index);
118 
119 #ifdef GRINS_HAVE_CPPUNIT
120 
121  CPPUNIT_ASSERT_DOUBLES_EQUAL(convergence_rate,calculated_rate,tol);
122 #else
123  if (std::abs(convergence_rate - calculated_rate) > tol)
124  {
125  std::stringstream ss;
126  ss <<"ERROR" <<std::endl
127  <<"calculated convergence rate: " <<calculated_rate <<std::endl
128  <<"desired convergence rate: " <<convergence_rate <<std::endl
129  <<"tolerance: " <<tol <<std::endl;
130 
131  libmesh_error_msg(ss.str());
132  }
133 #endif // GRINS_HAVE_CPPUNIT
134  }
135 
136  };
137 }
138 
139 #endif // GRINS_REGRESSION_HELPER_H
libMesh::Real linear_intercept(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
y-intercept for linear regression
void check_convergence_rate(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index, libMesh::Real convergence_rate, libMesh::Real tol)
Check the convergence rate of the supplied data to within the given absolute tolerance.
libMesh::Real linear_slope(std::vector< libMesh::Real > x, std::vector< libMesh::Real > y, unsigned int start_index, unsigned int end_index)
Slope for linear regression.

Generated on Tue Dec 19 2017 12:47:29 for GRINS-0.8.0 by  doxygen 1.8.9.1