Intrepid
Intrepid_ArrayToolsDefContractions.hpp
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 
49 namespace Intrepid {
50 
51 template<class Scalar, class ArrayOutFields, class ArrayInFieldsLeft, class ArrayInFieldsRight>
52 void ArrayTools::contractFieldFieldScalar(ArrayOutFields & outputFields,
53  const ArrayInFieldsLeft & leftFields,
54  const ArrayInFieldsRight & rightFields,
55  const ECompEngine compEngine,
56  const bool sumInto) {
57 
58 
59 #ifdef HAVE_INTREPID_DEBUG
60  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.rank() != 3 ), std::invalid_argument,
61  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Rank of the left input argument must equal 3!");
62  TEUCHOS_TEST_FOR_EXCEPTION( (rightFields.rank() != 3 ), std::invalid_argument,
63  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Rank of right input argument must equal 3!");
64  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 3 ), std::invalid_argument,
65  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Rank of output argument must equal 3!");
66  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
67  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
68  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(2) != rightFields.dimension(2) ), std::invalid_argument,
69  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Second dimensions (numbers of integration points) of the left and right input containers must agree!");
70  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
71  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
72  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != leftFields.dimension(1) ), std::invalid_argument,
73  ">>> ERROR (ArrayTools::contractFieldFieldScalar): First dimension of output container and first dimension of left input container must agree!");
74  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(2) != rightFields.dimension(1) ), std::invalid_argument,
75  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Second dimension of output container and first dimension of right input container must agree!");
76 #endif
77 
78  // get sizes
79  int numCells = leftFields.dimension(0);
80  int numLeftFields = leftFields.dimension(1);
81  int numRightFields = rightFields.dimension(1);
82  int numPoints = leftFields.dimension(2);
83 
84  switch(compEngine) {
85  case COMP_CPP: {
86  if (sumInto) {
87  for (int cl = 0; cl < numCells; cl++) {
88  for (int lbf = 0; lbf < numLeftFields; lbf++) {
89  for (int rbf = 0; rbf < numRightFields; rbf++) {
90  Scalar tmpVal(0);
91  for (int qp = 0; qp < numPoints; qp++) {
92  tmpVal += leftFields(cl, lbf, qp)*rightFields(cl, rbf, qp);
93  } // P-loop
94  outputFields(cl, lbf, rbf) += tmpVal;
95  } // R-loop
96  } // L-loop
97  } // C-loop
98  }
99  else {
100  for (int cl = 0; cl < numCells; cl++) {
101  for (int lbf = 0; lbf < numLeftFields; lbf++) {
102  for (int rbf = 0; rbf < numRightFields; rbf++) {
103  Scalar tmpVal(0);
104  for (int qp = 0; qp < numPoints; qp++) {
105  tmpVal += leftFields(cl, lbf, qp)*rightFields(cl, rbf, qp);
106  } // P-loop
107  outputFields(cl, lbf, rbf) = tmpVal;
108  } // R-loop
109  } // L-loop
110  } // C-loop
111  }
112  }
113  break;
114 
115  case COMP_BLAS: {
116  /*
117  GEMM parameters and their values.
118  (Note: It is assumed that the result needs to be transposed into row-major format.
119  Think of left and right input matrices as A(p x l) and B(p x r), respectively,
120  even though the indexing is ((C),L,P) and ((C),R,P). Due to BLAS formatting
121  assumptions, we are computing (A^T*B)^T = B^T*A.)
122  TRANSA TRANS
123  TRANSB NO_TRANS
124  M #rows(B^T) = number of right fields
125  N #cols(A) = number of left fields
126  K #cols(B^T) = number of integration points
127  ALPHA 1.0
128  A right data for cell cl = &rightFields[cl*skipR]
129  LDA #rows(B) = number of integration points
130  B left data for cell cl = &leftFields[cl*skipL]
131  LDB #rows(A) = number of integration points
132  BETA 0.0
133  C result for cell cl = &outputFields[cl*skipOp]
134  LDC #rows(C) = number of right fields
135  */
136  int skipL = numLeftFields*numPoints; // size of the left data chunk per cell
137  int skipR = numRightFields*numPoints; // size of the right data chunk per cell
138  int skipOp = numLeftFields*numRightFields; // size of the output data chunk per cell
139  Scalar alpha(1.0); // these are left unchanged by GEMM
140  Scalar beta(0.0);
141  if (sumInto) {
142  beta = 1.0;
143  }
144 
145  for (int cl=0; cl < numCells; cl++) {
146  /* Use this if data is used in row-major format */
147  Teuchos::BLAS<int, Scalar> myblas;
148  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
149  numRightFields, numLeftFields, numPoints,
150  alpha, &rightFields[cl*skipR], numPoints,
151  &leftFields[cl*skipL], numPoints,
152  beta, &outputFields[cl*skipOp], numRightFields);
153  /* Use this if data is used in column-major format */
154  /*
155  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
156  numLeftFields, numRightFields, numPoints,
157  alpha, &leftFields[cl*skipL], numPoints,
158  &rightFields[cl*skipR], numPoints,
159  beta, &outputFields[cl*skipOp], numLeftFields);
160  */
161  }
162  }
163  break;
164 
165  default:
166  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
167  ">>> ERROR (ArrayTools::contractFieldFieldScalar): Computational engine not defined!");
168  } // switch(compEngine)
169 } // end contractFieldFieldScalar
170 
171 
172 template<class Scalar, class ArrayOutFields, class ArrayInFieldsLeft, class ArrayInFieldsRight>
173 void ArrayTools::contractFieldFieldVector(ArrayOutFields & outputFields,
174  const ArrayInFieldsLeft & leftFields,
175  const ArrayInFieldsRight & rightFields,
176  const ECompEngine compEngine,
177  const bool sumInto) {
178 
179 #ifdef HAVE_INTREPID_DEBUG
180  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.rank() != 4 ), std::invalid_argument,
181  ">>> ERROR (ArrayTools::contractFieldFieldVector): Rank of the left input argument must equal 4!");
182  TEUCHOS_TEST_FOR_EXCEPTION( (rightFields.rank() != 4 ), std::invalid_argument,
183  ">>> ERROR (ArrayTools::contractFieldFieldVector): Rank of right input argument must equal 4!");
184  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 3 ), std::invalid_argument,
185  ">>> ERROR (ArrayTools::contractFieldFieldVector): Rank of output argument must equal 3!");
186  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
187  ">>> ERROR (ArrayTools::contractFieldFieldVector): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
188  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(2) != rightFields.dimension(2) ), std::invalid_argument,
189  ">>> ERROR (ArrayTools::contractFieldFieldVector): Second dimensions (numbers of integration points) of the left and right input containers must agree!");
190  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(3) != rightFields.dimension(3) ), std::invalid_argument,
191  ">>> ERROR (ArrayTools::contractFieldFieldVector): Third dimensions (numbers of vector components) of the left and right input containers must agree!");
192  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
193  ">>> ERROR (ArrayTools::contractFieldFieldVector): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
194  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != leftFields.dimension(1) ), std::invalid_argument,
195  ">>> ERROR (ArrayTools::contractFieldFieldVector): First dimension of output container and first dimension of left input container must agree!");
196  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(2) != rightFields.dimension(1) ), std::invalid_argument,
197  ">>> ERROR (ArrayTools::contractFieldFieldVector): Second dimension of output container and first dimension of right input container must agree!");
198 #endif
199 
200  // get sizes
201  int numCells = leftFields.dimension(0);
202  int numLeftFields = leftFields.dimension(1);
203  int numRightFields = rightFields.dimension(1);
204  int numPoints = leftFields.dimension(2);
205  int dimVec = leftFields.dimension(3);
206 
207  switch(compEngine) {
208  case COMP_CPP: {
209  if (sumInto) {
210  for (int cl = 0; cl < numCells; cl++) {
211  for (int lbf = 0; lbf < numLeftFields; lbf++) {
212  for (int rbf = 0; rbf < numRightFields; rbf++) {
213  Scalar tmpVal(0);
214  for (int qp = 0; qp < numPoints; qp++) {
215  for (int iVec = 0; iVec < dimVec; iVec++) {
216  tmpVal += leftFields(cl, lbf, qp, iVec)*rightFields(cl, rbf, qp, iVec);
217  } //D-loop
218  } // P-loop
219  outputFields(cl, lbf, rbf) += tmpVal;
220  } // R-loop
221  } // L-loop
222  } // C-loop
223  }
224  else {
225  for (int cl = 0; cl < numCells; cl++) {
226  for (int lbf = 0; lbf < numLeftFields; lbf++) {
227  for (int rbf = 0; rbf < numRightFields; rbf++) {
228  Scalar tmpVal(0);
229  for (int qp = 0; qp < numPoints; qp++) {
230  for (int iVec = 0; iVec < dimVec; iVec++) {
231  tmpVal += leftFields(cl, lbf, qp, iVec)*rightFields(cl, rbf, qp, iVec);
232  } //D-loop
233  } // P-loop
234  outputFields(cl, lbf, rbf) = tmpVal;
235  } // R-loop
236  } // L-loop
237  } // C-loop
238  }
239  }
240  break;
241 
242  case COMP_BLAS: {
243  /*
244  GEMM parameters and their values.
245  (Note: It is assumed that the result needs to be transposed into row-major format.
246  Think of left and right input matrices as A(p x l) and B(p x r), respectively,
247  even though the indexing is ((C),L,P) and ((C),R,P). Due to BLAS formatting
248  assumptions, we are computing (A^T*B)^T = B^T*A.)
249  TRANSA TRANS
250  TRANSB NO_TRANS
251  M #rows(B^T) = number of right fields
252  N #cols(A) = number of left fields
253  K #cols(B^T) = number of integration points * size of vector
254  ALPHA 1.0
255  A right data for cell cl = &rightFields[cl*skipR]
256  LDA #rows(B) = number of integration points * size of vector
257  B left data for cell cl = &leftFields[cl*skipL]
258  LDB #rows(A) = number of integration points * size of vector
259  BETA 0.0
260  C result for cell cl = &outputFields[cl*skipOp]
261  LDC #rows(C) = number of right fields
262  */
263  int numData = numPoints*dimVec;
264  int skipL = numLeftFields*numData; // size of the left data chunk per cell
265  int skipR = numRightFields*numData; // size of the right data chunk per cell
266  int skipOp = numLeftFields*numRightFields; // size of the output data chunk per cell
267  Scalar alpha(1.0); // these are left unchanged by GEMM
268  Scalar beta(0.0);
269  if (sumInto) {
270  beta = 1.0;
271  }
272 
273  for (int cl=0; cl < numCells; cl++) {
274  /* Use this if data is used in row-major format */
275  Teuchos::BLAS<int, Scalar> myblas;
276  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
277  numRightFields, numLeftFields, numData,
278  alpha, &rightFields[cl*skipR], numData,
279  &leftFields[cl*skipL], numData,
280  beta, &outputFields[cl*skipOp], numRightFields);
281  /* Use this if data is used in column-major format */
282  /*
283  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
284  numLeftFields, numRightFields, numData,
285  alpha, &leftFields[cl*skipL], numData,
286  &rightFields[cl*skipR], numData,
287  beta, &outputFields[cl*skipOp], numLeftFields);
288  */
289  }
290  }
291  break;
292 
293  default:
294  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
295  ">>> ERROR (ArrayTools::contractFieldFieldVector): Computational engine not defined!");
296  } // switch(compEngine)
297 } // end contractFieldFieldVector
298 
299 
300 template<class Scalar, class ArrayOutFields, class ArrayInFieldsLeft, class ArrayInFieldsRight>
301 void ArrayTools::contractFieldFieldTensor(ArrayOutFields & outputFields,
302  const ArrayInFieldsLeft & leftFields,
303  const ArrayInFieldsRight & rightFields,
304  const ECompEngine compEngine,
305  const bool sumInto) {
306 
307 #ifdef HAVE_INTREPID_DEBUG
308  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.rank() != 5 ), std::invalid_argument,
309  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Rank of the left input argument must equal 5!");
310  TEUCHOS_TEST_FOR_EXCEPTION( (rightFields.rank() != 5 ), std::invalid_argument,
311  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Rank of right input argument must equal 5!");
312  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 3 ), std::invalid_argument,
313  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Rank of output argument must equal 3!");
314  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
315  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
316  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(2) != rightFields.dimension(2) ), std::invalid_argument,
317  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Second dimensions (numbers of integration points) of the left and right input containers must agree!");
318  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(3) != rightFields.dimension(3) ), std::invalid_argument,
319  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Third dimensions (first tensor dimensions) of the left and right input containers must agree!");
320  TEUCHOS_TEST_FOR_EXCEPTION( (leftFields.dimension(4) != rightFields.dimension(4) ), std::invalid_argument,
321  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Fourth dimensions (second tensor dimensions) of the left and right input containers must agree!");
322  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != rightFields.dimension(0) ), std::invalid_argument,
323  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
324  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != leftFields.dimension(1) ), std::invalid_argument,
325  ">>> ERROR (ArrayTools::contractFieldFieldTensor): First dimension of output container and first dimension of left input container must agree!");
326  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(2) != rightFields.dimension(1) ), std::invalid_argument,
327  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Second dimension of output container and first dimension of right input container must agree!");
328 #endif
329 
330  // get sizes
331  int numCells = leftFields.dimension(0);
332  int numLeftFields = leftFields.dimension(1);
333  int numRightFields = rightFields.dimension(1);
334  int numPoints = leftFields.dimension(2);
335  int dim1Tensor = leftFields.dimension(3);
336  int dim2Tensor = leftFields.dimension(4);
337 
338  switch(compEngine) {
339  case COMP_CPP: {
340  if (sumInto) {
341  for (int cl = 0; cl < numCells; cl++) {
342  for (int lbf = 0; lbf < numLeftFields; lbf++) {
343  for (int rbf = 0; rbf < numRightFields; rbf++) {
344  Scalar tmpVal(0);
345  for (int qp = 0; qp < numPoints; qp++) {
346  for (int iTens1 = 0; iTens1 < dim1Tensor; iTens1++) {
347  for (int iTens2 = 0; iTens2 < dim2Tensor; iTens2++) {
348  tmpVal += leftFields(cl, lbf, qp, iTens1, iTens2)*rightFields(cl, rbf, qp, iTens1, iTens2);
349  } // D2-loop
350  } // D1-loop
351  } // P-loop
352  outputFields(cl, lbf, rbf) += tmpVal;
353  } // R-loop
354  } // L-loop
355  } // C-loop
356  }
357  else {
358  for (int cl = 0; cl < numCells; cl++) {
359  for (int lbf = 0; lbf < numLeftFields; lbf++) {
360  for (int rbf = 0; rbf < numRightFields; rbf++) {
361  Scalar tmpVal(0);
362  for (int qp = 0; qp < numPoints; qp++) {
363  for (int iTens1 = 0; iTens1 < dim1Tensor; iTens1++) {
364  for (int iTens2 = 0; iTens2 < dim2Tensor; iTens2++) {
365  tmpVal += leftFields(cl, lbf, qp, iTens1, iTens2)*rightFields(cl, rbf, qp, iTens1, iTens2);
366  } // D2-loop
367  } // D1-loop
368  } // P-loop
369  outputFields(cl, lbf, rbf) = tmpVal;
370  } // R-loop
371  } // L-loop
372  } // C-loop
373  }
374  }
375  break;
376 
377  case COMP_BLAS: {
378  /*
379  GEMM parameters and their values.
380  (Note: It is assumed that the result needs to be transposed into row-major format.
381  Think of left and right input matrices as A(p x l) and B(p x r), respectively,
382  even though the indexing is ((C),L,P) and ((C),R,P). Due to BLAS formatting
383  assumptions, we are computing (A^T*B)^T = B^T*A.)
384  TRANSA TRANS
385  TRANSB NO_TRANS
386  M #rows(B^T) = number of right fields
387  N #cols(A) = number of left fields
388  K #cols(B^T) = number of integration points * size of tensor
389  ALPHA 1.0
390  A right data for cell cl = &rightFields[cl*skipR]
391  LDA #rows(B) = number of integration points * size of tensor
392  B left data for cell cl = &leftFields[cl*skipL]
393  LDB #rows(A) = number of integration points * size of tensor
394  BETA 0.0
395  C result for cell cl = &outputFields[cl*skipOp]
396  LDC #rows(C) = number of right fields
397  */
398  int numData = numPoints*dim1Tensor*dim2Tensor;
399  int skipL = numLeftFields*numData; // size of the left data chunk per cell
400  int skipR = numRightFields*numData; // size of the right data chunk per cell
401  int skipOp = numLeftFields*numRightFields; // size of the output data chunk per cell
402  Scalar alpha(1.0); // these are left unchanged by GEMM
403  Scalar beta(0.0);
404  if (sumInto) {
405  beta = 1.0;
406  }
407 
408  for (int cl=0; cl < numCells; cl++) {
409  /* Use this if data is used in row-major format */
410  Teuchos::BLAS<int, Scalar> myblas;
411  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
412  numRightFields, numLeftFields, numData,
413  alpha, &rightFields[cl*skipR], numData,
414  &leftFields[cl*skipL], numData,
415  beta, &outputFields[cl*skipOp], numRightFields);
416  /* Use this if data is used in column-major format */
417  /*
418  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
419  numLeftFields, numRightFields, numData,
420  alpha, &leftFields[cl*skipL], numData,
421  &rightFields[cl*skipR], numData,
422  beta, &outputFields[cl*skipOp], numLeftFields);
423  */
424  }
425  }
426  break;
427 
428  default:
429  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
430  ">>> ERROR (ArrayTools::contractFieldFieldTensor): Computational engine not defined!");
431  } // switch(compEngine)
432 } // end contractFieldFieldTensor
433 
434 
435 template<class Scalar, class ArrayOutFields, class ArrayInData, class ArrayInFields>
436 void ArrayTools::contractDataFieldScalar(ArrayOutFields & outputFields,
437  const ArrayInData & inputData,
438  const ArrayInFields & inputFields,
439  const ECompEngine compEngine,
440  const bool sumInto) {
441 
442 
443 #ifdef HAVE_INTREPID_DEBUG
444  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.rank() != 3 ), std::invalid_argument,
445  ">>> ERROR (ArrayTools::contractDataFieldScalar): Rank of the fields input argument must equal 3!");
446  TEUCHOS_TEST_FOR_EXCEPTION( (inputData.rank() != 2 ), std::invalid_argument,
447  ">>> ERROR (ArrayTools::contractDataFieldScalar): Rank of the data input argument must equal 2!");
448  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 2 ), std::invalid_argument,
449  ">>> ERROR (ArrayTools::contractDataFieldScalar): Rank of output argument must equal 2!");
450  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(0) != inputData.dimension(0) ), std::invalid_argument,
451  ">>> ERROR (ArrayTools::contractDataFieldScalar): Zeroth dimensions (number of integration domains) of the fields and data input containers must agree!");
452  TEUCHOS_TEST_FOR_EXCEPTION( ( (inputFields.dimension(2) != inputData.dimension(1)) && (inputData.dimension(1) != 1) ), std::invalid_argument,
453  ">>> ERROR (ArrayTools::contractDataFieldScalar): Second dimension of fields input container and first dimension of data input container (number of integration points) must agree or first data dimension must be 1!");
454  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != inputFields.dimension(0) ), std::invalid_argument,
455  ">>> ERROR (ArrayTools::contractDataFieldScalar): Zeroth dimensions (numbers of integration domains) of the fields input and output containers must agree!");
456  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != inputFields.dimension(1) ), std::invalid_argument,
457  ">>> ERROR (ArrayTools::contractDataFieldScalar): First dimensions (number of fields) of the fields input and output containers must agree!");
458 #endif
459 
460  // get sizes
461  int numCells = inputFields.dimension(0);
462  int numFields = inputFields.dimension(1);
463  int numPoints = inputFields.dimension(2);
464  int numDataPoints = inputData.dimension(1);
465 
466  ECompEngine myCompEngine = (numDataPoints == 1 ? COMP_CPP : compEngine);
467 
468  switch(myCompEngine) {
469  case COMP_CPP: {
470  if (sumInto) {
471  if (numDataPoints != 1) { // nonconstant data
472  for (int cl = 0; cl < numCells; cl++) {
473  for (int lbf = 0; lbf < numFields; lbf++) {
474  Scalar tmpVal(0);
475  for (int qp = 0; qp < numPoints; qp++) {
476  tmpVal += inputFields(cl, lbf, qp)*inputData(cl, qp);
477  } // P-loop
478  outputFields(cl, lbf) += tmpVal;
479  } // F-loop
480  } // C-loop
481  }
482  else { // constant data
483  for (int cl = 0; cl < numCells; cl++) {
484  for (int lbf = 0; lbf < numFields; lbf++) {
485  Scalar tmpVal(0);
486  for (int qp = 0; qp < numPoints; qp++) {
487  tmpVal += inputFields(cl, lbf, qp)*inputData(cl, 0);
488  } // P-loop
489  outputFields(cl, lbf) += tmpVal;
490  } // F-loop
491  } // C-loop
492  } // numDataPoints
493  }
494  else {
495  if (numDataPoints != 1) { // nonconstant data
496  for (int cl = 0; cl < numCells; cl++) {
497  for (int lbf = 0; lbf < numFields; lbf++) {
498  Scalar tmpVal(0);
499  for (int qp = 0; qp < numPoints; qp++) {
500  tmpVal += inputFields(cl, lbf, qp)*inputData(cl, qp);
501  } // P-loop
502  outputFields(cl, lbf) = tmpVal;
503  } // F-loop
504  } // C-loop
505  }
506  else { // constant data
507  for (int cl = 0; cl < numCells; cl++) {
508  for (int lbf = 0; lbf < numFields; lbf++) {
509  Scalar tmpVal(0);
510  for (int qp = 0; qp < numPoints; qp++) {
511  tmpVal += inputFields(cl, lbf, qp)*inputData(cl, 0);
512  } // P-loop
513  outputFields(cl, lbf) = tmpVal;
514  } // F-loop
515  } // C-loop
516  } // numDataPoints
517  }
518  }
519  break;
520 
521  case COMP_BLAS: {
522  /*
523  GEMM parameters and their values.
524  (Note: It is assumed that the result needs to be transposed into row-major format.
525  Think of left and right input matrices as A(p x f) and B(p x 1), respectively,
526  even though the indexing is ((C),F,P) and ((C),P). Due to BLAS formatting
527  assumptions, we are computing (A^T*B)^T = B^T*A.)
528  TRANSA TRANS
529  TRANSB NO_TRANS
530  M #rows(B^T) = 1
531  N #cols(A) = number of input fields
532  K #cols(B^T) = number of integration points * size of data
533  ALPHA 1.0
534  A right data for cell cl = &rightFields[cl*skipR]
535  LDA #rows(B) = number of integration points * size of data
536  B left data for cell cl = &leftFields[cl*skipL]
537  LDB #rows(A) = number of integration points * size of data
538  BETA 0.0
539  C result for cell cl = &outputFields[cl*skipOp]
540  LDC #rows(C) = 1
541  */
542  int numData = numPoints;
543  int skipL = numFields*numPoints; // size of the left data chunk per cell
544  int skipR = numPoints; // size of the right data chunk per cell
545  int skipOp = numFields; // size of the output data chunk per cell
546  Scalar alpha(1.0); // these are left unchanged by GEMM
547  Scalar beta(0.0);
548  if (sumInto) {
549  beta = 1.0;
550  }
551 
552  for (int cl=0; cl < numCells; cl++) {
553  /* Use this if data is used in row-major format */
554  Teuchos::BLAS<int, Scalar> myblas;
555  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
556  1, numFields, numData,
557  alpha, &inputData[cl*skipR], numData,
558  &inputFields[cl*skipL], numData,
559  beta, &outputFields[cl*skipOp], 1);
560  /* Use this if data is used in column-major format */
561  /*
562  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
563  numFields, 1, numData,
564  alpha, &inputFields[cl*skipL], numData,
565  &inputData[cl*skipR], numData,
566  beta, &outputFields[cl*skipOp], numFields);
567  */
568  }
569  }
570  break;
571 
572  default:
573  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
574  ">>> ERROR (ArrayTools::contractDataFieldScalar): Computational engine not defined!");
575  } // switch(compEngine)
576 } // end contractDataFieldScalar
577 
578 
579 template<class Scalar, class ArrayOutFields, class ArrayInData, class ArrayInFields>
580 void ArrayTools::contractDataFieldVector(ArrayOutFields & outputFields,
581  const ArrayInData & inputData,
582  const ArrayInFields & inputFields,
583  const ECompEngine compEngine,
584  const bool sumInto) {
585 
586 #ifdef HAVE_INTREPID_DEBUG
587  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.rank() != 4 ), std::invalid_argument,
588  ">>> ERROR (ArrayTools::contractDataFieldVector): Rank of the fields input argument must equal 4!");
589  TEUCHOS_TEST_FOR_EXCEPTION( (inputData.rank() != 3 ), std::invalid_argument,
590  ">>> ERROR (ArrayTools::contractDataFieldVector): Rank of the data input argument must equal 3!");
591  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 2 ), std::invalid_argument,
592  ">>> ERROR (ArrayTools::contractDataFieldVector): Rank of output argument must equal 2!");
593  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(0) != inputData.dimension(0) ), std::invalid_argument,
594  ">>> ERROR (ArrayTools::contractDataFieldVector): Zeroth dimensions (number of integration domains) of the fields and data input containers must agree!");
595  TEUCHOS_TEST_FOR_EXCEPTION( ( (inputFields.dimension(2) != inputData.dimension(1)) && (inputData.dimension(1) != 1) ), std::invalid_argument,
596  ">>> ERROR (ArrayTools::contractDataFieldVector): Second dimension of the fields input container and first dimension of data input container (number of integration points) must agree or first data dimension must be 1!");
597  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(3) != inputData.dimension(2) ), std::invalid_argument,
598  ">>> ERROR (ArrayTools::contractDataFieldVector): Third dimension of the fields input container and second dimension of data input container (vector index) must agree!");
599  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != inputFields.dimension(0) ), std::invalid_argument,
600  ">>> ERROR (ArrayTools::contractDataFieldVector): Zeroth dimensions (numbers of integration domains) of the fields input and output containers must agree!");
601  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != inputFields.dimension(1) ), std::invalid_argument,
602  ">>> ERROR (ArrayTools::contractDataFieldVector): First dimensions of output container and fields input container (number of fields) must agree!");
603 #endif
604 
605  // get sizes
606  int numCells = inputFields.dimension(0);
607  int numFields = inputFields.dimension(1);
608  int numPoints = inputFields.dimension(2);
609  int dimVec = inputFields.dimension(3);
610  int numDataPoints = inputData.dimension(1);
611 
612  ECompEngine myCompEngine = (numDataPoints == 1 ? COMP_CPP : compEngine);
613 
614  switch(myCompEngine) {
615  case COMP_CPP: {
616  if (sumInto) {
617  if (numDataPoints != 1) { // nonconstant data
618  for (int cl = 0; cl < numCells; cl++) {
619  for (int lbf = 0; lbf < numFields; lbf++) {
620  Scalar tmpVal(0);
621  for (int qp = 0; qp < numPoints; qp++) {
622  for (int iVec = 0; iVec < dimVec; iVec++) {
623  tmpVal += inputFields(cl, lbf, qp, iVec)*inputData(cl, qp, iVec);
624  } // D-loop
625  } // P-loop
626  outputFields(cl, lbf) += tmpVal;
627  } // F-loop
628  } // C-loop
629  }
630  else { // constant data
631  for (int cl = 0; cl < numCells; cl++) {
632  for (int lbf = 0; lbf < numFields; lbf++) {
633  Scalar tmpVal(0);
634  for (int qp = 0; qp < numPoints; qp++) {
635  for (int iVec = 0; iVec < dimVec; iVec++) {
636  tmpVal += inputFields(cl, lbf, qp, iVec)*inputData(cl, 0, iVec);
637  } //D-loop
638  } // P-loop
639  outputFields(cl, lbf) += tmpVal;
640  } // F-loop
641  } // C-loop
642  } // numDataPoints
643  }
644  else {
645  if (numDataPoints != 1) { // nonconstant data
646  for (int cl = 0; cl < numCells; cl++) {
647  for (int lbf = 0; lbf < numFields; lbf++) {
648  Scalar tmpVal(0);
649  for (int qp = 0; qp < numPoints; qp++) {
650  for (int iVec = 0; iVec < dimVec; iVec++) {
651  tmpVal += inputFields(cl, lbf, qp, iVec)*inputData(cl, qp, iVec);
652  } // D-loop
653  } // P-loop
654  outputFields(cl, lbf) = tmpVal;
655  } // F-loop
656  } // C-loop
657  }
658  else { // constant data
659  for (int cl = 0; cl < numCells; cl++) {
660  for (int lbf = 0; lbf < numFields; lbf++) {
661  Scalar tmpVal(0);
662  for (int qp = 0; qp < numPoints; qp++) {
663  for (int iVec = 0; iVec < dimVec; iVec++) {
664  tmpVal += inputFields(cl, lbf, qp, iVec)*inputData(cl, 0, iVec);
665  } //D-loop
666  } // P-loop
667  outputFields(cl, lbf) = tmpVal;
668  } // F-loop
669  } // C-loop
670  } // numDataPoints
671  }
672  }
673  break;
674 
675  case COMP_BLAS: {
676  /*
677  GEMM parameters and their values.
678  (Note: It is assumed that the result needs to be transposed into row-major format.
679  Think of left and right input matrices as A(p x f) and B(p x 1), respectively,
680  even though the indexing is ((C),F,P) and ((C),P). Due to BLAS formatting
681  assumptions, we are computing (A^T*B)^T = B^T*A.)
682  TRANSA TRANS
683  TRANSB NO_TRANS
684  M #rows(B^T) = 1
685  N #cols(A) = number of input fields
686  K #cols(B^T) = number of integration points * size of data
687  ALPHA 1.0
688  A right data for cell cl = &rightFields[cl*skipR]
689  LDA #rows(B) = number of integration points * size of data
690  B left data for cell cl = &leftFields[cl*skipL]
691  LDB #rows(A) = number of integration points * size of data
692  BETA 0.0
693  C result for cell cl = &outputFields[cl*skipOp]
694  LDC #rows(C) = 1
695  */
696  int numData = numPoints*dimVec;
697  int skipL = numFields*numData; // size of the left data chunk per cell
698  int skipR = numData; // size of the right data chunk per cell
699  int skipOp = numFields; // size of the output data chunk per cell
700  Scalar alpha(1.0); // these are left unchanged by GEMM
701  Scalar beta(0.0);
702  if (sumInto) {
703  beta = 1.0;
704  }
705 
706  for (int cl=0; cl < numCells; cl++) {
707  /* Use this if data is used in row-major format */
708  Teuchos::BLAS<int, Scalar> myblas;
709  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
710  1, numFields, numData,
711  alpha, &inputData[cl*skipR], numData,
712  &inputFields[cl*skipL], numData,
713  beta, &outputFields[cl*skipOp], 1);
714  /* Use this if data is used in column-major format */
715  /*
716  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
717  numFields, 1, numData,
718  alpha, &inputFields[cl*skipL], numData,
719  &inputData[cl*skipR], numData,
720  beta, &outputFields[cl*skipOp], numFields);
721  */
722  }
723  }
724  break;
725 
726  default:
727  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
728  ">>> ERROR (ArrayTools::contractDataFieldVector): Computational engine not defined!");
729  } // switch(compEngine)
730 } // end contractDataFieldVector
731 
732 
733 template<class Scalar, class ArrayOutFields, class ArrayInData, class ArrayInFields>
734 void ArrayTools::contractDataFieldTensor(ArrayOutFields & outputFields,
735  const ArrayInData & inputData,
736  const ArrayInFields & inputFields,
737  const ECompEngine compEngine,
738  const bool sumInto) {
739 
740 #ifdef HAVE_INTREPID_DEBUG
741  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.rank() != 5 ), std::invalid_argument,
742  ">>> ERROR (ArrayTools::contractDataFieldTensor): Rank of the fields input argument must equal 5!");
743  TEUCHOS_TEST_FOR_EXCEPTION( (inputData.rank() != 4 ), std::invalid_argument,
744  ">>> ERROR (ArrayTools::contractDataFieldTensor): Rank of the data input argument must equal 4!");
745  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.rank() != 2 ), std::invalid_argument,
746  ">>> ERROR (ArrayTools::contractDataFieldTensor): Rank of output argument must equal 2!");
747  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(0) != inputData.dimension(0) ), std::invalid_argument,
748  ">>> ERROR (ArrayTools::contractDataFieldTensor): Zeroth dimensions (number of integration domains) of the fields and data input containers must agree!");
749  TEUCHOS_TEST_FOR_EXCEPTION( ( (inputFields.dimension(2) != inputData.dimension(1)) && (inputData.dimension(1) != 1) ), std::invalid_argument,
750  ">>> ERROR (ArrayTools::contractDataFieldTensor): Second dimension of the fields input container and first dimension of data input container (number of integration points) must agree or first data dimension must be 1!");
751  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(3) != inputData.dimension(2) ), std::invalid_argument,
752  ">>> ERROR (ArrayTools::contractDataFieldTensor): Third dimension of the fields input container and second dimension of data input container (first tensor dimension) must agree!");
753  TEUCHOS_TEST_FOR_EXCEPTION( (inputFields.dimension(4) != inputData.dimension(3) ), std::invalid_argument,
754  ">>> ERROR (ArrayTools::contractDataFieldTensor): Fourth dimension of the fields input container and third dimension of data input container (second tensor dimension) must agree!");
755  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(0) != inputFields.dimension(0) ), std::invalid_argument,
756  ">>> ERROR (ArrayTools::contractDataFieldTensor): Zeroth dimensions (numbers of integration domains) of the fields input and output containers must agree!");
757  TEUCHOS_TEST_FOR_EXCEPTION( (outputFields.dimension(1) != inputFields.dimension(1) ), std::invalid_argument,
758  ">>> ERROR (ArrayTools::contractDataFieldTensor): First dimensions (number of fields) of output container and fields input container must agree!");
759 #endif
760 
761  // get sizes
762  int numCells = inputFields.dimension(0);
763  int numFields = inputFields.dimension(1);
764  int numPoints = inputFields.dimension(2);
765  int dim1Tens = inputFields.dimension(3);
766  int dim2Tens = inputFields.dimension(4);
767  int numDataPoints = inputData.dimension(1);
768 
769  ECompEngine myCompEngine = (numDataPoints == 1 ? COMP_CPP : compEngine);
770 
771  switch(myCompEngine) {
772  case COMP_CPP: {
773  if (sumInto) {
774  if (numDataPoints != 1) { // nonconstant data
775  for (int cl = 0; cl < numCells; cl++) {
776  for (int lbf = 0; lbf < numFields; lbf++) {
777  Scalar tmpVal(0);
778  for (int qp = 0; qp < numPoints; qp++) {
779  for (int iTens1 = 0; iTens1 < dim1Tens; iTens1++) {
780  for (int iTens2 =0; iTens2 < dim2Tens; iTens2++) {
781  tmpVal += inputFields(cl, lbf, qp, iTens1, iTens2)*inputData(cl, qp, iTens1, iTens2);
782  } // D2-loop
783  } // D1-loop
784  } // P-loop
785  outputFields(cl, lbf) += tmpVal;
786  } // F-loop
787  } // C-loop
788  }
789  else { // constant data
790  for (int cl = 0; cl < numCells; cl++) {
791  for (int lbf = 0; lbf < numFields; lbf++) {
792  Scalar tmpVal(0);
793  for (int qp = 0; qp < numPoints; qp++) {
794  for (int iTens1 = 0; iTens1 < dim1Tens; iTens1++) {
795  for (int iTens2 = 0; iTens2 < dim2Tens; iTens2++) {
796  tmpVal += inputFields(cl, lbf, qp, iTens1, iTens2)*inputData(cl, 0, iTens1, iTens2);
797  } // D2-loop
798  } // D1-loop
799  } // P-loop
800  outputFields(cl, lbf) += tmpVal;
801  } // F-loop
802  } // C-loop
803  } // numDataPoints
804  }
805  else {
806  if (numDataPoints != 1) { // nonconstant data
807  for (int cl = 0; cl < numCells; cl++) {
808  for (int lbf = 0; lbf < numFields; lbf++) {
809  Scalar tmpVal(0);
810  for (int qp = 0; qp < numPoints; qp++) {
811  for (int iTens1 = 0; iTens1 < dim1Tens; iTens1++) {
812  for (int iTens2 =0; iTens2 < dim2Tens; iTens2++) {
813  tmpVal += inputFields(cl, lbf, qp, iTens1, iTens2)*inputData(cl, qp, iTens1, iTens2);
814  } // D2-loop
815  } // D1-loop
816  } // P-loop
817  outputFields(cl, lbf) = tmpVal;
818  } // F-loop
819  } // C-loop
820  }
821  else { // constant data
822  for (int cl = 0; cl < numCells; cl++) {
823  for (int lbf = 0; lbf < numFields; lbf++) {
824  Scalar tmpVal(0);
825  for (int qp = 0; qp < numPoints; qp++) {
826  for (int iTens1 = 0; iTens1 < dim1Tens; iTens1++) {
827  for (int iTens2 = 0; iTens2 < dim2Tens; iTens2++) {
828  tmpVal += inputFields(cl, lbf, qp, iTens1, iTens2)*inputData(cl, 0, iTens1, iTens2);
829  } // D2-loop
830  } // D1-loop
831  } // P-loop
832  outputFields(cl, lbf) = tmpVal;
833  } // F-loop
834  } // C-loop
835  } // numDataPoints
836  }
837  }
838  break;
839 
840  case COMP_BLAS: {
841  /*
842  GEMM parameters and their values.
843  (Note: It is assumed that the result needs to be transposed into row-major format.
844  Think of left and right input matrices as A(p x f) and B(p x 1), respectively,
845  even though the indexing is ((C),F,P) and ((C),P). Due to BLAS formatting
846  assumptions, we are computing (A^T*B)^T = B^T*A.)
847  TRANSA TRANS
848  TRANSB NO_TRANS
849  M #rows(B^T) = 1
850  N #cols(A) = number of input fields
851  K #cols(B^T) = number of integration points * size of data
852  ALPHA 1.0
853  A right data for cell cl = &rightFields[cl*skipR]
854  LDA #rows(B) = number of integration points * size of data
855  B left data for cell cl = &leftFields[cl*skipL]
856  LDB #rows(A) = number of integration points * size of data
857  BETA 0.0
858  C result for cell cl = &outputFields[cl*skipOp]
859  LDC #rows(C) = 1
860  */
861  int numData = numPoints*dim1Tens*dim2Tens;
862  int skipL = numFields*numData; // size of the left data chunk per cell
863  int skipR = numData; // size of the right data chunk per cell
864  int skipOp = numFields; // size of the output data chunk per cell
865  Scalar alpha(1.0); // these are left unchanged by GEMM
866  Scalar beta(0.0);
867  if (sumInto) {
868  beta = 1.0;
869  }
870 
871  for (int cl=0; cl < numCells; cl++) {
872  /* Use this if data is used in row-major format */
873  Teuchos::BLAS<int, Scalar> myblas;
874  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
875  1, numFields, numData,
876  alpha, &inputData[cl*skipR], numData,
877  &inputFields[cl*skipL], numData,
878  beta, &outputFields[cl*skipOp], 1);
879  /* Use this if data is used in column-major format */
880  /*
881  myblas.GEMM(Teuchos::TRANS, Teuchos::NO_TRANS,
882  numFields, 1, numData,
883  alpha, &inputFields[cl*skipL], numData,
884  &inputData[cl*skipR], numData,
885  beta, &outputFields[cl*skipOp], numFields);
886  */
887  }
888  }
889  break;
890 
891  default:
892  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
893  ">>> ERROR (ArrayTools::contractDataFieldTensor): Computational engine not defined!");
894  } // switch(compEngine)
895 } // end contractDataFieldTensor
896 
897 
898 template<class Scalar, class ArrayOutData, class ArrayInDataLeft, class ArrayInDataRight>
899 void ArrayTools::contractDataDataScalar(ArrayOutData & outputData,
900  const ArrayInDataLeft & inputDataLeft,
901  const ArrayInDataRight & inputDataRight,
902  const ECompEngine compEngine,
903  const bool sumInto) {
904 
905 #ifdef HAVE_INTREPID_DEBUG
906  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.rank() != 2 ), std::invalid_argument,
907  ">>> ERROR (ArrayTools::contractDataDataScalar): Rank of the left input argument must equal 2!");
908  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataRight.rank() != 2 ), std::invalid_argument,
909  ">>> ERROR (ArrayTools::contractDataDataScalar): Rank of right input argument must equal 2!");
910  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.rank() != 1 ), std::invalid_argument,
911  ">>> ERROR (ArrayTools::contractDataDataScalar): Rank of output argument must equal 1!");
912  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
913  ">>> ERROR (ArrayTools::contractDataDataScalar): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
914  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(1) != inputDataRight.dimension(1) ), std::invalid_argument,
915  ">>> ERROR (ArrayTools::contractDataDataScalar): First dimensions (numbers of integration points) of the left and right input containers must agree!");
916  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
917  ">>> ERROR (ArrayTools::contractDataDataScalar): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
918 #endif
919 
920  // get sizes
921  int numCells = inputDataLeft.dimension(0);
922  int numPoints = inputDataLeft.dimension(1);
923 
924  switch(compEngine) {
925  case COMP_CPP: {
926  if (sumInto) {
927  for (int cl = 0; cl < numCells; cl++) {
928  Scalar tmpVal(0);
929  for (int qp = 0; qp < numPoints; qp++) {
930  tmpVal += inputDataLeft(cl, qp)*inputDataRight(cl, qp);
931  } // P-loop
932  outputData(cl) += tmpVal;
933  } // C-loop
934  }
935  else {
936  for (int cl = 0; cl < numCells; cl++) {
937  Scalar tmpVal(0);
938  for (int qp = 0; qp < numPoints; qp++) {
939  tmpVal += inputDataLeft(cl, qp)*inputDataRight(cl, qp);
940  } // P-loop
941  outputData(cl) = tmpVal;
942  } // C-loop
943  }
944  }
945  break;
946 
947  case COMP_BLAS: {
948  int incr = 1; // increment
949  if (sumInto) {
950  for (int cl=0; cl < numCells; cl++) {
951  Teuchos::BLAS<int, Scalar> myblas;
952  outputData(cl) += myblas.DOT(numPoints, &inputDataLeft[cl*numPoints], incr, &inputDataRight[cl*numPoints], incr);
953  }
954  }
955  else {
956  for (int cl=0; cl < numCells; cl++) {
957  Teuchos::BLAS<int, Scalar> myblas;
958  outputData(cl) = myblas.DOT(numPoints, &inputDataLeft[cl*numPoints], incr, &inputDataRight[cl*numPoints], incr);
959  }
960  }
961  }
962  break;
963 
964  default:
965  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
966  ">>> ERROR (ArrayTools::contractDataDataScalar): Computational engine not defined!");
967  } // switch(compEngine)
968 } // end contractDataDataScalar
969 
970 
971 template<class Scalar, class ArrayOutData, class ArrayInDataLeft, class ArrayInDataRight>
972 void ArrayTools::contractDataDataVector(ArrayOutData & outputData,
973  const ArrayInDataLeft & inputDataLeft,
974  const ArrayInDataRight & inputDataRight,
975  const ECompEngine compEngine,
976  const bool sumInto) {
977 
978 #ifdef HAVE_INTREPID_DEBUG
979  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.rank() != 3 ), std::invalid_argument,
980  ">>> ERROR (ArrayTools::contractDataDataVector): Rank of the left input argument must equal 3!");
981  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataRight.rank() != 3 ), std::invalid_argument,
982  ">>> ERROR (ArrayTools::contractDataDataVector): Rank of right input argument must equal 3!");
983  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.rank() != 1 ), std::invalid_argument,
984  ">>> ERROR (ArrayTools::contractDataDataVector): Rank of output argument must equal 1!");
985  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
986  ">>> ERROR (ArrayTools::contractDataDataVector): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
987  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(1) != inputDataRight.dimension(1) ), std::invalid_argument,
988  ">>> ERROR (ArrayTools::contractDataDataVector): First dimensions (numbers of integration points) of the left and right input containers must agree!");
989  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(2) != inputDataRight.dimension(2) ), std::invalid_argument,
990  ">>> ERROR (ArrayTools::contractDataDataVector): Second dimensions (numbers of vector components) of the left and right input containers must agree!");
991  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
992  ">>> ERROR (ArrayTools::contractDataDataVector): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
993 #endif
994 
995  // get sizes
996  int numCells = inputDataLeft.dimension(0);
997  int numPoints = inputDataLeft.dimension(1);
998  int dimVec = inputDataLeft.dimension(2);
999 
1000  switch(compEngine) {
1001  case COMP_CPP: {
1002  if (sumInto) {
1003  for (int cl = 0; cl < numCells; cl++) {
1004  Scalar tmpVal(0);
1005  for (int qp = 0; qp < numPoints; qp++) {
1006  for (int iVec = 0; iVec < dimVec; iVec++) {
1007  tmpVal += inputDataLeft(cl, qp, iVec)*inputDataRight(cl, qp, iVec);
1008  } // D-loop
1009  } // P-loop
1010  outputData(cl) += tmpVal;
1011  } // C-loop
1012  }
1013  else {
1014  for (int cl = 0; cl < numCells; cl++) {
1015  Scalar tmpVal(0);
1016  for (int qp = 0; qp < numPoints; qp++) {
1017  for (int iVec = 0; iVec < dimVec; iVec++) {
1018  tmpVal += inputDataLeft(cl, qp, iVec)*inputDataRight(cl, qp, iVec);
1019  } // D-loop
1020  } // P-loop
1021  outputData(cl) = tmpVal;
1022  } // C-loop
1023  }
1024  }
1025  break;
1026 
1027  case COMP_BLAS: {
1028  int skip = numPoints*dimVec; // size of the left data chunk per cell
1029  int incr = 1; // increment
1030  if (sumInto) {
1031  for (int cl=0; cl < numCells; cl++) {
1032  Teuchos::BLAS<int, Scalar> myblas;
1033  outputData(cl) += myblas.DOT(skip, &inputDataLeft[cl*skip], incr, &inputDataRight[cl*skip], incr);
1034  }
1035  }
1036  else {
1037  for (int cl=0; cl < numCells; cl++) {
1038  Teuchos::BLAS<int, Scalar> myblas;
1039  outputData(cl) = myblas.DOT(skip, &inputDataLeft[cl*skip], incr, &inputDataRight[cl*skip], incr);
1040  }
1041  }
1042  }
1043  break;
1044 
1045  default:
1046  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
1047  ">>> ERROR (ArrayTools::contractDataDataVector): Computational engine not defined!");
1048  } // switch(compEngine)
1049 } // end contractDataDataVector
1050 
1051 
1052 template<class Scalar, class ArrayOutData, class ArrayInDataLeft, class ArrayInDataRight>
1053 void ArrayTools::contractDataDataTensor(ArrayOutData & outputData,
1054  const ArrayInDataLeft & inputDataLeft,
1055  const ArrayInDataRight & inputDataRight,
1056  const ECompEngine compEngine,
1057  const bool sumInto) {
1058 
1059 #ifdef HAVE_INTREPID_DEBUG
1060  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.rank() != 4 ), std::invalid_argument,
1061  ">>> ERROR (ArrayTools::contractDataDataTensor): Rank of the left input argument must equal 4");
1062  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataRight.rank() != 4 ), std::invalid_argument,
1063  ">>> ERROR (ArrayTools::contractDataDataTensor): Rank of right input argument must equal 4!");
1064  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.rank() != 1 ), std::invalid_argument,
1065  ">>> ERROR (ArrayTools::contractDataDataTensor): Rank of output argument must equal 1!");
1066  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
1067  ">>> ERROR (ArrayTools::contractDataDataTensor): Zeroth dimensions (number of integration domains) of the left and right input containers must agree!");
1068  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(1) != inputDataRight.dimension(1) ), std::invalid_argument,
1069  ">>> ERROR (ArrayTools::contractDataDataTensor): First dimensions (numbers of integration points) of the left and right input containers must agree!");
1070  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(2) != inputDataRight.dimension(2) ), std::invalid_argument,
1071  ">>> ERROR (ArrayTools::contractDataDataTensor): Second dimensions (first tensor dimensions) of the left and right input containers must agree!");
1072  TEUCHOS_TEST_FOR_EXCEPTION( (inputDataLeft.dimension(3) != inputDataRight.dimension(3) ), std::invalid_argument,
1073  ">>> ERROR (ArrayTools::contractDataDataTensor): Third dimensions (second tensor dimensions) of the left and right input containers must agree!");
1074  TEUCHOS_TEST_FOR_EXCEPTION( (outputData.dimension(0) != inputDataRight.dimension(0) ), std::invalid_argument,
1075  ">>> ERROR (ArrayTools::contractDataDataTensor): Zeroth dimensions (numbers of integration domains) of the input and output containers must agree!");
1076 #endif
1077 
1078  // get sizes
1079  int numCells = inputDataLeft.dimension(0);
1080  int numPoints = inputDataLeft.dimension(1);
1081  int dim1Tensor = inputDataLeft.dimension(2);
1082  int dim2Tensor = inputDataLeft.dimension(3);
1083 
1084  switch(compEngine) {
1085  case COMP_CPP: {
1086  if (sumInto) {
1087  for (int cl = 0; cl < numCells; cl++) {
1088  Scalar tmpVal(0);
1089  for (int qp = 0; qp < numPoints; qp++) {
1090  for (int iTens1 = 0; iTens1 < dim1Tensor; iTens1++) {
1091  for (int iTens2 = 0; iTens2 < dim2Tensor; iTens2++) {
1092  tmpVal += inputDataLeft(cl, qp, iTens1, iTens2)*inputDataRight(cl, qp, iTens1, iTens2);
1093  } // D2-loop
1094  } // D1-loop
1095  } // P-loop
1096  outputData(cl) += tmpVal;
1097  } // C-loop
1098  }
1099  else {
1100  for (int cl = 0; cl < numCells; cl++) {
1101  Scalar tmpVal(0);
1102  for (int qp = 0; qp < numPoints; qp++) {
1103  for (int iTens1 = 0; iTens1 < dim1Tensor; iTens1++) {
1104  for (int iTens2 = 0; iTens2 < dim2Tensor; iTens2++) {
1105  tmpVal += inputDataLeft(cl, qp, iTens1, iTens2)*inputDataRight(cl, qp, iTens1, iTens2);
1106  } // D2-loop
1107  } // D1-loop
1108  } // P-loop
1109  outputData(cl) = tmpVal;
1110  } // C-loop
1111  }
1112  }
1113  break;
1114 
1115  case COMP_BLAS: {
1116  int skip = numPoints*dim1Tensor*dim2Tensor; // size of the left data chunk per cell
1117  int incr = 1; // increment
1118  if (sumInto) {
1119  for (int cl=0; cl < numCells; cl++) {
1120  Teuchos::BLAS<int, Scalar> myblas;
1121  outputData(cl) += myblas.DOT(skip, &inputDataLeft[cl*skip], incr, &inputDataRight[cl*skip], incr);
1122  }
1123  }
1124  else {
1125  for (int cl=0; cl < numCells; cl++) {
1126  Teuchos::BLAS<int, Scalar> myblas;
1127  outputData(cl) = myblas.DOT(skip, &inputDataLeft[cl*skip], incr, &inputDataRight[cl*skip], incr);
1128  }
1129  }
1130  }
1131  break;
1132 
1133  default:
1134  TEUCHOS_TEST_FOR_EXCEPTION( ( ~isValidCompEngine(compEngine) ), std::invalid_argument,
1135  ">>> ERROR (ArrayTools::contractDataDataTensor): Computational engine not defined!");
1136  } // switch(compEngine)
1137 } // end contractDataDataTensor
1138 
1139 
1140 } // end namespace Intrepid
static void contractDataDataVector(ArrayOutData &outputData, const ArrayInDataLeft &inputDataLeft, const ArrayInDataRight &inputDataRight, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P and D of rank-3 containers with dimensions (C...
static void contractDataDataScalar(ArrayOutData &outputData, const ArrayInDataLeft &inputDataLeft, const ArrayInDataRight &inputDataRight, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; dimensions P of rank-2 containers with dimensions (C,P), and returns the result...
static void contractDataDataTensor(ArrayOutData &outputData, const ArrayInDataLeft &inputDataLeft, const ArrayInDataRight &inputDataRight, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P, D1 and D2 of rank-4 containers with dimensions (C...
static void contractFieldFieldTensor(ArrayOutFields &outputFields, const ArrayInFieldsLeft &leftFields, const ArrayInFieldsRight &rightFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P, D1, and D2 of two rank-5 containers with dimensions (...
static void contractFieldFieldScalar(ArrayOutFields &outputFields, const ArrayInFieldsLeft &leftFields, const ArrayInFieldsRight &rightFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; dimension P of two rank-3 containers with dimensions (C,L,P) and (C...
static void contractFieldFieldVector(ArrayOutFields &outputFields, const ArrayInFieldsLeft &leftFields, const ArrayInFieldsRight &rightFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P and D1 of two rank-4 containers with dimensions (C...
static void contractDataFieldVector(ArrayOutFields &outputFields, const ArrayInData &inputData, const ArrayInFields &inputFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P and D of a rank-4 container and a rank-3 container wit...
static void contractDataFieldScalar(ArrayOutFields &outputFields, const ArrayInData &inputData, const ArrayInFields &inputFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; dimensions P of a rank-3 containers and a rank-2 container with dimensions (C...
static void contractDataFieldTensor(ArrayOutFields &outputFields, const ArrayInData &inputData, const ArrayInFields &inputFields, const ECompEngine compEngine, const bool sumInto=false)
Contracts the &quot;point&quot; and &quot;space&quot; dimensions P, D1 and D2 of a rank-5 container and a rank-4 containe...