Intrepid
/usr/src/RPM/BUILD/trilinos10-10.6.4/packages/intrepid/example/FieldContainer/example_01.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_Time.hpp"
00037 #include "Teuchos_GlobalMPISession.hpp"
00038 
00039 using namespace std;
00040 using namespace Intrepid;
00041 
00042 int main(int argc, char *argv[]) {
00043 
00044   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00045 
00046   std::cout \
00047   << "===============================================================================\n" \
00048   << "|                                                                             |\n" \
00049   << "|                  Example use of the FieldContainer class                    |\n" \
00050   << "|                                                                             |\n" \
00051   << "|    1) Creating and filling FieldContainer objects                           |\n" \
00052   << "|    2) Accessing elements in FieldContainer objects                          |\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> dimension;
00064   Teuchos::Array<int> multiIndex;
00065   
00066   std::cout \
00067     << "===============================================================================\n"\
00068     << "| EXAMPLE 1: rank 2 multi-index: {u(p,i) | 0 <= p < 5; 0 <= i < 3 }           |\n"\
00069     << "===============================================================================\n\n";
00070   // This rank 2 multi-indexed value can be used to store values of 3D vector field
00071   // evaluated at 5 points, or values of gradient of scalar field evaluated at the points
00072   
00073   // Resize dimension and multiIndex for rank-2 multi-indexed value 
00074   dimension.resize(2);
00075   multiIndex.resize(2);
00076   
00077   // Load upper ranges for the two indices in the multi-indexed value
00078   dimension[0] = 5;
00079   dimension[1] = 3;
00080     
00081   // Create FieldContainer that can hold the rank-2 multi-indexed value
00082   FieldContainer<double> myContainer(dimension);
00083   
00084   // Fill with some data: leftmost index changes last, rightmost index changes first!  
00085   for(int p = 0; p < dimension[0]; p++){
00086     multiIndex[0] = p;
00087     
00088     for(int i = 0; i < dimension[1]; i++){
00089       multiIndex[1] = i;
00090       
00091       // Load value with multi-index {p,i} to container
00092       myContainer.setValue((double)(i+p), multiIndex);
00093     }
00094   }
00095   
00096   // Show container contents
00097   std::cout << myContainer;
00098   
00099   // Access by overloaded (), multiindex and []:
00100   multiIndex[0] = 3; 
00101   multiIndex[1] = 1;
00102   int enumeration = myContainer.getEnumeration(multiIndex);
00103 
00104   std::cout << "Access by ():          myContainer(" << 3 <<"," << 1 << ") = " << myContainer(3,1) << "\n";  
00105   std::cout << "Access by multi-index: myContainer{" << multiIndex[0] << multiIndex[1] << "} = "<< myContainer.getValue(multiIndex) <<"\n";
00106   std::cout << "Access by enumeration: myContainer[" << enumeration << "] = " << myContainer[enumeration] <<"\n";
00107   
00108   std::cout << "Assigning value by (): \n old value at (3,1) = " << myContainer(3,1) <<"\n";
00109   myContainer(3,1) = 999.99;
00110   std::cout << " new value at (3,1) = " << myContainer(3,1) <<"\n";
00111       
00112   
00113   std::cout << "\n" \
00114   << "===============================================================================\n"\
00115   << "| EXAMPLE 2: rank 3 multi-index: {u(p,i,j) | 0 <=p< 5; 0 <= i<2, 0<=j<3       |\n"\
00116   << "===============================================================================\n\n";
00117   // This rank-3 value can be used to store subset of second partial derivatives  values
00118   // of a scalar function at p points.
00119   
00120   // Resize dimension and multiIndex for rank-3 multi-indexed value 
00121   dimension.resize(3);
00122   multiIndex.resize(3);
00123   
00124   // Define upper ranges for the three indices in the multi-indexed value
00125   dimension[0] = 5;
00126   dimension[1] = 2;
00127   dimension[2] = 3;
00128   
00129   // Reset the existing container to accept rank-3 value with the specified index ranges
00130   myContainer.resize(dimension);
00131   
00132   // Fill with some data
00133   for(int p = 0; p < dimension[0]; p++){
00134     multiIndex[0] = p;
00135     for(int i = 0; i < dimension[1]; i++){
00136       multiIndex[1] = i;
00137       for(int j = 0; j < dimension[2]; j++){
00138         multiIndex[2] = j;
00139         
00140         // Load value with multi-index {p,i} to container
00141         myContainer.setValue((double)(p+i+j), multiIndex);
00142       }
00143     }
00144   }
00145   
00146   // Display contents
00147   std::cout << myContainer;
00148   
00149   // Access by overloaded (), multiindex and []:
00150   multiIndex[0] = 3; 
00151   multiIndex[1] = 1;
00152   multiIndex[2] = 2;
00153   enumeration = myContainer.getEnumeration(multiIndex);
00154     
00155   std::cout << "Access by ():          myContainer(" << 3 <<"," << 1 << "," << 2 << ") = " << myContainer(3,1,2) << "\n";  
00156   std::cout << "Access by multi-index: myContainer{" << multiIndex[0] << multiIndex[1] << multiIndex[2] << "} = "<< myContainer.getValue(multiIndex) <<"\n";
00157   std::cout << "Access by enumeration: myContainer[" << enumeration << "] = " << myContainer[enumeration] <<"\n";
00158 
00159   std::cout << "Assigning value by (): \n old value at (3,1,2) = " << myContainer(3,1,2) <<"\n";
00160   myContainer(3,1,2) = -999.999;
00161   std::cout << " new value at (3,1,2) = " << myContainer(3,1,2) <<"\n";
00162   
00163   
00164   std::cout << "\n" \
00165   << "===============================================================================\n"\
00166   << "| EXAMPLE 4: making rank-5 FieldContainer from data array and index range array |\n"\
00167   << "===============================================================================\n\n";
00168   // Initialize dimension for rank-5 multi-index value
00169   dimension.resize(5);
00170   dimension[0] = 5;
00171   dimension[1] = 2;
00172   dimension[2] = 3;
00173   dimension[3] = 4;
00174   dimension[4] = 6;
00175   
00176   // Define Teuchos::Array to store values with dimension equal to the number of multi-indexed values
00177   Teuchos::Array<double> dataTeuchosArray(5*2*3*4*6);
00178   
00179   // Fill with data
00180   int counter = 0;
00181   for(int i=0; i < dimension[0]; i++){
00182     for(int j=0; j < dimension[1]; j++){
00183       for(int k=0; k < dimension[2]; k++){
00184         for(int l = 0; l < dimension[3]; l++){
00185           for(int m = 0; m < dimension[4]; m++){
00186             dataTeuchosArray[counter] = (double)counter;
00187             counter++;
00188           }
00189         }
00190       }
00191     }
00192   }
00193   
00194   // Create FieldContainer from data array and index array and show it
00195   FieldContainer<double> myNewContainer(dimension, dataTeuchosArray);
00196   std::cout << myNewContainer;
00197 
00198   // Access by overloaded (), multiindex and []:
00199   multiIndex.resize(myNewContainer.rank());
00200   multiIndex[0] = 3; 
00201   multiIndex[1] = 1;
00202   multiIndex[2] = 2;
00203   multiIndex[3] = 2;
00204   multiIndex[4] = 5;
00205   enumeration = myNewContainer.getEnumeration(multiIndex);
00206     
00207   std::cout << "Access by ():          myNewContainer(" << 3 <<"," << 1 << "," << 2 << "," << 2 << "," << 5 << ") = " << myNewContainer(3,1,2,2,5) << "\n";  
00208   std::cout << "Access by multi-index: myNewContainer{" << multiIndex[0] << multiIndex[1] << multiIndex[2] << multiIndex[3] << multiIndex[4] << "} = "<< myNewContainer.getValue(multiIndex) <<"\n";
00209   std::cout << "Access by enumeration: myNewContainer[" << enumeration << "] = " << myNewContainer[enumeration] <<"\n";
00210 
00211   std::cout << "Assigning value by (): \n old value at (3,1,2,2,5) = " << myNewContainer(3,1,2,2,5) <<"\n";
00212   myNewContainer(3,1,2,2,5) = -888.888;
00213   std::cout << " new value at (3,1,2,2,5) = " << myNewContainer(3,1,2,2,5) <<"\n";
00214   
00215   
00216   std::cout << "\n" \
00217     << "===============================================================================\n"\
00218     << "| EXAMPLE 5: making trivial FieldContainers and storing a single zero         |\n"\
00219     << "===============================================================================\n\n";
00220   
00221   // Make trivial container by resetting the index range to zero rank (no indices) and then
00222   // using resize method
00223   dimension.resize(0);
00224   myContainer.resize(dimension);
00225   std::cout << myContainer;
00226   
00227   // Make trivial container by using clear method:
00228   myNewContainer.clear();
00229   std::cout << myNewContainer;
00230   
00231   // Now use initialize() to reset the container to hold a single zero
00232   myNewContainer.initialize();
00233   std::cout << myNewContainer;
00234   
00235   
00236   std::cout << "\n" \
00237     << "===============================================================================\n"\
00238     << "| EXAMPLE 6: Timing read and write operations using () and getValue           |\n"\
00239     << "===============================================================================\n\n";
00240   
00241   // Initialize dimensions for rank-5 multi-index value
00242   int dim0 = 10;     // number of cells
00243   int dim1 = 50;     // number of points
00244   int dim2 = 27;     // number of functions
00245   int dim3 = 3;      // 1st space dim
00246   int dim4 = 3;      // 2nd space dim
00247   
00248   FieldContainer<double> myTensorContainer(dim0, dim1, dim2, dim3, dim4);
00249   multiIndex.resize(myTensorContainer.rank());
00250   double aValue;
00251   
00252   Teuchos::Time timerGetValue("Reading and writing from rank-5 container using getValue");
00253   timerGetValue.start();
00254   for(int i0 = 0; i0 < dim0; i0++){
00255     multiIndex[0] = i0;
00256     for(int i1 = 0; i1 < dim1; i1++){ 
00257       multiIndex[1] = i1;
00258       for(int i2 = 0; i2 < dim2; i2++) {
00259         multiIndex[2] = i2;
00260         for(int i3 = 0; i3 < dim3; i3++) {
00261           multiIndex[3] = i3;
00262           for(int i4 =0; i4 < dim4; i4++) { 
00263             multiIndex[4] = i4;
00264             
00265             aValue = myTensorContainer.getValue(multiIndex);
00266             myTensorContainer.setValue(999.999,multiIndex);
00267             
00268           }
00269         }
00270       }
00271     }
00272   }
00273   timerGetValue.stop();
00274   std::cout << " Time to read and write from container using getValue: " << timerGetValue.totalElapsedTime() <<"\n";
00275   
00276   Teuchos::Time timerRound("Reading and writing from rank-5 container using ()");
00277   timerRound.start();
00278   for(int i0 = 0; i0 < dim0; i0++){
00279     for(int i1 = 0; i1 < dim1; i1++) { 
00280       for(int i2 = 0; i2 < dim2; i2++) {
00281         for(int i3 = 0; i3 < dim3; i3++) {
00282           for(int i4 =0; i4 < dim4; i4++) { 
00283             
00284             aValue = myTensorContainer(i0,i1,i2,i3,i4);
00285             myTensorContainer(i0,i1,i2,i3,i4) = 999.999;
00286             
00287           }
00288         }
00289       }
00290     }
00291   }
00292   timerRound.stop();
00293   std::cout << " Time to read and write from container using (): " << timerRound.totalElapsedTime() <<"\n";
00294     
00295   std::cout << "\n" \
00296     << "===============================================================================\n"\
00297     << "| EXAMPLE 6: Specialized methods of FieldContainer                            |\n"\
00298     << "===============================================================================\n\n";
00299   
00300    return 0;
00301 }
00302 
00303 
00304 
00305 
00306