CppNoddy  0.92
Loading...
Searching...
No Matches
OneD_Node_Mesh.h
Go to the documentation of this file.
1/// \file OneD_Node_Mesh.h
2/// A specification for a one dimensional mesh object.
3
4#ifndef ONED_NODE_MESH_H
5#define ONED_NODE_MESH_H
6
7#include <complex>
8#include <vector>
9#include <fstream>
10#include <iostream>
11#include <iomanip>
12#include <cassert>
13
14#include <DenseVector.h>
15
16namespace CppNoddy {
17
18 /// A one dimensional mesh utility object. Data can be placed
19 /// into the mesh for interpolation to a new mesh or computation
20 /// of integrals of the data. The default typing assumes that the
21 /// mesh is along the real line. You can store (complex) data across a set
22 /// of points in the complex plane using the second typename.
23 template < typename _Type, typename _Xtype = double >
25 public:
26
27 /// Default constructor
29 // default to zero variables
30 m_nv = 0;
31 }
32
33 /// ctor for a given nodal distribution
34 /// \param nodes The positions of the nodal points
35 /// \param nvars The number of variables to store in the mesh
36 OneD_Node_Mesh(const DenseVector<_Xtype>& nodes, const std::size_t nvars) :
37 m_nv(nvars), m_X(nodes) {
38#ifdef PARANOID
39 for(unsigned i = 1; i < m_X.size(); ++i) {
40 /*
41 if ( m_X[i+1] < m_X[i] )
42 {
43 std::string problem;
44 problem = "The OneD_Node_Mesh has been passed a vector of nodes that are\n";
45 problem += "not in INCREASING order. This will screw up some of the methods\n";
46 problem += "in the class. We should fix this .....\n";
47 throw ExceptionRuntime( problem );
48 }
49 */
50 }
51#endif
52 // set the contents to zero
53 m_vars = DenseVector<_Type>(m_nv * m_X.size(), _Type(0.0));
54 }
55
56 /// implicit conversion ctor for D_complex from double data
57 template <typename _sourceType>
59
60 /// ctor from an existing file
61 /// \param filename Filename of the data file
62 /// \param nnodes Number of nodes
63 /// \param nvars Number of variables stored at each node
64 OneD_Node_Mesh(std::string filename, const std::size_t nnodes, const std::size_t nvars ) :
65 m_nv(nvars) {
66 m_X = DenseVector<_Xtype>(nnodes, _Xtype(0.0) ); //coordinates, currently empty
67 m_vars = DenseVector<_Type>(nvars*nnodes, _Type(0.0) ); // nodal values
68 read(filename, true); // true => reset the nodal coordinates using file
69 }
70
71 /// Destructor
73 {}
74
75 /// Access a variable at a node
76 /// \param i The index of the node to be accessed
77 /// \param var The variable to return the data for
78 /// \return The variable stored at the node
79 _Type& operator()(const std::size_t i, const std::size_t var);
80
81 /// Access a variable at a node
82 /// \param i The index of the node to be accessed
83 /// \param var The variable to return the data for
84 /// \return The variable stored at the node
85 const _Type& operator()(const std::size_t i, const std::size_t var) const;
86
87 /// Access a nodal position
88 /// \param node The nodal position to return
89 /// \return The spatial position of this node
90 const _Xtype& coord(const std::size_t& node) const;
91
92 /// Access a nodal position
93 /// \param node The nodal position to return
94 /// \return The spatial position of this node
95 _Xtype& coord(const std::size_t& node);
96
97 /// Set the variables stored at A SPECIFIED node
98 /// \param node The nodal index to be set
99 /// \param u The vector of VARIABLES to be written to this nodal point
100 void set_nodes_vars(const std::size_t node, const DenseVector<_Type>& u);
101
102 /// Get the variables stored at A SPECIFIED node
103 /// \param node The nodal index to be returned
104 /// \return The vector of VARIABLES stored at this nodal point
105 DenseVector<_Type> get_nodes_vars(const std::size_t& node) const;
106
107 /// Get the variable data at an interpolated position using
108 /// a first order scheme. Doesn't really make sense unless the
109 /// data is along the real line.
110 /// \param pos The position to interpolate at.
111 /// \return A vector of interpolated variables.
113
114 /// \return The number of nodal points in the mesh
115 std::size_t get_nnodes() const;
116
117 /// \return The number of variables that have data stored at
118 /// each nodal point
119 std::size_t get_nvars() const;
120
121 /// \return A vector of the nodal positions for this mesh
122 const DenseVector<_Xtype>& nodes() const;
123
124 /// Find a list of approximate locations at which a specified
125 /// variable attains a given value. First order only.
126 /// \param var The variable to be examined for zeros
127 /// \param value The value to find
128 DenseVector<double> find_roots1(const std::size_t& var, double value = 0.0) const;
129
130 /// Integrate over the domain. Typically useful for finite volume methods.
131 /// \param var The variable-index to be integrated over the mesh using a
132 /// trapezium rule.
133 /// \return The integral value.
134 _Type integral2(std::size_t var = 0) const;
135
136 /// Compute the integral of the absolute variable squared: |variable|^2.
137 /// \param var The variable-index to be integrated
138 /// \return The integral of the square of the absolute value.
139 _Xtype squared_integral2(std::size_t var = 0) const;
140
141 /// Integrate over the domain with a Simpson rule.
142 /// \param var The variable-index to be integrated over the mesh using a
143 /// trapezium rule.
144 /// \return The integral value.
145 _Type integral4(std::size_t var = 0) const;
146
147 /// For each nodal point we push each variable into a vector
148 /// in sequence. So the returned vector has the data
149 /// v_00,,...,v_0n,v_10,...,v1n,....v_mn, where the first index
150 /// denotes the nodal point 0-m and the second the variable 0-n.
151 /// \return A vector of the variables stored in the mesh
152 const DenseVector<_Type>& vars_as_vector() const;
153
154 /// Set the variables of this mesh from a vector.
155 /// \param vec The vector to be used.
157
158 /// \return An STL vector of dense vectors of the variables in the mesh
159 const std::vector<DenseVector<_Type> >& get_vars() const;
160
161 /// A simple method for dumping data to std::cout
162 void dump() const;
163
164 /// A simple method for dumping data to a file for gnuplot
165 /// \param filename The filename to write the data to (will overwrite)
166 /// \param precision Precision of the output strings
167 void dump_gnu(std::string filename, int precision = 10) const;
168
169 /// Interpolate this mesh data (linearly) into a new
170 /// mesh with nodal points defined in the argument list.
171 /// \param z The nodal coordinates to be used in the new mesh.
173
174 /// Scale the whole contents of the mesh.
175 /// \param x The value to multiply the contents of the mesh by
176 void scale(_Type x) {
177 m_vars.scale(x);
178 }
179
180 /// Normalise all data in the mesh based on one variable.
181 /// \param var This var will have its peak (absolute) value as +/-unity following
182 /// the normalisation. All other variables will also be rescaled by
183 /// the same amount.
184 void normalise(const std::size_t& var) {
185 double maxval(max_abs(var));
186 m_vars.scale(1./maxval);
187 }
188
189 // double maxAbsLocation(unsigned var);
190 // double dep_max_abs_location(unsigned var) {
191 // double max(0.0);
192 // std::size_t maxIndex(0);
193 // // step through the nodes
194 // for(std::size_t node = 0; node < m_X.size(); ++node) {
195 // if(std::abs(m_vars[ node * m_nv + var ]) > max) {
196 // maxIndex = node;
197 // max = std::abs( m_vars[ maxIndex*m_nv + var ]);
198 // }
199 // }
200 // if ( ( maxIndex == 0 ) || ( maxIndex == m_X.size()-1 ) ) {
201 // std::cout << "[WARNING] MaxAbsLocation: maximumum absolute nodal value is first/last node. \n";
202 // return m_X[ maxIndex ];
203 // }
204 // double f1,f2,f3;
205 // double x1,x2,x3;
206 // f1 = std::abs(m_vars[ (maxIndex-1) * m_nv + var ]);
207 // f2 = std::abs(m_vars[ maxIndex * m_nv + var ]);
208 // f3 = std::abs(m_vars[ (maxIndex+1) * m_nv + var ]);
209 // x1 = m_X[maxIndex-1];
210 // x2 = m_X[maxIndex];
211 // x3 = m_X[maxIndex+1];
212 // return ( f1*(x2+x3)/((x1-x2)*(x1-x3)) + f2*(x1+x3)/((x2-x1)*(x2-x3)) + f3*(x1+x2)/((x3-x1)*(x3-x2)) )
213 // / ( 2.*f1/((x1-x2)*(x1-x3)) + 2.*f2/((x2-x1)*(x2-x3)) + 2.*f3/((x3-x1)*(x3-x2)) );
214 // }
215
216 /// Find the maximum stored absolute value in the mesh for a given variable in a range
217 /// of the domain
218 /// \param var The variable index whose maximum is being asked for
219 /// \param left Only examine the sub-range x>left
220 /// \param right Only examine the sub-range x<right
221 /// \return The value of the maximum (abs value)
222 //double dep_max_abs_location_range(unsigned var, double left, double right);
223
224
225 /// Find the maximum stored absolute value in the mesh for a given variable -- no interpolation is used
226 /// \param var The variable index whose maximum is being asked for
227 /// \return The value of the maximum (abs value)
228 double max_abs(unsigned var) {
229 double max(0.0);
230 // step through the nodes
231 for(unsigned node = 0; node < m_X.size(); ++node) {
232 if(std::abs(m_vars[ node * m_nv + var ]) > max) {
233 max = std::abs(m_vars[ node * m_nv + var ]);
234 }
235 }
236 return max;
237 }
238
239 /// Assign mesh contents using a filename
240 /// \param filename Filename that contains the data
241 /// \param reset A boolean, if true then coordinate data is overwritten using the file data. Default is false.
242 void read( std::string filename, const bool reset = false );
243
244 protected:
245
246 // number of variables
247 std::size_t m_nv;
248 // store nodal points
250 // store the nodal values
252
253 };
254
255 // INLINE THE ACCESS METHODS
256
257 template<typename _Type, typename _Xtype>
258 template<typename _sourceType>
260 // there is implicit conversion of DenseVector so this will
261 // allow copy of OneD_Node_Mesh<double> to OneD_Node_Mesh<D_complex>.
262 m_nv = source.get_nvars();
263 m_X = source.nodes();
264 m_vars = source.vars_as_vector();
265 }
266
267 template < typename _Type, typename _Xtype >
268 inline _Type& OneD_Node_Mesh<_Type, _Xtype>::operator()(const std::size_t i, const std::size_t var) {
269 return m_vars[ i * m_nv + var ];
270 }
271
272 template < typename _Type, typename _Xtype >
273 inline const _Type& OneD_Node_Mesh<_Type, _Xtype>::operator()(const std::size_t i, const std::size_t var) const {
274 return m_vars[ i * m_nv + var ];
275 }
276
277 template < typename _Type, typename _Xtype >
278 inline const _Xtype& OneD_Node_Mesh<_Type, _Xtype>::coord(const std::size_t& node) const {
279 return m_X[ node ];
280 }
281
282 template < typename _Type, typename _Xtype >
283 inline _Xtype& OneD_Node_Mesh<_Type, _Xtype>::coord(const std::size_t& node) {
284 return m_X[ node ];
285 }
286
287}
288
289#endif // ONED_NODE_MESH_H
Specification for a templated DenseVector class – a dense, dynamic, vector object.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A one dimensional mesh utility object.
void normalise(const std::size_t &var)
Normalise all data in the mesh based on one variable.
OneD_Node_Mesh(std::string filename, const std::size_t nnodes, const std::size_t nvars)
ctor from an existing file
_Xtype squared_integral2(std::size_t var=0) const
Compute the integral of the absolute variable squared: |variable|^2.
std::size_t get_nvars() const
void set_vars_from_vector(const DenseVector< _Type > &vec)
Set the variables of this mesh from a vector.
const DenseVector< _Xtype > & nodes() const
void read(std::string filename, const bool reset=false)
Assign mesh contents using a filename.
DenseVector< _Type > get_nodes_vars(const std::size_t &node) const
Get the variables stored at A SPECIFIED node.
void dump() const
A simple method for dumping data to std::cout.
OneD_Node_Mesh(const DenseVector< _Xtype > &nodes, const std::size_t nvars)
ctor for a given nodal distribution
virtual ~OneD_Node_Mesh()
Destructor.
DenseVector< _Type > get_interpolated_vars(const _Xtype &pos) const
Get the variable data at an interpolated position using a first order scheme.
const _Xtype & coord(const std::size_t &node) const
Access a nodal position.
void set_nodes_vars(const std::size_t node, const DenseVector< _Type > &u)
Set the variables stored at A SPECIFIED node.
OneD_Node_Mesh()
Default constructor.
_Type & operator()(const std::size_t i, const std::size_t var)
Access a variable at a node.
double max_abs(unsigned var)
Find the maximum stored absolute value in the mesh for a given variable in a range of the domain.
const DenseVector< _Type > & vars_as_vector() const
For each nodal point we push each variable into a vector in sequence.
const std::vector< DenseVector< _Type > > & get_vars() const
std::size_t get_nnodes() const
_Xtype & coord(const std::size_t &node)
Access a nodal position.
void scale(_Type x)
Scale the whole contents of the mesh.
DenseVector< _Type > m_vars
_Type integral4(std::size_t var=0) const
Integrate over the domain with a Simpson rule.
DenseVector< _Xtype > m_X
OneD_Node_Mesh(const OneD_Node_Mesh< _sourceType > &source)
implicit conversion ctor for D_complex from double data
DenseVector< double > find_roots1(const std::size_t &var, double value=0.0) const
Find a list of approximate locations at which a specified variable attains a given value.
_Type integral2(std::size_t var=0) const
Integrate over the domain.
void remesh1(const DenseVector< _Xtype > &z)
Interpolate this mesh data (linearly) into a new mesh with nodal points defined in the argument list.
const _Type & operator()(const std::size_t i, const std::size_t var) const
Access a variable at a node.
void dump_gnu(std::string filename, int precision=10) const
A simple method for dumping data to a file for gnuplot.
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt