Intrepid
example_02.cpp
Go to the documentation of this file.
1// @HEADER
2// ************************************************************************
3//
4// Intrepid Package
5// Copyright (2007) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Pavel Bochev (pbboche@sandia.gov)
38// Denis Ridzal (dridzal@sandia.gov), or
39// Kara Peterson (kjpeter@sandia.gov)
40//
41// ************************************************************************
42// @HEADER
43
50#include "Teuchos_GlobalMPISession.hpp"
51
52using namespace std;
53using namespace Intrepid;
54
55int main(int argc, char *argv[]) {
56
57 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
58
59 cout \
60 << "===============================================================================\n" \
61 << "| |\n" \
62 << "| Example use of the FieldContainer class |\n" \
63 << "| |\n" \
64 << "| 1) using FieldContainer in DEBUG mode: |\n" \
65 << "| requires intrepid to be configured with --enable-intrepid-debug |\n" \
66 << "| See /test/FieldContainer/test_02.cpp for more examples |\n" \
67 << "| |\n" \
68 << "| Questions? Contact Pavel Bochev (pbboche@sandia.gov) or |\n" \
69 << "| Denis Ridzal (dridzal@sandia.gov). |\n" \
70 << "| |\n" \
71 << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \
72 << "| Trilinos website: http://trilinos.sandia.gov |\n" \
73 << "| |\n" \
74 << "===============================================================================\n\n";
75
76 // Define variables to create and use FieldContainers
77 Teuchos::Array<int> dimensions;
78 Teuchos::Array<int> multiIndex;
79
80 // Initialize dimensions for rank-4 multi-index value
81 dimensions.resize(4);
82 dimensions[0] = 5;
83 dimensions[1] = 3;
84 dimensions[2] = 2;
85 dimensions[3] = 7;
86
87 // Create a FieldContainer
88 FieldContainer<double> myContainer(dimensions);
89
90 cout << "\n" \
91 << "===============================================================================\n"\
92 << "| EXAMPLE 1: Debug mode |\n"\
93 << "===============================================================================\n\n";
94
95 // Trying to get enumeration using multi-index with the wrong rank (myContainer's rank is 4,
96 // whereas multiIndex has rank 5)
97 cout \
98 << "===============================================================================\n"\
99 << " Trying to get enumeration using multi-index with the wrong rank: \n\n";
100 try{
101 multiIndex.resize(5);
102 multiIndex[0] = 3;
103 multiIndex[1] = 1;
104 multiIndex[2] = 2;
105 multiIndex[3] = 2;
106 multiIndex[4] = 6;
107 myContainer.getEnumeration(multiIndex);
108 }
109 catch (const std::logic_error & err) {
110 cout << err.what() << "\n";
111 }
112
113 // Trying to get enumeration using multi-index that is out of bounds: 3rd index is 4, must be <2
114 cout \
115 << "===============================================================================\n"\
116 << " Trying to get enumeration using multi-index that is out of bounds: \n\n";
117 try{
118 multiIndex.resize(4);
119 multiIndex[0] = 3;
120 multiIndex[1] = 1;
121 multiIndex[2] = 4;
122 multiIndex[3] = 2;
123 myContainer.getEnumeration(multiIndex);
124 }
125 catch (const std::logic_error & err) {
126 cout << err.what() << "\n\n";
127 }
128
129 // Trying to set values from array whose size is less than FieldContainer size
130 cout \
131 << "===============================================================================\n"\
132 << " Trying to set values from array whose size is less than FieldContainer's size: \n\n";
133
134 // Change one of the values of the dimensions to a lesser value: original value was 5
135 dimensions[0] = 4;
136
137 // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
138 Teuchos::Array<double> dataTeuchosArray(4*3*2*7);
139
140 // Fill with data
141 int counter = 0;
142 for(int i=0; i < dimensions[0]; i++){
143 for(int j=0; j < dimensions[1]; j++){
144 for(int k=0; k < dimensions[2]; k++){
145 for(int l = 0; l < dimensions[3]; l++){
146 dataTeuchosArray[counter] = (double)counter;
147 counter++;
148 }
149 }
150 }
151 }
152
153 // Now try to stuff this data into FieldContainer
154 try{
155 myContainer.setValues(dataTeuchosArray);
156 }
157 catch (const std::logic_error & err) {
158 cout << err.what() << "\n";
159 }
160
161 // Trying to set values from array whose size is greater than FieldContainer's size
162 cout \
163 << "===============================================================================\n"\
164 << " Trying to set values from array whose size is greater than FieldContainer's size: \n\n";
165 // Change one of the values of the dimensions to a lesser value: restore dimensions[0] to the
166 // value used to construct the LexArray and change dimensions[2] to a greater value
167 dimensions[0] = 5;
168 dimensions[2] = 3;
169
170 // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
171 dataTeuchosArray.resize(5*3*3*7);
172
173 // Fill with data
174 counter = 0;
175 for(int i=0; i < dimensions[0]; i++){
176 for(int j=0; j < dimensions[1]; j++){
177 for(int k=0; k < dimensions[2]; k++){
178 for(int l = 0; l < dimensions[3]; l++){
179 dataTeuchosArray[counter] = (double)counter;
180 counter++;
181 }
182 }
183 }
184 }
185
186 // Now try to stuff this data into FieldContainer
187 try{
188 myContainer.setValues(dataTeuchosArray);
189 }
190 catch (const std::logic_error & err) {
191 cout << err.what() << "\n";
192 }
193
194
195 // Trying to use [] with enumeration that is out of range (size of myContainer is 210)
196 cout \
197 << "===============================================================================\n"\
198 << " Trying to use [] with enumeration that is out of range: \n\n";
199 try{
200 myContainer[1000];
201 }
202 catch (const std::logic_error & err) {
203 cout << err.what() << "\n\n";
204 }
205
206 // Trying to create FieldContainer using incompatible data array and dimensions. In this example
207 // dataTeuchosArray corresponds to dimensions = {5,3,3,7} but we change the dimensions to one
208 // that does not match the data. Note that if we permute the values in dimensions it will be
209 // compatible with the data because it will specify the same size for the container. However,
210 // index bound permutation reshapes the container!
211 cout \
212 << "===============================================================================\n"\
213 << " Trying to create FieldContainer using incompatible data array and dimensions: \n\n";
214 try{
215 dimensions[0] = 5;
216 dimensions[1] = 3;
217 dimensions[2] = 3;
218 dimensions[3] = 8;
219
220 FieldContainer<double> myOtherContainer(dimensions, dataTeuchosArray);
221 }
222 catch (const std::logic_error & err) {
223 cout << err.what() << endl;
224 }
225
226 return 0;
227}
Header file for utility class to provide multidimensional containers.