Intrepid
http://trilinos.sandia.gov/packages/docs/r10.10/packages/intrepid/example/FieldContainer/example_02.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ************************************************************************
00003 //
00004 //                           Intrepid Package
00005 //                 Copyright (2007) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Pavel Bochev  (pbboche@sandia.gov)
00038 //                    Denis Ridzal  (dridzal@sandia.gov), or
00039 //                    Kara Peterson (kjpeter@sandia.gov)
00040 //
00041 // ************************************************************************
00042 // @HEADER
00043 
00049 #include "Intrepid_FieldContainer.hpp"
00050 #include "Teuchos_GlobalMPISession.hpp"
00051 
00052 using namespace std;
00053 using namespace Intrepid;
00054 
00055 int main(int argc, char *argv[]) {
00056 
00057   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00058 
00059   cout \
00060   << "===============================================================================\n" \
00061   << "|                                                                             |\n" \
00062   << "|                  Example use of the FieldContainer class                    |\n" \
00063   << "|                                                                             |\n" \
00064   << "|    1) using FieldContainer in DEBUG mode:                                   |\n" \
00065   << "|       requires intrepid to be configured with --enable-intrepid-debug       |\n" \
00066   << "|       See /test/FieldContainer/test_02.cpp for more examples                |\n" \
00067   << "|                                                                             |\n" \
00068   << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov) or                   |\n" \
00069   << "|                      Denis Ridzal (dridzal@sandia.gov).                     |\n" \
00070   << "|                                                                             |\n" \
00071   << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00072   << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00073   << "|                                                                             |\n" \
00074   << "===============================================================================\n\n";
00075   
00076   // Define variables to create and use FieldContainers
00077   Teuchos::Array<int> dimensions;
00078   Teuchos::Array<int> multiIndex;
00079   
00080   // Initialize dimensions for rank-4 multi-index value
00081   dimensions.resize(4);
00082   dimensions[0] = 5;
00083   dimensions[1] = 3;
00084   dimensions[2] = 2;
00085   dimensions[3] = 7;
00086   
00087   // Create a FieldContainer
00088   FieldContainer<double> myContainer(dimensions);
00089   
00090   cout << "\n" \
00091     << "===============================================================================\n"\
00092     << "| EXAMPLE 1: Debug mode                                                       |\n"\
00093     << "===============================================================================\n\n";
00094   
00095   // Trying to  get enumeration using multi-index with the wrong rank (myContainer's rank is 4, 
00096   // whereas multiIndex has rank 5)
00097   cout \
00098     << "===============================================================================\n"\
00099     << " Trying to  get enumeration using multi-index with the wrong rank: \n\n";
00100   try{
00101     multiIndex.resize(5);
00102     multiIndex[0] = 3; 
00103     multiIndex[1] = 1;
00104     multiIndex[2] = 2;
00105     multiIndex[3] = 2;
00106     multiIndex[4] = 6;
00107     myContainer.getEnumeration(multiIndex);
00108   }
00109   catch(std::logic_error err){
00110     cout << err.what() << "\n"; 
00111   }
00112   
00113   // Trying to get enumeration using multi-index that is out of bounds: 3rd index is 4, must be <2 
00114   cout \
00115     << "===============================================================================\n"\
00116     << " Trying to get enumeration using multi-index that is out of bounds: \n\n";
00117   try{
00118     multiIndex.resize(4);
00119     multiIndex[0] = 3; 
00120     multiIndex[1] = 1;
00121     multiIndex[2] = 4;
00122     multiIndex[3] = 2;
00123     myContainer.getEnumeration(multiIndex);
00124   }
00125   catch(std::logic_error err){
00126     cout << err.what() << "\n\n"; 
00127   }
00128   
00129   // Trying to set values from array whose size is less than FieldContainer size
00130   cout \
00131     << "===============================================================================\n"\
00132     << " Trying to set values from array whose size is less than FieldContainer's size: \n\n";
00133 
00134   // Change one of the values of the dimensions to a lesser value: original value was 5 
00135   dimensions[0] = 4;
00136   
00137   // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00138   Teuchos::Array<double> dataTeuchosArray(4*3*2*7);
00139   
00140   // Fill with data
00141   int counter = 0;
00142   for(int i=0; i < dimensions[0]; i++){
00143     for(int j=0; j < dimensions[1]; j++){
00144       for(int k=0; k < dimensions[2]; k++){
00145         for(int l = 0; l < dimensions[3]; l++){
00146           dataTeuchosArray[counter] = (double)counter;
00147           counter++;
00148         }
00149       }
00150     }
00151   }
00152   
00153   // Now try to stuff this data into FieldContainer
00154   try{
00155     myContainer.setValues(dataTeuchosArray);
00156   }
00157   catch(std::logic_error err){
00158     cout << err.what() << "\n";
00159   }
00160   
00161   // Trying to set values from array whose size is greater than FieldContainer's size
00162   cout \
00163     << "===============================================================================\n"\
00164     << " Trying to set values from array whose size is greater than FieldContainer's size: \n\n";
00165   // Change one of the values of the dimensions to a lesser value: restore dimensions[0] to the 
00166   // value used to construct the LexArray and change dimensions[2] to a greater value
00167   dimensions[0] = 5;
00168   dimensions[2] = 3;
00169   
00170   // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00171   dataTeuchosArray.resize(5*3*3*7);
00172   
00173   // Fill with data
00174   counter = 0;
00175   for(int i=0; i < dimensions[0]; i++){
00176     for(int j=0; j < dimensions[1]; j++){
00177       for(int k=0; k < dimensions[2]; k++){
00178         for(int l = 0; l < dimensions[3]; l++){
00179           dataTeuchosArray[counter] = (double)counter;
00180           counter++;
00181         }
00182       }
00183     }
00184   }
00185   
00186   // Now try to stuff this data into FieldContainer
00187   try{
00188     myContainer.setValues(dataTeuchosArray);
00189   }
00190   catch(std::logic_error err){
00191     cout << err.what() << "\n";
00192   }
00193   
00194   
00195   // Trying to use [] with enumeration that is out of range (size of myContainer is 210)
00196   cout \
00197     << "===============================================================================\n"\
00198     << " Trying to use [] with enumeration that is out of range: \n\n";
00199   try{
00200     myContainer[1000];
00201   }
00202   catch(std::logic_error err){
00203     cout << err.what() << "\n\n"; 
00204   }
00205   
00206   // Trying to create FieldContainer using incompatible data array and dimensions. In this example
00207   // dataTeuchosArray corresponds to dimensions = {5,3,3,7} but we change the dimensions to one
00208   // that does not match the data. Note that if we permute the values in dimensions it will be
00209   // compatible with the data because it will specify the same size for the container. However, 
00210   // index bound permutation reshapes the container!
00211   cout \
00212     << "===============================================================================\n"\
00213     << " Trying to create FieldContainer using incompatible data array and dimensions: \n\n";
00214   try{
00215     dimensions[0] = 5;
00216     dimensions[1] = 3;
00217     dimensions[2] = 3;
00218     dimensions[3] = 8;
00219     
00220     FieldContainer<double> myOtherContainer(dimensions, dataTeuchosArray);
00221   }
00222   catch(std::logic_error err){
00223     cout << err.what() << endl;
00224   }
00225   
00226   return 0;
00227 }