Intrepid
/usr/src/RPM/BUILD/trilinos10-10.6.4/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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Pavel Bochev (pbboche@sandia.gov) or
00025 //                    Denis Ridzal (dridzal@sandia.gov).
00026 //
00027 // ************************************************************************
00028 // @HEADER
00029 
00035 #include "Intrepid_FieldContainer.hpp"
00036 #include "Teuchos_GlobalMPISession.hpp"
00037 
00038 using namespace std;
00039 using namespace Intrepid;
00040 
00041 int main(int argc, char *argv[]) {
00042 
00043   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00044 
00045   cout \
00046   << "===============================================================================\n" \
00047   << "|                                                                             |\n" \
00048   << "|                  Example use of the FieldContainer class                    |\n" \
00049   << "|                                                                             |\n" \
00050   << "|    1) using FieldContainer in DEBUG mode:                                   |\n" \
00051   << "|       requires intrepid to be configured with --enable-intrepid-debug       |\n" \
00052   << "|       See /test/FieldContainer/test_02.cpp for more examples                |\n" \
00053   << "|                                                                             |\n" \
00054   << "|  Questions? Contact  Pavel Bochev (pbboche@sandia.gov) or                   |\n" \
00055   << "|                      Denis Ridzal (dridzal@sandia.gov).                     |\n" \
00056   << "|                                                                             |\n" \
00057   << "|  Intrepid's website: http://trilinos.sandia.gov/packages/intrepid           |\n" \
00058   << "|  Trilinos website:   http://trilinos.sandia.gov                             |\n" \
00059   << "|                                                                             |\n" \
00060   << "===============================================================================\n\n";
00061   
00062   // Define variables to create and use FieldContainers
00063   Teuchos::Array<int> dimensions;
00064   Teuchos::Array<int> multiIndex;
00065   
00066   // Initialize dimensions for rank-4 multi-index value
00067   dimensions.resize(4);
00068   dimensions[0] = 5;
00069   dimensions[1] = 3;
00070   dimensions[2] = 2;
00071   dimensions[3] = 7;
00072   
00073   // Create a FieldContainer
00074   FieldContainer<double> myContainer(dimensions);
00075   
00076   cout << "\n" \
00077     << "===============================================================================\n"\
00078     << "| EXAMPLE 1: Debug mode                                                       |\n"\
00079     << "===============================================================================\n\n";
00080   
00081   // Trying to  get enumeration using multi-index with the wrong rank (myContainer's rank is 4, 
00082   // whereas multiIndex has rank 5)
00083   cout \
00084     << "===============================================================================\n"\
00085     << " Trying to  get enumeration using multi-index with the wrong rank: \n\n";
00086   try{
00087     multiIndex.resize(5);
00088     multiIndex[0] = 3; 
00089     multiIndex[1] = 1;
00090     multiIndex[2] = 2;
00091     multiIndex[3] = 2;
00092     multiIndex[4] = 6;
00093     myContainer.getEnumeration(multiIndex);
00094   }
00095   catch(std::logic_error err){
00096     cout << err.what() << "\n"; 
00097   }
00098   
00099   // Trying to get enumeration using multi-index that is out of bounds: 3rd index is 4, must be <2 
00100   cout \
00101     << "===============================================================================\n"\
00102     << " Trying to get enumeration using multi-index that is out of bounds: \n\n";
00103   try{
00104     multiIndex.resize(4);
00105     multiIndex[0] = 3; 
00106     multiIndex[1] = 1;
00107     multiIndex[2] = 4;
00108     multiIndex[3] = 2;
00109     myContainer.getEnumeration(multiIndex);
00110   }
00111   catch(std::logic_error err){
00112     cout << err.what() << "\n\n"; 
00113   }
00114   
00115   // Trying to set values from array whose size is less than FieldContainer size
00116   cout \
00117     << "===============================================================================\n"\
00118     << " Trying to set values from array whose size is less than FieldContainer's size: \n\n";
00119 
00120   // Change one of the values of the dimensions to a lesser value: original value was 5 
00121   dimensions[0] = 4;
00122   
00123   // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00124   Teuchos::Array<double> dataTeuchosArray(4*3*2*7);
00125   
00126   // Fill with data
00127   int counter = 0;
00128   for(int i=0; i < dimensions[0]; i++){
00129     for(int j=0; j < dimensions[1]; j++){
00130       for(int k=0; k < dimensions[2]; k++){
00131         for(int l = 0; l < dimensions[3]; l++){
00132           dataTeuchosArray[counter] = (double)counter;
00133           counter++;
00134         }
00135       }
00136     }
00137   }
00138   
00139   // Now try to stuff this data into FieldContainer
00140   try{
00141     myContainer.setValues(dataTeuchosArray);
00142   }
00143   catch(std::logic_error err){
00144     cout << err.what() << "\n";
00145   }
00146   
00147   // Trying to set values from array whose size is greater than FieldContainer's size
00148   cout \
00149     << "===============================================================================\n"\
00150     << " Trying to set values from array whose size is greater than FieldContainer's size: \n\n";
00151   // Change one of the values of the dimensions to a lesser value: restore dimensions[0] to the 
00152   // value used to construct the LexArray and change dimensions[2] to a greater value
00153   dimensions[0] = 5;
00154   dimensions[2] = 3;
00155   
00156   // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00157   dataTeuchosArray.resize(5*3*3*7);
00158   
00159   // Fill with data
00160   counter = 0;
00161   for(int i=0; i < dimensions[0]; i++){
00162     for(int j=0; j < dimensions[1]; j++){
00163       for(int k=0; k < dimensions[2]; k++){
00164         for(int l = 0; l < dimensions[3]; l++){
00165           dataTeuchosArray[counter] = (double)counter;
00166           counter++;
00167         }
00168       }
00169     }
00170   }
00171   
00172   // Now try to stuff this data into FieldContainer
00173   try{
00174     myContainer.setValues(dataTeuchosArray);
00175   }
00176   catch(std::logic_error err){
00177     cout << err.what() << "\n";
00178   }
00179   
00180   
00181   // Trying to use [] with enumeration that is out of range (size of myContainer is 210)
00182   cout \
00183     << "===============================================================================\n"\
00184     << " Trying to use [] with enumeration that is out of range: \n\n";
00185   try{
00186     myContainer[1000];
00187   }
00188   catch(std::logic_error err){
00189     cout << err.what() << "\n\n"; 
00190   }
00191   
00192   // Trying to create FieldContainer using incompatible data array and dimensions. In this example
00193   // dataTeuchosArray corresponds to dimensions = {5,3,3,7} but we change the dimensions to one
00194   // that does not match the data. Note that if we permute the values in dimensions it will be
00195   // compatible with the data because it will specify the same size for the container. However, 
00196   // index bound permutation reshapes the container!
00197   cout \
00198     << "===============================================================================\n"\
00199     << " Trying to create FieldContainer using incompatible data array and dimensions: \n\n";
00200   try{
00201     dimensions[0] = 5;
00202     dimensions[1] = 3;
00203     dimensions[2] = 3;
00204     dimensions[3] = 8;
00205     
00206     FieldContainer<double> myOtherContainer(dimensions, dataTeuchosArray);
00207   }
00208   catch(std::logic_error err){
00209     cout << err.what() << endl;
00210   }
00211   
00212   return 0;
00213 }