52 #include <blitz/blitz.h>
53 #include <blitz/array.h>
54 #include <blitz/tinyvec.h>
55 #include <blitz/tinyvec-et.h>
56 #include <blitz/tinymat.h>
57 #include <blitz/matrix.h>
60 #ifdef HAVE_FEENABLEEXCEPT
66 #define MATVEC_DEBUG 1
69 #ifndef GRADIENT_DEBUG
70 #define GRADIENT_DEBUG 1
89 return 2 * (
doublereal(rand()) / RAND_MAX) - 1;
95 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>,
doublereal, Gradient<0> >::ExpressionType Expr003;
96 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>, Gradient<0>, doublereal>::ExpressionType Expr004;
97 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>, Expr003, Expr004>::ExpressionType Expr005;
98 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>, Expr005, Expr001>::ExpressionType Expr006;
99 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>, Expr005, Expr006>::ExpressionType Expr007;
103 typedef ScalarBinaryExpressionTraits<FuncPlus, Gradient<0>, Expr010, Expr007>::ExpressionType Expr011;
117 std::cout <<
"Expr001=" <<
typeid(Expr001).name() << std::endl;
118 std::cout <<
"Expr002=" <<
typeid(Expr002).name() << std::endl;
119 std::cout <<
"Expr003=" <<
typeid(Expr003).name() << std::endl;
120 std::cout <<
"Expr004=" <<
typeid(Expr004).name() << std::endl;
121 std::cout <<
"Expr005=" <<
typeid(Expr005).name() << std::endl;
122 std::cout <<
"Expr006=" <<
typeid(Expr006).name() << std::endl;
123 std::cout <<
"Expr007=" <<
typeid(Expr007).name() << std::endl;
124 std::cout <<
"T001=" <<
typeid(T001).name() << std::endl;
125 std::cout <<
"T002=" <<
typeid(T002).name() << std::endl;
126 std::cout <<
"T003=" <<
typeid(T003).name() << std::endl;
127 std::cout <<
"T004=" <<
typeid(T004).name() << std::endl;
128 std::cout <<
"T005=" <<
typeid(T005).name() << std::endl;
129 std::cout <<
"T006=" <<
typeid(T006).name() << std::endl;
130 std::cout <<
"T007=" <<
typeid(T007).name() << std::endl;
131 std::cout <<
"T008=" <<
typeid(T008).name() << std::endl;
132 std::cout <<
"T009=" <<
typeid(T009).name() << std::endl;
133 std::cout <<
"T010=" <<
typeid(T010).name() << std::endl;
134 std::cout <<
"T011=" <<
typeid(T011).name() << std::endl;
135 assert(
typeid(T001) ==
typeid(doublereal));
136 assert(
typeid(T002) ==
typeid(Gradient<0>));
137 assert(
typeid(T003) ==
typeid(Gradient<0>));
138 assert(
typeid(T004) ==
typeid(Gradient<0>));
139 assert(
typeid(T005) ==
typeid(Gradient<0>));
140 assert(
typeid(T006) ==
typeid(Gradient<0>));
141 assert(
typeid(T007) ==
typeid(Gradient<0>));
142 assert(
typeid(T008) ==
typeid(doublereal));
143 assert(
typeid(T009) ==
typeid(doublereal));
144 assert(
typeid(T010) ==
typeid(doublereal));
145 assert(
typeid(T011) ==
typeid(Gradient<0>));
151 std::cout <<
typeid(s003).name() << std::endl;
153 std::cout <<
"sizeof(Matrix<Gradient<12>, 3, 3>())="
156 std::cout <<
"sizeof(Vector<Gradient<12> >())="
159 std::cout <<
"sizeof(Matrix<Gradient<12>, 3, 3>() * Vector<Gradient<12>, 3>())="
163 std::cout <<
"v3.iGetMaxSize()=" << v3.
iGetMaxSize() << std::endl;
165 std::cout <<
"v0.iGetMaxSize()=" << v0.
iGetMaxSize() << std::endl;
167 std::cout <<
typeid(C001).name() << std::endl;
168 std::cout <<
typeid(C002).name() << std::endl;
175 template <
typename T, index_type N>
176 void func(
LocalDofMap* pDofMap,
const blitz::TinyVector<T, N>&
a,
const blitz::TinyVector<T, N>& b, blitz::TinyVector<T, N>&
c) {
182 const T d1 = blitz::dot(a, b) * (2. / 1.5);
184 blitz::TinyVector<T, N> b_d1;
186 for (
int i = 0; i < N; ++i) {
190 c = (a * r1 + b * r2) / 2. - (a * r3 - b) * r1 + b_d1;
192 T d1 = blitz::dot(a, b);
194 c = (a * r1 + b * r2) / 2. - (a * r3 - b) * r1 + b * d1;
199 template <
typename T, index_type N>
205 const T d1 =
Dot(a, b) * (2. / 1.5);
206 c = (a * (r1 / 2) + b * (r2 / 2.)) - (a * (r3 * r1) - b * r1) + b * d1;
209 template <
typename T>
218 for (
int i = 0; i < N; ++i) {
219 d1 += a[i] * b[i] * (2. / 1.5);
222 for (
int i = 0; i < N; ++i) {
223 c[i] = (a[i] * r1 + b[i] * r2) / 2. - (a[i] * r3 - b[i]) * r1 + b[i] * d1;
227 template <
typename T, index_type N>
228 void func2(
const Matrix<T, N, N>& A,
const Vector<T, N>& b,
const Vector<T, N>& c,
Vector<T, N>& d,
doublereal e,
doublereal& dt) {
236 template <
typename T>
245 const T tmp1 = b[3] - c__[3];
246 const T tmp2 = b[2] - c__[2];
247 const T tmp3 = b[1] - c__[1];
248 const T tmp4 = tmp3 * A[7];
249 const T tmp5 = tmp1 * A[9];
250 const T tmp6 = tmp1 * A[12];
251 const T tmp7 = tmp1 * A[6];
252 const T tmp8 = tmp2 * A[11];
253 const T tmp10 = tmp3 * A[10];
254 const T tmp11 = tmp6 + tmp8 + tmp10;
255 const T tmp13 = tmp5 + tmp2 * A[8] + tmp4;
256 const T tmp14 = tmp7 + tmp2 * A[5] + tmp3 * A[4];
258 d__[1] = (A[10] * tmp11 + A[7] * tmp13 + A[4] * tmp14) * e;
259 d__[2] = (A[11] * tmp11 + A[8] * tmp13 + A[5] * tmp14) * e;
260 d__[3] = (A[12] * tmp11 + A[9] * tmp13 + A[6] * tmp14) * e;
297 inline void func2ad(
const Matrix<doublereal, 3, 3>& A,
const Vector<doublereal, 3>& b,
const Vector<doublereal, 3>& c,
Vector<doublereal, 3>& d,
const doublereal& e,
LocalDofMap*,
doublereal& dt) {
301 for (
int i = 0; i < 3; ++i) {
302 for (
int j = 0; j < 3; ++j) {
303 A_F[j][i] = A(i + 1, j + 1);
316 template <index_type N_SIZE>
317 inline void func2ad(
const Matrix<
Gradient<N_SIZE>, 3, 3>& A,
const Vector<
Gradient<N_SIZE>, 3>& b,
const Vector<
Gradient<N_SIZE>, 3>& c,
Vector<
Gradient<N_SIZE>, 3>& d,
const doublereal& e,
LocalDofMap* pDofMap,
doublereal& dt) {
322 for (
int i = 0; i < 3; ++i) {
323 for (
int j = 0; j < 3; ++j) {
331 b_F[i] = b(i + 1).dGetValue();
332 c_F[i] =
c(i + 1).dGetValue();
335 bd_F[i][k] = b(i + 1).dGetDerivativeGlobal(k);
336 cd_F[i][k] =
c(i + 1).dGetDerivativeGlobal(k);
342 func2ad_dv_(A_F, Ad_F, b_F, bd_F, c_F, cd_F, d_F, dd_F, e, N_SIZE);
346 for (
int i = 0; i < 3; ++i) {
347 d(i + 1).SetValuePreserve(d_F[i]);
350 d(i + 1).SetDerivativeGlobal(k, dd_F[i][k]);
355 template <
typename T>
356 void func3(
const Matrix<T, 3, 3>& R1,
const Matrix<T, 3, 3>& R2,
const Vector<T, 3>& a,
const Vector<T, 3>& b,
Vector<T, 3>& c,
doublereal e) {
357 c = (
Cross(R1 * a, R2 * b) +
Cross(R2 * a, R1 * b)) * e;
361 void func3<doublereal>(
const Matrix<doublereal, 3, 3>& R1,
const Matrix<doublereal, 3, 3>& R2,
const Vector<doublereal, 3>&
a,
const Vector<doublereal, 3>& b,
Vector<doublereal, 3>&
c,
doublereal e);
364 void func3<Gradient<0> >(
const Matrix<Gradient<0> , 3, 3>& R1,
const Matrix<Gradient<0> , 3, 3>& R2,
const Vector<Gradient<0> , 3>&
a,
const Vector<Gradient<0> , 3>& b,
Vector<Gradient<0> , 3>&
c,
doublereal e);
366 template <
typename T>
368 assert(!std::isnan(a));
369 assert(!std::isnan(b));
370 doublereal dTolAbs = std::max<T>(1., std::max<T>(std::abs(a), std::abs(b))) * dTolRel;
371 return std::abs(a - b) <= dTolAbs;
374 template <index_type N_SIZE>
401 template <
typename T, index_type N>
405 for (
int i = 0; i <
NLoops; ++i) {
409 std::cerr <<
"Vector (" <<
typeid(T).name() <<
"): " <<
toc() <<
"s" << std::endl;
413 for (
int i = 0; i <
NLoops; ++i) {
417 std::cerr <<
"Array (" <<
typeid(T).name() <<
"): " <<
toc() <<
"s" << std::endl;
419 std::cout <<
"a=" << std::endl << a << std::endl;
420 std::cout <<
"b=" << std::endl << b << std::endl;
421 std::cout <<
"c=" << std::endl << c << std::endl;
422 std::cout <<
"c1=" << std::endl << c1 << std::endl;
424 const doublereal dTol =
sqrt(std::numeric_limits<doublereal>::epsilon());
426 for (
int i = 1; i <= N; ++i) {
431 template <
typename T>
432 void callFunc2(
LocalDofMap* pDofMap,
const Matrix<T, 3, 3>& A,
const Vector<T, 3>& b,
const Vector<T, 3>& c,
Vector<T, 3>& d,
Vector<T, 3>& d_C,
Vector<T, 3>& d_F) {
435 for (
int i = 0; i <
NLoops; ++i) {
439 std::cerr <<
"matvec (" <<
typeid(T).name() <<
"): " << dtMatVec <<
"s" << std::endl;
442 for (
int i = 0; i <
NLoops; ++i) {
446 std::cerr <<
"C (" <<
typeid(T).name() <<
"): " << dtC <<
"s" << std::endl;
449 for (
int i = 0; i <
NLoops; ++i) {
453 std::cerr <<
"F77 (" <<
typeid(T).name() <<
"): " << dtF <<
"s" << std::endl;
455 std::cerr <<
"overhead matvec:" << dtMatVec / std::max(std::numeric_limits<double>::epsilon(), dtF) << std::endl;
456 std::cerr <<
"overhead C:" << dtC / std::max(std::numeric_limits<double>::epsilon(), dtF) << std::endl;
458 std::cout <<
"A=" << std::endl << A << std::endl;
459 std::cout <<
"b=" << std::endl << b << std::endl;
460 std::cout <<
"c=" << std::endl << c << std::endl;
461 std::cout <<
"d=" << std::endl << d << std::endl;
462 std::cout <<
"d_C=" << std::endl << d_C << std::endl;
463 std::cout <<
"d_F=" << std::endl << d_F << std::endl;
465 const doublereal dTol = 10 * std::numeric_limits<scalar_deriv_type>::epsilon();
467 for (
int i = 1; i <= 3; ++i) {
468 assert(
bCompare(d(i), d_C(i), dTol));
469 assert(
bCompare(d(i), d_F(i), dTol));
473 template <index_type N>
479 a(i + 1).SetValuePreserve(100*(i + 1));
480 b(i + 1).SetValuePreserve(1000*(i + 10));
483 a(i + 1).SetDerivativeGlobal(i, -1. - 10.);
485 b(i + 1).SetDerivativeGlobal(i, -2. - 20.);
494 std::cout <<
"c=" << std::endl << c << std::endl;
495 std::cout <<
"d=" << std::endl << d << std::endl;
497 const doublereal dTol =
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon());
499 for (
int i = 1; i <= N; ++i) {
504 for (
int i = 0; i < N; ++i) {
505 c_C[i] =
c(i + 1).dGetValue();
507 for (
int j = 0; j < N; ++j) {
508 cd_C[i][j] =
c(i + 1).dGetDerivativeGlobal(j);
514 for (
int i = 1; i <= N; ++i) {
522 for (
int i = 1; i <= N; ++i) {
523 s4 += (
a(i) * 2 - b(i) * 3) / 1.5 +
c(i);
531 for (
int i = 1; i <= N; ++i) {
538 for (
int i = 1; i <= N; ++i) {
539 d4 += (
a(i) + b(i)) * (
a(i) - b(i));
548 for (
int i = 1; i <= N; ++i) {
549 d4 += (
a(i) + b(i)) * b(i);
552 assert(d3.bIsEqual(d4));
556 std::cout <<
"s1=" << s1 << std::endl;
557 std::cout <<
"s2=" << s2 << std::endl;
558 std::cout <<
"s3=" << s3 << std::endl;
559 std::cout <<
"s4=" << s4 << std::endl;
560 std::cout <<
"d1=" << d1 << std::endl;
561 std::cout <<
"d2=" << d2 << std::endl;
562 std::cout <<
"d3=" << d3 << std::endl;
563 std::cout <<
"d4=" << d4 << std::endl;
586 std::cout <<
"d=" << d << std::endl;
587 std::cout <<
"d5=" << d5 << std::endl;
589 for (
int i = 1; i <= N; ++i) {
590 assert(
bCompare(d(i), d5(i), dTol));
595 template <index_type N>
598 blitz::TinyVector<Gradient<N>, N>
a, b;
601 a(i).SetValuePreserve(100*(i + 1));
602 b(i).SetValuePreserve(1000*(i + 10));
605 a(i).SetDerivativeGlobal(i, -1. - 10.);
607 b(i).SetDerivativeGlobal(i, -2. - 20.);
610 blitz::TinyVector<Gradient<N>, N>
c;
614 for (
int i = 0; i <
NLoops; ++i) {
618 for (
int i = 0; i < N; ++i) {
619 c_C[i] =
c(i).dGetValue();
621 for (
int j = 0; j < N; ++j) {
622 cd_C[i][j] =
c(i).dGetDerivativeGlobal(j);
626 std::cerr <<
"blitz vector (Gradient): " <<
toc() <<
"s" << std::endl;
628 std::cout <<
"a=" << std::endl << a << std::endl;
629 std::cout <<
"b=" << std::endl << b << std::endl;
630 std::cout <<
"c=" << std::endl << c << std::endl;
634 template <index_type N>
639 a(i + 1) = 100*(i + 1);
640 b(i + 1) = 1000*(i + 10);
649 const doublereal dTol =
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon());
651 for (
int i = 1; i <= N; ++i) {
653 assert(d(i) == -
c(i));
656 for (
int i = 0; i < N; ++i) {
662 for (
int i = 0; i < N; ++i) {
674 for (
int i = 1; i <= N; ++i) {
675 s4 += (
a(i) * 2 - b(i) * 3) / 1.5 +
c(i);
684 for (
int i = 1; i <= N; ++i) {
688 assert(
bCompare(d2, d1, std::numeric_limits<scalar_deriv_type>::epsilon()));
690 std::cout <<
"s1=" << s1 << std::endl;
691 std::cout <<
"s2=" << s2 << std::endl;
692 std::cout <<
"s3=" << s3 << std::endl;
693 std::cout <<
"s4=" << s4 << std::endl;
694 std::cout <<
"d1=" << d1 << std::endl;
695 std::cout <<
"d2=" << d2 << std::endl;
713 for (
int i = 1; i <= N; ++i) {
714 assert(
bCompare(d(i), d5(i), dTol));
717 std::cout <<
"d=" << d << std::endl;
718 std::cout <<
"d5=" << d5 << std::endl;
721 template <index_type N_SIZE>
728 b(i + 1).SetValuePreserve(100*(i + 1));
730 c(i + 1).SetValuePreserve(1000*(i + 10));
734 c(i + 1).SetDerivativeGlobal(k, 3.5 * (k + 1) + 3.2 * (i + 1));
735 b(i + 1).SetDerivativeGlobal(k, -17.4 * (k + 1) + 7.5 *(i + 1));
739 A(i + 1, j + 1).SetValuePreserve(100*(i + 1) + j + 1);
742 A(i + 1, j + 1).SetDerivativeGlobal(k, -9.2 * (k + 1.) + 10.5 * (i + 1) + 3.2 * (j + 1) + 7.5);
761 assert(
bCompare(A(i, j), A_2(i, j)));
765 const doublereal dTol =
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon());
775 assert(
bCompare(A(i, j), A_3(i, j), dTol));
784 assert(
bCompare(A(i, j), A_4(i, j), dTol));
793 assert(
bCompare(b(i), b_3(i), dTol));
801 assert(
bCompare(b(i), b_4(i), dTol));
806 b += b * 0.75 /
Alias(b(1));
808 b /= (A(1, 3) + A(2, 3));
815 assert(
bCompare(b(i), b_5(i), dTol));
824 Vector<Gradient<N_SIZE>, 2> e = SubVector<2, 3>(b / 2.) + SubVector<1, 2>(b * 3.);
829 b =
Alias(b) * 2.3 + c * 5.0;
837 b(i + 1)=(100*(i + 1));
838 c(i + 1)=(1000*(i + 10));
841 A(i + 1, j + 1) = (100*(i + 1) + j + 1);
857 A(i + 1, j + 1) = 10 * (i + 1) + j + 1;
863 R(i + 1, j + 1) = 10 * (i + 1) + j + 1;
869 B(i + 1, j + 1) = (10 * (i + 1) + j + 1);
873 std::cout <<
"A=" << std::endl << A << std::endl;
877 std::cout <<
"A^T=" << std::endl << A_T << std::endl;
883 assert(
bCompare(A(i, j), A_T(j, i)));
884 assert(
bCompare(A(i, j), A_T2(j, i)));
892 assert(
bCompare(A2(i, j), A(i, j)));
896 std::cout <<
"\nrows of A:" << std::endl;
900 std::cout << i - 1 <<
": ";
903 assert(r(j) == A(i, j));
904 std::cout << r(j) <<
" ";
907 std::cout << std::endl;
910 std::cout <<
"\ncolumns of A:" << std::endl;
914 std::cout << j - 1 <<
": ";
917 assert(
c(i) == A(i, j));
918 std::cout <<
c(i) <<
" ";
921 std::cout << std::endl;
928 for (
int i = 1; i <= 3; ++i) {
929 for (
int j = 1; j <= 3; ++j) {
930 assert(
bCompare(D(i, j), -A(i, j)));
934 assert(E(1, 1) == -630);
935 assert(E(1, 2) == -1130);
936 assert(E(1, 3) == -1630);
937 assert(E(2, 1) == -1130);
938 assert(E(2, 2) == -2030);
939 assert(E(2, 3) == -2930);
940 assert(E(3, 1) == -1630);
941 assert(E(3, 2) == -2930);
942 assert(E(3, 3) == -4230);
948 x(i + 1) = 100 * (i + 1);
966 for (
index_type i = 1; i <= d.iGetNumRows(); ++i) {
967 assert(d(i) == e(i));
982 assert(h(i) == h2(i));
983 assert(h(i) == h3(i));
984 assert(h(i) == h4(i));
985 assert(h(i) == h5(i));
986 assert(h(i) == h6(i));
989 std::cout <<
"B=" << std::endl << B << std::endl;
990 std::cout <<
"A * B = C" << std::endl << C << std::endl;
992 std::cout <<
"R=" << std::endl << R << std::endl;
993 std::cout <<
"x = " << std::endl << x << std::endl;
994 std::cout <<
"A * x = b" << std::endl << b << std::endl;
995 std::cout <<
"R * A * x = c" << std::endl << c << std::endl;
996 std::cout <<
"A^T * b = d" << std::endl << d << std::endl;
997 std::cout <<
"A^T * A * x = e" << std::endl << e << std::endl;
998 std::cout <<
"A * A^T * A * 2 * x = f" << std::endl << f << std::endl;
999 std::cout <<
"A * B * g = h" << std::endl << h << std::endl;
1002 assert(b2(i) == b(i));
1003 assert(b3(i) == 3 * b(i));
1004 assert(b4(i) == b(i));
1005 assert(b5(i) == b(i));
1006 assert(b6(i) == b(i));
1009 assert(b(1) == 13000);
1010 assert(b(2) == 23000);
1011 assert(b(3) == 33000);
1013 assert(
c(1) == 848000);
1014 assert(
c(2) == 1538000);
1015 assert(
c(3) == 2228000);
1017 assert(d(1) == 1649000.00);
1018 assert(d(2) == 1718000.00);
1019 assert(d(3) == 1787000.00);
1020 assert(d(4) == 1856000.00);
1022 assert(C(1, 1) == 1.35e3);
1023 assert(C(2, 1) == 2.39e3);
1024 assert(C(3, 1) == 3.43e3);
1025 assert(C(1, 2) == 1.4e3);
1026 assert(C(2, 2) == 2.48e3);
1027 assert(C(3, 2) == 3.56e3);
1029 assert(h(1) == -394350);
1030 assert(h(2) == -699310);
1031 assert(h(3) == -1004270);
1040 assert(
bCompare(e123(1), (b(2) / 2. + b(1) * 3)));
1041 assert(
bCompare(e123(2), (b(3) / 2. + b(2) * 3)));
1047 F(i, j) = i * 10 + j;
1055 assert(
bCompare(G(i, j), F(i + 2, j + 4)));
1096 std::cout <<
"F=\n" <<
Tabular(F, 5) << std::endl;
1097 std::cout <<
"G=\n" <<
Tabular(G, 5) << std::endl;
1098 std::cout <<
"G1=\n" << G1 << std::endl;
1099 std::cout <<
"G2=\n" << G2 << std::endl;
1100 std::cout <<
"G3=\n" << G3 << std::endl;
1101 std::cout <<
"G4=\n" << G4 << std::endl;
1102 std::cout <<
"M=\n" <<
Tabular(M, 5) << std::endl;
1105 namespace testMatVecProductGradient_testData {
1107 template <
typename S, index_type N_rows, index_type N_SIZE>
1111 for (index_type j = 0; j <
index_type(
sizeof(ref.der[0])/
sizeof(ref.der[0][0])); ++j) {
1112 std::cout <<
"v(" << i + 1 <<
")=" << v(i + 1).dGetDerivativeGlobal(j + 1) << std::endl;
1113 std::cout <<
"ref.der[" << i <<
"][" << j <<
"]=" << ref.der[i][j] << std::endl;
1115 const bool bOK = bCompare<scalar_deriv_type>(v(i + 1).dGetDerivativeGlobal(j + 1), ref.der[i][j], dTol);
1116 std::cout <<
"dTol=" << dTol <<
" :[" << (bOK ?
"OK" :
"NOK") <<
"]" << std::endl;
1122 template <
typename S, index_type N_SIZE>
1126 for (index_type j = 0; j <
index_type(
sizeof(ref.der[0])/
sizeof(ref.der[0][0])); ++j) {
1139 {{220, 0, 0, 240, 0, 0, 260, 0, 0, 280, 0, 0},
1140 {210, 110, 0, 220, 120, 0, 230, 130, 0, 240, 140, 0},
1141 {310, 0, 110, 320, 0, 120, 330, 0, 130, 340, 0, 140}}};
1150 {{-220, 0, 0, -240, 0, 0, -260, 0, 0, -280, 0, 0},
1151 {-210, -110, 0, -220, -120, 0, -230, -130, 0, -240, -140, 0},
1152 {-310, 0, -110, -320, 0, -120, -330, 0, -130, -340, 0, -140}}};
1161 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1162 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1163 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
1172 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1173 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1174 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
1183 {{0, 0, -0, 0, 0, -0, 0, 0, -0, 0, 0, -0},
1184 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1185 {0, -0, 0, 0, -0, 0, 0, -0, 0, 0, -0, 0}}};
1194 {{220, 0, 0, 240, 0, 0, 260, 0, 0, 280, 0, 0},
1195 {210, 110, 0, 220, 120, 0, 230, 130, 0, 240, 140, 0},
1196 {310, 0, 110, 320, 0, 120, 330, 0, 130, 340, 0, 140}}};
1205 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1206 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1207 {-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0}}};
1213 {-74031.42857142857,
1216 {{-1390.571428571429, -389.7142857142857, -229.4285714285714, -1446.857142857143, -425.1428571428572, -250.2857142857143, -1503.142857142857, -460.5714285714286, -271.1428571428572, -1559.428571428571, -496, -292},
1217 {-611.1428571428571, 0, -493.4285714285714, -585.1428571428571, 0, -538.2857142857143, -559.1428571428571, 0, -583.1428571428571, -533.1428571428571, 0, -628},
1218 {1400.857142857143, 493.4285714285714, 0, 1487.428571428571, 538.2857142857143, 0, 1574, 583.1428571428571, 0, 1660.571428571429, 628, 0}}};
1226 -1337944.571428571},
1227 {{-2648.085714285714, -3602.028571428571, 6118.514285714286, -3602.457142857142, -3929.485714285714, 6674.742857142858, -4556.82857142857, -4256.942857142857, 7230.971428571428, -5511.200000000002, -4584.4, 7787.2},
1228 {-39236.54285714286, -12579.28571428571, -2844.914285714286, -41293.65714285714, -13722.85714285714, -3103.542857142857, -43350.77142857143, -14866.42857142857, -3362.171428571429, -45407.88571428572, -16010, -3620.8},
1229 {-19746.11428571428, -2844.914285714286, -9421.657142857142, -19748.8, -3103.542857142857, -10278.17142857143, -19751.48571428571, -3362.171428571428, -11134.68571428571, -19754.17142857143, -3620.8, -11991.2}}};
1237 -6689722.857142856},
1238 {{-13240.42857142857, -18010.14285714286, 30592.57142857143, -18012.28571428571, -19647.42857142857, 33373.71428571429, -22784.14285714285, -21284.71428571429, 36154.85714285714, -27556.00000000001, -22922, 38936},
1239 {-196182.7142857143, -62896.42857142857, -14224.57142857143, -206468.2857142857, -68614.28571428572, -15517.71428571428, -216753.8571428572, -74332.14285714284, -16810.85714285714, -227039.4285714286, -80050, -18104},
1240 {-98730.57142857142, -14224.57142857143, -47108.28571428571, -98743.99999999997, -15517.71428571429, -51390.85714285714, -98757.42857142858, -16810.85714285714, -55673.42857142857, -98770.85714285713, -18104, -59956}}};
1247 {{5765, -803, 1364, 6130, -876, 1488, 6495, -949, 1612, 6860, -1022, 1736}}};
1254 {{5765, -803, 1364, 6130, -876, 1488, 6495, -949, 1612, 6860, -1022, 1736}}};
1261 {{17624000, 2486000, 3586000, 18428000, 2712000, 3912000, 19232000, 2938000, 4238000, 20036000, 3164000, 4564000}}};
1267 {20810.33397137105},
1268 {{423.4434686210582, 59.72994002450923, 86.15911702650448, 442.760794357062, 65.15993457219189, 93.99176402891398, 462.0781200930658, 70.58992911987455, 101.8244110313235, 481.3954458290696, 76.01992366755721, 109.657058033733}}};
1272 template <
typename T, index_type N_rows>
1296 template <
typename T, index_type iRowCount>
1302 template <
typename T, index_type iRowCount>
1311 template <index_type iRowCount, index_type iMaxDeriv,
typename Function,
typename FunctionF77>
1312 void testVecOp(
const int M,
const int N, Function f, FunctionF77 f77,
const char*
function) {
1317 x(i).SetValuePreserve(i * 100);
1319 y(i).SetValuePreserve(i);
1322 for (
int j = 0; j < N; ++j) {
1323 x(i).SetDerivativeGlobal(j, (j + 1));
1324 y(i).SetDerivativeGlobal(j, (j + 1));
1330 for (
int i = 0; i < M; ++i) {
1336 std::cerr <<
"C++: testVecAdd<" << iRowCount <<
"," << iMaxDeriv <<
">(" << M <<
", " << N <<
", \"" <<
function <<
"\"):" << dt << std::endl;
1338 doublereal xF[iRowCount], yF[iRowCount], zF[iRowCount];
1343 for (
int i = 0; i < iRowCount; ++i) {
1344 xF[i] = x(i + 1).dGetValue();
1345 yF[i] = y(i + 1).dGetValue();
1348 for (
int j = 0; j < N; ++j) {
1349 xdF[i * N + j] = x(i + 1).dGetDerivativeLocal(j);
1350 ydF[i * N + j] = y(i + 1).dGetDerivativeLocal(j);
1351 zdF[i * N + j] = 0.;
1357 for (
int i = 0; i < M; ++i) {
1358 f77(xF, xdF, yF, ydF, zF, zdF, iRowCount, N);
1363 std::cerr <<
"F77: testVecAdd<" << iRowCount <<
"," << iMaxDeriv <<
">(" << M <<
", " << N <<
", \"" <<
function <<
"\"):" << dtF77 << std::endl;
1364 std::cerr <<
"overhead=" << dt/std::max(std::numeric_limits<doublereal>::epsilon(), dtF77) << std::endl;
1366 for (
int i = 0; i < iRowCount; ++i) {
1371 for (
int j = 0; j < N; ++j) {
1372 assert(xdF[i * N + j] == x(i + 1).dGetDerivativeLocal(j));
1373 assert(ydF[i * N + j] == y(i + 1).dGetDerivativeLocal(j));
1374 assert(zdF[i * N + j] == z(i + 1).dGetDerivativeLocal(j));
1389 A(i + 1, j + 1).SetValue(10 * (i + 1) + j + 1);
1391 A(i + 1, j + 1).SetDerivativeGlobal(j * A.
iGetNumRows() + i + 1, 1.);
1398 for (
int i = 1; i <= 3; ++i) {
1399 for (
int j = 1; j <= 4; ++j) {
1400 B1(i, j) = 3.5 * A(i, j);
1401 Ad(i, j) = A(i, j).dGetValue();
1409 Matrix<Gradient<0>, 3, 4> G1 = A - B1;
1410 Matrix<Gradient<0>, 3, 4> H1 = A * 3.5 + B1 / 4.;
1411 Matrix<Gradient<0>, 3, 4>
I1 = (A * 2. - B1 / 5.) * 3.5;
1412 Matrix<Gradient<0>, 3, 4> J1 = A * B1(1, 1) + I1 * H1(2, 2);
1413 Matrix<Gradient<0>, 3, 4> K1 = (I1 + J1) * H1(3, 4);
1415 const doublereal dTol = 10 * std::numeric_limits<scalar_deriv_type>::epsilon();
1417 for (
int i = 1; i <= 3; ++i) {
1418 for (
int j = 1; j <= 4; ++j) {
1431 std::cout <<
"A=" << std::endl << A << std::endl;
1433 std::cout <<
"A=" << std::endl << A << std::endl;
1435 Matrix<Gradient<0>, 4, 3> A_T =
Transpose(A);
1437 std::cout <<
"A^T=" << std::endl << A_T << std::endl;
1441 Matrix<Gradient<0>, 3, 4> B = -A;
1443 Matrix<Gradient<0>, 3, 3> D = -(A *
Transpose(A));
1447 assert(
bCompare(A(i, j), A_T(j, i)));
1448 assert(
bCompare(A(i, j), A_T2(j, i)));
1457 assert(
bCompare(A2(i, j), A(i, j)));
1461 std::cout <<
"\nrows of A:" << std::endl;
1464 Matrix<Gradient<0>, 3, 4>::RowVectorType r = A.
GetRow(i);
1465 std::cout << i - 1 <<
": ";
1468 assert(
bCompare(r(j), A(i, j), std::numeric_limits<scalar_deriv_type>::epsilon()));
1469 std::cout << r(j) <<
" ";
1472 std::cout << std::endl;
1475 std::cout <<
"\ncolumns of A:" << std::endl;
1478 Matrix<Gradient<0>, 3, 4>::ColumnVectorType c = A.
GetCol(j);
1479 std::cout << j - 1 <<
": ";
1482 assert(
bCompare(
c(i), A(i, j), std::numeric_limits<scalar_deriv_type>::epsilon()));
1483 std::cout <<
c(i) <<
" ";
1486 std::cout << std::endl;
1494 x(i) = A(1, i) * 10.;
1495 v(i) = x(i).dGetValue();
1500 b1_2 = A(1, 1) * x(1);
1501 b1_2 += A(1, 2) * x(2);
1502 b1_2 += A(1, 3) * x(3);
1503 b1_2 += A(1, 4) * x(4);
1505 b_1(1) = A(1, 1) * x(1) + A(1, 2) * x(2) + A(1, 3) * x(3) + A(1, 4) * x(4);
1509 b_1(2) = A(2, 1) * x(1) + A(2, 2) * x(2) + A(2, 3) * x(3) + A(2, 4) * x(4);
1510 b_1(3) = A(3, 1) * x(1) + A(3, 2) * x(2) + A(3, 3) * x(3) + A(3, 4) * x(4);
1513 using namespace testMatVecProductGradient_testData;
1537 Matrix<Gradient<0>, 3, 3> skew_gmf(
MatCrossVec(g - f));
1543 for (
int i = 1; i <= 3; ++i) {
1544 for (
int j = 1; j <= 3; ++j) {
1549 assert(skew_g(i, j).bIsEqual(-skew_g(j, i)));
1550 assert(skew_gmf(i, j).bIsEqual(-skew_gmf(j, i)));
1555 assert(skew_g(1, 2).bIsEqual(-g(3)));
1556 assert(skew_g(2, 1).bIsEqual(g(3)));
1557 assert(skew_g(1, 3).bIsEqual(g(2)));
1558 assert(skew_g(3, 1).bIsEqual(-g(2)));
1559 assert(skew_g(2, 3).bIsEqual(-g(1)));
1560 assert(skew_g(3, 2).bIsEqual(g(1)));
1562 assert(skew_gmf(1, 2).bIsEqual(-gmf(3)));
1563 assert(skew_gmf(2, 1).bIsEqual(gmf(3)));
1564 assert(skew_gmf(1, 3).bIsEqual(gmf(2)));
1565 assert(skew_gmf(3, 1).bIsEqual(-gmf(2)));
1566 assert(skew_gmf(2, 3).bIsEqual(-gmf(1)));
1567 assert(skew_gmf(3, 2).bIsEqual(gmf(1)));
1592 assert(r.bIsEqual(r1));
1593 assert(r.bIsEqual(r2));
1594 assert(r.bIsEqual(r3));
1596 std::cout <<
"x = " << std::endl << x << std::endl;
1597 std::cout <<
"b = A * x" << std::endl << b << std::endl;
1598 std::cout <<
"c = -A * x" << std::endl << c << std::endl;
1599 std::cout <<
"d = cross(c, b) * 5" << std::endl << d << std::endl;
1600 std::cout <<
"e = cross(c + d, b) / 2" << std::endl << e << std::endl;
1601 std::cout <<
"f = cross(e, d + c) - e" << std::endl << f << std::endl;
1602 std::cout <<
"g = cross(d + e, f - c) + b" << std::endl << g << std::endl;
1603 std::cout <<
"h = " << std::endl << h << std::endl;
1604 std::cout <<
"i = cross(d, h) * 5.7" << std::endl << i << std::endl;
1605 std::cout <<
"j = cross(h, g) / 3.5" << std::endl << j << std::endl;
1606 std::cout <<
"k = cross(h, h) + h * 3" << std::endl << k << std::endl;
1607 std::cout <<
"l = cross(h + h, j) / 2" << std::endl << l << std::endl;
1608 std::cout <<
"m = cross(h, j - i) * 5" << std::endl << m << std::endl;
1609 std::cout <<
"n = cross(h + h, h) + h" << std::endl << n << std::endl;
1610 std::cout <<
"o = cross(h, h + h) * 2" << std::endl << o << std::endl;
1611 std::cout <<
"p = cross(h * 2.5, h / 3) - h" << std::endl << p << std::endl;
1612 std::cout <<
"r = Dot(h, g) = " << r << std::endl;
1613 std::cout <<
"s = Dot(g, h) = " << s << std::endl;
1614 std::cout <<
"t = Dot(g, g) = " << t << std::endl;
1615 std::cout <<
"u = Dot(h, h) = " << u << std::endl;
1616 std::cout <<
"norm_g1 = Norm(g) = " << norm_g1 << std::endl;
1617 std::cout <<
"norm_g2 = Norm(Cross(d + e, f - c) + b) = " << norm_g2 << std::endl;
1618 std::cout <<
"z=" << z << std::endl;
1619 std::cout <<
"zd=" << zd << std::endl;
1621 for (
index_type i = 1; i <= b2.iGetNumRows(); ++i) {
1624 assert(
bCompare(b2(i), b(i), dTol));
1627 assert(
bCompare(b5(i), b(i), dTol));
1628 assert(
bCompare(b6(i), b(i), dTol));
1629 assert(
bCompare(b7(i), b(i), dTol));
1668 std::cout <<
"after reset ...\n";
1669 std::cout <<
"A=" << A << std::endl;
1670 std::cout <<
"x=" << x << std::endl;
1671 std::cout <<
"b=" << b << std::endl;
1674 template <index_type N_SIZE>
1684 A(i + 1, j + 1).SetValue(10. * (i + 1) + j + 1 + rand());
1686 for (
int k = 0; k < iNumDeriv; ++k) {
1687 A(i + 1, j + 1).SetDerivativeGlobal(k, 1000. * (i + 1) + 100. * (j + 1) + k + 1 + rand());
1694 B(i + 1, j + 1).SetValue(1000. * (i + 1) + 100. * (j + 1) + rand());
1696 for (
int k = 0; k < iNumDeriv; ++k) {
1697 B(i + 1, j + 1).SetDerivativeGlobal(k, 10000. * (i + 1) + 1000. * (j + 1) + 10. * (k + 1) + rand());
1706 for (
int i = 0; i < N; ++i) {
1712 std::cerr <<
"iMaxDerivatives=" << iNumDeriv << std::endl;
1713 std::cerr <<
"dt=" << dt << std::endl;
1714 std::cout <<
"A=\n" << A << std::endl;
1715 std::cout <<
"B=\n" << B << std::endl;
1716 std::cout <<
"C=\n" << C << std::endl;
1720 template <index_type N>
1721 void testMatVecDoubleBlitz(
doublereal c_C[N]) {
1722 blitz::TinyVector<doublereal, N>
a, b;
1726 b(i) = 1000*(i + 10);
1729 blitz::TinyVector<doublereal, N>
c, c1;
1733 for (
int i = 0; i <
NLoops; ++i) {
1737 for (
int i = 0; i < N; ++i) {
1741 std::cerr <<
"blitz vector (doublereal): " <<
toc() <<
"s" << std::endl;
1743 std::cout <<
"a=" << std::endl << a << std::endl;
1744 std::cout <<
"b=" << std::endl << b << std::endl;
1745 std::cout <<
"c=" << std::endl << c << std::endl;
1749 template <index_type N_rows>
1751 std::cerr <<
"---------------------------\ntestMatVecCopy<" << N_rows <<
">()\n";
1757 v(i).SetValuePreserve(i);
1761 std::cout <<
"v=" << std::endl << v << std::endl;
1765 Gradient<4> g1(v(1), &dofMap2), gi(v(i), &dofMap2), gim1(v(i - 1), &dofMap2), gip1(v(i + 1), &dofMap2);
1766 Gradient<4> x = 1000 * g1 + 100 * gi + 10 * gip1 + gim1;
1767 std::cout <<
"x(" << i <<
")=" << x << std::endl;
1769 assert(x.
dGetValue() == 1000 + 100 * i + 10 * (i + 1) + i - 1);
1783 std::cout <<
"v=" << v << std::endl;
1786 namespace gradVecAssTest {
1789 template <
typename T>
1792 T dCosAlpha(
cos(d));
1793 T dSinAlpha(
sin(d));
1798 T dCosGamma(
cos(d));
1799 T dSinGamma(
sin(d));
1801 R(1, 1) = dCosBeta*dCosGamma;
1802 R(2, 1) = dCosAlpha*dSinGamma + dSinAlpha*dSinBeta*dCosGamma;
1803 R(3, 1) = dSinAlpha*dSinGamma - dCosAlpha*dSinBeta*dCosGamma;
1804 R(1, 2) = -dCosBeta*dSinGamma;
1805 R(2, 2) = dCosAlpha*dCosGamma - dSinAlpha*dSinBeta*dSinGamma;
1806 R(3, 2) = dSinAlpha*dCosGamma + dCosAlpha*dSinBeta*dSinGamma;
1808 R(2, 3) = -dSinAlpha*dCosBeta;
1809 R(3, 3) = dCosAlpha*dCosBeta;
1822 :iFirstDofIndex(-1), R0(R_0), W0(W_0) {
1824 for (
int i = 0; i < 3; ++i) {
1825 X(i + 1).SetValuePreserve(X_0(i + 1));
1828 XP(i + 1).SetValuePreserve(XP_0(i + 1));
1830 XP(i + 1).SetDerivativeLocal(i, -1.);
1832 g(i + 1).SetValuePreserve(0.);
1835 gP(i + 1).SetValuePreserve(0.);
1837 gP(i + 1).SetDerivativeLocal(i, -1.);
1841 void SetValue(blitz::Array<doublereal, 1>& XCurr, blitz::Array<doublereal, 1>& XPrimeCurr) {
1842 assert(iFirstDofIndex != -1);
1844 for (
int i = 0; i < 3; ++i) {
1845 XCurr(iFirstDofIndex + i) = X(i + 1).dGetValue();
1846 XPrimeCurr(iFirstDofIndex + i) = XP(i + 1).dGetValue();
1847 XCurr(iFirstDofIndex + i + 3) = g(i + 1).dGetValue();
1848 XPrimeCurr(iFirstDofIndex + i + 3) = gP(i + 1).dGetValue();
1852 void Update(
const blitz::Array<doublereal, 1>& XCurr,
const blitz::Array<doublereal, 1>& XPrimeCurr,
doublereal dCoef) {
1853 assert(iFirstDofIndex != -1);
1855 for (
int i = 0; i < 3; ++i) {
1856 X(i + 1).SetValuePreserve(XCurr(iFirstDofIndex + i));
1857 X(i + 1).SetDerivativeLocal(i, -dCoef);
1858 XP(i + 1).SetValuePreserve(XPrimeCurr(iFirstDofIndex + i));
1859 g(i + 1).SetValuePreserve(XCurr(iFirstDofIndex + i + 3));
1860 g(i + 1).SetDerivativeLocal(i, -dCoef);
1861 gP(i + 1).SetValuePreserve(XPrimeCurr(iFirstDofIndex + i + 3));
1867 void UpdateRotation() {
1874 for (
int i = 1; i <= 3; ++i) {
1875 RDelta(i, i).SetValuePreserve(1.);
1879 for (
int i = 1; i <= 3; ++i) {
1880 for (
int j = 1; j <= 3; ++j) {
1881 RDelta(i, j) += d * (skew_g(i, j) + 0.5 * skew_skew_g(i, j));
1882 G(i, j) += 0.5 * d * skew_g(i, j);
1889 for (
int i = 1; i <= 3; ++i) {
1895 W = G * gP + RDelta * W0;
1898 void AfterConvergence(
const blitz::Array<doublereal, 1>& XCurr,
const blitz::Array<doublereal, 1>& XPrimeCurr) {
1899 for (
int i = 1; i <= 3; ++i) {
1900 W0(i) = W(i).dGetValue();
1901 g(i).SetValuePreserve(0.);
1902 gP(i).SetValuePreserve(0.);
1904 for (
int j = 1; j <= 3; ++j) {
1905 R0(i, j) =
R(i, j).dGetValue();
1910 void SetFirstDofIndex(
int iDofIndex) {
1911 iFirstDofIndex = iDofIndex;
1914 int iGetFirstIndex()
const {
1915 return iFirstDofIndex;
1918 int iGetNumDof()
const {
1923 for (
int i = 1; i <= 3; ++i) {
1924 XCurr(i) = X(i).dGetValue();
1928 template <index_type N_SIZE>
1930 assert(iFirstDofIndex != -1);
1931 assert(pDofMap != 0);
1933 for (
int i = 1; i <= 3; ++i) {
1934 XCurr(i).SetValuePreserve(X(i).
dGetValue());
1935 XCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + X(i).iGetStartIndexLocal(), iFirstDofIndex + X(i).iGetEndIndexLocal(),
MapVectorBase::GLOBAL, 0.);
1937 for (
index_type j = X(i).iGetStartIndexLocal(); j < X(i).iGetEndIndexLocal(); ++j) {
1938 XCurr(i).SetDerivativeGlobal(iFirstDofIndex + j, X(i).dGetDerivativeLocal(j));
1944 for (
int i = 1; i <= 3; ++i) {
1945 VCurr(i) = XP(i).dGetValue();
1949 template <index_type N_SIZE>
1951 assert(iFirstDofIndex != -1);
1952 assert(pDofMap != 0);
1954 for (
int i = 1; i <= 3; ++i) {
1955 VCurr(i).SetValuePreserve(XP(i).
dGetValue());
1956 VCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + XP(i).iGetStartIndexLocal(), iFirstDofIndex + XP(i).iGetEndIndexLocal(),
MapVectorBase::GLOBAL, 0.);
1958 for (
index_type j = XP(i).iGetStartIndexLocal(); j < XP(i).iGetEndIndexLocal(); ++j) {
1959 VCurr(i).SetDerivativeGlobal(iFirstDofIndex + j, XP(i).dGetDerivativeLocal(j));
1965 for (
int i = 1; i <= 3; ++i) {
1966 for (
int j = 1; j <= 3; ++j) {
1967 RCurr(i, j) =
R(i, j).dGetValue();
1972 template <index_type N_SIZE>
1974 assert(iFirstDofIndex != -1);
1975 assert(pDofMap != 0);
1977 for (
int i = 1; i <= 3; ++i) {
1978 for (
int j = 1; j <= 3; ++j) {
1979 RCurr(i, j).SetValuePreserve(
R(i, j).
dGetValue());
1980 RCurr(i, j).DerivativeResizeReset(pDofMap, iFirstDofIndex +
R(i, j).iGetStartIndexLocal() + 3, iFirstDofIndex +
R(i, j).iGetEndIndexLocal() + 3,
MapVectorBase::GLOBAL, 0.);
1982 for (
index_type k =
R(i, j).iGetStartIndexLocal(); k <
R(i, j).iGetEndIndexLocal(); ++k) {
1983 RCurr(i, j).SetDerivativeGlobal(iFirstDofIndex + k + 3,
R(i, j).dGetDerivativeLocal(k));
1994 for (
int i = 1; i <= 3; ++i) {
1995 WCurr(i) = W(i).dGetValue();
1999 template <index_type N_SIZE>
2001 assert(iFirstDofIndex != -1);
2002 assert(pDofMap != 0);
2004 for (
int i = 1; i <= 3; ++i) {
2005 WCurr(i).SetValuePreserve(W(i).
dGetValue());
2006 WCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + W(i).iGetStartIndexLocal() + 3, iFirstDofIndex + W(i).iGetEndIndexLocal() + 3,
MapVectorBase::GLOBAL, 0.);
2008 for (
index_type j = W(i).iGetStartIndexLocal(); j < W(i).iGetEndIndexLocal(); ++j) {
2009 WCurr(i).SetDerivativeGlobal(iFirstDofIndex + j + 3, W(i).dGetDerivativeLocal(j));
2022 static const int NADVars = 3;
2028 template <
typename T>
2033 ResItem(
int iEquIndex_=-1, T dCoef_=T(0.))
2034 :iEquIndex(iEquIndex_), dCoef(dCoef_) {
2041 ResizeReset(iNumRows, iNumCols);
2045 oWorkMat.resize(iNumRows, iNumCols);
2046 oRowIndex.resize(iNumRows);
2047 oColIndex.resize(iNumCols);
2048 oWorkMat.initialize(0.);
2049 oRowIndex.initialize(-1);
2050 oColIndex.initialize(-1);
2053 void PutRowIndex(
int iSubRow,
int iRow) {
2054 assert(iSubRow < oRowIndex.rows());
2055 oRowIndex(iSubRow) = iRow;
2058 void PutColIndex(
int iSubCol,
int iCol) {
2059 assert(iSubCol < oColIndex.rows());
2060 oColIndex(iSubCol) = iCol;
2063 void PutCoef(
int iSubRow,
int iSubCol,
doublereal dCoef) {
2064 assert(iSubRow >= 0 && iSubRow < oWorkMat.rows());
2065 assert(iSubCol >= 0 && iSubCol < oWorkMat.cols());
2067 oWorkMat(iSubRow, iSubCol) = dCoef;
2070 void IncCoef(
int iSubRow,
int iSubCol,
doublereal dCoef) {
2071 assert(iSubRow >= 0 && iSubRow < oWorkMat.rows());
2072 assert(iSubCol >= 0 && iSubCol < oWorkMat.cols());
2074 oWorkMat(iSubRow, iSubCol) += dCoef;
2077 void AddTo(blitz::Array<doublereal, 2>& JacMat)
const {
2078 for (
int i = 0; i < oWorkMat.rows(); ++i) {
2079 for (
int j = 0; j < oWorkMat.cols(); ++j) {
2080 assert(oRowIndex(i) >= 0);
2081 assert(oRowIndex(i) < JacMat.rows());
2082 assert(oColIndex(j) >= 0);
2083 assert(oColIndex(j) < JacMat.cols());
2084 JacMat(oRowIndex(i), oColIndex(j)) += oWorkMat(i, j);
2090 blitz::Array<doublereal, 2> oWorkMat;
2091 blitz::Array<int, 1> oRowIndex, oColIndex;
2101 JacItem(
int iEquIndex=-1,
int iDofIndex=-1,
doublereal dCoef=0.)
2102 :iEquIndex(iEquIndex), iDofIndex(iDofIndex), dCoef(dCoef) {
2106 typedef std::vector<JacItem> VectorType;
2107 typedef VectorType::const_iterator const_iterator;
2110 if (iNumItems > 0) {
2111 WorkMat.reserve(iNumItems);
2115 template <index_type N_SIZE,
typename T>
2117 pElem->AssRes(WorkVec, dCoef, pDofMap);
2120 for (
int i = 0; i < WorkVec.rows(); ++i) {
2121 const ResItem<Gradient<N_SIZE> >& resItem = WorkVec(i);
2123 for (
index_type j = resItem.dCoef.iGetStartIndexLocal(); j < resItem.dCoef.iGetEndIndexLocal(); ++j) {
2124 index_type iDofIndex = resItem.dCoef.iGetGlobalDof(j);
2125 InsertItem(JacItem(resItem.iEquIndex, iDofIndex, resItem.dCoef.dGetDerivativeLocal(j)));
2132 void ResizeReset(
int iNumItems) {
2133 WorkMat.resize(iNumItems);
2136 int iGetSize()
const {
return WorkMat.size(); }
2138 void InsertItem(
const JacItem& item) {
2139 WorkMat.push_back(item);
2142 void AddTo(blitz::Array<doublereal, 2>& JacMat)
const {
2143 for (const_iterator j = begin(); j != end(); ++j) {
2144 JacMat(j->iEquIndex, j->iDofIndex) += j->dCoef;
2147 const_iterator begin()
const {
return WorkMat.begin(); }
2148 const_iterator end()
const {
return WorkMat.end(); }
2156 virtual blitz::Array<ResItem<doublereal>, 1>& AssRes(blitz::Array<ResItem<doublereal>, 1>& WorkVec,
doublereal dCoef)=0;
2161 virtual ~Element(){ }
2164 class Element1:
public Element {
2170 static const int NADVars = 12;
2174 Element1(
Node* node1_,
2184 dofMap(iGetNumCols()) {
2215 template <
typename T>
2216 blitz::Array<ResItem<T>, 1>& AssRes(blitz::Array<ResItem<T>, 1>& WorkVec,
doublereal dCoef,
LocalDofMap *pDofMap) {
2217 WorkVec.resize(iGetNumRows());
2220 Vec3 X1, X2,
V1,
V2, W1, W2;
2223 node1->GetXCurr(X1, pDofMap);
2224 node1->GetVCurr(V1, pDofMap);
2225 node1->GetRCurr(R1, pDofMap);
2226 node1->GetWCurr(W1, pDofMap);
2227 node2->GetXCurr(X2, pDofMap);
2228 node2->GetVCurr(V2, pDofMap);
2229 node2->GetRCurr(R2, pDofMap);
2230 node2->GetWCurr(W2, pDofMap);
2232 const Vec3 R1o1 = R1 * o1;
2233 const Vec3 R2o2 = R2 * o2;
2234 const Vec3 dX =
Transpose(R1) * Vec3(X1 + R1o1 - X2 - R2o2);
2236 const Vec3 F1 = R1 * Vec3(-S * dX - D * dV);
2237 const Vec3 M1 =
Cross(R1o1, F1), M2 =
Cross(R2o2, -F1);
2239 for (
int i = 0; i < 6; ++i) {
2240 WorkVec(i).iEquIndex = node1->iGetFirstIndex() + i;
2241 WorkVec(i + 6).iEquIndex = node2->iGetFirstIndex() + i;
2244 for (
int i = 0; i < 3; ++i) {
2245 WorkVec(i).dCoef = F1(i + 1);
2246 WorkVec(i + 3).dCoef = M1(i + 1);
2247 WorkVec(i + 6).dCoef = -F1(i + 1);
2248 WorkVec(i + 9).dCoef = M2(i + 1);
2254 virtual blitz::Array<ResItem<doublereal>, 1>& AssRes(blitz::Array<ResItem<doublereal>, 1>& WorkVec,
doublereal dCoef) {
2255 return AssRes(WorkVec, dCoef, 0);
2259 blitz::Array<ResItem<Gradient<NADVars> >, 1> WorkVec;
2260 return WorkMat.AssJac(
this, &dofMap, WorkVec, dCoef);
2264 WorkMat.
ResizeReset(iGetNumRows(), iGetNumCols());
2268 for (
int i = 0; i < 6; ++i) {
2269 WorkMat.
PutColIndex(i, node1->iGetFirstIndex() + i);
2270 WorkMat.
PutColIndex(i + 6, node2->iGetFirstIndex() + i);
2271 WorkMat.
PutRowIndex(i, node1->iGetFirstIndex() + i);
2272 WorkMat.
PutRowIndex(i + 6, node2->iGetFirstIndex() + i);
2275 const Vec3& W1_0 = node1->GetWRef();
2276 const Vec3& W2_0 = node2->GetWRef();
2277 const Mat3x3& R1_0 = node1->GetRRef();
2278 const Mat3x3& R2_0 = node2->GetRRef();
2280 Vec3 X1, X2,
V1,
V2, W1, W2;
2283 node1->GetXCurr(X1, 0);
2284 node1->GetVCurr(V1, 0);
2285 node1->GetRCurr(R1, 0);
2286 node1->GetWCurr(W1, 0);
2288 node2->GetXCurr(X2, 0);
2289 node2->GetVCurr(V2, 0);
2290 node2->GetRCurr(R2, 0);
2291 node2->GetWCurr(W2, 0);
2293 #ifdef ASS_JAC_USE_TEMP_EXPR
2295 const Vec3 R1o1 = Vec3(R1 * o1);
2297 const Vec3 R1_0o1 = Vec3(R1_0 * o1);
2299 const Vec3 R2o2 = Vec3(R2 * o2);
2301 const Vec3 R2_0o2 = Vec3(R2_0 * o2);
2303 const Vec3 dX = Vec3(Mat3x3(
Transpose(R1)) * Vec3(Vec3(Vec3(X1 + R1o1) - X2) - R2o2));
2304 const Vec3 dV = Vec3(Mat3x3(
Transpose(R1)) * Vec3(Vec3(Vec3(V1 + Vec3(
Cross(W1, R1o1))) - V2) - Vec3(
Cross(W2, R2o2))));
2305 const Vec3 F1_R1 = Vec3(-Vec3(Vec3(S * dX) + Vec3(D * dV)));
2306 const Vec3 F1 = Vec3(R1 * F1_R1);
2307 const Vec3 F2 = Vec3(-F1);
2309 const Mat3x3 dF1_dX1 = Mat3x3(-Mat3x3(R1 * Mat3x3(S *
Transpose(R1))));
2311 const Mat3x3 ddX_dg1 = Mat3x3(Mat3x3(Mat3x3(
Transpose(R1_0)) * Mat3x3(
MatCrossVec(Vec3(Vec3(Vec3(X1 + R1o1) - X2) - R2o2)))) - Mat3x3(Mat3x3(
Transpose(R1)) * skew_R1_0o1));
2312 const Mat3x3 ddV_dg1 = Mat3x3(Mat3x3(
Transpose(R1_0)) * Mat3x3(
MatCrossVec(Vec3(Vec3(Vec3(V1 + Vec3(
Cross(W1, R1o1))) - V2) - Vec3(
Cross(W2, R2o2))))))
2314 const Mat3x3 dF1_dg1 = Mat3x3(Mat3x3(
MatCrossVec(Vec3(R1_0 * Vec3(-F1_R1)))) - Mat3x3(R1 * Mat3x3(Mat3x3(S * ddX_dg1) + Mat3x3(D * ddV_dg1))));
2316 const Mat3x3 dF1_dX2 = Mat3x3(R1 * Mat3x3(S * Mat3x3(
Transpose(R1))));
2317 const Mat3x3 ddX_dg2 = Mat3x3(Mat3x3(
Transpose(R1)) * skew_R2_0o2);
2318 const Mat3x3 ddV_dg2 = Mat3x3(Mat3x3(
Transpose(R1)) * Mat3x3(Mat3x3(skew_R2o2 * Mat3x3(-skew_W2_0)) + Mat3x3(skew_W2_0 * skew_R2_0o2)));
2319 const Mat3x3 dF1_dg2 = Mat3x3(Mat3x3(-R1) * Mat3x3(Mat3x3(S * ddX_dg2) + Mat3x3(D * ddV_dg2)));
2321 const Mat3x3 dF2_dX1 = Mat3x3(-dF1_dX1);
2322 const Mat3x3 dF2_dg1 = Mat3x3(-dF1_dg1);
2323 const Mat3x3 dF2_dX2 = Mat3x3(-dF1_dX2);
2324 const Mat3x3 dF2_dg2 = Mat3x3(-dF1_dg2);
2326 const Mat3x3 dM1_dX1 = Mat3x3(skew_R1o1 * dF1_dX1);
2327 const Mat3x3 dM1_dg1 = Mat3x3(Mat3x3(Mat3x3(
MatCrossVec(F1)) * skew_R1_0o1) + Mat3x3(skew_R1o1 * dF1_dg1));
2328 const Mat3x3 dM1_dX2 = Mat3x3(skew_R1o1 * dF1_dX2);
2329 const Mat3x3 dM1_dg2 = Mat3x3(skew_R1o1 * dF1_dg2);
2331 const Mat3x3 dM2_dX1 = Mat3x3(skew_R2o2 * dF2_dX1);
2332 const Mat3x3 dM2_dg1 = Mat3x3(skew_R2o2 * dF2_dg1);
2333 const Mat3x3 dM2_dX2 = Mat3x3(skew_R2o2 * dF2_dX2);
2334 const Mat3x3 dM2_dg2 = Mat3x3(Mat3x3(Mat3x3(
MatCrossVec(F2)) * skew_R2_0o2) + Mat3x3(skew_R2o2 * dF2_dg2));
2336 const Mat3x3 dF1_dV1 = Mat3x3(R1 * Mat3x3(Mat3x3(-D) * Mat3x3(
Transpose(R1))));
2337 const Mat3x3 ddV_dgP1 = Mat3x3(Mat3x3(-Mat3x3(
Transpose(R1))) * skew_R1o1);
2338 const Mat3x3 dF1_dgP1 = Mat3x3(R1 * Mat3x3(Mat3x3(-D) * ddV_dgP1));
2339 const Mat3x3 dF1_dV2 = Mat3x3(R1 * Mat3x3(D * Mat3x3(
Transpose(R1))));
2340 const Mat3x3 ddV_dgP2 = Mat3x3(Mat3x3(
Transpose(R1)) * skew_R2o2);
2341 const Mat3x3 dF1_dgP2 = Mat3x3(R1 * Mat3x3(Mat3x3(-D) * ddV_dgP2));
2343 const Mat3x3 dM1_dV1 = Mat3x3(skew_R1o1 * dF1_dV1);
2344 const Mat3x3 dM1_dgP1 = Mat3x3(skew_R1o1 * dF1_dgP1);
2345 const Mat3x3 dM1_dV2 = Mat3x3(skew_R1o1 * dF1_dV2);
2346 const Mat3x3 dM1_dgP2 = Mat3x3(skew_R1o1 * dF1_dgP2);
2348 const Mat3x3 dF2_dV1 = Mat3x3(-dF1_dV1);
2349 const Mat3x3 dF2_dgP1 = Mat3x3(-dF1_dgP1);
2350 const Mat3x3 dF2_dV2 = Mat3x3(-dF1_dV2);
2351 const Mat3x3 dF2_dgP2 = Mat3x3(-dF1_dgP2);
2353 const Mat3x3 dM2_dV1 = Mat3x3(skew_R2o2 * dF2_dV1);
2354 const Mat3x3 dM2_dgP1 = Mat3x3(skew_R2o2 * dF2_dgP1);
2355 const Mat3x3 dM2_dV2 = Mat3x3(skew_R2o2 * dF2_dV2);
2356 const Mat3x3 dM2_dgP2 = Mat3x3(skew_R2o2 * dF2_dgP2);
2359 const Vec3 R1o1 = R1 * o1;
2361 const Vec3 R1_0o1 = R1_0 * o1;
2363 const Vec3 R2o2 = R2 * o2;
2365 const Vec3 R2_0o2 = R2_0 * o2;
2367 const Vec3 dX =
Transpose(R1) * Vec3(X1 + R1o1 - X2 - R2o2);
2369 const Vec3 F1_R1 = -(S * dX + D * dV);
2370 const Vec3 F1 = R1 * F1_R1;
2371 const Vec3 F2 = -F1;
2373 const Mat3x3 dF1_dX1 = -R1 * Mat3x3(S *
Transpose(R1));
2378 const Mat3x3 dF1_dg1 = Mat3x3(
MatCrossVec(R1_0 * (-F1_R1))) - R1 * Mat3x3(S * ddX_dg1 + D * ddV_dg1);
2380 const Mat3x3 dF1_dX2 = R1 * Mat3x3(S *
Transpose(R1));
2381 const Mat3x3 ddX_dg2 =
Transpose(R1) * skew_R2_0o2;
2382 const Mat3x3 ddV_dg2 =
Transpose(R1) * Mat3x3(skew_R2o2 * (-skew_W2_0) + skew_W2_0 * skew_R2_0o2);
2383 const Mat3x3 dF1_dg2 = -R1 * Mat3x3(S * ddX_dg2 + D * ddV_dg2);
2385 const Mat3x3 dF2_dX1 = -dF1_dX1;
2386 const Mat3x3 dF2_dg1 = -dF1_dg1;
2387 const Mat3x3 dF2_dX2 = -dF1_dX2;
2388 const Mat3x3 dF2_dg2 = -dF1_dg2;
2390 const Mat3x3 dM1_dX1 = skew_R1o1 * dF1_dX1;
2391 const Mat3x3 dM1_dg1 = Mat3x3(
MatCrossVec(F1)) * skew_R1_0o1 + skew_R1o1 * dF1_dg1;
2392 const Mat3x3 dM1_dX2 = skew_R1o1 * dF1_dX2;
2393 const Mat3x3 dM1_dg2 = skew_R1o1 * dF1_dg2;
2395 const Mat3x3 dM2_dX1 = skew_R2o2 * dF2_dX1;
2396 const Mat3x3 dM2_dg1 = skew_R2o2 * dF2_dg1;
2397 const Mat3x3 dM2_dX2 = skew_R2o2 * dF2_dX2;
2398 const Mat3x3 dM2_dg2 = Mat3x3(
MatCrossVec(F2)) * skew_R2_0o2 + skew_R2o2 * dF2_dg2;
2400 const Mat3x3 dF1_dV1 = R1 * Mat3x3((-D) *
Transpose(R1));
2401 const Mat3x3 ddV_dgP1 = -
Transpose(R1) * skew_R1o1;
2402 const Mat3x3 dF1_dgP1 = R1 * Mat3x3((-D) * ddV_dgP1);
2403 const Mat3x3 dF1_dV2 = R1 * Mat3x3(D *
Transpose(R1));
2404 const Mat3x3 ddV_dgP2 =
Transpose(R1) * skew_R2o2;
2405 const Mat3x3 dF1_dgP2 = R1 * Mat3x3((-D) * ddV_dgP2);
2407 const Mat3x3 dM1_dV1 = skew_R1o1 * dF1_dV1;
2408 const Mat3x3 dM1_dgP1 = skew_R1o1 * dF1_dgP1;
2409 const Mat3x3 dM1_dV2 = skew_R1o1 * dF1_dV2;
2410 const Mat3x3 dM1_dgP2 = skew_R1o1 * dF1_dgP2;
2412 const Mat3x3 dF2_dV1 = -dF1_dV1;
2413 const Mat3x3 dF2_dgP1 = -dF1_dgP1;
2414 const Mat3x3 dF2_dV2 = -dF1_dV2;
2415 const Mat3x3 dF2_dgP2 = -dF1_dgP2;
2417 const Mat3x3 dM2_dV1 = skew_R2o2 * dF2_dV1;
2418 const Mat3x3 dM2_dgP1 = skew_R2o2 * dF2_dgP1;
2419 const Mat3x3 dM2_dV2 = skew_R2o2 * dF2_dV2;
2420 const Mat3x3 dM2_dgP2 = skew_R2o2 * dF2_dgP2;
2423 for (
int i = 0; i < 3; ++i) {
2424 for (
int j = 0; j < 3; ++j) {
2425 WorkMat.
PutCoef(i, j, -dF1_dV1(i + 1, j + 1) - dCoef * dF1_dX1(i + 1, j + 1));
2426 WorkMat.
PutCoef(i, j + 3, -dF1_dgP1(i + 1, j + 1) - dCoef * dF1_dg1(i + 1, j + 1));
2427 WorkMat.
PutCoef(i, j + 6, -dF1_dV2(i + 1, j + 1) - dCoef * dF1_dX2(i + 1, j + 1));
2428 WorkMat.
PutCoef(i, j + 9, -dF1_dgP2(i + 1, j + 1) - dCoef * dF1_dg2(i + 1, j + 1));
2430 WorkMat.
PutCoef(i + 3, j, -dM1_dV1(i + 1, j + 1) - dCoef * dM1_dX1(i + 1, j + 1));
2431 WorkMat.
PutCoef(i + 3, j + 3, -dM1_dgP1(i + 1, j + 1) - dCoef * dM1_dg1(i + 1, j + 1));
2432 WorkMat.
PutCoef(i + 3, j + 6, -dM1_dV2(i + 1, j + 1) - dCoef * dM1_dX2(i + 1, j + 1));
2433 WorkMat.
PutCoef(i + 3, j + 9, -dM1_dgP2(i + 1, j + 1) - dCoef * dM1_dg2(i + 1, j + 1));
2435 WorkMat.
PutCoef(i + 6, j, -dF2_dV1(i + 1, j + 1) - dCoef * dF2_dX1(i + 1, j + 1));
2436 WorkMat.
PutCoef(i + 6, j + 3, -dF2_dgP1(i + 1, j + 1) - dCoef * dF2_dg1(i + 1, j + 1));
2437 WorkMat.
PutCoef(i + 6, j + 6, -dF2_dV2(i + 1, j + 1) - dCoef * dF2_dX2(i + 1, j + 1));
2438 WorkMat.
PutCoef(i + 6, j + 9, -dF2_dgP2(i + 1, j + 1) - dCoef * dF2_dg2(i + 1, j + 1));
2440 WorkMat.
PutCoef(i + 9, j, -dM2_dV1(i + 1, j + 1) - dCoef * dM2_dX1(i + 1, j + 1));
2441 WorkMat.
PutCoef(i + 9, j + 3, -dM2_dgP1(i + 1, j + 1) - dCoef * dM2_dg1(i + 1, j + 1));
2442 WorkMat.
PutCoef(i + 9, j + 6, -dM2_dV2(i + 1, j + 1) - dCoef * dM2_dX2(i + 1, j + 1));
2443 WorkMat.
PutCoef(i + 9, j + 9, -dM2_dgP2(i + 1, j + 1) - dCoef * dM2_dg2(i + 1, j + 1));
2450 index_type iGetNumRows()
const {
return 12; }
2451 index_type iGetNumCols()
const {
return NADVars; }
2454 void testAssembly() {
2462 for (
int loop = 0; loop <
NLoopsAss; ++loop) {
2463 const int iNumNodes = 3;
2467 for (
int i = 0; i < iNumNodes; ++i) {
2470 for (
int j = 0; j < 3; ++j) {
2471 X0(j + 1) = ((i + 1) * 10 + j + 1);
2472 XP0(j + 1) = ((i + 1) * 1000 + (j + 1) * 100);
2473 Phi0(j + 1) = ((i + 1) * 0.1 + (j + 1) * 0.01);
2474 W0(j + 1) = ((i + 1) * 0.1 + (j + 1) * 0.01);
2481 nodes[i] =
new Node(X0, XP0, R0, W0);
2486 for (
int i = 0; i < iNumNodes; ++i) {
2487 nodes[i]->SetFirstDofIndex(iNumDof);
2491 const int iNumElem = iNumNodes - 1;
2493 Element* elements[iNumElem] = {0};
2495 for (
int i = 0; i < iNumElem; ++i) {
2507 elements[i] =
new Element1(nodes[i], o1, nodes[i + 1], o2, s, d);
2510 blitz::Array<doublereal, 1> XCurr(iNumDof), XPrimeCurr(iNumDof);
2512 XCurr.initialize(0.);
2513 XPrimeCurr.initialize(0.);
2515 for (
int i = 0; i < iNumNodes; ++i) {
2516 nodes[i]->
SetValue(XCurr, XPrimeCurr);
2519 blitz::Array<ResItem<doublereal>, 1> WorkVec;
2522 blitz::Array<doublereal, 1> ResVec;
2523 blitz::Array<doublereal, 2> JacMatAD, JacMat;
2525 ResVec.resize(iNumDof);
2526 JacMatAD.resize(iNumDof, iNumDof);
2527 JacMat.resize(iNumDof, iNumDof);
2529 ResVec.initialize(0.);
2530 JacMatAD.initialize(0.);
2531 JacMat.initialize(0.);
2535 for (
int iTime = 0; iTime < 100; ++iTime) {
2538 for (
int i = 0; i < iNumNodes; ++i) {
2539 nodes[i]->
Update(XCurr, XPrimeCurr, dCoef);
2542 for (
int i = 0; i < iNumElem; ++i) {
2545 elements[i]->AssRes(WorkVec, dCoef);
2549 for (
int j = 0; j < WorkVec.rows(); ++j) {
2550 ResVec(WorkVec(j).iEquIndex) += WorkVec(j).dCoef;
2555 elements[i]->AssJac(WorkMatSp, dCoef);
2559 WorkMatSp.
AddTo(JacMatAD);
2562 elements[i]->AssJac(WorkMatFull, dCoef);
2565 WorkMatFull.
AddTo(JacMat);
2568 for (
int i = 0; i < JacMat.rows(); ++i) {
2569 for (
int j = 0; j < JacMat.cols(); ++j) {
2570 const doublereal dTol =
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon()) * std::max(1., std::max(std::abs(JacMat(i, j)), std::abs(JacMatAD(i, j))));
2571 if(std::abs(JacMat(i, j) - JacMatAD(i, j)) > dTol) {
2572 throw std::runtime_error(
"testAssembly(): incorrect result");
2579 std::cout <<
"ResVec = [" << std::endl;
2581 for (
int i = 0; i < iNumDof; ++i) {
2582 std::cout << std::setw(10) << ResVec(i) << std::endl;
2585 std::cout <<
"]" << std::endl;
2587 std::cout <<
"JacMatAD = [" << std::endl;
2589 for (
int i = 0; i < iNumDof; ++i) {
2590 for (
int j = 0; j < iNumDof; ++j) {
2591 std::cout << std::setw(10) << std::setprecision(16) << JacMatAD(i, j) <<
" ";
2593 std::cout << std::endl;
2596 std::cout <<
"]" << std::endl;
2598 std::cout <<
"JacMat = [" << std::endl;
2600 for (
int i = 0; i < iNumDof; ++i) {
2601 for (
int j = 0; j < iNumDof; ++j) {
2602 std::cout << std::setw(10) << std::setprecision(16) << JacMat(i, j) <<
" ";
2604 std::cout << std::endl;
2607 std::cout <<
"]" << std::endl;
2610 for (
int i = 0; i < iNumElem; ++i) {
2614 for (
int i = 0; i < iNumNodes; ++i) {
2621 std::cerr <<
"number of iterations:" << iIterCnt << std::endl;
2622 std::cerr <<
"AssRes:" << tckRes / iIterCnt << std::endl;
2623 std::cerr <<
"AssJacAD:" << tckJacAD / iIterCnt << std::endl;
2624 std::cerr <<
"AssJac:" << tckJac / iIterCnt << std::endl;
2625 std::cerr <<
"overhead:" << tckJacAD / tckJac << std::endl;
2626 std::cerr <<
"total time:" << (tckEnd - tckStart) / iIterCnt << std::endl;
2635 for (
int n = 0; n <
NLoops; ++n) {
2639 g2(i) = g1(i) =
random1() * 1e-1;
2640 v2(i) = v1(i) =
random1() * 1e1;
2650 Vec3 CCv2 = CC2 * v2;
2654 Mat3x3 A2 = R2 * C2;
2663 std::cout <<
"g1=" << std::endl << g1 << std::endl;
2664 std::cout <<
"g2=" << std::endl << g2 << std::endl;
2665 std::cout <<
"R1=" << std::endl << R1 << std::endl;
2666 std::cout <<
"R2=" << std::endl << R2 << std::endl;
2667 std::cout <<
"C1=" << std::endl << C1 << std::endl;
2668 std::cout <<
"C2=" << std::endl << C2 << std::endl;
2669 std::cout <<
"CC1=" << std::endl << CC1 << std::endl;
2670 std::cout <<
"CC2=" << std::endl << CC2 << std::endl;
2671 std::cout <<
"CCv1=" << std::endl << CCv1 << std::endl;
2672 std::cout <<
"CCv2=" << std::endl << CCv2 << std::endl;
2673 std::cout <<
"CCv3=" << std::endl << CCv3 << std::endl;
2674 std::cout <<
"CCv4=" << std::endl << CCv4 << std::endl;
2675 std::cout <<
"A1=" << std::endl << A1 << std::endl;
2676 std::cout <<
"A2=" << std::endl << A2 << std::endl;
2677 std::cout <<
"A3=" << std::endl << A3 << std::endl;
2678 std::cout <<
"A4=" << std::endl << A4 << std::endl;
2681 const doublereal dTol = 10*std::numeric_limits<scalar_deriv_type>::epsilon();
2685 assert(std::abs(R1(i, j) - R2(i, j)) < dTol);
2686 assert(std::abs(C1(i, j) - C2(i, j)) < dTol);
2687 assert(std::abs(CC1(i, j) - CC2(i, j)) < dTol);
2688 assert(std::abs(A1(i, j) - A2(i, j)) < dTol);
2689 assert(std::abs(A1(i, j) - A3(i, j)) < dTol);
2690 assert(std::abs(A1(i, j) - A4(i, j)) < dTol);
2691 assert(std::abs(X1(i, j) - X2(i, j)) < dTol);
2693 assert(std::abs(CCv1(i) - CCv2(i)) < dTol);
2694 assert(std::abs(CCv3(i) - CCv1(i)) < dTol);
2695 assert(std::abs(CCv4(i) - CCv1(i)) < dTol);
2696 assert(v3(i) == v1(i));
2699 assert(v4(i) == v1(i));
2712 for (
integer i = 1; i <= N; ++i) {
2719 for (
integer i = 1; i <= N; ++i) {
2723 for (
integer k = 1; k <= 4; ++k) {
2727 WorkMat.AddItem(i, g);
2730 std::cout <<
"WorkVec=" << std::endl;
2732 std::cout <<
" " << vh.
dGetCoef(i) << std::endl;
2735 std::cout << std::endl;
2737 std::cout <<
"WorkMat=" << std::endl;
2742 <<
" " << mh.
dGetCoef(i, 0) << std::endl;
2745 std::cout << std::endl;
2755 for (
integer i = 1; i <= 3; ++i) {
2768 for (
integer i = 1; i <= 3; ++i) {
2769 g(i).SetValue(i * 10.);
2771 for (
integer k = 1; k <= 4; ++k) {
2772 g(i).SetDerivativeGlobal(k, i + k * 0.1);
2776 WorkMat.AddItem(1, g);
2784 WorkMat2.AddItem(4, g2);
2786 std::cout <<
"v=" << std::endl << v << std::endl;
2787 std::cout <<
"WorkVec=" << std::endl;
2792 std::cout << std::endl;
2794 std::cout <<
"g=" << std::endl << g << std::endl;
2796 std::cout <<
"WorkMat=" << std::endl;
2801 <<
" " << mh.
dGetCoef(i, 0) << std::endl;
2804 std::cout << std::endl;
2810 A(1, 1) = 0.658371336838182;
2811 A(1, 2) = 0.733036075010795;
2812 A(2, 1) = 0.483830962404444;
2813 A(2, 2) = 0.395950513263802;
2819 std::cout <<
"A=" <<
Tabular(A) << std::endl;
2820 std::cout <<
"Inv(A)=" <<
Tabular(invA) << std::endl;
2821 std::cout <<
"Det(A)=" << detA << std::endl;
2822 std::cout <<
"Inv(A)*A=" <<
Tabular(B1) << std::endl;
2823 std::cout <<
"A * Inv(A)=" <<
Tabular(B2) << std::endl;
2825 const doublereal dTol =
sqrt(std::numeric_limits<doublereal>::epsilon());
2827 assert(
bCompare(B1(1, 1), 1., dTol));
2828 assert(
bCompare(B2(1, 1), 1., dTol));
2829 assert(
bCompare(B1(2, 2), 1., dTol));
2830 assert(
bCompare(B2(2, 2), 1., dTol));
2831 assert(
bCompare(B1(1, 2), 0., dTol));
2832 assert(
bCompare(B2(1, 2), 0., dTol));
2833 assert(
bCompare(B1(2, 1), 0., dTol));
2834 assert(
bCompare(B2(2, 1), 0., dTol));
2836 assert(
bCompare(invA(1, 1), -4.21299780160758, dTol));
2837 assert(
bCompare(invA(2, 2), -7.00521126207687, dTol));
2838 assert(
bCompare(invA(1, 2), 7.79965998039245, dTol));
2839 assert(
bCompare(invA(2, 1), 5.14806449967026, dTol));
2840 assert(
bCompare(detA, -0.0939830809103952, dTol));
2844 const Mat3x3 A(1.32972137393521, 0.61849905020148, 0.709385530146435,
2845 0.61849905020148, 0.290559435134808, 0.344471283014357,
2846 0.709385530146435, 0.344471283014357, 0.752776020323268);
2848 const Vec3 b(0.815664130323409,
2852 const doublereal dTol =
sqrt(std::numeric_limits<doublereal>::epsilon());
2854 const Vec3 x1 = A.
Solve(b);
2859 std::cout <<
"b=" << b << std::endl;
2860 std::cout <<
"x1=" << x1 << std::endl;
2861 std::cout <<
"x2=" << x2 << std::endl;
2862 std::cout <<
"f1=" << f1 << std::endl;
2863 std::cout <<
"f2=" << f2 << std::endl;
2882 template <index_type N>
2884 doublereal c_MatVecGradient[N], cd_MatVecGradient[N][N];
2887 std::cerr <<
"---------------------------\ntestMatVecGradient<" << N <<
">()\n";
2888 testMatVecGradient<N>(c_MatVecGradient, cd_MatVecGradient);
2890 std::cerr <<
"---------------------------\ntestMatVecDouble<" << N <<
">()\n";
2891 testMatVecDouble<N>(c_MatVecDouble);
2895 doublereal c_MatVecGradientBlitz[N], cd_MatVecGradientBlitz[N][N];
2896 std::cerr <<
"---------------------------\ntestMatVecDoubleBlitz<" << N <<
">()\n";
2897 testMatVecDoubleBlitz<N>(c_MatVecDoubleBlitz);
2899 std::cerr <<
"---------------------------\ntestMatVecGradientBlitz<" << N <<
">()\n";
2900 testMatVecGradientBlitz<N>(c_MatVecGradientBlitz, cd_MatVecGradientBlitz);
2901 #endif // HAVE_BLITZ
2903 const doublereal dTol = 10 * std::numeric_limits<scalar_deriv_type>::epsilon();
2905 for (
int i = 0; i < N; ++i) {
2906 assert(
bCompare(c_MatVecGradient[i], c_MatVecDouble[i], dTol));
2908 assert(
bCompare(c_MatVecGradient[i], c_MatVecDoubleBlitz[i], dTol));
2909 assert(
bCompare(c_MatVecGradient[i], c_MatVecGradientBlitz[i], dTol));
2911 for (
int j = 0; j < N; ++j) {
2912 assert(
bCompare(cd_MatVecGradient[i][j], cd_MatVecGradientBlitz[i][j]));
2918 template <
int iNumDofMax>
2920 const int iNumDof = 6;
2922 assert(iNumDofMax == 0 || iNumDofMax >= iNumDof);
2938 for (
int loop = 0; loop < N; ++loop) {
2939 for (
int i = 0; i < 3; ++i) {
2940 X(i + 1).SetValuePreserve(0.);
2942 Phi(i + 1).SetValuePreserve(0.);
2954 for (
int i = 0; i < 3; ++i) {
2955 for (
int j = 0; j < iNumDof; ++j) {
2956 jac(i + 1, j + 1) = Y(i + 1).dGetDerivativeGlobal(j);
2961 std::cout <<
"calculation time: " << calc/N <<
"s\n";
2964 std::cout <<
"x=" << X << std::endl;
2965 std::cout <<
"y=" << Y << std::endl;
2966 std::cout <<
"jac=\n";
2968 for (
int i = 0; i < 3; ++i) {
2969 for (
int j = 0; j < iNumDof; ++j) {
2970 std::cout << jac(i + 1, j + 1) <<
'\t';
2972 std::cout << std::endl;
2976 template <
int iNumDofMax>
2978 const int iNumDof = 12;
2980 assert(iNumDofMax == 0 || iNumDofMax >= iNumDof);
3000 for (
int loop = 0; loop < N; ++loop) {
3001 for (
int i = 0; i < 3; ++i) {
3002 X1(i + 1).SetValuePreserve(0.);
3004 Phi1(i + 1).SetValuePreserve(0.);
3006 X2(i + 1).SetValuePreserve(0.);
3008 Phi2(i + 1).SetValuePreserve(0.);
3017 Y = X1 + R1 * o1 - X2 - R2 * o2;
3021 for (
int i = 0; i < 3; ++i) {
3022 for (
int j = 0; j < iNumDof; ++j) {
3023 jac(i + 1, j + 1) = Y(i + 1).dGetDerivativeGlobal(j);
3028 std::cout <<
"calculation time: " << calc/N <<
"s\n";
3031 std::cout <<
"X1=" << X1 << std::endl;
3032 std::cout <<
"X2=" << X2 << std::endl;
3033 std::cout <<
"Y=" << Y << std::endl;
3034 std::cout <<
"jac=\n";
3036 for (
int i = 0; i < 3; ++i) {
3037 for (
int j = 0; j < iNumDof; ++j) {
3038 std::cout << jac(i + 1, j + 1) <<
'\t';
3040 std::cout << std::endl;
3044 template <
int iNumDofMax>
3046 const int iNumDof = 12;
3048 assert(iNumDofMax == 0 || iNumDofMax >= iNumDof);
3055 Vec3 X1, X2, Y, Phi1, Phi2;
3072 for (
int loop = 0; loop < N; ++loop) {
3073 for (
int i = 0; i < 3; ++i) {
3074 X1(i + 1).SetValuePreserve(0.);
3076 Phi1(i + 1).SetValuePreserve(0.);
3078 X2(i + 1).SetValuePreserve(0.);
3080 Phi2(i + 1).SetValuePreserve(0.);
3089 Y =
Transpose(R2) * Vec3(X1 + R1 * o1 - X2) - o2;
3093 for (
int i = 0; i < 3; ++i) {
3094 for (
int j = 0; j < iNumDof; ++j) {
3095 jac(i + 1, j + 1) = Y(i + 1).dGetDerivativeGlobal(j);
3100 std::cout <<
"calculation time: " << calc/N <<
"s\n";
3103 std::cout <<
"X1=" << X1 << std::endl;
3104 std::cout <<
"X2=" << X2 << std::endl;
3105 std::cout <<
"Y=" << Y << std::endl;
3106 std::cout <<
"jac=\n";
3108 for (
int i = 0; i < 3; ++i) {
3109 for (
int j = 0; j < iNumDof; ++j) {
3110 std::cout << jac(i + 1, j + 1) <<
'\t';
3112 std::cout << std::endl;
3121 A(i, j) = 100 * i + j;
3127 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3136 for (
int i = 0; i < M; ++i) {
3137 x = x1 * 3. + x2 * 5.;
3143 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3149 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3161 A(i, j) = 100 * i + j;
3167 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3176 for (
int i = 0; i < M; ++i) {
3177 x = x1 * 3. + x2 * 5.;
3183 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3189 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3201 A(i, j) = 100 * i + j;
3207 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3216 for (
int i = 0; i < iNumLoops; ++i) {
3217 x = x1 * 3. + x2 * 5.;
3221 std::cerr <<
"Matrix<DYNAMIC_SIZE, DYNAMIC_SIZE>: " << (
mbdyn_clock_time() - start) / iNumLoops <<
"s\n";
3223 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3229 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3237 template <index_type N_SIZE>
3239 assert((N_SIZE == 0) || (N_SIZE >= iNumDeriv));
3247 A(i, j) = 100 * i + j;
3253 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3254 x1(i).SetValuePreserve(i);
3258 x1(i).SetDerivativeLocal(k, -1. * k);
3261 x2(i).SetValuePreserve(10 * i);
3265 x2(i).SetDerivativeLocal(k, 10. * k);
3273 for (
int i = 0; i < M; ++i) {
3274 x = x1 * 3. + x2 * 5.;
3278 std::cerr <<
"Mat3xN * Gradient: " << (
mbdyn_clock_time() - start) / M <<
"s\n";
3280 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3286 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3293 template <index_type N_SIZE>
3295 assert((N_SIZE == 0) || (N_SIZE >= iNumDeriv));
3303 A(i, j) = 100 * i + j;
3309 for (
index_type i = 1; i <= x1.iGetNumRows(); ++i) {
3310 x1(i).SetValuePreserve(i);
3314 x1(i).SetDerivativeLocal(k, -1. * k);
3317 x2(i).SetValuePreserve(10 * i);
3321 x2(i).SetDerivativeLocal(k, 10. * k);
3329 for (
int i = 0; i < M; ++i) {
3333 std::cerr <<
"Transpose(Mat3xN) * Gradient: " << (
mbdyn_clock_time() - start) / M <<
"s\n";
3335 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3341 b_i += A(j, i) * (3. * x1(j) + 5. * x2(j));
3348 template <index_type N_SIZE>
3350 assert((N_SIZE == 0) || (N_SIZE >= iNumDeriv));
3358 A(i, j) = 100 * i + j;
3364 for (
index_type i = 1; i <= x1.iGetNumRows(); ++i) {
3365 x1(i).SetValuePreserve(i);
3369 x1(i).SetDerivativeLocal(k, -1. * k);
3372 x2(i).SetValuePreserve(10 * i);
3376 x2(i).SetDerivativeLocal(k, 10. * k);
3384 for (
int i = 0; i < M; ++i) {
3388 std::cerr <<
"Transpose(MatNxN) * Gradient: " << (
mbdyn_clock_time() - start) / M <<
"s\n";
3390 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3396 b_i += A(j, i) * (3. * x1(j) + 5. * x2(j));
3403 template <index_type N_SIZE>
3406 assert((N_SIZE == 0) || (N_SIZE >= iNumDeriv));
3414 A(i, j) = 100 * i + j;
3420 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3421 x1(i).SetValuePreserve(i);
3425 x1(i).SetDerivativeLocal(k, -1. * k);
3428 x2(i).SetValuePreserve(10 * i);
3432 x2(i).SetDerivativeLocal(k, 10. * k);
3440 for (
int i = 0; i < M; ++i) {
3441 x = x1 * 3. + x2 * 5.;
3445 std::cerr <<
"MatNxN * Gradient: " << (
mbdyn_clock_time() - start) / M <<
"s\n";
3447 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3453 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3460 template <index_type N_SIZE>
3463 assert((N_SIZE == 0) || (N_SIZE >= iNumDeriv));
3471 A(i, j).SetValuePreserve(100 * i + j);
3475 A(i, j).SetDerivativeLocal(k, 1000. * i + 100. * j + k + 1);
3482 for (
index_type i = 1; i <= x.iGetNumRows(); ++i) {
3483 x1(i).SetValuePreserve(i);
3487 x1(i).SetDerivativeLocal(k, -1. * k);
3490 x2(i).SetValuePreserve(10 * i);
3494 x2(i).SetDerivativeLocal(k, 10. * k);
3502 for (
int i = 0; i < iNumLoops; ++i) {
3503 x = x1 * 3. + x2 * 5.;
3507 std::cerr <<
"Matrix<Gradient,DYNAMIC_SIZE,DYNAMIC_SIZE> * Gradient: " << (
mbdyn_clock_time() - start) / iNumLoops <<
"s\n";
3509 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3515 b_i += A(i, j) * (3. * x1(j) + 5. * x2(j));
3522 template <index_type N_ROWS, index_type N_COLS>
3525 assert((N_ROWS == iNumRows) || (N_ROWS ==
DYNAMIC_SIZE));
3526 assert((N_COLS == iNumCols) || (N_COLS ==
DYNAMIC_SIZE));
3532 A(i, j) = 100 * i + j;
3538 for (
index_type i = 1; i <= x1.iGetNumRows(); ++i) {
3547 for (
int i = 0; i < iNumLoops; ++i) {
3551 std::cerr <<
"Transpose(Matrix<doublereal>,"
3552 << iNumRows <<
"(" << N_ROWS <<
")," << iNumCols <<
"(" << N_COLS <<
")"
3553 <<
">) * Vector<doublereal>,"
3554 << iNumRows <<
"(" << N_ROWS <<
")>: " << (
mbdyn_clock_time() - start) / iNumLoops <<
"s\n";
3556 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3562 b_i += A(j, i) * (3. * x1(j) + 5. * x2(j));
3569 template <index_type N_DERIV, index_type N_ROWS, index_type N_COLS>
3572 assert((N_DERIV == 0) || (N_DERIV >= iNumDeriv));
3573 assert((N_ROWS == iNumRows) || (N_ROWS ==
DYNAMIC_SIZE));
3574 assert((N_COLS == iNumCols) || (N_COLS ==
DYNAMIC_SIZE));
3582 A(i, j).SetValuePreserve(100 * i + j);
3586 A(i, j).SetDerivativeLocal(k, 1000. * i + 100. * j + k + 1);
3593 for (
index_type i = 1; i <= x1.iGetNumRows(); ++i) {
3594 x1(i).SetValuePreserve(i);
3598 x1(i).SetDerivativeLocal(k, -1. * k);
3601 x2(i).SetValuePreserve(10 * i);
3605 x2(i).SetDerivativeLocal(k, 10. * k);
3613 for (
int i = 0; i < iNumLoops; ++i) {
3617 std::cerr <<
"Transpose(Matrix<Gradient<" << iNumDeriv <<
"(" << N_DERIV <<
")"
3618 <<
">," << iNumRows <<
"(" << N_ROWS <<
")," << iNumCols <<
"(" << N_COLS <<
")"
3619 <<
">) * Vector<Gradient<" << iNumDeriv <<
"(" << N_DERIV <<
")"
3620 <<
">," << iNumRows <<
"(" << N_ROWS <<
")>: " << (
mbdyn_clock_time() - start) / iNumLoops <<
"s\n";
3622 const doublereal tol =
sqrt(std::numeric_limits<doublereal>::epsilon());
3628 b_i += A(j, i) * (3. * x1(j) + 5. * x2(j));
3646 const doublereal tol1 = 10. * std::numeric_limits<doublereal>::epsilon();
3652 for (
int k = 0; k < 3 *
NLoops; ++k) {
3656 for (
int i = 1; i <= 3; ++i) {
3657 g0_(i) = (2. * rand() / RAND_MAX - 1.);
3660 g0_ *= (2. * rand() / RAND_MAX - 1.) * alpha *
M_PI / g0_.
Norm();
3662 if (k >= NLoops && k < 2 * NLoops) {
3663 g0_ *=
sqrt(std::numeric_limits<doublereal>::epsilon());
3664 }
else if (k >= 2 * NLoops) {
3665 g0_ *= std::numeric_limits<doublereal>::epsilon();
3672 const Mat3x3 X1(1., g0_);
3679 assert(
bCompare(G1(i, j), G2(i, j), tol1));
3680 assert(
bCompare(R1(i, j), R2(i, j), tol1));
3681 assert(
bCompare(X1(i, j), X2(i, j), tol1));
3687 }
else if (k == 1) {
3697 }
else if (k == 2) {
3707 }
else if (k == 3) {
3717 }
else if (k == 4) {
3718 R2(1,1)=-2.2841125213377644e-01;
3719 R2(1,2)=9.5997603033895429e-01;
3720 R2(1,3)=1.6209355654480440e-01;
3721 R2(2,1)=9.5997603033895418e-01;
3722 R2(2,2)=1.9435901751267337e-01;
3723 R2(2,3)=2.0166951551033063e-01;
3724 R2(3,1)=1.6209355654480423e-01;
3725 R2(3,2)=2.0166951551033083e-01;
3726 R2(3,3)=-9.6594776537889704e-01;
3733 R2_(i, j) = R2(i, j);
3741 assert(
bCompare(g1(i), g2(i), std::numeric_limits<doublereal>::epsilon()) ||
bCompare(
fabs(g1(i) - g2(i)), 2 *
M_PI, std::numeric_limits<doublereal>::epsilon()));
3747 #ifdef HAVE_FEENABLEEXCEPT
3748 feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
3751 NLoops = atol(argv[1]);
3755 NLoopsAss = atol(argv[2]);
3762 if (NLoopsAss < 1) {
3766 std::cerr <<
"MatManip_test()\n";
3770 std::cerr <<
"---------------------------\ntestScalarTypeTraits()\n";
3779 std::cerr <<
"---------------------------\ntestMatVecGradient2<1>()\n";
3780 testMatVecGradient2<1>();
3782 std::cerr <<
"---------------------------\ntestMatVecGradient2<2>()\n";
3783 testMatVecGradient2<2>();
3785 std::cerr <<
"---------------------------\ntestMatVecGradient2<3>()\n";
3786 testMatVecGradient2<3>();
3788 std::cerr <<
"---------------------------\ntestMatVecGradient2<4>()\n";
3789 testMatVecGradient2<4>();
3791 std::cerr <<
"---------------------------\ntestMatVecGradient2<5>()\n";
3792 testMatVecGradient2<5>();
3794 std::cerr <<
"---------------------------\ntestMatVecGradient2<6>()\n";
3795 testMatVecGradient2<6>();
3797 std::cerr <<
"---------------------------\ntestMatVecGradient2<8>()\n";
3798 testMatVecGradient2<8>();
3800 std::cerr <<
"---------------------------\ntestMatVecGradient2<10>()\n";
3801 testMatVecGradient2<10>();
3803 std::cerr <<
"---------------------------\ntestMatVecGradient2<12>()\n";
3804 testMatVecGradient2<12>();
3806 std::cerr <<
"---------------------------\ntestMatVecDouble2()\n";
3809 std::cerr <<
"---------------------------\ntestMatVecProduct()\n";
3811 std::cerr <<
"---------------------------\ntestMatVecProductGradient()\n";
3814 std::cerr <<
"---------------------------\ntestMatVecProductGradient2()\n";
3815 testMatVecProductGradient2<1>(1,
NLoops);
3816 testMatVecProductGradient2<4>(4,
NLoops);
3817 testMatVecProductGradient2<8>(8,
NLoops);
3818 testMatVecProductGradient2<12>(12,
NLoops);
3819 testMatVecProductGradient2<32>(32,
NLoops);
3820 testMatVecProductGradient2<64>(64,
NLoops);
3821 testMatVecProductGradient2<0>(256,
NLoops);
3823 std::cerr <<
"---------------------------\ntestMatVecCopy()\n";
3824 testMatVecCopy<1>();
3825 testMatVecCopy<3>();
3826 testMatVecCopy<5>();
3827 testMatVecCopy<9>();
3830 std::cerr <<
"---------------------------\ntestAssembly()\n";
3831 gradVecAssTest::testAssembly();
3834 std::cerr <<
"---------------------------\ntestMatVec3()\n";
3837 std::cerr <<
"---------------------------\ntestSubVecAss()\n";
3840 std::cerr <<
"---------------------------\ntestSubVecAssMatVec()\n";
3843 std::cerr <<
"---------------------------\ntestInv()\n";
3846 std::cerr <<
"----------------------------\ntestSolve()\n";
3850 const int N = argc > 3 ? atoi(argv[3]) : 1;
3851 const int nr = 4, nc = 4;
3854 for (
int i = 1; i <= nr; ++i)
3856 for (
int j = 1; j <= nc; ++j)
3863 clock_t start = clock();
3865 for (
int i = 0; i < N; ++i)
3870 std::cerr <<
"time: " << double(clock()-start)/N/CLOCKS_PER_SEC << std::endl;
3873 std::cerr <<
"----------------------------\ncppad_benchmark1<0>()\n";
3874 cppad_benchmark1<0>(
NLoops);
3876 std::cerr <<
"----------------------------\ncppad_benchmark1<6>()\n";
3877 cppad_benchmark1<6>(
NLoops);
3879 std::cerr <<
"----------------------------\ncppad_benchmark2<0>()\n";
3880 cppad_benchmark2<0>(
NLoops);
3882 std::cerr <<
"----------------------------\ncppad_benchmark2<12>()\n";
3883 cppad_benchmark2<12>(
NLoops);
3885 std::cerr <<
"----------------------------\ncppad_benchmark3<0>()\n";
3886 cppad_benchmark3<0>(
NLoops);
3888 std::cerr <<
"----------------------------\ncppad_benchmark3<12>()\n";
3889 cppad_benchmark3<12>(
NLoops);
3895 testVecOp<3, 16>(
NLoops, 16, doVecAdd<Gradient<16>, 3>, __FC_DECL__(
func2addad_dv),
"add");
3897 testVecOp<12, 2>(
NLoops, 2, doVecAdd<Gradient<2>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3898 testVecOp<12, 4>(
NLoops, 4, doVecAdd<Gradient<4>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3899 testVecOp<12, 8>(
NLoops, 8, doVecAdd<Gradient<8>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3900 testVecOp<12, 16>(
NLoops, 16, doVecAdd<Gradient<16>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3905 testVecOp<3, 16>(
NLoops, 16, doVecMul<Gradient<16>, 3>, __FC_DECL__(
func2mulad_dv),
"mul");
3907 testVecOp<12, 2>(
NLoops, 2, doVecAdd<Gradient<2>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3908 testVecOp<12, 4>(
NLoops, 4, doVecAdd<Gradient<4>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3909 testVecOp<12, 8>(
NLoops, 8, doVecAdd<Gradient<8>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3910 testVecOp<12, 16>(
NLoops, 16, doVecAdd<Gradient<16>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3916 testVecOp<3, 0>(
NLoops, 256, doVecAdd<Gradient<0>, 3>, __FC_DECL__(
func2addad_dv),
"add");
3917 testVecOp<3, 0>(
NLoops, 512, doVecAdd<Gradient<0>, 3>, __FC_DECL__(
func2addad_dv),
"add");
3918 testVecOp<3, 0>(
NLoops, 1024, doVecAdd<Gradient<0>, 3>, __FC_DECL__(
func2addad_dv),
"add");
3920 testVecOp<12, 0>(
NLoops, 2, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3921 testVecOp<12, 0>(
NLoops, 4, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3922 testVecOp<12, 0>(
NLoops, 8, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3923 testVecOp<12, 0>(
NLoops, 16, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3924 testVecOp<12, 0>(
NLoops, 256, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3925 testVecOp<12, 0>(
NLoops, 512, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3926 testVecOp<12, 0>(
NLoops, 1024, doVecAdd<Gradient<0>, 12>, __FC_DECL__(
func2addad_dv),
"add");
3932 testVecOp<3, 0>(
NLoops, 256, doVecMul<Gradient<0>, 3>, __FC_DECL__(
func2mulad_dv),
"mul");
3933 testVecOp<3, 0>(
NLoops, 512, doVecMul<Gradient<0>, 3>, __FC_DECL__(
func2mulad_dv),
"mul");
3934 testVecOp<3, 0>(
NLoops, 1024, doVecMul<Gradient<0>, 3>, __FC_DECL__(
func2mulad_dv),
"mul");
3936 testVecOp<12, 0>(
NLoops, 2, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3937 testVecOp<12, 0>(
NLoops, 4, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3938 testVecOp<12, 0>(
NLoops, 8, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3939 testVecOp<12, 0>(
NLoops, 16, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3940 testVecOp<12, 0>(
NLoops, 256, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3941 testVecOp<12, 0>(
NLoops, 512, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3942 testVecOp<12, 0>(
NLoops, 1024, doVecMul<Gradient<0>, 12>, __FC_DECL__(
func2mulad_dv),
"mul");
3952 Mat3xN_test_grad<4>(4, 3,
NLoops);
3953 Mat3xN_test_grad<5>(5, 10,
NLoops);
3954 Mat3xN_test_grad<0>(20, 100,
NLoops);
3956 Mat3xNT_test_grad<4>(4, 3,
NLoops);
3957 Mat3xNT_test_grad<5>(5, 10,
NLoops);
3958 Mat3xNT_test_grad<0>(20, 100,
NLoops);
3960 MatNxN_test_grad<4>(4, 3,
NLoops);
3961 MatNxN_test_grad<5>(5, 10,
NLoops);
3962 MatNxN_test_grad<0>(20, 100,
NLoops);
3964 MatNxNT_test_grad<4>(4, 3,
NLoops);
3965 MatNxNT_test_grad<5>(5, 10,
NLoops);
3966 MatNxNT_test_grad<0>(20, 100,
NLoops);
3971 MatDynamic_test_grad<3>(3, 4, 5,
NLoops);
3972 MatDynamic_test_grad<5>(5, 10, 20,
NLoops);
3973 MatDynamic_test_grad<0>(15, 20, 30,
NLoops);
3975 MatDynamicT_test<10, 20>(10, 20,
NLoops);
3976 MatDynamicT_test<DYNAMIC_SIZE, 20>(10, 20,
NLoops);
3977 MatDynamicT_test<10, DYNAMIC_SIZE>(10, 20,
NLoops);
3978 MatDynamicT_test<DYNAMIC_SIZE, DYNAMIC_SIZE>(10, 20,
NLoops);
3980 MatDynamicT_test_grad<5, 10, 20>(5, 10, 20,
NLoops);
3981 MatDynamicT_test_grad<5, DYNAMIC_SIZE, 20>(5, 10, 20,
NLoops);
3982 MatDynamicT_test_grad<5, 10, DYNAMIC_SIZE>(5, 10, 20,
NLoops);
3983 MatDynamicT_test_grad<5, DYNAMIC_SIZE, DYNAMIC_SIZE>(5, 10, 20,
NLoops);
3984 MatDynamicT_test_grad<0, 10, 20>(5, 10, 20,
NLoops);
3985 MatDynamicT_test_grad<0, DYNAMIC_SIZE, 20>(5, 10, 20,
NLoops);
3986 MatDynamicT_test_grad<0, 10, DYNAMIC_SIZE>(5, 10, 20,
NLoops);
3987 MatDynamicT_test_grad<0, DYNAMIC_SIZE, DYNAMIC_SIZE>(5, 10, 20,
NLoops);
3990 std::cerr <<
"\nNo tests have been done" << std::endl;
3992 std::cerr <<
"\nAll tests passed" << std::endl;
3995 std::cerr <<
"MATVEC_DEBUG=" <<
MATVEC_DEBUG << std::endl;
GradientExpression< UnaryExpr< FuncExp, Expr > > exp(const GradientExpression< Expr > &u)
scalar_deriv_type dGetDerivativeGlobal(index_type iGlobalDof) const
void testMatVecDouble(doublereal c_C[N])
void SetValue(scalar_func_type dVal)
bool bIsEqual(const Gradient &g) const
void callFunc2(LocalDofMap *pDofMap, const Matrix< T, 3, 3 > &A, const Vector< T, 3 > &b, const Vector< T, 3 > &c, Vector< T, 3 > &d, Vector< T, 3 > &d_C, Vector< T, 3 > &d_F)
scalar_func_type dGetValue(scalar_func_type d)
bool bCompare(const T &a, const T &b, doublereal dTolRel=0.)
void PutColIndex(integer iSubCol, integer iCol)
index_type iGetStartIndexLocal() const
void func2(const Matrix< T, N, N > &A, const Vector< T, N > &b, const Vector< T, N > &c, Vector< T, N > &d, doublereal e, doublereal &dt)
void doVecMul(const Vector< T, iRowCount > &x, const Vector< T, iRowCount > &y, Vector< T, iRowCount > &z)
MatrixExpression< TransposedMatrix< MatrixDirectExpr< Matrix< T, N_rows, N_cols > > >, N_cols, N_rows > Transpose(const Matrix< T, N_rows, N_cols > &A)
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
virtual const doublereal & dGetCoef(integer i) const
void cppad_benchmark1(const int N)
static const struct testMatVecProductGradient_testData::test_s oct_s
int main(int argc, char *argv[])
static const index_type DYNAMIC_SIZE
GradientExpression< UnaryExpr< FuncSin, Expr > > sin(const GradientExpression< Expr > &u)
integer iGetNumRows(void) const
const MatCross_Manip MatCross
static const struct testMatVecProductGradient_testData::test_l oct_l
void func(const Vector< T, N > &a, const Vector< T, N > &b, Vector< T, N > &c)
doublereal Norm(void) const
void Mat3xN_test_grad(int iNumDeriv, int N, int M)
integer iGetNumRows(void) const
double mbdyn_clock_time()
void MatDynamicT_test_grad(index_type iNumDeriv, index_type iNumRows, index_type iNumCols, int iNumLoops)
static const struct testMatVecProductGradient_testData::test_m oct_m
void doVecAdd(const Vector< T, iRowCount > &x, const Vector< T, iRowCount > &y, Vector< T, iRowCount > &z)
index_type iGetNumRows() const
void MatManip_test(int NLoops)
const Mat3x3 Eye3(1., 0., 0., 0., 1., 0., 0., 0., 1.)
MatrixHandler & AddTo(MatrixHandler &MH) const
void PutCoef(integer iRow, integer iCol, const doublereal &dCoef)
void AddItem(const integer iRow, const double dCoef)
static const struct testMatVecProductGradient_testData::test_c oct_c
template void func3< doublereal >(const Matrix< doublereal, 3, 3 > &R1, const Matrix< doublereal, 3, 3 > &R2, const Vector< doublereal, 3 > &a, const Vector< doublereal, 3 > &b, Vector< doublereal, 3 > &c, doublereal e)
void cppad_benchmark2(const int N)
MatrixHandler & AddTo(MatrixHandler &MH) const
void func2ad_dv(const doublereal A[3][3], const doublereal Ad[3][3][nbdirsmax], const doublereal b[3], const doublereal bd[3][nbdirsmax], const doublereal c[3], const doublereal cd[3][nbdirsmax], doublereal d[3], doublereal dd[3][nbdirsmax], const doublereal &e, const integer &nbdirs)
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
T Det(const Matrix< T, 2, 2 > &A)
index_type iGetNumCols() const
MatrixInit< MatGInit< T, VectorDirectExpr< Vector< T, 3 > > >, T, 3, 3 > MatGVec(const Vector< T, 3 > &g)
const ScalarType * pGetMat() const
MatrixInit< MatRInit< T, VectorDirectExpr< Vector< T, 3 > > >, T, 3, 3 > MatRVec(const Vector< T, 3 > &g)
Vec3 VecRot(const Mat3x3 &Phi)
index_type iGetNumRows() const
static const struct testMatVecProductGradient_testData::test_e oct_e
static const struct testMatVecProductGradient_testData::test_j oct_j
static index_type iGetMaxSize()
void MatDynamic_test_grad(index_type iNumDeriv, index_type iNumRows, index_type iNumCols, int iNumLoops)
void SetDerivativeGlobal(index_type iGlobalDof, scalar_deriv_type dCoef)
virtual integer iGetSize(void) const
ColumnVectorType GetCol(index_type iCol) const
MatrixExpression< MatrixDirectExpr< Matrix< T, N_rows, N_cols > >, N_rows, N_cols > Direct(const Matrix< T, N_rows, N_cols > &A)
static const struct testMatVecProductGradient_testData::test_d oct_d
static const struct testMatVecProductGradient_testData::test_f oct_f
void testSubVecAssMatVec()
void testScalarTypeTraits()
Matrix< T, 2, 2 > Inv(const Matrix< T, 2, 2 > &A)
const doublereal & dGetCoef(integer iSubIt, integer iDmy) const
static const char * dof[]
static const struct testMatVecProductGradient_testData::test_g oct_g
virtual integer iGetRowIndex(integer iSubRow) const
void MatDynamicT_test(index_type iNumRows, index_type iNumCols, int iNumLoops)
integer iGetNumRows(void) const
static const struct testMatVecProductGradient_testData::test_i oct_i
void func2ad(const doublereal A[3][3], const doublereal b[3], const doublereal c[3], doublereal d[3], const doublereal &e)
integer iGetNumCols(void) const
void Mat3xNT_test_grad(int iNumDeriv, int N, int M)
Vec3 Solve(const Vec3 &v) const
integer iGetColIndex(integer iSubIt) const
void callFunc(LocalDofMap *pDofMap, const Vector< T, N > &a, const Vector< T, N > &b, Vector< T, N > &c, Vector< T, N > &c1)
integer iGetNumCols(void) const
void func2mulad_dv(const doublereal x[], const doublereal xd[], const doublereal y[], const doublereal yd[], doublereal z[], doublereal zd[], const integer &n, const integer &nbdirs)
DotTraits< VectorExprLhs, VectorExprRhs, N_rows, N_rows >::ExpressionType Dot(const VectorExpression< VectorExprLhs, N_rows > &u, const VectorExpression< VectorExprRhs, N_rows > &v)
void testVecOp(const int M, const int N, Function f, FunctionF77 f77, const char *function)
static const struct testMatVecProductGradient_testData::test_b oct_b
void func3(const Matrix< T, 3, 3 > &R1, const Matrix< T, 3, 3 > &R2, const Vector< T, 3 > &a, const Vector< T, 3 > &b, Vector< T, 3 > &c, doublereal e)
void cppad_benchmark3(const int N)
void func2addad_dv(const doublereal x[], const doublereal xd[], const doublereal y[], const doublereal yd[], doublereal z[], doublereal zd[], const integer &n, const integer &nbdirs)
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
VectorExpression< VectorCrossExpr< VectorLhsExpr, VectorRhsExpr >, 3 > Cross(const VectorExpression< VectorLhsExpr, 3 > &u, const VectorExpression< VectorRhsExpr, 3 > &v)
void DerivativeResizeReset(LocalDofMap *pMap, index_type iStartGlobal, index_type iEndGlobal, MapVectorBase::GlobalScope s, scalar_deriv_type dVal)
virtual void ResizeReset(integer, integer)
void testMatVecProductGradient()
VectorInit< VecRotInit< T, MatrixDirectExpr< Matrix< T, 3, 3 > > >, T, 3 > VecRotMat(const Matrix< T, 3, 3 > &R)
Vec3 LDLSolve(const Vec3 &v) const
index_type iGetNumRows() const
VectorExpression< VectorExpr, N_rows >::ScalarType Norm(const VectorExpression< VectorExpr, N_rows > &u)
static std::stack< cleanup * > c
void MatNxN_test_grad(int iNumDeriv, int N, int M)
virtual unsigned int iGetNumDof(void) const =0
RowVectorType GetRow(index_type iRow) const
GradientExpression< DirectExpr< Gradient< N_SIZE >, true > > Alias(const Gradient< N_SIZE > &g)
const MatCrossCross_Manip MatCrossCross
void MatNxNT_test_grad(int iNumDeriv, int N, int M)
void MatDynamic_test(index_type iNumRows, index_type iNumCols, index_type iNumLoops)
Matrix< T, 3, 3 > & Euler123ToMatR(const Vector< T, 3 > &v, Matrix< T, 3, 3 > &R)
void PutRowIndex(integer iSubRow, integer iRow)
static const struct testMatVecProductGradient_testData::test_r oct_r
TabularMatrixView< T, N_rows, N_cols > Tabular(const Matrix< T, N_rows, N_cols > &A, int iColWidth=10)
SumTraits< VectorExpressionType, N_rows, N_rows >::ExpressionType Sum(const VectorExpression< VectorExpressionType, N_rows > &v)
GradientExpression< UnaryExpr< FuncCos, Expr > > cos(const GradientExpression< Expr > &u)
scalar_func_type dGetValue() const
static const doublereal a
T Norm_1(const Vector< T, N_rows > &u)
MatrixInit< MatCrossCrossInit< T, VectorDirectExpr< Vector< T, 3 > > >, T, 3, 3 > MatCrossCrossVec(const Vector< T, 3 > &v)
void testMatVecGradient2()
void testGradient(index_type N)
static const std::vector< doublereal > v0
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *h=0)
void testMatVecGradient(doublereal c_C[N], doublereal cd_C[N][N])
static const struct testMatVecProductGradient_testData::test_norm_g oct_norm_g
MatrixInit< MatCrossInit< T, VectorDirectExpr< Vector< T, 3 > > >, T, 3, 3 > MatCrossVec(const Vector< T, 3 > &v, doublereal d=0.)
index_type iGetEndIndexLocal() const
static const struct testMatVecProductGradient_testData::test_t oct_t
void testMatVecProductGradient2(index_type iNumDeriv, int N)
index_type iGetMaxDerivatives() const
integer iGetRowIndex(integer iSubIt) const
void Mat3xN_test(int N, int M)
virtual void Update(const VectorHandler &XCurr, const VectorHandler &XPrimeCurr)
void MatNxN_test(int N, int M)