52 #ifdef HAVE_FEENABLEEXCEPT 
   58 #include <blitz/blitz.h> 
   59 #include <blitz/array.h> 
   60 #include <blitz/tinyvec.h> 
   61 #include <blitz/tinyvec-et.h> 
   62 #include <blitz/tinymat.h> 
   63 #include <blitz/matrix.h> 
   67 #define M_PI 3.14159265358979 
   70 #ifndef GRADIENT_DEBUG 
   71         #define GRADIENT_DEBUG 1 
   87         return 2 * (
doublereal(rand()) / RAND_MAX) - 1;
 
   92         doublereal dTolAbs = std::max<T>(1., std::max<T>(std::abs(a), std::abs(b))) * dTolRel;
 
   94         if (!(std::abs(a - b) <= dTolAbs)) {
 
   95                 std::cerr << 
"a = " << a << 
" != b = " << b << std::endl;
 
  102 template <index_type N_SIZE>
 
  126 template <
int N_SIZE>
 
  128         std::cout << 
"testRangeVector<" << N_SIZE << 
">();\n";
 
  129     assert(N_SIZE == 0 || N_SIZE >= 6);
 
  145     vf.Reserve(N_SIZE == 0 ? vf.iGetEndIndex() * 2 : 2 * N_SIZE);
 
  155         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  158     for (
index_type i = vf.iGetStartIndex(); i < vf.iGetEndIndex(); ++i) {
 
  159         std::cout << 
"vf[" << i << 
"]=" << vf.GetValue(i) << std::endl;
 
  163         std::cout << 
"v2[" << i << 
"]=" << v2.
GetValue(i) << std::endl;
 
  167         std::cout << 
"v3[" << i << 
"]=" << v3.
GetValue(i) << std::endl;
 
  170     std::cout << std::endl;
 
  174         assert(v.
GetValue(i) == vf.GetValue(i));
 
  190     vf.ResizeReset(0, 12, 0.0f);
 
  192     for (
index_type i = vf.iGetStartIndex(); i < vf.iGetEndIndex(); ++i)
 
  194         vf.SetValue(i, 
float(i + 1));
 
  197     vf2.
ResizeReset(vf.iGetStartIndex(), vf.iGetEndIndex(), 0.0f);
 
  199     for (
index_type i = vf.iGetStartIndexVector(); i < vf.iGetEndIndexVector(); ++i)
 
  204     for (
index_type i = vf.iGetStartIndex(); i < vf.iGetEndIndex(); ++i)
 
  206         assert(vf.GetValue(i) == vf2.
GetValue(i));
 
  210         std::cout << 
"vf2[" << i << 
"]=" << vf2.
GetValue(i) << std::endl;
 
  213     std::cout << std::endl;
 
  220         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  223     std::cout << std::endl;
 
  230         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  233     std::cout << std::endl;
 
  240         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  243     std::cout << std::endl;
 
  250         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  253     std::cout << std::endl;
 
  260         std::cout << 
"v[" << i << 
"]=" << v.
GetValue(i) << std::endl;
 
  263     std::cout << std::endl;
 
  266 template <
int N_SIZE>
 
  268     assert(N_SIZE == 0 || N_SIZE == 19);
 
  271     const int iCount = 4;
 
  282         for (
int i = 0; i < iCount; ++i) {
 
  286                 std::cout << 
"v[" << i << 
"][" << j << 
"->" << dof.
iGetGlobalDof(j) << 
"]=" << dVal << std::endl;
 
  295             std::cout << 
"v1[" << j << 
"->" << dof.
iGetGlobalDof(j) << 
"]=" << dVal << std::endl;
 
  303             std::cout << 
"v2[" << j << 
"->" << dof.
iGetGlobalDof(j) << 
"]=" << dVal << std::endl;
 
  308 template <
typename T>
 
  310     f = ((((3 * u + 2 * v) * (u - v) / (1 - w) * 
pow(u/w, v - 1) * 
sin(v) * 
cos(w) * (1 - 
tan(-w + v))) * e + 1. - 11. + 4.5 - 1.) * 3.5 / 2.8 + u - v) * w / u;
 
  313 template <
typename T>
 
  320         f *= 
pow(u/w, v - 1);
 
  323         f *= (1 - 
tan(-w + v));
 
  326 #if GRADIENT_DEBUG > 0 
  336 #if GRADIENT_DEBUG > 0 
  343 #if GRADIENT_DEBUG > 0 
  371         const doublereal tmp7 = ((2*v+3*u)*tmp4*tmp5);
 
  379         const doublereal tmp15 = (tmp9*tmp12*tmp4*tmp8);
 
  387         const doublereal tmp23 = ((((tmp9*tmp14)/tmp10) * e + 1. - 11. + 4.5 - 1.) * tmp21 + u - v);
 
  391     doublereal df_du = ((tmp16/(u*tmp10)+tmp20+(3*tmp13)/tmp10)*tmp22 + 1.) * w / u - tmp23 * w / (u * u);
 
  393     doublereal df_dv = (((tmp19*
log(tmp3)*tmp18)/tmp10-tmp20+(2*tmp13)/tmp10+(tmp9*tmp12*
cos(v)*tmp18)/tmp10-tmp17)*tmp22 - 1.) * w / u;
 
  395     doublereal df_dw = (-(tmp19*tmp6*
sin(w)*tmp5)/tmp10-tmp16/(tmp10*w)+(tmp9*tmp14)/
pow(1-w,2)+tmp17)*tmp22 * w / u + tmp23 / u;
 
  398         fd[i] = df_du * ud[i] + df_dv * vd[i] + df_dw * wd[i];
 
  402 template <
int N_SIZE>
 
  404     assert(N_SIZE == 0 || N_SIZE == N);
 
  438     doublereal t_AD = 0., t_AD_tmp = 0., t_C = 0., t_d = 0.;
 
  440     for (
int i = 0; i < 
NLoops; ++i) {
 
  448         t_AD += stop - start;
 
  457         func(u, v, w, e, f_tmp);
 
  460         t_AD_tmp += stop - start;
 
  477                 assert(
bCompare(f, f_tmp, std::numeric_limits<doublereal>::epsilon()));
 
  479             assert(
bCompare(f.
dGetValue(), f_d, std::numeric_limits<doublereal>::epsilon()));
 
  483                 assert(err < 
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon()));
 
  487     std::cerr << 
"testGradient<" << N_SIZE << 
">(" << N << 
")" << std::endl;
 
  489     std::cout << 
"dof map:" << std::endl << dof << std::endl;
 
  490     std::cout << 
"u=" << u << std::endl;
 
  491     std::cout << 
"v=" << v << std::endl;
 
  492     std::cout << 
"w=" << w << std::endl;
 
  493     std::cout << 
"f=" << f << std::endl;
 
  494     std::cout << 
"f_tmp=" << f_tmp << std::endl;
 
  496     std::cout << 
"f_C=" << f_C << std::endl;
 
  497     std::cout << 
"fd_C=" << std::endl;
 
  499     for (
int i = 0; i < N; ++i) {
 
  500         std::cout << 
" " << fd_C[i] << std::endl;
 
  503     std::cout << std::endl;
 
  505     std::cerr << 
"t_AD=" << t_AD << 
"s" << std::endl;
 
  506     std::cerr << 
"t_d=" << t_d << 
"s" << std::endl;
 
  507     std::cerr << 
"t_AD_tmp=" << t_AD_tmp << 
"s" << std::endl;
 
  508     std::cerr << 
"t_C=" << t_C << 
"s" << std::endl;
 
  509     std::cerr << 
"overhead:" << t_AD / t_C << std::endl;
 
  510     std::cerr << 
"overhead tmp:" << t_AD_tmp / t_C << std::endl;
 
  518 template <index_type N_SIZE>
 
  520         assert(N_SIZE == 0 || N_SIZE == 3);
 
  522         const doublereal dTol = 
sqrt(std::numeric_limits<doublereal>::epsilon());
 
  551     for (
int i = 0; i < 
NLoops; ++i) {
 
  559         t_AD += stop - start;
 
  580                 assert(bCompare<scalar_deriv_type>(f.
dGetDerivativeGlobal(j), fd_C[j], 
sqrt(std::numeric_limits<scalar_deriv_type>::epsilon())));
 
  585     std::cerr << 
"testGradient2<" << N_SIZE << 
">(3)" << std::endl;
 
  587     std::cout << 
"dof map:" << std::endl << dof << std::endl;
 
  588     std::cout << 
"u=" << u << std::endl;
 
  589     std::cout << 
"v=" << v << std::endl;
 
  590     std::cout << 
"w=" << w << std::endl;
 
  591     std::cout << 
"f=" << f << std::endl;
 
  593     std::cout << 
"f_C=" << f_C << std::endl;
 
  594     std::cout << 
"fd_C=" << std::endl;
 
  596     for (
int i = 0; i < 3; ++i) {
 
  597         std::cout << 
" " << fd_C[i] << std::endl;
 
  600     std::cout << std::endl;
 
  602     std::cerr << 
"t_AD=" << t_AD << 
"s" << std::endl;
 
  603     std::cerr << 
"t_C=" << t_C << 
"s" << std::endl;
 
  604     std::cerr << 
"overhead:" << t_AD / t_C << std::endl;
 
  608     w = 3 * u + 2 * v + 0.5 * 
Alias(w);
 
  612     std::cout << 
"w=" << w << std::endl;
 
  613     std::cout << 
"w2=" << w2 << std::endl;
 
  634     for (
int i = 0; i < 
NLoops; ++i) {
 
  639         func3(u, v, w, e, f);
 
  642         t_AD += stop - start;
 
  650         std::cerr << 
"testGradientCopy(" << N << 
")\n";
 
  654         for (
int i = 0; i < 
NLoops; ++i) {
 
  657                 for (
int i = 0; i < N; ++i) {
 
  663                 for (
int i = 0; i < N; ++i) {
 
  669                 for (
int i = 0; i < N; ++i) {
 
  677                 assert(u != 100.001);
 
  684                 assert(100.001 != u);
 
  690                 assert(v != 200.001);
 
  697                 assert(200.001 != v);
 
  706                         std::cout << std::endl;
 
  707                         std::cout << 
"map1=" << std::endl << map1 << std::endl;
 
  708                         std::cout << 
"u=" << u << std::endl;
 
  709                         std::cout << 
"v=" << v << std::endl;
 
  710                         std::cout << 
"w=" << w << std::endl;
 
  711                         std::cout << 
"x=" << x << std::endl;
 
  712                         std::cout << std::endl;
 
  718                         std::cout << 
"map2=" << std::endl << map2 << std::endl;
 
  719                         std::cout << 
"y=" << y << std::endl;
 
  720                         std::cout << std::endl;
 
  726         std::cerr << 
"t=" << 
toc() << 
"s" << std::endl;
 
  729 template <index_type N_SIZE>
 
  737                 std::cerr << 
"testDifferentDofMaps<" << N_SIZE << 
">(" << N << 
"): operator+=()\n";
 
  738                 std::cerr << 
"g1=" << g1 << std::endl;
 
  739                 std::cerr << 
"g2=" << g2 << std::endl;
 
  743                 std::cerr << 
"g1=" << g1 << std::endl;
 
  748                         if (i < 100 + N || i >= 102) {
 
  750                                 if (i >= 102 && i < 100 + N) {
 
  752                                 } 
else if (i < 100 + N) {
 
  763                 const doublereal u = 3, v = 4, du = 5., dv = 6.;
 
  767                 std::cerr << 
"testDifferentDofMaps<" << N_SIZE << 
">(" << N << 
"): operator*=()\n";
 
  768                 std::cerr << 
"g1=" << g1 << std::endl;
 
  769                 std::cerr << 
"g2=" << g2 << std::endl;
 
  773                 std::cerr << 
"g1=" << g1 << std::endl;
 
  778                         if (i < 100 + N || i >= 102) {
 
  780                                 if (i >= 102 && i < 100 + N) {
 
  781                                         assert(d == du * v + u * dv);
 
  782                                 } 
else if (i < 100 + N) {
 
  792 template <index_type N_SIZE>
 
  808         std::cout << 
"A11=" << A11 << std::endl;
 
  809         std::cout << 
"A12=" << A12 << std::endl;
 
  810         std::cout << 
"A13=" << A13 << std::endl;
 
  811         std::cout << 
"A14=" << A14 << std::endl;
 
  823         std::cout << 
"x1=" << x1 << std::endl;
 
  824         std::cout << 
"x2=" << x2 << std::endl;
 
  825         std::cout << 
"x3=" << x3 << std::endl;
 
  826         std::cout << 
"x4=" << x4 << std::endl;
 
  833         std::cout << 
"y1=" << y1 << std::endl;
 
  834         std::cout << 
"y2=" << y2 << std::endl;
 
  835         std::cout << 
"y3=" << y3 << std::endl;
 
  836         std::cout << 
"y4=" << y4 << std::endl;
 
  845         std::cout << 
"y=" << y << std::endl;
 
  846         std::cout << 
"y_1=" << y_1 << std::endl;
 
  847         std::cout << 
"y_2=" << y_2 << std::endl;
 
  848         std::cout << 
"y_3=" << y_3 << std::endl;
 
  849         std::cout << 
"y_4=" << y_4 << std::endl;
 
  875 namespace gradAssTest {
 
  877 using namespace blitz;
 
  879 const int I1 = 0, 
I2 = 1, 
I3 = 2;
 
  881 template <
typename T>
 
  882 void Euler123ToMatR(
const TinyVector<T, 3>& theta, TinyMatrix<T, 3, 3>& R1) {
 
  883     const T cos_theta2 = 
cos(theta(
I2));
 
  884     const T cos_theta3 = 
cos(theta(
I3));
 
  885     const T sin_theta2 = 
sin(theta(
I2));
 
  886     const T sin_theta3 = 
sin(theta(
I3));
 
  887     const T sin_theta1 = 
sin(theta(
I1));
 
  888     const T cos_theta1 = 
cos(theta(
I1));
 
  889     const T sin_theta1_sin_theta2 = sin_theta1 * sin_theta2;
 
  890     const T cos_theta1_sin_theta3 = cos_theta1 * sin_theta3;
 
  891     const T cos_theta1_cos_theta3 = cos_theta1 * cos_theta3;
 
  892     R1(
I1, 
I1) = cos_theta2 * cos_theta3;
 
  893     R1(
I1, 
I2) = cos_theta2 * sin_theta3;
 
  894     R1(
I1, 
I3) = -sin_theta2;
 
  895     R1(
I2, 
I1) = sin_theta1_sin_theta2 * cos_theta3 - cos_theta1_sin_theta3;
 
  896     R1(
I2, 
I2) = sin_theta1_sin_theta2 * sin_theta3 + cos_theta1_cos_theta3;
 
  897     R1(
I2, 
I3) = sin_theta1 * cos_theta2;
 
  898     R1(
I3, 
I1) = sin_theta1 * sin_theta3 + cos_theta1_cos_theta3 * sin_theta2;
 
  899     R1(
I3, 
I2) = cos_theta1_sin_theta3 * sin_theta2 - sin_theta1 * cos_theta3;
 
  900     R1(
I3, 
I3) = cos_theta1 * cos_theta2;
 
  903 template <
typename T, 
int N_rows, 
int N_cols>
 
  904 TinyMatrix<T, N_cols, N_rows>& transpose(
const TinyMatrix<T, N_rows, N_cols>& A, TinyMatrix<T, N_cols, N_rows>& A_T) {
 
  905     for (
int i = 0; i < N_rows; ++i) {
 
  906         for (
int j = 0; j < N_cols; ++j) {
 
  914 template <
typename T, 
int N_rows, 
int N_cols>
 
  915 TinyVector<T, N_cols>& MatTDotVec(
const TinyMatrix<T, N_rows, N_cols>& A, 
const TinyVector<T, N_rows>& x, TinyVector<T, N_cols>& v) {
 
  916     for (
int i = 0; i < N_cols; ++i) {
 
  919         for (
int j = 0; j < N_rows; ++j) {
 
  920             v(i) += A(j, i) * x(j);
 
  927 template <
typename T, 
int R1, 
int C1, 
int C2>
 
  928 TinyMatrix<T, R1, C2> product(
const TinyMatrix<T, R1, C1>& A, 
const TinyMatrix<T, C1, C2>& B) {
 
  929     TinyMatrix<T, R1, C2> C;
 
  932     for (
int i = 0; i < R1; ++i) {
 
  933         for (
int j = 0; j < C2; ++j) {
 
  934             for (
int k = 0; k < C1; ++k) {
 
  935                 C(i, j) += A(i, k) * B(k, j);
 
  943 template <
typename T>
 
  944 TinyMatrix<T, 3, 3>& skew(TinyMatrix<T, 3, 3>& skew_g, 
const TinyVector<T, 3>& g) {
 
  945     skew_g(0, 0) = T(0.);
 
  946     skew_g(1, 1) = T(0.);
 
  947     skew_g(2, 2) = T(0.);
 
  949     skew_g(0, 1) = -g(2);
 
  953     skew_g(2, 0) = -g(1);
 
  955     skew_g(1, 2) = -g(0);
 
  961 template <
typename T>
 
  962 TinyMatrix<T, 3, 3> skew(
const TinyVector<T, 3>& g) {
 
  963     TinyMatrix<T, 3, 3> skew_g;
 
  975 template <
typename T>
 
  976 TinyMatrix<T, 3, 3>& skew_skew(TinyMatrix<T, 3, 3>& skew_g_skew_g, 
const TinyVector<T, 3>& g) {
 
  977     skew_g_skew_g(0, 0) = -g(2)*g(2)-g(1)*g(1);
 
  978     skew_g_skew_g(0, 1) = g(0)*g(1);
 
  979     skew_g_skew_g(0, 2) = g(0)*g(2);
 
  980     skew_g_skew_g(1, 0) = g(0)*g(1);
 
  981     skew_g_skew_g(1, 1) = -g(2)*g(2)-g(0)*g(0);
 
  982     skew_g_skew_g(1, 2) = g(1)*g(2);
 
  983     skew_g_skew_g(2, 0) = g(0)*g(2);
 
  984     skew_g_skew_g(2, 1) = g(1)*g(2);
 
  985     skew_g_skew_g(2, 2) = -g(1)*g(1)-g(0)*g(0);
 
  987     return skew_g_skew_g;
 
  990 template <
typename T>
 
  991 TinyMatrix<T, 3, 3> transpose(
const TinyMatrix<T, 3, 3>& A) {
 
  992     TinyMatrix<T, 3, 3> At;
 
  994     for (
int i = 0; i < 3; ++i) {
 
  995         for (
int j = 0; j < 3; ++j) {
 
 1003 template <
typename T, 
int N_rows, 
int N_cols>
 
 1004 TinyMatrix<T, N_rows, N_cols> 
operator-(
const TinyMatrix<T, N_rows, N_cols>& A) {
 
 1005     TinyMatrix<T, N_rows, N_cols> B(A);
 
 1007     for (
int i = 0; i < N_rows; ++i) {
 
 1008         for (
int j = 0; j < N_cols; ++j) {
 
 1016 template <
typename T, 
int N_rows, 
int N_cols>
 
 1017 TinyMatrix<T, N_rows, N_cols> 
operator-(
const TinyMatrix<T, N_rows, N_cols>& A, 
const TinyMatrix<T, N_rows, N_cols>& B) {
 
 1018     TinyMatrix<T, N_rows, N_cols> C(A);
 
 1020     for (
int i = 0; i < N_rows; ++i) {
 
 1021         for (
int j = 0; j < N_cols; ++j) {
 
 1029 template <
typename T, 
int N_rows, 
int N_cols>
 
 1030 TinyMatrix<T, N_rows, N_cols> 
operator+(
const TinyMatrix<T, N_rows, N_cols>& A, 
const TinyMatrix<T, N_rows, N_cols>& B) {
 
 1031     TinyMatrix<T, N_rows, N_cols> C(A);
 
 1033     for (
int i = 0; i < N_rows; ++i) {
 
 1034         for (
int j = 0; j < N_cols; ++j) {
 
 1044     Node(
const TinyVector<doublereal, 3>& X_0,
 
 1045          const TinyVector<doublereal, 3>& XP_0,
 
 1046          const TinyMatrix<doublereal, 3, 3>& R_0,
 
 1047          const TinyVector<doublereal, 3>& W_0)
 
 1048         :iFirstDofIndex(-1), R0(R_0), W0(W_0) {
 
 1050         for (
int i = 0; i < 3; ++i) {
 
 1051             X(i).SetValuePreserve(X_0(i));
 
 1054             XP(i).SetValuePreserve(XP_0(i));
 
 1056             XP(i).SetDerivativeLocal(i, -1.); 
 
 1058             g(i).SetValuePreserve(0.);
 
 1061             gP(i).SetValuePreserve(0.);
 
 1063             gP(i).SetDerivativeLocal(i, -1.); 
 
 1067     void SetValue(Array<doublereal, 1>& XCurr, Array<doublereal, 1>& XPrimeCurr) {
 
 1068         assert(iFirstDofIndex != -1);
 
 1070         for (
int i = 0; i < 3; ++i) {
 
 1071             XCurr(iFirstDofIndex + i) = X(i).dGetValue();
 
 1072             XPrimeCurr(iFirstDofIndex + i) = XP(i).dGetValue();
 
 1073             XCurr(iFirstDofIndex + i + 3) = g(i).dGetValue();
 
 1074             XPrimeCurr(iFirstDofIndex + i + 3) = gP(i).dGetValue();
 
 1078     void Update(
const Array<doublereal, 1>& XCurr, 
const Array<doublereal, 1>& XPrimeCurr, 
doublereal dCoef) {
 
 1079         assert(iFirstDofIndex != -1);
 
 1081         for (
int i = 0; i < 3; ++i) {
 
 1082             X(i).SetValuePreserve(XCurr(iFirstDofIndex + i));
 
 1083             X(i).SetDerivativeLocal(i, -dCoef);
 
 1084             XP(i).SetValuePreserve(XPrimeCurr(iFirstDofIndex + i));
 
 1085             g(i).SetValuePreserve(XCurr(iFirstDofIndex + i + 3));
 
 1086             g(i).SetDerivativeLocal(i, -dCoef);
 
 1087             gP(i).SetValuePreserve(XPrimeCurr(iFirstDofIndex + i + 3));
 
 1093     void UpdateRotation() {
 
 1094         TinyMatrix<Gradient<NADVars>, 3, 3> RDelta, G;
 
 1095         TinyMatrix<Gradient<NADVars>, 3, 3> skew_g;
 
 1096         TinyMatrix<Gradient<NADVars>, 3, 3> skew_skew_g;
 
 1099         skew_skew(skew_skew_g, g);
 
 1103         for (
int i = 0; i < 3; ++i) {
 
 1108         for (
int i = 0; i < 3; ++i) {
 
 1109             for (
int j = 0; j < 3; ++j) {
 
 1110                 RDelta(i, j) += d * (skew_g(i, j) + 0.5 * skew_skew_g(i, j));
 
 1111                 G(i, j) += 0.5 * d * skew_g(i, j);
 
 1115         R = product(RDelta, R0);
 
 1116         W = product(G, gP) + product(RDelta, W0);
 
 1119     void AfterConvergence(
const Array<doublereal, 1>& XCurr, 
const Array<doublereal, 1>& XPrimeCurr) {
 
 1120         for (
int i = 0; i < 3; ++i) {
 
 1121             W0(i) = W(i).dGetValue();
 
 1122             g(i).SetValuePreserve(0.);
 
 1123             gP(i).SetValuePreserve(0.);
 
 1125             for (
int j = 0; j < 3; ++j) {
 
 1126                 R0(i, j) = 
R(i, j).dGetValue();
 
 1131     void SetFirstDofIndex(
int iDofIndex) {
 
 1132         iFirstDofIndex = iDofIndex;
 
 1135     int iGetFirstIndex()
 const {
 
 1136         return iFirstDofIndex;
 
 1139     int iGetNumDof()
 const {
 
 1143     void GetXCurr(TinyVector<doublereal, 3>& XCurr, 
LocalDofMap*)
 const {
 
 1144         for (
int i = 0; i < 3; ++i) {
 
 1145             XCurr(i) = X(i).dGetValue();
 
 1149     template <index_type N_SIZE>
 
 1151         assert(iFirstDofIndex != -1);
 
 1152         assert(pDofMap != 0);
 
 1154         for (
int i = 0; i < 3; ++i) {
 
 1155             XCurr(i).SetValuePreserve(X(i).
dGetValue());
 
 1156             XCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + X(i).iGetStartIndexLocal(), iFirstDofIndex + X(i).iGetEndIndexLocal(), 
MapVectorBase::GLOBAL, 0.);
 
 1158             for (
index_type j = X(i).iGetStartIndexLocal(); j < X(i).iGetEndIndexLocal(); ++j) {
 
 1159                 XCurr(i).SetDerivativeGlobal(iFirstDofIndex + j, X(i).dGetDerivativeLocal(j));
 
 1164     void GetVCurr(TinyVector<doublereal, 3>& VCurr, 
LocalDofMap*)
 const {
 
 1165         for (
int i = 0; i < 3; ++i) {
 
 1166             VCurr(i) = XP(i).dGetValue();
 
 1170     template <index_type N_SIZE>
 
 1172         assert(iFirstDofIndex != -1);
 
 1173         assert(pDofMap != 0);
 
 1175         for (
int i = 0; i < 3; ++i) {
 
 1176             VCurr(i).SetValuePreserve(XP(i).
dGetValue());
 
 1177             VCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + XP(i).iGetStartIndexLocal(), iFirstDofIndex + XP(i).iGetEndIndexLocal(), 
MapVectorBase::GLOBAL, 0.);
 
 1179             for (
index_type j = XP(i).iGetStartIndexLocal(); j < XP(i).iGetEndIndexLocal(); ++j) {
 
 1180                 VCurr(i).SetDerivativeGlobal(iFirstDofIndex + j, XP(i).dGetDerivativeLocal(j));
 
 1185     void GetRCurr(TinyMatrix<doublereal, 3, 3>& RCurr, 
LocalDofMap*)
 const {
 
 1186         for (
int i = 0; i < 3; ++i) {
 
 1187             for (
int j = 0; j < 3; ++j) {
 
 1188                 RCurr(i, j) = 
R(i, j).dGetValue();
 
 1193     template <index_type N_SIZE>
 
 1195         assert(iFirstDofIndex != -1);
 
 1196         assert(pDofMap != 0);
 
 1198         for (
int i = 0; i < 3; ++i) {
 
 1199             for (
int j = 0; j < 3; ++j) {
 
 1200                 RCurr(i, j).SetValuePreserve(
R(i, j).
dGetValue());
 
 1201                 RCurr(i, j).DerivativeResizeReset(pDofMap, iFirstDofIndex + 
R(i, j).iGetStartIndexLocal() + 3, iFirstDofIndex + 
R(i, j).iGetEndIndexLocal() + 3, 
MapVectorBase::GLOBAL, 0.);
 
 1203                 for (
index_type k = 
R(i, j).iGetStartIndexLocal(); k < 
R(i, j).iGetEndIndexLocal(); ++k) {
 
 1204                         RCurr(i, j).SetDerivativeGlobal(iFirstDofIndex + k + 3, 
R(i, j).dGetDerivativeLocal(k));
 
 1210     const TinyMatrix<doublereal, 3, 3>& GetRRef()
 const {
 
 1214     void GetWCurr(TinyVector<doublereal, 3>& WCurr, 
LocalDofMap*)
 const {
 
 1215         for (
int i = 0; i < 3; ++i) {
 
 1216             WCurr(i) = W(i).dGetValue();
 
 1220     template <index_type N_SIZE>
 
 1222         assert(iFirstDofIndex != -1);
 
 1223         assert(pDofMap != 0);
 
 1225         for (
int i = 0; i < 3; ++i) {
 
 1226             WCurr(i).SetValuePreserve(W(i).
dGetValue());
 
 1227             WCurr(i).DerivativeResizeReset(pDofMap, iFirstDofIndex + W(i).iGetStartIndexLocal() + 3, iFirstDofIndex + W(i).iGetEndIndexLocal() + 3, 
MapVectorBase::GLOBAL, 0.);
 
 1229             for (
index_type j = W(i).iGetStartIndexLocal(); j < W(i).iGetEndIndexLocal(); ++j) {
 
 1230                 WCurr(i).SetDerivativeGlobal(iFirstDofIndex + j + 3, W(i).dGetDerivativeLocal(j));
 
 1235     const TinyVector<doublereal, 3>& GetWRef()
 const {
 
 1241     TinyMatrix<doublereal, 3, 3> R0;
 
 1242     TinyVector<doublereal, 3> W0;
 
 1243     static const int NADVars = 3;
 
 1244     TinyVector<Gradient<NADVars>, 3> X, XP, g, gP, W;
 
 1245     TinyMatrix<Gradient<NADVars>, 3, 3> 
R;
 
 1249 template <
typename T>
 
 1254     ResItem(
int iEquIndex_=-1, T dCoef_=T(0.))
 
 1255         :iEquIndex(iEquIndex_), dCoef(dCoef_) {
 
 1262         ResizeReset(iNumRows, iNumCols);
 
 1266         oWorkMat.resize(iNumRows, iNumCols);
 
 1267         oRowIndex.resize(iNumRows);
 
 1268         oColIndex.resize(iNumCols);
 
 1269         oWorkMat.initialize(0.);
 
 1270         oRowIndex.initialize(-1);
 
 1271         oColIndex.initialize(-1);
 
 1274     void PutRowIndex(
int iSubRow, 
int iRow) {
 
 1275         assert(iSubRow < oRowIndex.rows());
 
 1276         oRowIndex(iSubRow) = iRow;
 
 1279     void PutColIndex(
int iSubCol, 
int iCol) {
 
 1280         assert(iSubCol < oColIndex.rows());
 
 1281         oColIndex(iSubCol) = iCol;
 
 1284     void PutCoef(
int iSubRow, 
int iSubCol, 
doublereal dCoef) {
 
 1285         assert(iSubRow >= 0 && iSubRow < oWorkMat.rows());
 
 1286         assert(iSubCol >= 0 && iSubCol < oWorkMat.cols());
 
 1288         oWorkMat(iSubRow, iSubCol) = dCoef;
 
 1291     void IncCoef(
int iSubRow, 
int iSubCol, 
doublereal dCoef) {
 
 1292         assert(iSubRow >= 0 && iSubRow < oWorkMat.rows());
 
 1293         assert(iSubCol >= 0 && iSubCol < oWorkMat.cols());
 
 1295         oWorkMat(iSubRow, iSubCol) += dCoef;
 
 1298     void AddTo(Array<doublereal, 2>& JacMat)
 const {
 
 1299         for (
int i = 0; i < oWorkMat.rows(); ++i) {
 
 1300             for (
int j = 0; j < oWorkMat.cols(); ++j) {
 
 1301                 assert(oRowIndex(i) >= 0);
 
 1302                 assert(oRowIndex(i) < JacMat.rows());
 
 1303                 assert(oColIndex(j) >= 0);
 
 1304                 assert(oColIndex(j) < JacMat.cols());
 
 1305                 JacMat(oRowIndex(i), oColIndex(j)) += oWorkMat(i, j);
 
 1311     Array<doublereal, 2> oWorkMat;
 
 1312     Array<int, 1> oRowIndex, oColIndex;
 
 1322         JacItem(
int iEquIndex=-1, 
int iDofIndex=-1, 
doublereal dCoef=0.)
 
 1323             :iEquIndex(iEquIndex), iDofIndex(iDofIndex), dCoef(dCoef) {
 
 1327     typedef std::vector<JacItem> VectorType;
 
 1328     typedef VectorType::const_iterator const_iterator;
 
 1331         if (iNumItems > 0) {
 
 1332             WorkMat.reserve(iNumItems);
 
 1336     template <index_type N_SIZE, 
typename T>
 
 1338         pElem->AssRes(WorkVec, dCoef, pDofMap);
 
 1341         for (
int i = 0; i < WorkVec.rows(); ++i) {
 
 1342             const ResItem<Gradient<N_SIZE> >& resItem = WorkVec(i);
 
 1344             for (
index_type j = resItem.dCoef.iGetStartIndexLocal(); j < resItem.dCoef.iGetEndIndexLocal(); ++j) {
 
 1345                 index_type iDofIndex = resItem.dCoef.iGetGlobalDof(j);
 
 1346                 InsertItem(JacItem(resItem.iEquIndex, iDofIndex, resItem.dCoef.dGetDerivativeLocal(j)));
 
 1353     void ResizeReset(
int iNumItems) {
 
 1354         WorkMat.resize(iNumItems);
 
 1357     int iGetSize()
 const { 
return WorkMat.size(); }
 
 1359     void InsertItem(
const JacItem& item) {
 
 1360         WorkMat.push_back(item);
 
 1363     void AddTo(Array<doublereal, 2>& JacMat)
 const {
 
 1364         for (const_iterator j = begin(); j != end(); ++j) {
 
 1365             JacMat(j->iEquIndex, j->iDofIndex) += j->dCoef;
 
 1368     const_iterator begin()
 const { 
return WorkMat.begin(); }
 
 1369     const_iterator end()
 const { 
return WorkMat.end(); }
 
 1377     virtual blitz::Array<ResItem<doublereal>, 1>& AssRes(blitz::Array<ResItem<doublereal>, 1>& WorkVec, 
doublereal dCoef)=0;
 
 1382     virtual ~Element() { }
 
 1385 class Element1: 
public Element {
 
 1389     TinyVector<doublereal, 3> o1, o2;
 
 1390     TinyMatrix<doublereal, 3, 3> S, D;
 
 1391     static const int NADVars = 12;
 
 1395     Element1(
Node* node1_,
 
 1396              const TinyVector<doublereal, 3>& o1_,
 
 1398              const TinyVector<doublereal, 3>& o2_,
 
 1405          dofMap(iGetNumCols()) {
 
 1439     template <
typename T>
 
 1440     Array<ResItem<T>, 1>& AssRes(Array<ResItem<T>, 1>& WorkVec, 
doublereal dCoef, 
LocalDofMap *pDofMap) {
 
 1441         WorkVec.resize(iGetNumRows());
 
 1443         TinyVector<T, 3> X1, X2, 
V1, 
V2, W1, W2;
 
 1444         TinyMatrix<T, 3, 3> R1, R2;
 
 1446         node1->GetXCurr(X1, pDofMap);
 
 1447         node1->GetVCurr(V1, pDofMap);
 
 1448         node1->GetRCurr(R1, pDofMap);
 
 1449         node1->GetWCurr(W1, pDofMap);
 
 1450         node2->GetXCurr(X2, pDofMap);
 
 1451         node2->GetVCurr(V2, pDofMap);
 
 1452         node2->GetRCurr(R2, pDofMap);
 
 1453         node2->GetWCurr(W2, pDofMap);
 
 1455         const TinyVector<T, 3> R1o1 = product(R1, o1);
 
 1456         const TinyVector<T, 3> R2o2 = product(R2, o2);
 
 1457         const TinyMatrix<T, 3, 3> R1_T = transpose(R1);
 
 1458         const TinyVector<T, 3> dX = product(R1_T, TinyVector<T, 3>(X1 + R1o1 - X2 - R2o2));
 
 1459         const TinyVector<T, 3> dV = product(R1_T, TinyVector<T, 3>(V1 + cross(W1, R1o1) - V2 - cross(W2, R2o2)));
 
 1460         const TinyVector<T, 3> F1 = product(R1, TinyVector<T, 3>(product(-S, dX) - product(D, dV)));
 
 1461         const TinyVector<T, 3> M1 = cross(R1o1, F1), M2 = -cross(R2o2, F1);
 
 1463         for (
int i = 0; i < 6; ++i) {
 
 1464             WorkVec(i).iEquIndex = node1->iGetFirstIndex() + i;
 
 1465             WorkVec(i + 6).iEquIndex = node2->iGetFirstIndex() + i;
 
 1468         for (
int i = 0; i < 3; ++i) {
 
 1469             WorkVec(i).dCoef = F1(i);
 
 1470             WorkVec(i + 3).dCoef = M1(i);
 
 1471             WorkVec(i + 6).dCoef = -F1(i);
 
 1472             WorkVec(i + 9).dCoef = M2(i);
 
 1478     virtual Array<ResItem<doublereal>, 1>& AssRes(Array<ResItem<doublereal>, 1>& WorkVec, 
doublereal dCoef) {
 
 1479         return AssRes(WorkVec, dCoef, 0);
 
 1483         Array<ResItem<Gradient<NADVars> >, 1> WorkVec;
 
 1484         return WorkMat.AssJac(
this, &dofMap, WorkVec, dCoef);
 
 1488         WorkMat.
ResizeReset(iGetNumRows(), iGetNumCols());
 
 1490         for (
int i = 0; i < 6; ++i) {
 
 1491             WorkMat.
PutColIndex(i, node1->iGetFirstIndex() + i);
 
 1492             WorkMat.
PutColIndex(i + 6, node2->iGetFirstIndex() + i);
 
 1493             WorkMat.
PutRowIndex(i, node1->iGetFirstIndex() + i);
 
 1494             WorkMat.
PutRowIndex(i + 6, node2->iGetFirstIndex() + i);
 
 1497         const TinyVector<doublereal, 3>& W1_0 = node1->GetWRef();
 
 1498         const TinyVector<doublereal, 3>& W2_0 = node2->GetWRef();
 
 1499         const TinyMatrix<doublereal, 3, 3>& R1_0 = node1->GetRRef();
 
 1500         const TinyMatrix<doublereal, 3, 3>& R2_0 = node2->GetRRef();
 
 1502         TinyVector<doublereal, 3> X1, X2, 
V1, 
V2, W1, W2;
 
 1503         TinyMatrix<doublereal, 3, 3> R1, R2;
 
 1505         node1->GetXCurr(X1, 0);
 
 1506         node1->GetVCurr(V1, 0);
 
 1507         node1->GetRCurr(R1, 0);
 
 1508         node1->GetWCurr(W1, 0);
 
 1510         node2->GetXCurr(X2, 0);
 
 1511         node2->GetVCurr(V2, 0);
 
 1512         node2->GetRCurr(R2, 0);
 
 1513         node2->GetWCurr(W2, 0);
 
 1515         const TinyMatrix<doublereal, 3, 3> skew_W2_0 = skew(W2_0);
 
 1516         const TinyVector<doublereal, 3> R1o1 = product(R1, o1);
 
 1517         const TinyMatrix<doublereal, 3, 3> skew_R1o1 = skew(R1o1);
 
 1518         const TinyVector<doublereal, 3> R1_0o1 = product(R1_0, o1);
 
 1519         const TinyMatrix<doublereal, 3, 3> skew_R1_0o1 = skew(R1_0o1);
 
 1520         const TinyVector<doublereal, 3> R2o2 = product(R2, o2);
 
 1521         const TinyMatrix<doublereal, 3, 3> skew_R2o2 = skew(R2o2);
 
 1522         const TinyVector<doublereal, 3> R2_0o2 = product(R2_0, o2);
 
 1523         const TinyMatrix<doublereal, 3, 3> skew_R2_0o2 = skew(R2_0o2);
 
 1524         const TinyMatrix<doublereal, 3, 3> R1_T = transpose(R1);
 
 1525         const TinyVector<doublereal, 3> dX = product(R1_T, TinyVector<doublereal, 3>(X1 + R1o1 - X2 - R2o2));
 
 1526         const TinyVector<doublereal, 3> dV = product(R1_T, TinyVector<doublereal, 3>(V1 + cross(W1, R1o1) - V2 - cross(W2, R2o2)));
 
 1527         const TinyVector<doublereal, 3> F1_R1 = -(product(S, dX) + product(D, dV));
 
 1528         const TinyVector<doublereal, 3> F1 = product(R1, F1_R1);
 
 1529         const TinyVector<doublereal, 3> F2 = -F1;
 
 1530         const TinyVector<doublereal, 3> M1 = cross(R1o1, F1), M2 = -cross(R2o2, F1);
 
 1531         const TinyMatrix<doublereal, 3, 3> R1_0_T = transpose(R1_0);
 
 1532         const TinyMatrix<doublereal, 3, 3> dF1_dX1 = product(R1, product(-S, R1_T));
 
 1534         const TinyMatrix<doublereal, 3, 3> ddX_dg1 = product(R1_0_T, skew(TinyVector<doublereal, 3>(X1 + R1o1 - X2 - R2o2))) - product(R1_T, skew_R1_0o1);
 
 1535         const TinyMatrix<doublereal, 3, 3> ddV_dg1 = product(R1_0_T, skew(TinyVector<doublereal, 3>(V1 + cross(W1, R1o1) - V2 - cross(W2, R2o2))))
 
 1536                                                + product(R1_T, TinyMatrix<doublereal, 3, 3>(product(skew(R1o1), skew(W1_0)) - product(skew(W1), skew_R1_0o1)));
 
 1537         const TinyMatrix<doublereal, 3, 3> dF1_dg1 = skew(TinyVector<doublereal, 3>(product(R1_0, TinyVector<doublereal, 3>(-F1_R1))))
 
 1538                                                - TinyMatrix<doublereal, 3, 3>(product(R1, TinyMatrix<doublereal, 3, 3>(product(S, ddX_dg1) + product(D, ddV_dg1))));
 
 1540         const TinyMatrix<doublereal, 3, 3> dF1_dX2 = product(R1, product(S, R1_T));
 
 1541         const TinyMatrix<doublereal, 3, 3> ddX_dg2 = product(R1_T, skew_R2_0o2);
 
 1542         const TinyMatrix<doublereal, 3, 3> ddV_dg2 = product(R1_T, TinyMatrix<doublereal, 3, 3>(product(skew_R2o2, -skew_W2_0)) + TinyMatrix<doublereal, 3, 3>(product(skew_W2_0, skew_R2_0o2)));
 
 1543         const TinyMatrix<doublereal, 3, 3> dF1_dg2 = -product(R1, product(S, ddX_dg2) + product(D, ddV_dg2));
 
 1545         const TinyMatrix<doublereal, 3, 3> dF2_dX1 = -dF1_dX1;
 
 1546         const TinyMatrix<doublereal, 3, 3> dF2_dg1 = -dF1_dg1;
 
 1547         const TinyMatrix<doublereal, 3, 3> dF2_dX2 = -dF1_dX2;
 
 1548         const TinyMatrix<doublereal, 3, 3> dF2_dg2 = -dF1_dg2;
 
 1550         const TinyMatrix<doublereal, 3, 3> dM1_dX1 = product(skew_R1o1, dF1_dX1);
 
 1551         const TinyMatrix<doublereal, 3, 3> dM1_dg1 = product(skew(F1), skew_R1_0o1) + product(skew_R1o1, dF1_dg1);
 
 1552         const TinyMatrix<doublereal, 3, 3> dM1_dX2 = product(skew_R1o1, dF1_dX2);
 
 1553         const TinyMatrix<doublereal, 3, 3> dM1_dg2 = product(skew_R1o1, dF1_dg2);
 
 1555         const TinyMatrix<doublereal, 3, 3> dM2_dX1 = product(skew_R2o2, dF2_dX1);
 
 1556         const TinyMatrix<doublereal, 3, 3> dM2_dg1 = product(skew_R2o2, dF2_dg1);
 
 1557         const TinyMatrix<doublereal, 3, 3> dM2_dX2 = product(skew_R2o2, dF2_dX2);
 
 1558         const TinyMatrix<doublereal, 3, 3> dM2_dg2 = product(skew(F2), skew_R2_0o2) + product(skew_R2o2, dF2_dg2);
 
 1560         const TinyMatrix<doublereal, 3, 3> dF1_dV1 = product(R1, product(-D, R1_T));
 
 1561         const TinyMatrix<doublereal, 3, 3> ddV_dgP1 = product(-R1_T, skew_R1o1);
 
 1562         const TinyMatrix<doublereal, 3, 3> dF1_dgP1 = product(R1, product(-D, ddV_dgP1));
 
 1563         const TinyMatrix<doublereal, 3, 3> dF1_dV2 = product(R1, product(D, R1_T));
 
 1564         const TinyMatrix<doublereal, 3, 3> ddV_dgP2 = product(R1_T, skew_R2o2);
 
 1565         const TinyMatrix<doublereal, 3, 3> dF1_dgP2 = product(R1, product(-D, ddV_dgP2));
 
 1567         const TinyMatrix<doublereal, 3, 3> dM1_dV1 = product(skew_R1o1, dF1_dV1);
 
 1568         const TinyMatrix<doublereal, 3, 3> dM1_dgP1 = product(skew_R1o1, dF1_dgP1);
 
 1569         const TinyMatrix<doublereal, 3, 3> dM1_dV2 = product(skew_R1o1, dF1_dV2);
 
 1570         const TinyMatrix<doublereal, 3, 3> dM1_dgP2 = product(skew_R1o1, dF1_dgP2);
 
 1572         const TinyMatrix<doublereal, 3, 3> dF2_dV1 = -dF1_dV1;
 
 1573         const TinyMatrix<doublereal, 3, 3> dF2_dgP1 = -dF1_dgP1;
 
 1574         const TinyMatrix<doublereal, 3, 3> dF2_dV2 = -dF1_dV2;
 
 1575         const TinyMatrix<doublereal, 3, 3> dF2_dgP2 = -dF1_dgP2;
 
 1577         const TinyMatrix<doublereal, 3, 3> dM2_dV1 = product(skew_R2o2, dF2_dV1);
 
 1578         const TinyMatrix<doublereal, 3, 3> dM2_dgP1 = product(skew_R2o2, dF2_dgP1);
 
 1579         const TinyMatrix<doublereal, 3, 3> dM2_dV2 = product(skew_R2o2, dF2_dV2);
 
 1580         const TinyMatrix<doublereal, 3, 3> dM2_dgP2 = product(skew_R2o2, dF2_dgP2);
 
 1582         for (
int i = 0; i < 3; ++i) {
 
 1583             for (
int j = 0; j < 3; ++j) {
 
 1584                 WorkMat.
PutCoef(i, j,     -dF1_dV1(i, j)  - dCoef * dF1_dX1(i, j));
 
 1585                 WorkMat.
PutCoef(i, j + 3, -dF1_dgP1(i, j) - dCoef * dF1_dg1(i, j));
 
 1586                 WorkMat.
PutCoef(i, j + 6, -dF1_dV2(i, j)  - dCoef * dF1_dX2(i, j));
 
 1587                 WorkMat.
PutCoef(i, j + 9, -dF1_dgP2(i, j) - dCoef * dF1_dg2(i, j));
 
 1589                 WorkMat.
PutCoef(i + 3, j,     -dM1_dV1(i, j)  - dCoef * dM1_dX1(i, j));
 
 1590                 WorkMat.
PutCoef(i + 3, j + 3, -dM1_dgP1(i, j) - dCoef * dM1_dg1(i, j));
 
 1591                 WorkMat.
PutCoef(i + 3, j + 6, -dM1_dV2(i, j)  - dCoef * dM1_dX2(i, j));
 
 1592                 WorkMat.
PutCoef(i + 3, j + 9, -dM1_dgP2(i, j) - dCoef * dM1_dg2(i, j));
 
 1594                 WorkMat.
PutCoef(i + 6, j,     -dF2_dV1(i, j)  - dCoef * dF2_dX1(i, j));
 
 1595                 WorkMat.
PutCoef(i + 6, j + 3, -dF2_dgP1(i, j) - dCoef * dF2_dg1(i, j));
 
 1596                 WorkMat.
PutCoef(i + 6, j + 6, -dF2_dV2(i, j)  - dCoef * dF2_dX2(i, j));
 
 1597                 WorkMat.
PutCoef(i + 6, j + 9, -dF2_dgP2(i, j)  - dCoef * dF2_dg2(i, j));
 
 1599                 WorkMat.
PutCoef(i + 9, j,     -dM2_dV1(i, j)  - dCoef * dM2_dX1(i, j));
 
 1600                 WorkMat.
PutCoef(i + 9, j + 3, -dM2_dgP1(i, j) - dCoef * dM2_dg1(i, j));
 
 1601                 WorkMat.
PutCoef(i + 9, j + 6, -dM2_dV2(i, j)  - dCoef * dM2_dX2(i, j));
 
 1602                 WorkMat.
PutCoef(i + 9, j + 9, -dM2_dgP2(i, j) - dCoef * dM2_dg2(i, j));
 
 1608     index_type iGetNumRows()
 const { 
return 12; }
 
 1609     index_type iGetNumCols()
 const { 
return NADVars; }
 
 1612 TinyMatrix<doublereal, 3, 3> 
Euler123ToMatR(
const TinyVector<doublereal, 3>& v) {
 
 1623     TinyMatrix<doublereal, 3, 3> 
R;
 
 1626     R(0, 0) = dCosBeta*dCosGamma;
 
 1627     R(1, 0) = dCosAlpha*dSinGamma + dSinAlpha*dSinBeta*dCosGamma;
 
 1628         R(2, 0) = dSinAlpha*dSinGamma - dCosAlpha*dSinBeta*dCosGamma;
 
 1629     R(0, 1) = -dCosBeta*dSinGamma;
 
 1630     R(1, 1) = dCosAlpha*dCosGamma - dSinAlpha*dSinBeta*dSinGamma;
 
 1631     R(2, 1) = dSinAlpha*dCosGamma + dCosAlpha*dSinBeta*dSinGamma;
 
 1633     R(1, 2) = -dSinAlpha*dCosBeta;
 
 1634     R(2, 2) = dCosAlpha*dCosBeta;
 
 1639 void testAssembly() {
 
 1647     for (
int loop = 0; loop < 
NLoopsAss; ++loop) {
 
 1648         const int iNumNodes = 3;
 
 1652         for (
int i = 0; i < iNumNodes; ++i) {
 
 1653             TinyVector<doublereal, 3> X0, XP0, Phi0, W0;
 
 1655             for (
int j = 0; j < 3; ++j) {
 
 1656                 X0(j) = ((i + 1) * 10 + j + 1);
 
 1657                 XP0(j) = ((i + 1) * 1000 + (j + 1) * 100);
 
 1658                 Phi0(j) = ((i + 1) * 0.1 + (j + 1) * 0.01);
 
 1659                 W0(j) = ((i + 1) * 0.1 + (j + 1) * 0.01);
 
 1664             nodes[i] = 
new Node(X0, XP0, R0, W0);
 
 1669         for (
int i = 0; i < iNumNodes; ++i) {
 
 1670             nodes[i]->SetFirstDofIndex(iNumDof);
 
 1674         const int iNumElem = iNumNodes - 1;
 
 1676         Element* elements[iNumElem] = {0};
 
 1678         for (
int i = 0; i < iNumElem; ++i) {
 
 1679             TinyVector<doublereal, 3> o1, o2;
 
 1690             elements[i] = 
new Element1(nodes[i], o1, nodes[i + 1], o2, s, d);
 
 1693         Array<doublereal, 1> XCurr(iNumDof), XPrimeCurr(iNumDof);
 
 1695         XCurr.initialize(0.);
 
 1696         XPrimeCurr.initialize(0.);
 
 1698         for (
int i = 0; i < iNumNodes; ++i) {
 
 1699             nodes[i]->
SetValue(XCurr, XPrimeCurr);
 
 1702         Array<ResItem<doublereal>, 1> WorkVec;
 
 1705         Array<doublereal, 1> ResVec;
 
 1706         Array<doublereal, 2> JacMatAD, JacMat;
 
 1708         ResVec.resize(iNumDof);
 
 1709         JacMatAD.resize(iNumDof, iNumDof);
 
 1710         JacMat.resize(iNumDof, iNumDof);
 
 1712         ResVec.initialize(0.);
 
 1713         JacMatAD.initialize(0.);
 
 1714         JacMat.initialize(0.);
 
 1718         for (
int iTime = 0; iTime < 100; ++iTime) {
 
 1721             for (
int i = 0; i < iNumNodes; ++i) {
 
 1722                 nodes[i]->
Update(XCurr, XPrimeCurr, dCoef);
 
 1725             for (
int i = 0; i < iNumElem; ++i) {
 
 1728                 elements[i]->AssRes(WorkVec, dCoef);
 
 1732                 for (
int j = 0; j < WorkVec.rows(); ++j) {
 
 1733                     ResVec(WorkVec(j).iEquIndex) += WorkVec(j).dCoef;
 
 1738                 elements[i]->AssJac(WorkMatSp, dCoef);
 
 1742                 WorkMatSp.
AddTo(JacMatAD);
 
 1746                 elements[i]->AssJac(WorkMatFull, dCoef);
 
 1750                 WorkMatFull.
AddTo(JacMat);
 
 1753             for (
int i = 0; i < JacMat.rows(); ++i) {
 
 1754                 for (
int j = 0; j < JacMat.cols(); ++j) {
 
 1755                     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))));
 
 1756                     if(std::abs(JacMat(i, j) - JacMatAD(i, j)) > dTol) {
 
 1757                         throw std::runtime_error(
"testAssembly(): incorrect result");
 
 1764             std::cout << 
"ResVec = [" << std::endl;
 
 1766             for (
int i = 0; i < iNumDof; ++i) {
 
 1767                 std::cout << setw(10) << ResVec(i) << std::endl;
 
 1770             std::cout << 
"]" << std::endl;
 
 1772             std::cout << 
"JacMatAD = [" << std::endl;
 
 1774             for (
int i = 0; i < iNumDof; ++i) {
 
 1775                 for (
int j = 0; j < iNumDof; ++j) {
 
 1776                     std::cout << setw(10) << std::setprecision(16) << JacMatAD(i, j) << 
" ";
 
 1778                 std::cout << std::endl;
 
 1781             std::cout << 
"]" << std::endl;
 
 1783             std::cout << 
"JacMat = [" << std::endl;
 
 1785             for (
int i = 0; i < iNumDof; ++i) {
 
 1786                 for (
int j = 0; j < iNumDof; ++j) {
 
 1787                     std::cout << setw(10) << std::setprecision(16) << JacMat(i, j) << 
" ";
 
 1789                 std::cout << std::endl;
 
 1792             std::cout << 
"]" << std::endl;
 
 1795         for (
int i = 0; i < iNumElem; ++i) {
 
 1799         for (
int i = 0; i < iNumNodes; ++i) {
 
 1806     std::cerr << 
"number of iterations:" << iIterCnt << std::endl;
 
 1807     std::cerr << 
"AssRes:" << tckRes / iIterCnt << std::endl;
 
 1808     std::cerr << 
"AssJacAD:" << tckJacAD / iIterCnt << std::endl;
 
 1809     std::cerr << 
"AssJac:" << tckJac / iIterCnt << std::endl;
 
 1810     std::cerr << 
"overhead:" << tckJacAD / tckJac << std::endl;
 
 1811     std::cerr << 
"total time:" << (tckEnd - tckStart) / iIterCnt << std::endl;
 
 1818 #ifdef HAVE_FEENABLEEXCEPT 
 1819         feenableexcept(FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW);
 
 1827                 NLoopsAss = atol(argv[2]);
 
 1830     testRangeVector<0>();
 
 1831     testRangeVector<6>();
 
 1833     testMapVector<19>();
 
 1844     testGradient<10>(10);
 
 1845     testGradient<12>(12);
 
 1857     testGradientLin<4>(4);
 
 1858     testGradientLin<0>(4);
 
 1861     gradAssTest::testAssembly();
 
 1864     testDifferentDofMaps<3>(1);
 
 1865     testDifferentDofMaps<4>(2);
 
 1866     testDifferentDofMaps<5>(3);
 
 1867     testDifferentDofMaps<6>(4);
 
 1868     testDifferentDofMaps<7>(5);
 
 1869     testDifferentDofMaps<8>(6);
 
 1871     for (
integer i = 1; i <= 10; ++i) {
 
 1872         testDifferentDofMaps<0>(i);
 
 1873         testDifferentDofMaps<12>(i);
 
 1874         testDifferentDofMaps<14>(i);
 
 1875         testDifferentDofMaps<16>(i);
 
 1879     std::cerr << 
"All tests passed" << std::endl;
 
 1881     std::cerr << 
"No tests have been done" << std::endl;
 
void SetVectorValue(index_type i, const vector_type &d)
scalar_deriv_type dGetDerivativeGlobal(index_type iGlobalDof) const 
bool bIsEqual(const Gradient &g) const 
scalar_func_type dGetValue(scalar_func_type d)
void PutColIndex(integer iSubCol, integer iCol)
index_type iGetStartIndexLocal() const 
GradientExpression< BinaryExpr< FuncPow, LhsExpr, RhsExpr > > pow(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
void Reserve(index_type iMaxSizeNew)
GradientExpression< UnaryExpr< FuncSin, Expr > > sin(const GradientExpression< Expr > &u)
doublereal sec(doublereal x)
index_type iGetEndIndexLocal() const 
double mbdyn_clock_time()
int main(int argc, char *argv[])
MatrixHandler & AddTo(MatrixHandler &MH) const 
void PutCoef(integer iRow, integer iCol, const doublereal &dCoef)
index_type iGetStartIndexLocal() const 
MatrixHandler & AddTo(MatrixHandler &MH) const 
void testGradientLin(int N)
GradientExpression< UnaryExpr< FuncFabs, Expr > > fabs(const GradientExpression< Expr > &u)
void ResizeReset(index_type iStartNew, index_type iEndNew, const T &dVal)
index_type iGetStartIndexVector() const 
void func(const T &u, const T &v, const T &w, doublereal e, T &f)
void SetDerivativeGlobal(index_type iGlobalDof, scalar_deriv_type dCoef)
index_type iGetEndIndexVector() const 
void testDifferentDofMaps(index_type N)
void testGradientCopy(int N)
GradientExpression< BinaryExpr< FuncMinus, LhsExpr, RhsExpr > > operator-(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
GradientExpression< UnaryExpr< FuncLog, Expr > > log(const GradientExpression< Expr > &u)
static const char * dof[]
void func_tmp(const T &u, const T &v, const T &w, doublereal e, T &f)
void funcDeriv(index_type N, doublereal u, const doublereal ud[], doublereal v, const doublereal vd[], doublereal w, const doublereal wd[], doublereal e, doublereal &f, doublereal fd[])
void SetValuePreserve(scalar_func_type dVal)
index_type iGetGlobalDof(index_type iLocal) const 
bool bCompare(const T &a, const T &b, doublereal dTolRel=0.)
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)
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
void DerivativeResizeReset(LocalDofMap *pMap, index_type iStartGlobal, index_type iEndGlobal, MapVectorBase::GlobalScope s, scalar_deriv_type dVal)
virtual void ResizeReset(integer, integer)
GradientExpression< BinaryExpr< FuncPlus, LhsExpr, RhsExpr > > operator+(const GradientExpression< LhsExpr > &u, const GradientExpression< RhsExpr > &v)
virtual unsigned int iGetNumDof(void) const =0
GradientExpression< DirectExpr< Gradient< N_SIZE >, true > > Alias(const Gradient< N_SIZE > &g)
index_type iGetStartIndex() const 
Matrix< T, 3, 3 > & Euler123ToMatR(const Vector< T, 3 > &v, Matrix< T, 3, 3 > &R)
vector_type GetVectorValue(index_type i) const 
void PutRowIndex(integer iSubRow, integer iRow)
scalar_type dGetLocalVector(index_type i) const 
GradientExpression< UnaryExpr< FuncCos, Expr > > cos(const GradientExpression< Expr > &u)
scalar_func_type dGetValue() const 
static const doublereal a
scalar_type GetValue(index_type i) const 
void testGradient(index_type N)
scalar_deriv_type dGetDerivativeLocal(index_type iLocalDof) const 
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *h=0)
index_type iGetEndIndexLocal() const 
GradientExpression< UnaryExpr< FuncTan, Expr > > tan(const GradientExpression< Expr > &u)
#define GRADIENT_ASSERT(expr)
void SetValue(index_type i, const scalar_type &d)
index_type iGetEndIndex() const 
void ResizePreserve(index_type iStartNew, index_type iEndNew)
virtual void Update(const VectorHandler &XCurr, const VectorHandler &XPrimeCurr)