integrator_bdf.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
39 
40 using namespace std;
41 
43 
44 
45 //
46 // PUBLIC MEMBER FUNCTIONS:
47 //
48 
50  :Integrator( ){
51 
53 }
54 
55 
57  :Integrator( ){
58 
59  init( rhs_ );
60 }
61 
63  :Integrator( arg ){
64 
65  constructAll( arg );
66 }
67 
68 
69 
71 
72  deleteAll();
73 }
74 
75 
77 
78  if ( this != &arg ){
79  deleteAll();
80  Integrator::operator=( arg );
81  constructAll(arg);
82  }
83  return *this;
84 }
85 
86 
87 
89 
90  // RHS:
91  // ---------
92  rhs = new DifferentialEquation( rhs_ );
93  m = rhs->getDim ();
94  ma = rhs->getNXA ();
95  mdx = rhs->getNDX ();
96  mn = rhs->getN ();
97  mu = rhs->getNU ();
98  mui = rhs->getNUI ();
99  mp = rhs->getNP ();
100  mpi = rhs->getNPI ();
101  mw = rhs->getNW ();
103 
104  rhs->makeImplicit();
105  allocateMemory();
106 
107  return SUCCESSFUL_RETURN;
108 }
109 
110 
112 
113 
114  int run1, run2, run3;
115 
116  if( m < 1 ){
118  return;
119  }
120 
121  if( mdx > m-ma ){
123  return;
124  }
125 
126  if( m < md+ma ) md = m - ma;
127 
128 
130 
131  initialAlgebraicResiduum = new double[m];
132  relaxationConstant = 5.0;
133 
134 
135  // BUTCHER TABLEAU:
136  // ----------------
137  dim = 7;
138  A = new double*[dim];
139  b4 = new double [dim];
140  b5 = new double [dim];
141  c = new double [dim];
142 
143  for( run1 = 0; run1 < dim; run1++ ){
144  A[run1] = new double[dim];
145  }
146 
148 
149  // RK-STARTER:
150  // -----------
151  ndir = rhs->getNumberOfVariables() + 1 + 2*md;
152 
153  eta4 = new double [m];
154  eta5 = new double [m];
155  eta4_ = new double [m];
156  eta5_ = new double [m];
157 
158  for( run1 = 0; run1 < m; run1++ ){
159 
160  eta4 [run1] = 0.0;
161  eta5 [run1] = 0.0;
162  eta4_[run1] = 0.0;
163  eta5_[run1] = 0.0;
164  }
165 
166 
167  k = new double**[4];
168  k2 = new double**[4];
169  l = new double**[4];
170  l2 = new double**[4];
171 
172  for( run3 = 0; run3 < 4; run3++ ){
173 
174  k [run3] = new double*[dim];
175  k2 [run3] = new double*[dim];
176  l [run3] = new double*[dim];
177  l2 [run3] = new double*[dim];
178 
179  for( run1 = 0; run1 < dim; run1++ ){
180  k [run3][run1] = new double[m];
181  k2 [run3][run1] = new double[m];
182 
183  for( run2 = 0; run2 < m; run2++ ){
184  k [run3][run1][run2] = 0.0;
185  k2[run3][run1][run2] = 0.0;
186  }
187 
188  l [run3][run1] = new double[ndir];
189  l2 [run3][run1] = new double[ndir];
190 
191  for( run2 = 0; run2 < ndir; run2++ ){
192  l [run3][run1][run2] = 0.0;
193  l2[run3][run1][run2] = 0.0;
194  }
195  }
196  }
197 
198 
199  iseed = new double[ndir];
200 
201  x = new double [ndir];
202 
203  for( run1 = 0; run1 < ndir; run1++ ){
204  x[run1] = 0.0;
205  iseed[run1] = 0.0;
206  }
207 
208  t = 0.0;
209 
210 
211  // BDF-METHOD:
212  // -----------
213  nstep = 5;
214  psi = (double**)calloc(1,sizeof(double*));
215  psi_ = new double[nstep];
216  gamma = (double**)calloc(1,sizeof(double*));
217 
218  c2.init(m);
219  c2.setZero();
220 
221  nOfNewtonSteps = (int*)calloc(1,sizeof(int));
222  eta = new double*[4];
223  eta2 = new double*[4];
224 
225  nOfNewtonSteps[0] = 0;
226 
227  psi [0] = (double*)calloc(nstep,sizeof(double));
228  gamma [0] = (double*)calloc(nstep,sizeof(double));
229 
230  nablaY.init( nstep, m );
231  nablaY.setZero();
232 
233  nablaY_.init( nstep, m );
234  nablaY_.setZero();
235 
236  phi.init( nstep, m );
237  phi.setZero();
238 
239  delta.init( nstep, m );
240  delta.setZero();
241 
242 
243  for( run1 = 0; run1 < nstep; run1++ ){
244  psi_ [run1] = 0.0;
245  psi [0][run1] = 0.0;
246  gamma[0][run1] = 0.0;
247  }
248 
249  maxNM = 1;
250  M = (DMatrix**)calloc(maxNM,sizeof(DMatrix*));
251 // qr.resize( maxNM );
252  M_index = (int*)calloc(maxNM,sizeof(int));
253 
254  M_index[0] = 0;
255  M [0] = 0;
256  nOfM = 0;
257 
258 
259  for( run1 = 0; run1 < 4; run1++ ){
260  eta [run1] = new double[m];
261  eta2[run1] = new double[m];
262  for( run2 = 0; run2 < m; run2++ ){
263  eta [run1][run2] = 0.0;
264  eta2[run1][run2] = 0.0;
265  }
266  }
267 
268 
269  F = new double[m];
270  F2 = new double[m];
271 
272 
273  // INTERNAL INDEX LISTS:
274  // ---------------------
275  diff_index = new int[m];
276 
277  for( run1 = 0; run1 < md; run1++ ){
278  diff_index[run1] = rhs->getStateEnumerationIndex( run1 );
279  if( diff_index[run1] == rhs->getNumberOfVariables() ){
280  diff_index[run1] = diff_index[run1] + 1 + run1;
281  }
282  }
283 
284  ddiff_index = new int[md];
285 
286  for( run1 = 0; run1 < md; run1++ ){
287  ddiff_index[run1] = rhs->index( VT_DDIFFERENTIAL_STATE, run1 );
288  if( ddiff_index[run1] == rhs->getNumberOfVariables() ){
289  ddiff_index[run1] = ddiff_index[run1] + 1 + md + run1;
290  }
291  }
292 
293 
294  alg_index = new int[ma];
295 
296  for( run1 = 0; run1 < ma; run1++ ){
297  alg_index [run1] = rhs->index( VT_ALGEBRAIC_STATE, run1 );
298  diff_index[md+run1] = alg_index [run1];
299  }
300 
301  control_index = new int[mu ];
302 
303  for( run1 = 0; run1 < mu; run1++ ){
304  control_index[run1] = rhs->index( VT_CONTROL, run1 );
305  }
306 
307  parameter_index = new int[mp ];
308 
309  for( run1 = 0; run1 < mp; run1++ ){
310  parameter_index[run1] = rhs->index( VT_PARAMETER, run1 );
311  }
312 
313  int_control_index = new int[mui];
314 
315  for( run1 = 0; run1 < mui; run1++ ){
316  int_control_index[run1] = rhs->index( VT_INTEGER_CONTROL, run1 );
317  }
318 
319  int_parameter_index = new int[mpi];
320 
321  for( run1 = 0; run1 < mpi; run1++ ){
323  }
324 
325  disturbance_index = new int[mw ];
326 
327  for( run1 = 0; run1 < mw; run1++ ){
328  disturbance_index[run1] = rhs->index( VT_DISTURBANCE, run1 );
329  }
330 
331  time_index = rhs->index( VT_TIME, 0 );
332 
333 
334  // OTHERS:
335  // -------
336  diff_scale.init(m);
337 
338  for( run1 = 0; run1 < md; run1++ ){
339  diff_scale(run1) = rhs->scale( VT_DIFFERENTIAL_STATE, run1 );
340  }
341  for( run1 = 0; run1 < ma; run1++ ){
342  diff_scale(md+run1) = rhs->scale( VT_ALGEBRAIC_STATE, run1 );
343  }
344 
345  initial_guess = new double[m];
346 
347  for( run1 = 0; run1 < m; run1++ ){
348 
349  initial_guess[run1] = 0.0;
350  }
351 
352 
353  // SENSITIVITIES:
354  // --------------
355  c2G .init(md);
356  c2G2.init(md);
357  c2G3.init(md);
358 
359  phiG.init(nstep,m);
360  phiG2.init(nstep,m);
361  phiG3.init(nstep,m);
362 
363  deltaG.init(nstep,m);
364  deltaG2.init(nstep,m);
365  deltaG3.init(nstep,m);
366 
367  kH = new double**[dim];
368  for( run1 = 0; run1 < dim; run1++ ){
369  kH[run1] = new double*[nstep];
370  for( run2 = 0; run2 < nstep; run2++ ){
371  kH[run1][run2] = new double[ndir];
372  }
373  }
374  zH = new double*[6];
375  for( run1 = 0; run1 < 6; run1++ ){
376  zH[run1] = new double[ndir];
377  }
378 
379  kH2 = new double**[dim];
380  for( run1 = 0; run1 < dim; run1++ ){
381  kH2[run1] = new double*[nstep];
382  for( run2 = 0; run2 < nstep; run2++ ){
383  kH2[run1][run2] = new double[ndir];
384  }
385  }
386  zH2 = new double*[6];
387  for( run1 = 0; run1 < 6; run1++ ){
388  zH2[run1] = new double[ndir];
389  }
390  kH3 = new double**[dim];
391  for( run1 = 0; run1 < dim; run1++ ){
392  kH3[run1] = new double*[nstep];
393  for( run2 = 0; run2 < nstep; run2++ ){
394  kH3[run1][run2] = new double[ndir];
395  }
396  }
397  zH3 = new double*[6];
398  for( run1 = 0; run1 < 6; run1++ ){
399  zH3[run1] = new double[ndir];
400  }
401 
402 
403  // STORAGE:
404  // --------
405  maxAlloc = 1;
406 }
407 
408 
410 
412  relaxationConstant = 0.0;
413 
414  dim = 0; A = 0; b4 = 0; b5 = 0; c = 0;
415 
416  eta4 = 0; eta5 = 0;
417  eta4_ = 0; eta5_ = 0;
418 
419  k = 0; k2 = 0; l = 0; l2 = 0;
420 
421  iseed = 0; nstep = 0; psi = 0;
422  psi_ = 0; gamma = 0; eta = 0;
423  eta2 = 0; x = 0; t = 0;
424 
425  nOfNewtonSteps = 0;
426  maxNM = 0; M = 0; M_index = 0; nOfM = 0;
427 
428  F = 0; F2 = 0;
429 
430  initial_guess = 0;
431 
432  ndir = 0; G = 0; etaG = 0;
433 
434  G2 = 0; G3 = 0; etaG2 = 0; etaG3 = 0;
435  H = 0; etaH = 0; kH = 0; zH = 0;
436  H2 = 0; H3 = 0; etaH2 = 0; etaH3 = 0;
437  kH2 = 0; zH2 = 0; kH3 = 0; zH3 = 0;
438 
439  maxAlloc = 0;
440 
441  nFcnEvaluations = 0;
442  nJacEvaluations = 0;
443 }
444 
445 
447 
448  int run1, run2, run3;
449 
450  rhs = new DifferentialEquation( *arg.rhs );
451 
452  m = arg.m;
453  ma = arg.ma;
454  mdx = arg.mdx;
455  mn = arg.mn;
456  mu = arg.mu;
457  mui = arg.mui;
458  mp = arg.mp;
459  mpi = arg.mpi;
460  mw = arg.mw;
461  md = m-ma;
462 
463  initialAlgebraicResiduum = new double[m] ;
465 
466 
467  // BUTCHER TABLEAU:
468  // ----------------
469  dim = arg.dim;
470  A = new double*[dim];
471  b4 = new double [dim];
472  b5 = new double [dim];
473  c = new double [dim];
474 
475  for( run1 = 0; run1 < dim; run1++ ){
476  A[run1] = new double[dim];
477  }
478 
480 
481 
482  // RK-STARTER:
483  // -----------
484  ndir = rhs->getNumberOfVariables() + 1 + 2*md;
485 
486  eta4 = new double [m];
487  eta5 = new double [m];
488  eta4_ = new double [m];
489  eta5_ = new double [m];
490 
491  for( run1 = 0; run1 < m; run1++ ){
492 
493  eta4 [run1] = 0.0;
494  eta5 [run1] = 0.0;
495  eta4_[run1] = 0.0;
496  eta5_[run1] = 0.0;
497  }
498 
499 
500  k = new double**[4];
501  k2 = new double**[4];
502  l = new double**[4];
503  l2 = new double**[4];
504 
505  for( run3 = 0; run3 < 4; run3++ ){
506 
507  k [run3] = new double*[dim];
508  k2 [run3] = new double*[dim];
509  l [run3] = new double*[dim];
510  l2 [run3] = new double*[dim];
511 
512  for( run1 = 0; run1 < dim; run1++ ){
513  k [run3][run1] = new double[m];
514  k2 [run3][run1] = new double[m];
515 
516  for( run2 = 0; run2 < m; run2++ ){
517  k [run3][run1][run2] = 0.0;
518  k2[run3][run1][run2] = 0.0;
519  }
520 
521  l [run3][run1] = new double[ndir];
522  l2 [run3][run1] = new double[ndir];
523 
524  for( run2 = 0; run2 < ndir; run2++ ){
525  l [run3][run1][run2] = 0.0;
526  l2[run3][run1][run2] = 0.0;
527  }
528  }
529  }
530 
531  iseed = new double[ndir];
532 
533  x = new double [ndir];
534 
535  for( run1 = 0; run1 < ndir; run1++ ){
536  x[run1] = 0.0;
537  iseed[run1] = 0.0;
538  }
539 
540  t = 0.0;
541 
542 
543  // BDF-METHOD:
544  // -----------
545  nstep = 5;
546  psi = (double**)calloc(1,sizeof(double*));
547  psi_ = new double[nstep];
548  gamma = (double**)calloc(1,sizeof(double*));
549 
550  c2.init(m);
551  c2.setZero();
552 
553  nOfNewtonSteps = (int*)calloc(1,sizeof(int));
554  eta = new double*[4];
555  eta2 = new double*[4];
556 
557  nOfNewtonSteps[0] = 0;
558 
559  psi [0] = (double*)calloc(nstep,sizeof(double));
560  gamma [0] = (double*)calloc(nstep,sizeof(double));
561 
562  nablaY.init( nstep, m );
563  nablaY.setZero();
564 
565  nablaY_.init( nstep, m );
566  nablaY_.setZero();
567 
568  phi.init( nstep, m );
569  phi.setZero();
570 
571  delta.init( nstep, m );
572  delta.setZero();
573 
574  for( run1 = 0; run1 < nstep; run1++ ){
575 
576  psi_ [run1] = 0.0;
577  psi [0][run1] = 0.0;
578  gamma[0][run1] = 0.0;
579  }
580 
581 
582  maxNM = 1;
583  M = (DMatrix**)calloc(maxNM,sizeof(DMatrix*));
584  M_index = (int*)calloc(maxNM,sizeof(int));
585 
586  M_index[0] = 0;
587  M [0] = 0;
588  nOfM = 0;
589 
590  las = arg.las;
591 
592  for( run1 = 0; run1 < 4; run1++ ){
593  eta [run1] = new double[m];
594  eta2[run1] = new double[m];
595  for( run2 = 0; run2 < m; run2++ ){
596  eta [run1][run2] = 0.0;
597  eta2[run1][run2] = 0.0;
598  }
599  }
600 
601  F = new double[m];
602  F2 = new double[m];
603 
604 
605  // SETTINGS:
606  // ---------
607  h = (double*)calloc(1,sizeof(double));
608  h[0] = 0.001 ;
609  hini = 0.001 ;
610  hmin = 0.000001 ;
611  hmax = 1.0e10 ;
612 
613  tune = 0.5 ;
614  TOL = 0.000001 ;
615 
616 
617  // INTERNAL INDEX LISTS:
618  // ---------------------
619  diff_index = new int[m];
620 
621  for( run1 = 0; run1 < md; run1++ ){
622  diff_index[run1] = rhs->index( VT_DIFFERENTIAL_STATE, run1 );
623  if( diff_index[run1] == rhs->getNumberOfVariables() ){
624  diff_index[run1] = diff_index[run1] + 1 + run1;
625  }
626  }
627 
628  ddiff_index = new int[md];
629 
630  for( run1 = 0; run1 < md; run1++ ){
631  ddiff_index[run1] = rhs->index( VT_DDIFFERENTIAL_STATE, run1 );
632  if( ddiff_index[run1] == rhs->getNumberOfVariables() ){
633  ddiff_index[run1] = ddiff_index[run1] + 1 + md + run1;
634  }
635  }
636 
637  alg_index = new int[ma];
638 
639  for( run1 = 0; run1 < ma; run1++ ){
640  alg_index [run1] = rhs->index( VT_ALGEBRAIC_STATE, run1 );
641  diff_index[md+run1] = alg_index [run1];
642  }
643 
644  control_index = new int[mu ];
645 
646  for( run1 = 0; run1 < mu; run1++ ){
647  control_index[run1] = rhs->index( VT_CONTROL, run1 );
648  }
649 
650  parameter_index = new int[mp ];
651 
652  for( run1 = 0; run1 < mp; run1++ ){
653  parameter_index[run1] = rhs->index( VT_PARAMETER, run1 );
654  }
655 
656  int_control_index = new int[mui];
657 
658  for( run1 = 0; run1 < mui; run1++ ){
659  int_control_index[run1] = rhs->index( VT_INTEGER_CONTROL, run1 );
660  }
661 
662  int_parameter_index = new int[mpi];
663 
664  for( run1 = 0; run1 < mpi; run1++ ){
666  }
667 
668  disturbance_index = new int[mw ];
669 
670  for( run1 = 0; run1 < mw; run1++ ){
671  disturbance_index[run1] = rhs->index( VT_DISTURBANCE, run1 );
672  }
673 
674  time_index = rhs->index( VT_TIME, 0 );
675 
676 
677  // OTHERS:
678  // -------
679  maxNumberOfSteps = 1000;
680  count = 0 ;
681  count2 = 0 ;
682  count3 = 0 ;
683 
684  diff_scale.init(m);
685 
686  for( run1 = 0; run1 < md; run1++ ){
687  diff_scale(run1) = rhs->scale( VT_DIFFERENTIAL_STATE, run1 );
688  }
689  for( run1 = 0; run1 < ma; run1++ ){
690  diff_scale(md+run1) = rhs->scale( VT_ALGEBRAIC_STATE, run1 );
691  }
692 
693  initial_guess = new double[m];
694 
695  for( run1 = 0; run1 < m; run1++ ){
696 
697  initial_guess[run1] = 0.0;
698  }
699 
700 
701  // PRINT-LEVEL:
702  // ------------
703  PrintLevel = LOW;
704 
705 
706  // SENSITIVITIES:
707  // --------------
708  nFDirs = 0 ;
709  nBDirs = 0 ;
710 
711  nFDirs2 = 0 ;
712  nBDirs2 = 0 ;
713 
714  G = NULL;
715  etaG = NULL;
716 
717  G2 = NULL;
718  G3 = NULL;
719  etaG2 = NULL;
720  etaG3 = NULL;
721 
722  c2G.init(md);
723  c2G2.init(md);
724  c2G3.init(md);
725 
726  phiG.init(nstep,m);
727  phiG2.init(nstep,m);
728  phiG3.init(nstep,m);
729 
730  deltaG.init(nstep,m);
731  deltaG2.init(nstep,m);
732  deltaG3.init(nstep,m);
733 
734 
735  H = NULL;
736  etaH = NULL;
737 
738  kH = new double**[dim];
739  for( run1 = 0; run1 < dim; run1++ ){
740  kH[run1] = new double*[nstep];
741  for( run2 = 0; run2 < nstep; run2++ ){
742  kH[run1][run2] = new double[ndir];
743  }
744  }
745  zH = new double*[6];
746  for( run1 = 0; run1 < 6; run1++ ){
747  zH[run1] = new double[ndir];
748  }
749 
750  H2 = NULL;
751  H3 = NULL;
752  etaH2 = NULL;
753  etaH3 = NULL;
754 
755  kH2 = new double**[dim];
756  for( run1 = 0; run1 < dim; run1++ ){
757  kH2[run1] = new double*[nstep];
758  for( run2 = 0; run2 < nstep; run2++ ){
759  kH2[run1][run2] = new double[ndir];
760  }
761  }
762  zH2 = new double*[6];
763  for( run1 = 0; run1 < 6; run1++ ){
764  zH2[run1] = new double[ndir];
765  }
766  kH3 = new double**[dim];
767  for( run1 = 0; run1 < dim; run1++ ){
768  kH3[run1] = new double*[nstep];
769  for( run2 = 0; run2 < nstep; run2++ ){
770  kH3[run1][run2] = new double[ndir];
771  }
772  }
773  zH3 = new double*[6];
774  for( run1 = 0; run1 < 6; run1++ ){
775  zH3[run1] = new double[ndir];
776  }
777 
778  // THE STATE OF AGGREGATION:
779  // -------------------------
780  soa = SOA_UNFROZEN;
781 
782 
783  // STORAGE:
784  // --------
785  maxAlloc = 1;
786 
787  nFcnEvaluations = 0;
788  nJacEvaluations = 0;
789 }
790 
791 
793 
794  return new IntegratorBDF(*this);
795 }
796 
797 
799 
800  int run1, run2;
801 
802  if( initialAlgebraicResiduum != 0 )
803  delete[] initialAlgebraicResiduum;
804 
805 
806  // BUTCHER-
807  // TABLEAU:
808  // ----------
809  for( run1 = 0; run1 < dim; run1++ ){
810  delete[] A[run1];
811  }
812 
813  delete[] A;
814  delete[] b4;
815  delete[] b5;
816  delete[] c ;
817 
818 
819  // RK-ALGORITHM:
820  // -------------
821  if( eta4 != NULL ){
822  delete[] eta4;
823  }
824  if( eta4_ != NULL ){
825  delete[] eta4_;
826  }
827  if( eta5 != NULL ){
828  delete[] eta5;
829  }
830  if( eta5_ != NULL ){
831  delete[] eta5_;
832  }
833 
834 
835 
836  for( run2 = 0; run2 < 4; run2++ ){
837 
838  for( run1 = 0; run1 < dim; run1++ ){
839  if( k[run2] != NULL )
840  delete[] k[run2][run1] ;
841  if( k2[run2] != NULL )
842  delete[] k2[run2][run1];
843  if( l[run2] != NULL )
844  delete[] l[run2][run1] ;
845  if( l2[run2] != NULL )
846  delete[] l2[run2][run1];
847  }
848 
849  if( k != NULL )
850  delete[] k[run2] ;
851 
852  if( k2 != NULL )
853  delete[] k2[run2];
854 
855  if( l != NULL )
856  delete[] l[run2] ;
857 
858  if( l2 != NULL )
859  delete[] l2[run2];
860  }
861 
862 
863  if( k != NULL )
864  delete[] k;
865 
866  if( k2!= NULL )
867  delete[] k2;
868 
869  if( l != NULL )
870  delete[] l ;
871 
872  if( l2!= NULL )
873  delete[] l2;
874 
875  if( iseed != NULL ){
876  delete[] iseed;
877  }
878 
879  if( x != NULL )
880  delete[] x;
881 
882 
883 
884  for( run1 = 0; run1 < maxAlloc; run1++ ){
885 
886  if( psi[run1] != NULL ){
887  free(psi[run1]);
888  }
889  if( gamma[run1] != NULL ){
890  free(gamma[run1]);
891  }
892  }
893 
894  if( psi != NULL ){
895  free(psi);
896  }
897 
898  if( psi_ != NULL ){
899  delete[] psi_;
900  }
901 
902  if( gamma != NULL )
903  free(gamma);
904 
905  if( nOfNewtonSteps != NULL )
906  free(nOfNewtonSteps);
907 
908  for( run1 = 0; run1 < 4; run1++ ){
909 
910  if( eta != NULL )
911  delete[] eta[run1];
912  if( eta2 != NULL )
913  delete[] eta2[run1];
914  }
915 
916  delete[] eta ;
917  delete[] eta2 ;
918 
919  for( run1 = 0; run1 < maxNM; run1++ ){
920  if( M[run1] != 0 )
921  delete M[run1];
922  }
923 
924  if( M != NULL ){
925  free(M);
926  free(M_index);
927  }
928 
929  if( F != NULL )
930  delete[] F;
931  if( F2 != NULL )
932  delete[] F2;
933 
934 
935  // OTHERS:
936  // -------
937  if( initial_guess != NULL ){
938  delete[] initial_guess;
939  }
940 
941  // SENSITIVITIES:
942  // --------------
943 
944  if( G != NULL )
945  delete[] G;
946 
947  if( etaG != NULL )
948  delete[] etaG;
949 
950  if( G2 != NULL )
951  delete[] G2;
952 
953  if( G3 != NULL )
954  delete[] G3;
955 
956  if( etaG2 != NULL )
957  delete[] etaG2;
958 
959  if( etaG3 != NULL )
960  delete[] etaG3;
961 
962 
963  // ----------------------------------------
964 
965  if( H != NULL )
966  delete[] H;
967 
968  // ----------------------------------------
969 
970  if( H2 != NULL )
971  delete[] H2;
972  if( H3 != NULL )
973  delete[] H3;
974 
975  for( run1 = 0; run1 < nstep; run1++ ){
976  if( etaH != NULL )
977  delete[] etaH[run1];
978  if( etaH2 != NULL )
979  delete[] etaH2[run1];
980  if( etaH3 != NULL )
981  delete[] etaH3[run1];
982  }
983  if( etaH != NULL )
984  delete[] etaH;
985  if( etaH2 != NULL )
986  delete[] etaH2;
987  if( etaH3 != NULL )
988  delete[] etaH3;
989 
990 
991  for( run1 = 0; run1 < dim; run1++ ){
992  for( run2 = 0; run2 < nstep; run2++ ){
993  if( kH != NULL )
994  delete[] kH[run1][run2];
995  if( kH2 != NULL )
996  delete[] kH2[run1][run2];
997  if( kH3 != NULL )
998  delete[] kH3[run1][run2];
999  }
1000  if( kH != NULL )
1001  delete[] kH[run1];
1002  if( kH2 != NULL )
1003  delete[] kH2[run1];
1004  if( kH3 != NULL )
1005  delete[] kH3[run1];
1006  }
1007  if( kH != NULL )
1008  delete[] kH;
1009  if( kH2 != NULL )
1010  delete[] kH2;
1011  if( kH3 != NULL )
1012  delete[] kH3;
1013 
1014 
1015  for( run1 = 0; run1 < 6; run1++ ){
1016  if( zH != NULL )
1017  delete[] zH[run1];
1018  if( zH2 != NULL )
1019  delete[] zH2[run1];
1020  if( zH3 != NULL )
1021  delete[] zH3[run1];
1022  }
1023  if( zH != NULL )
1024  delete[] zH;
1025  if( zH2 != NULL )
1026  delete[] zH2;
1027  if( zH3 != NULL )
1028  delete[] zH3;
1029 }
1030 
1031 
1033 
1034 
1035  if( soa != SOA_UNFROZEN ){
1036  if( PrintLevel != NONE ){
1038  }
1039  return RET_ALREADY_FROZEN;
1040  }
1041 
1043  return SUCCESSFUL_RETURN;
1044 }
1045 
1046 
1048 
1049  if( soa != SOA_UNFROZEN ){
1050  if( PrintLevel != NONE ){
1052  }
1053  return RET_ALREADY_FROZEN;
1054  }
1055 
1057  return SUCCESSFUL_RETURN;
1058 }
1059 
1060 
1062 
1063  int run1, run2;
1064 
1065  for( run1 = 1; run1 < maxAlloc; run1++ ){
1066 
1067  if( psi[run1] != NULL ){
1068  free(psi[run1]);
1069  }
1070  if( gamma[run1] != NULL ){
1071  free(gamma[run1]);
1072  }
1073  }
1074 
1075  maxAlloc = 1;
1076 
1077  psi = (double**)realloc(psi,maxAlloc*sizeof(double*));
1078  gamma = (double**)realloc(gamma,maxAlloc*sizeof(double*));
1079  nOfNewtonSteps = (int*)realloc(nOfNewtonSteps,(maxAlloc)*sizeof(int));
1080 
1081  for( run1 = 0; run1 < maxAlloc; run1++ ){
1082  nOfNewtonSteps[run1] = 0;
1083  }
1084 
1085  for( run1 = 0; run1 < maxAlloc; run1++ ){
1086 
1087  for( run2 = 0; run2 < 5; run2++ ){
1088  psi [run1][run2] = 0.0;
1089  gamma[run1][run2] = 0.0;
1090  }
1091  }
1092 
1093  for( run1 = 0; run1 < maxNM; run1++ ){
1094  if( M[run1] != 0 )
1095  delete M[run1];
1096  M[run1] = 0;
1097  }
1098 
1099  maxNM = 1;
1100  nOfM = 0;
1101  M = (DMatrix**)realloc(M,maxNM*sizeof(DMatrix*));
1102  M_index = (int*)realloc(M_index,maxAlloc*sizeof(int));
1103 
1104  h = (double*)realloc(h,maxAlloc*sizeof(double));
1105 
1106  soa = SOA_UNFROZEN;
1107 
1108  return SUCCESSFUL_RETURN;
1109 }
1110 
1111 
1113  const DVector &xa ,
1114  const DVector &p ,
1115  const DVector &u ,
1116  const DVector &w ,
1117  const Grid &t_ ){
1118 
1119  int run1;
1120  returnValue returnvalue;
1121 
1122  if( rhs == NULL ){
1123  return ACADOERROR(RET_TRIVIAL_RHS);
1124  }
1125 
1127 
1128  timeInterval = t_;
1129 
1131  iStore.init( mn , timeInterval );
1132 
1135 
1136  if( x0.isEmpty() == BT_TRUE ) return ACADOERROR(RET_MISSING_INPUTS);
1137 
1138  if( (int) x0.getDim() < md )
1140 
1141  for( run1 = 0; run1 < md; run1++ ){
1142  eta4[run1] = x0(run1);
1143  eta5[run1] = x0(run1);
1144  xStore(0,run1) = x0(run1);
1145  x[diff_index[run1]] = x0(run1);
1146  }
1147 
1149  h[0] = hini;
1150 
1151  if( timeInterval.getIntervalLength() - h[0] < EPS ){
1153  }
1154 
1155  if( h[0] < 10.0*EPS )
1157  }
1158 
1159  if( ma > 0 ){
1160 
1161  if( (int) xa.getDim() < ma )
1163 
1164  for( run1 = 0; run1 < ma; run1++ ){
1165  eta4[md+run1] = xa(run1);
1166  eta5[md+run1] = xa(run1);
1167  xStore(0,md+run1) = xa(run1);
1168  x[diff_index[md+run1]] = xa(run1);
1169  initial_guess[md+run1] = xa(run1);
1170  }
1171  }
1172 
1173  if( nFDirs != 0 )
1174  for( run1 = 0; run1 < md; run1++ )
1175  etaG[run1] = fseed(diff_index[run1]);
1176 
1177  if( mp > 0 ){
1178 
1179  if( (int) p.getDim() < mp )
1181 
1182  for( run1 = 0; run1 < mp; run1++ ){
1183  x[parameter_index[run1]] = p(run1);
1184  }
1185  }
1186 
1187  if( mu > 0 ){
1188 
1189  if( (int) u.getDim() < mu )
1191 
1192  for( run1 = 0; run1 < mu; run1++ ){
1193  x[control_index[run1]] = u(run1);
1194  }
1195  }
1196 
1197 
1198  if( mw > 0 ){
1199 
1200  if( (int) w.getDim() < mw )
1202 
1203  for( run1 = 0; run1 < mw; run1++ ){
1204  x[disturbance_index[run1]] = w(run1);
1205  }
1206  }
1207 
1208  // log initial step
1209  logCurrentIntegratorStep( x0,xa );
1210 
1211  // start the time measurement:
1212  // ---------------------------
1213 
1214  totalTime.start();
1215  nFcnEvaluations = 0;
1216  nJacEvaluations = 0;
1217 
1218 
1219  // initialize the scaling based on the initial states:
1220  // ---------------------------------------------------
1221 
1222  double atol;
1223  get( ABSOLUTE_TOLERANCE, atol );
1224 
1225  for( run1 = 0; run1 < m; run1++ )
1226  diff_scale(run1) = fabs(eta4[run1]) + atol/TOL;
1227 
1228 
1229  returnvalue = rhs[0].evaluate( 0, x, initialAlgebraicResiduum );
1230 
1231  if( returnvalue != SUCCESSFUL_RETURN ){
1232 
1233  totalTime.stop();
1234  return ACADOWARNING(returnvalue);
1235  }
1236 
1237  // PRINTING:
1238  // ---------
1239  if( PrintLevel == MEDIUM ){
1240  acadoPrintCopyrightNotice( "IntegratorBDF -- A BDF integrator (order 4)." );
1241  }
1242  if( PrintLevel == HIGH ){
1243  cout << "BDF: t = " << t << "\t";
1244  for( run1 = 0; run1 < md; run1++ ){
1245  cout << "x[" << run1 << "] = " << scientific << eta4[run1] << " ";
1246  }
1247  for( run1 = 0; run1 < ma; run1++ ){
1248  cout << "xq[" << run1 << "] = " << eta4[md+run1] << " ";
1249  }
1250  cout << endl;
1251  }
1252 
1253 
1254  count3 = 0;
1255 
1256  returnvalue = rk_start();
1257 
1258  if( returnvalue != SUCCESSFUL_RETURN ){
1259 
1260  totalTime.stop();
1261 
1262  if( PrintLevel != NONE )
1263  return ACADOERROR(returnvalue);
1264 
1265  return returnvalue;
1266  }
1267 
1269 
1270  if( timeInterval.getLastTime() - t - h[0] < EPS ){
1271  h[0] = timeInterval.getLastTime() - t;
1272  }
1273  }
1274 
1275  returnvalue = RET_FINAL_STEP_NOT_PERFORMED_YET;
1276 
1277  count = 7;
1278 
1279  while( returnvalue == RET_FINAL_STEP_NOT_PERFORMED_YET && count <= maxNumberOfSteps ){
1280 
1281  returnvalue = step(count);
1282  count++;
1283  }
1284 
1285  // log final step
1287 
1288  count2 = count-1;
1289 
1290 
1291  for( run1 = 0; run1 < mn; run1++ )
1292  iStore( 0, run1 ) = iStore( 1, run1 );
1293 
1294 
1295  // stop the measurement of total time:
1296  // -----------------------------------
1297  totalTime.stop();
1298 
1299 
1300 // cout <<"numIntSteps = %d\n",count-1);
1301 
1302 
1303  // SET THE LOGGING INFORMATION:
1304  // ----------------------------------------------------------------------------------------
1305 
1314 
1315  // ----------------------------------------------------------------------------------------
1316 
1317 
1318 
1319  if( count > maxNumberOfSteps ){
1320 
1321  if( PrintLevel != NONE )
1324  }
1325 
1326 
1327  // PRINTING:
1328  // ---------
1329  if( PrintLevel == MEDIUM ){
1330 
1331  cout << "\n Results at t = " << t << " : \n\n";
1332  for( run1 = 0; run1 < md; run1++ ){
1333  cout << "x[" << run1 << "] = " << scientific << nablaY(0,run1) << " ";
1334  }
1335  for( run1 = 0; run1 < ma; run1++ ){
1336  cout << "x[" << run1 << "] = " << scientific << nablaY(0,md+run1) << " ";
1337  }
1338  cout << endl;
1340  }
1341 
1342  int printIntegratorProfile = 0;
1343  get( PRINT_INTEGRATOR_PROFILE,printIntegratorProfile );
1344 
1345  if ( (BooleanType)printIntegratorProfile == BT_TRUE )
1346  {
1348  }
1349  else
1350  {
1351  if( PrintLevel == MEDIUM || PrintLevel == HIGH )
1352  cout << "BDF: number of steps: " << count - 1 << endl;
1353  }
1354 
1355  return returnvalue;
1356 }
1357 
1358 
1360  const DVector &pSeed ,
1361  const DVector &uSeed ,
1362  const DVector &wSeed ,
1363  const int &order ){
1364 
1365  if( order == 2 ){
1366  return setForwardSeed2( xSeed, pSeed, uSeed, wSeed );
1367  }
1368  if( order < 1 || order > 2 ){
1370  }
1371 
1372  if( nBDirs > 0 ){
1374  }
1375 
1376  int run2;
1377 
1378  if( G != NULL ){
1379  delete[] G;
1380  G = NULL;
1381  }
1382 
1383  if( etaG != NULL ){
1384  delete[] etaG;
1385  etaG = NULL;
1386  }
1387 
1388  nFDirs = 1;
1389 
1390  fseed.init(ndir);
1391  fseed.setZero();
1392 
1393  G = new double[ndir];
1394 
1395  for( run2 = 0; run2 < (ndir); run2++ )
1396  G[run2] = 0.0;
1397 
1398  etaG = new double[m];
1399 
1400  nablaG.init( nstep, m );
1401 
1402 
1403  if( xSeed.getDim() != 0 )
1404  for( run2 = 0; run2 < md; run2++ )
1405  fseed(diff_index[run2]) = xSeed(run2);
1406 
1407  if( pSeed.getDim() != 0 )
1408  for( run2 = 0; run2 < mp; run2++ ){
1409  fseed(parameter_index[run2]) = pSeed(run2);
1410  G [parameter_index[run2]] = pSeed(run2);
1411  }
1412 
1413  if( uSeed.getDim() != 0 )
1414  for( run2 = 0; run2 < mu; run2++ ){
1415  fseed(control_index[run2]) = uSeed(run2);
1416  G [control_index[run2]] = uSeed(run2);
1417  }
1418 
1419  if( wSeed.getDim() != 0 )
1420  for( run2 = 0; run2 < mw; run2++ ){
1421  fseed(disturbance_index[run2]) = wSeed(run2);
1422  G [disturbance_index[run2]] = wSeed(run2);
1423  }
1424 
1425  return SUCCESSFUL_RETURN;
1426 }
1427 
1428 
1430  const DVector &pSeed ,
1431  const DVector &uSeed ,
1432  const DVector &wSeed ){
1433 
1434  int run2;
1435 
1436 
1437  if( G2 != NULL ){
1438  delete[] G2;
1439  G2 = NULL;
1440  }
1441 
1442  if( G3 != NULL ){
1443  delete[] G3;
1444  G3 = NULL;
1445  }
1446 
1447  if( etaG2 != NULL ){
1448  delete[] etaG2;
1449  etaG2 = NULL;
1450  }
1451 
1452  if( etaG3 != NULL ){
1453  delete[] etaG3;
1454  etaG3 = NULL;
1455  }
1456 
1457 
1458  nFDirs2 = 1;
1459 
1460  fseed2.init(ndir);
1461  fseed2.setZero();
1462 
1463  G2 = new double[ndir];
1464  G3 = new double[ndir];
1465 
1466  for( run2 = 0; run2 < (ndir); run2++ ){
1467  G2[run2] = 0.0;
1468  G3[run2] = 0.0;
1469  }
1470 
1471  etaG2 = new double[m];
1472  etaG3 = new double[m];
1473 
1474  nablaG2.init( nstep, m );
1475  nablaG3.init( nstep, m );
1476 
1477  if( xSeed.getDim() != 0 )
1478  for( run2 = 0; run2 < md; run2++ )
1479  fseed2(diff_index[run2]) = xSeed(run2);
1480 
1481  if( pSeed.getDim() != 0 )
1482  for( run2 = 0; run2 < mp; run2++ ){
1483  fseed2(parameter_index[run2]) = pSeed(run2);
1484  G2 [parameter_index[run2]] = pSeed(run2);
1485  }
1486 
1487  if( uSeed.getDim() != 0 )
1488  for( run2 = 0; run2 < mu; run2++ ){
1489  fseed2(control_index[run2]) = uSeed(run2);
1490  G2 [control_index[run2]] = uSeed(run2);
1491  }
1492 
1493  if( wSeed.getDim() != 0 )
1494  for( run2 = 0; run2 < mw; run2++ ){
1495  fseed2(disturbance_index[run2]) = wSeed(run2);
1496  G2 [disturbance_index[run2]] = wSeed(run2);
1497  }
1498 
1499  return SUCCESSFUL_RETURN;
1500 }
1501 
1502 
1504 
1505  if( order == 2 ){
1506  return setBackwardSeed2(seed);
1507  }
1508  if( order < 1 || order > 2 ){
1510  }
1511 
1512  if( nFDirs > 0 ){
1514  }
1515 
1516  int run1, run2, run3;
1517 
1518  if( H != NULL ){
1519  delete[] H;
1520  H = NULL;
1521  }
1522 
1523  for( run1 = 0; run1 < nstep; run1++ ){
1524  if( etaH != NULL )
1525  delete[] etaH[run1];
1526  }
1527  if( etaH != NULL ){
1528  delete[] etaH;
1529  etaH = 0;
1530  }
1531 
1532  nBDirs = 1;
1533 
1534  bseed.init(m);
1535  bseed.setZero();
1536  H = new double[m];
1537 
1538  if( seed.getDim() != 0 )
1539  for( run2 = 0; run2 < md; run2++ )
1540  bseed(run2) = seed(run2);
1541 
1542  etaH = new double*[nstep];
1543  for( run3 = 0; run3 < nstep; run3++ ){
1544  etaH[run3] = new double[ndir];
1545  for( run2 = 0; run2 < ndir; run2++ ){
1546  etaH[run3][run2] = 0.0;
1547  }
1548  }
1549 
1550  nablaH.init( nstep, ndir );
1551  nablaH.setZero();
1552 
1553  nablaH_.init( nstep, ndir );
1554  nablaH_.setZero();
1555 
1556  deltaH.init( nstep, ndir );
1557  deltaH.setZero();
1558 
1559  c2H.init(ndir);
1560  c2H.setZero();
1561 
1562  return SUCCESSFUL_RETURN;
1563 }
1564 
1565 
1567 
1568  int run1, run2, run3;
1569 
1570  if( H2 != NULL ){
1571  delete[] H2;
1572  H2 = NULL;
1573  }
1574  if( H3 != NULL ){
1575  delete[] H3;
1576  H3 = NULL;
1577  }
1578 
1579  for( run1 = 0; run1 < nstep; run1++ ){
1580  if( etaH2 != NULL )
1581  delete[] etaH2[run1];
1582  if( etaH3 != NULL )
1583  delete[] etaH3[run1];
1584  }
1585  if( etaH2 != NULL ){
1586  delete[] etaH2;
1587  etaH2 = 0;
1588  }
1589  if( etaH3 != NULL ){
1590  delete[] etaH3;
1591  etaH3 = 0;
1592  }
1593 
1594  nBDirs2 = 1;
1595 
1596  bseed2.init(m);
1597 
1598  H2 = new double[m];
1599  H3 = new double[m];
1600 
1601  if( seed.getDim() != 0 ){
1602  for( run2 = 0; run2 < md; run2++ ){
1603  bseed2(run2) = seed(run2);
1604  }
1605  }
1606 
1607  etaH2 = new double*[nstep];
1608  for( run3 = 0; run3 < nstep; run3++ ){
1609  etaH2[run3] = new double[ndir];
1610  for( run2 = 0; run2 < ndir; run2++ ){
1611  etaH2[run3][run2] = 0.0;
1612  }
1613  }
1614  etaH3 = new double*[nstep];
1615  for( run3 = 0; run3 < nstep; run3++ ){
1616  etaH3[run3] = new double[ndir];
1617  for( run2 = 0; run2 < ndir; run2++ ){
1618  etaH3[run3][run2] = 0.0;
1619  }
1620  }
1621 
1622  nablaH2.init( nstep, ndir );
1623  nablaH2.setZero();
1624 
1625  nablaH3.init( nstep, ndir );
1626  nablaH3.setZero();
1627 
1628  nablaH2_.init( nstep, ndir );
1629  nablaH2_.setZero();
1630 
1631  nablaH3_.init( nstep, ndir );
1632  nablaH3_.setZero();
1633 
1634  deltaH2.init( nstep, ndir );
1635  deltaH2.setZero();
1636 
1637  deltaH3.init( nstep, ndir );
1638  deltaH3.setZero();
1639 
1640  c2H2.init( ndir );
1641  c2H2.setZero();
1642 
1643  c2H3.init( ndir );
1644  c2H3.setZero();
1645 
1646  return SUCCESSFUL_RETURN;
1647 }
1648 
1649 
1651 
1652  int run1;
1653  returnValue returnvalue;
1654 
1655  if( rhs == NULL ){
1656  return ACADOERROR(RET_TRIVIAL_RHS);
1657  }
1658 
1659  if( soa != SOA_EVERYTHING_FROZEN ){
1660  return ACADOERROR(RET_NOT_FROZEN);
1661  }
1662 
1663  if( nBDirs2 == 0 && nFDirs != 0 ){
1665  dxStore.init ( md+ma, timeInterval );
1666  for( run1 = 0; run1 < md; run1++ ){
1667  etaG[run1] = fseed(diff_index[run1]);
1668  }
1669  }
1670 
1671  nablaH.setZero();
1672 
1673  if( nBDirs != 0 ){
1674  for( run1 = 0; run1 < md; run1++ ){
1675  nablaH(0,diff_index[run1]) = bseed(run1);
1676  }
1677  }
1678 
1679  if( nFDirs2 != 0 ){
1682  for( run1 = 0; run1 < md; run1++ ){
1683  etaG2[run1] = fseed2(diff_index[run1]);
1684  etaG3[run1] = 0.0;
1685  }
1686  }
1687 
1688  nablaH2.setZero();
1689  nablaH3.setZero();
1690 
1691  if( nBDirs2 != 0 ){
1692  for( run1 = 0; run1 < md; run1++ ){
1693  nablaH2(0,diff_index[run1]) = bseed2(run1);
1694  }
1695  }
1696 
1697 
1698  returnvalue = RET_FINAL_STEP_NOT_PERFORMED_YET;
1699 
1700  if( nBDirs > 0 || nBDirs2 > 0 ){
1701 
1703  h[0] = 1.0;
1704 
1705  int oldCount = count;
1706 
1707  count--;
1708  while( returnvalue == RET_FINAL_STEP_NOT_PERFORMED_YET && count >= 7 ){
1709 
1710  returnvalue = step( count );
1711  count--;
1712  }
1713 
1714  if( returnvalue != SUCCESSFUL_RETURN &&
1715  returnvalue != RET_FINAL_STEP_NOT_PERFORMED_YET ){
1716  count = oldCount;
1717  if( PrintLevel != NONE )
1718  return ACADOERROR(returnvalue);
1719  return returnvalue;
1720  }
1721 
1722  returnvalue = rk_start();
1723 
1724  if( returnvalue == SUCCESSFUL_RETURN ){
1725 
1726  // PRINTING:
1727  // ---------
1728  if( PrintLevel == MEDIUM )
1730 
1731  count = oldCount;
1732 
1733  return SUCCESSFUL_RETURN;
1734  }
1735  count = oldCount;
1736  }
1737  else{
1738 
1740 
1741  returnvalue = rk_start();
1742 
1743  if( returnvalue != SUCCESSFUL_RETURN ){
1744  if( PrintLevel != NONE )
1745  return ACADOERROR(returnvalue);
1746  return returnvalue;
1747  }
1748 
1749  returnvalue = RET_FINAL_STEP_NOT_PERFORMED_YET;
1750  count = 7;
1751  while( returnvalue == RET_FINAL_STEP_NOT_PERFORMED_YET &&
1752  count <= maxNumberOfSteps ){
1753 
1754  returnvalue = step(count);
1755  count++;
1756  }
1757 
1758  if( nBDirs2 == 0 && nFDirs != 0 )
1759  for( run1 = 0; run1 < m; run1++ )
1760  dxStore( 0, run1 ) = dxStore( 1, run1 );
1761 
1762  if( nFDirs2 != 0 )
1763  for( run1 = 0; run1 < m; run1++ )
1764  ddxStore( 0, run1 ) = ddxStore( 1, run1 );
1765 
1766  if( count > maxNumberOfSteps ){
1767  if( PrintLevel != NONE )
1770  }
1771 
1772  // PRINTING:
1773  // ---------
1774  if( PrintLevel == MEDIUM )
1776 
1777  return SUCCESSFUL_RETURN;
1778  }
1779 
1780  if( PrintLevel != NONE )
1781  return ACADOERROR(returnvalue);
1782 
1783  return returnvalue;
1784 }
1785 
1786 
1788 
1789  int run1;
1790  double E = EPS;
1791 
1793 
1794  int oldAlloc = maxAlloc;
1795 
1796  if( number_ >= maxAlloc ){
1797  maxAlloc = 2*maxAlloc;
1798 
1799  psi = (double**)realloc(psi,maxAlloc*sizeof(double*));
1800  gamma = (double**)realloc(gamma,maxAlloc*sizeof(double*));
1801  nOfNewtonSteps = (int*)realloc(nOfNewtonSteps,(maxAlloc)*sizeof(int));
1802 
1803  for( run1 = oldAlloc; run1 < maxAlloc; run1++ ){
1804  nOfNewtonSteps[run1] = 0;
1805  }
1806  for( run1 = oldAlloc; run1 < maxAlloc; run1++ ){
1807  psi [run1] = (double*)calloc(nstep,sizeof(double));
1808  gamma[run1] = (double*)calloc(nstep,sizeof(double));
1809  }
1810 
1811  M_index = (int*)realloc(M_index,maxAlloc*sizeof(int));
1812  h = (double*)realloc(h,maxAlloc*sizeof(double));
1813  }
1814  }
1815 
1816 
1818  rel_time_scale = h[0];
1819  h[0] = h[number_];
1820  }
1821 
1822  if( soa == SOA_FREEZING_MESH ||
1823  soa == SOA_MESH_FROZEN ||
1824  soa == SOA_UNFROZEN ){
1825 
1826  if( number_ == 7 ){
1827  E = determinePredictor(7, BT_TRUE );
1828  }
1829  else{
1830  E = determinePredictor(7, BT_FALSE );
1831  }
1832  }
1833  if( soa == SOA_FREEZING_ALL ){
1834 
1835  if( number_ == 7 ){
1836  E = determinePredictor(number_, BT_TRUE );
1837  }
1838  else{
1839  E = determinePredictor(number_, BT_FALSE );
1840  }
1841  }
1842 
1843 
1845 
1846  int number_of_rejected_steps = 0;
1847 
1848  if( E < 0.0 ){
1850  }
1851 
1852  // REJECT THE STEP IF GIVEN TOLERANCE IS NOT ACHIEVED:
1853  // ---------------------------------------------------
1854  while( E >= TOL ){
1855 
1856  if( PrintLevel == HIGH ){
1857 
1858  cout << "STEP REJECTED: error estimate = " << scientific << E
1859  << " required local tolerance = " << TOL << endl;
1860  }
1861 
1862  number_of_rejected_steps++;
1863 
1864  if( soa == SOA_FREEZING_MESH ||
1865  soa == SOA_UNFROZEN ){
1866 
1867  psi[7][0] = psi_[0];
1868  psi[7][1] = psi_[1];
1869  psi[7][2] = psi_[2];
1870  psi[7][3] = psi_[3];
1871  }
1872  if( soa == SOA_FREEZING_ALL ){
1873 
1874  psi[number_][0] = psi_[0];
1875  psi[number_][1] = psi_[1];
1876  psi[number_][2] = psi_[2];
1877  psi[number_][3] = psi_[3];
1878  }
1879 
1880 
1881  if( h[0] <= hmin + EPS ){
1883  }
1884  h[0] = 0.5*h[0];
1885  if( E > 0.9*INFTY ) h[0] = 0.2*h[0];
1886 
1887  if( h[0] < hmin ){
1888  h[0] = hmin;
1889  }
1890 
1891  if( soa == SOA_FREEZING_MESH ||
1892  soa == SOA_UNFROZEN ){
1893 
1894  E = determinePredictor( 7, BT_FALSE );
1895  }
1896  if( soa == SOA_FREEZING_ALL ){
1897 
1898  E = determinePredictor( number_, BT_FALSE );
1899  }
1900 
1901  if( E < 0.0 ){
1903  }
1904  }
1905 
1906  count3 += number_of_rejected_steps;
1907  }
1908 
1909  // PROCEED IF THE STEP IS ACCEPTED:
1910  // --------------------------------
1911 
1913 
1914  // log current step
1916 
1917 
1918  // compute forward derivatives if requested:
1919  // ------------------------------------------
1920 
1921  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ){
1922 
1923  if( nBDirs != 0 ){
1925  }
1926 
1928  determineBDFEtaGForward(number_);
1929  }
1930  else{
1932  }
1933  }
1934  if( nBDirs > 0 ){
1935 
1936  if( soa != SOA_EVERYTHING_FROZEN ){
1937  return ACADOERROR(RET_NOT_FROZEN);
1938  }
1939  if( nFDirs != 0 || nBDirs2 != 0 || nFDirs2 != 0 ){
1941  }
1942  determineBDFEtaHBackward(number_);
1943  }
1944  if( nFDirs2 > 0 ){
1945 
1946  if( soa != SOA_EVERYTHING_FROZEN ){
1947  return ACADOERROR(RET_NOT_FROZEN);
1948  }
1949  if( nBDirs != 0 || nBDirs2 != 0 || nFDirs != 1 ){
1951  }
1952  determineBDFEtaGForward2(number_);
1953  }
1954  if( nBDirs2 > 0 ){
1955 
1956  if( soa != SOA_EVERYTHING_FROZEN ){
1957  return ACADOERROR(RET_NOT_FROZEN);
1958  }
1959  if( nBDirs != 0 || nFDirs2 != 0 || nFDirs != 1 ){
1961  }
1962 
1963  determineBDFEtaHBackward2(number_);
1964  }
1965 
1966 
1967  // increase the time:
1968  // ----------------------------------------------
1969 
1970  if( nBDirs > 0 || nBDirs2 > 0 ){
1971 
1972  t = t - h[0];
1973  }
1974  else{
1975 
1976  t = t + h[0];
1977  }
1978 // printf( "t = %e, stepsize = %e\n", t,h[0] );
1979 
1980  // PRINTING:
1981  // ---------
1983 
1984 
1985  // STORAGE:
1986  // --------
1987 
1988  if( soa == SOA_FREEZING_MESH || soa == SOA_FREEZING_ALL ){
1989 
1990  if( number_ >= maxAlloc){
1991 
1992  maxAlloc = 2*maxAlloc;
1993  h = (double*)realloc(h,maxAlloc*sizeof(double));
1994  }
1995 
1996  h[number_] = h[0];
1997  }
1998 
1999  if( nFDirs == 0 && nBDirs == 0 && nFDirs2 == 0 && nBDirs == 0 ){
2000 
2001  interpolate( number_, nablaY, xStore );
2002 
2003  int i1 = timeInterval.getFloorIndex( t );
2004  int i2 = timeInterval.getFloorIndex( t + c[6]*h[0] );
2005  int jj;
2006 
2007  for( jj = i1+1; jj <= i2; jj++ )
2008  for( run1 = 0; run1 < mn; run1++ )
2009  iStore( jj, run1 ) = x[rhs->index( VT_INTERMEDIATE_STATE, run1 )];
2010  }
2011  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ) interpolate( number_, nablaG , dxStore );
2012  if( nFDirs2 > 0 ) interpolate( number_, nablaG3, ddxStore );
2013 
2014 
2015  if( nBDirs == 0 || nBDirs2 == 0 ){
2016 
2017  // Stop the algorithm if t >= te:
2018  // ----------------------------------------------
2019  if( t >= timeInterval.getLastTime() - EPS ){
2021  for( run1 = 0; run1 < m; run1++ ){
2022  if ( acadoIsNaN( nablaY(0,run1) ) == BT_TRUE )
2024  x[diff_index[run1]] = nablaY(0,run1);
2025  }
2026 
2027  if( soa == SOA_FREEZING_MESH ){
2028  soa = SOA_MESH_FROZEN;
2029  }
2030  if( soa == SOA_FREEZING_ALL ){
2032  }
2033 
2034  return SUCCESSFUL_RETURN;
2035  }
2036  }
2037 
2038 
2040 
2041 
2042  // recompute the scaling based on the actual states:
2043  // -------------------------------------------------
2044 
2045  double atol;
2046  get( ABSOLUTE_TOLERANCE, atol );
2047 
2048  for( run1 = 0; run1 < m; run1++ )
2049  diff_scale(run1) = fabs(nablaY(0,run1)) + atol/TOL;
2050 
2051 
2052  // apply a numeric stabilization of the step size control:
2053  // -------------------------------------------------------
2054  double Emin = 1e-3*sqrt(TOL)*pow( hini, 2.5 );
2055 
2056  if( E < Emin ) E = Emin ;
2057  if( E < 10.0*EPS ) E = 10.0*EPS;
2058 
2059 
2060 
2061  // determine the new step size:
2062  // ----------------------------------------------
2063  rel_time_scale = h[0];
2064  h[0] = h[0]*pow( tune*(TOL/E) , 0.20 );
2065 
2066  if( h[0] / rel_time_scale > 2.0 ) h[0] = 2.0*rel_time_scale;
2067 
2068  if( h[0] > hmax ){
2069  h[0] = hmax;
2070  }
2071  if( h[0] < hmin ){
2072  h[0] = hmin;
2073  }
2074 
2075  if( t + h[0] >= timeInterval.getLastTime() ){
2076  h[0] = timeInterval.getLastTime()-t;
2077  }
2078  }
2079 
2081 }
2082 
2083 
2084 
2086 
2088 }
2089 
2090 
2091 
2093 
2094  int run1;
2095 
2096  if( (int) xEnd[0].getDim() != m )
2098 
2099  for( run1 = 0; run1 < m; run1++ )
2100  xEnd[0](run1) = nablaY(0,run1);
2101 
2102  return SUCCESSFUL_RETURN;
2103 }
2104 
2105 
2107 
2108  int run1;
2109 
2110  if( Dx == NULL ){
2111  return SUCCESSFUL_RETURN;
2112  }
2113 
2114  if( order == 1 && nFDirs2 == 0 ){
2115 
2116  if( (int) Dx[0].getNumCols() != nFDirs )
2118  if( (int) Dx[0].getNumRows() != m )
2120 
2121  for( run1 = 0; run1 < m; run1++ ){
2122  Dx[0](run1,0) = nablaG(0,run1);
2123  }
2124  }
2125 
2126  if( order == 2 ){
2127 
2128  if( (int) Dx[0].getNumCols() != nFDirs2 )
2130  if( (int) Dx[0].getNumRows() != m )
2132 
2133  for( run1 = 0; run1 < m; run1++ ){
2134  Dx[0](run1,0) = nablaG3(0,run1);
2135  }
2136  }
2137 
2138  if( order == 1 && nFDirs2 > 0 ){
2139 
2140  if( (int) Dx[0].getNumCols() != nFDirs2 )
2142  if( (int) Dx[0].getNumRows() != m )
2144 
2145  for( run1 = 0; run1 < m; run1++ ){
2146  Dx[0](run1,0) = nablaG2(0,run1);
2147  }
2148  }
2149 
2150  if( order < 1 || order > 2 ){
2152  }
2153 
2154  return SUCCESSFUL_RETURN;
2155 }
2156 
2157 
2159  DVector &Dx_p ,
2160  DVector &Dx_u ,
2161  DVector &Dx_w ,
2162  int order ) const{
2163 
2164  if( order == 1 && nBDirs2 == 0 )
2165  copyBackward( Dx_x0, Dx_p, Dx_u, Dx_w, nablaH );
2166 
2167  if( order == 1 && nBDirs2 > 0 )
2168  copyBackward( Dx_x0, Dx_p, Dx_u, Dx_w, nablaH2 );
2169 
2170  if( order == 2 )
2171  copyBackward( Dx_x0, Dx_p, Dx_u, Dx_w, nablaH3 );
2172 
2173  if( order < 1 || order > 2 )
2175 
2176  return SUCCESSFUL_RETURN;
2177 }
2178 
2179 
2180 
2182 
2183  int run1;
2184 
2185  if( dx0 != NULL ){
2186 
2187  for( run1 = 0; run1 < md; run1++ ){
2188  initial_guess[run1] = dx0[run1];
2189  }
2190  }
2191  else{
2192 
2193  for( run1 = 0; run1 < md; run1++ ){
2194  initial_guess[run1] = 0.0;
2195  }
2196  }
2197 
2198  for( run1 = 0; run1 < md; run1++ ){
2199  initial_guess[md+run1] = 0.0;
2200  }
2201 
2202  return SUCCESSFUL_RETURN;
2203 }
2204 
2205 
2207 
2208  return count2-2;
2209 }
2210 
2212 
2213  return count3;
2214 }
2215 
2216 
2217 
2218 
2220 
2221  return h[0];
2222 }
2223 
2224 
2225 
2226 //
2227 // PROTECTED MEMBER FUNCTIONS:
2228 //
2229 
2230 
2232 
2233  int run1;
2234  double pp;
2235 
2237 
2238  if( soa != SOA_FREEZING_ALL ){
2239 
2240  psi_[0] = psi[number_][0];
2241  psi_[1] = psi[number_][1];
2242  psi_[2] = psi[number_][2];
2243  psi_[3] = psi[number_][3];
2244 
2245  psi[number_][3] = psi[number_][2] + h[0];
2246  psi[number_][2] = psi[number_][1] + h[0];
2247  psi[number_][1] = psi[number_][0] + h[0];
2248  psi[number_][0] = h[0];
2249 
2250  gamma[number_][4] = 1.0/psi[number_][0] + 1.0/psi[number_][1]
2251  + 1.0/psi[number_][2] + 1.0/psi[number_][3];
2252  }
2253  else{
2254 
2255  psi[number_][3] = psi[number_-1][2] + h[0];
2256  psi[number_][2] = psi[number_-1][1] + h[0];
2257  psi[number_][1] = psi[number_-1][0] + h[0];
2258  psi[number_][0] = h[0];
2259 
2260  gamma[number_][4] = 1.0/psi[number_][0] + 1.0/psi[number_][1]
2261  + 1.0/psi[number_][2] + 1.0/psi[number_][3];
2262  }
2263  }
2264 
2265  const double scalT = h[0]/rel_time_scale;
2266 
2267  pp = psi[number_][0]*scalT/h[0];
2268  for( run1 = 0; run1 < m; run1++ )
2269  phi(1,run1) = pp*nablaY(1,run1);
2270 
2271  pp *= psi[number_][1]*scalT/h[0];
2272  for( run1 = 0; run1 < m; run1++ )
2273  phi(2,run1) = pp*nablaY(2,run1);
2274 
2275  pp *= psi[number_][2]*scalT/h[0];
2276  for( run1 = 0; run1 < m; run1++ )
2277  phi(3,run1) = pp*nablaY(3,run1);
2278 
2279  pp *= psi[number_][3]*scalT/h[0];
2280  for( run1 = 0; run1 < m; run1++ )
2281  phi(4,run1) = pp*nablaY(4,run1);
2282 
2283 
2284  for( run1 = 0; run1 < m; run1++ )
2285  delta(1,run1) = nablaY(0,run1) + phi(1,run1);
2286 
2287  for( run1 = 0; run1 < m; run1++ )
2288  delta(2,run1) = delta(1,run1) + phi(2,run1);
2289 
2290  for( run1 = 0; run1 < m; run1++ )
2291  delta(3,run1) = delta(2,run1) + phi(3,run1);
2292 
2293  for( run1 = 0; run1 < m; run1++ )
2294  eta[0][run1] = delta(3,run1) + phi(4,run1);
2295 
2296 
2297  for( run1 = 0; run1 < md; run1++ ){
2298 
2299  c2(run1) = - nablaY(0,run1)/psi[number_][0]
2300  - delta (1,run1)/psi[number_][1]
2301  - delta (2,run1)/psi[number_][2]
2302  - delta (3,run1)/psi[number_][3];
2303  }
2304 
2305  returnValue returnvalue;
2306 
2307  correctorTime.start();
2308 
2309  returnvalue = determineCorrector(number_, ini);
2310 
2311  correctorTime.stop();
2312 
2313  if( returnvalue == RET_UNSUCCESSFUL_RETURN_FROM_INTEGRATOR_BDF ){
2314  return -1;
2315  }
2316  if( returnvalue != SUCCESSFUL_RETURN ){
2317  return INFTY;
2318  }
2319 
2320  for( run1 = 0; run1 < m; run1++ ){
2321 
2322  nablaY_(0,run1) = eta[nOfNewtonSteps[number_]][run1];
2323 
2324  nablaY_(1,run1) = nablaY_(0,run1) - nablaY(0,run1);
2325  nablaY_(2,run1) = (nablaY_(1,run1) - nablaY(1,run1)*scalT)*(h[0]/psi[number_][1]);
2326  nablaY_(3,run1) = (nablaY_(2,run1) - nablaY(2,run1)*scalT*scalT)*(h[0]/psi[number_][2]);
2327  nablaY_(4,run1) = (nablaY_(3,run1) - nablaY(3,run1)*scalT*scalT*scalT)*(h[0]/psi[number_][3]);
2328  }
2329 
2330  double EE = EPS;
2331  for( run1 = 0; run1 < md; run1++ ){
2332  if( (eta[0][run1]-nablaY_(0,run1))/diff_scale(run1) >= EE ){
2333  EE = (eta[0][run1]-nablaY_(0,run1))/diff_scale(run1);
2334  }
2335  if( (eta[0][run1]-nablaY_(0,run1))/diff_scale(run1) <= -EE ){
2336  EE = -(eta[0][run1]-nablaY_(0,run1))/diff_scale(run1);
2337  }
2338  }
2339 
2340  return 0.05*EE;
2341 }
2342 
2343 
2345 
2346  int run1, run2;
2347  int newtonsteps;
2348 
2349  double norm1 = 0.0;
2350  double norm2 = 0.0;
2351 
2352  BooleanType COMPUTE_JACOBIAN = BT_FALSE;
2353  BooleanType JACOBIAN_COMPUTED = BT_FALSE;
2354 
2356  COMPUTE_JACOBIAN = ini;
2357  }
2358 
2360  nOfNewtonSteps[stepnumber] = 0;
2361  }
2362 
2363  if( stepnumber > 0 ){
2364  M_index[stepnumber] = M_index[stepnumber-1];
2365  }
2366 
2367  newtonsteps = 0;
2368 
2369  while( newtonsteps < 3 ){
2370 
2371  // evaluate F:
2372  // -----------
2373  x[time_index] = t + h[0];
2374  for( run2 = 0; run2 < m; run2++ ){
2375  x[diff_index[run2]] = eta[newtonsteps][run2];
2376  }
2377  for( run2 = 0; run2 < md; run2++ ){
2378  x[ddiff_index[run2]] = gamma[stepnumber][4]*eta[newtonsteps][run2]+c2(run2);
2379  }
2380 
2382 
2383  if( rhs[0].evaluate( 3*stepnumber+newtonsteps, x, F ) != SUCCESSFUL_RETURN ){
2385  }
2386 
2388 
2389  nFcnEvaluations++;
2391 
2392  if( COMPUTE_JACOBIAN == BT_TRUE ){
2393 
2395 
2396  if( PrintLevel == HIGH ){
2397  cout << "(RE-) COMPUTE-JACOBIAN... \n";
2398  }
2399 
2400  if( soa == SOA_FREEZING_MESH || soa == SOA_FREEZING_ALL ){
2401 
2402  if( nOfM >= maxNM ){
2403  int oldMN = maxNM;
2404  maxNM += maxNM;
2405  M = (DMatrix**)realloc(M, maxNM*sizeof(DMatrix*));
2406  for( run1 = oldMN; run1 < maxNM; run1++ )
2407  M[run1] = 0;
2408  }
2409  M_index[stepnumber] = nOfM;
2410  M[nOfM] = new DMatrix(m,m);
2411  nOfM++;
2412  }
2413  else{
2414  if( M[0] == 0 ) M[0] = new DMatrix(m,m);
2415  M_index[stepnumber] = 0;
2416  M[0]->init(m,m);
2417  }
2418 
2419  for( run1 = 0; run1 < md; run1++ ){
2420  iseed[ddiff_index[run1]] = gamma[stepnumber][4];
2421  iseed[ diff_index[run1]] = 1.0;
2422  if( rhs[0].AD_forward( 3*stepnumber+newtonsteps, iseed,
2423  k2[0][0] ) != SUCCESSFUL_RETURN ){
2425  }
2426  for( run2 = 0; run2 < m; run2++ )
2427  M[M_index[stepnumber]]->operator()(run2,run1) = k2[0][0][run2];
2428 
2429  iseed[ddiff_index[run1]] = 0.0;
2430  iseed[ diff_index[run1]] = 0.0;
2431  }
2432 
2433  for( run1 = 0; run1 < ma; run1++ ){
2434  iseed[ diff_index[md+run1]] = 1.0;
2435  if( rhs[0].AD_forward( 3*stepnumber+newtonsteps, iseed,
2436  k2[0][0] ) != SUCCESSFUL_RETURN ){
2438  }
2439  for( run2 = 0; run2 < m; run2++ )
2440  M[M_index[stepnumber]]->operator()(run2,md+run1) = k2[0][0][run2];
2441 
2442  iseed[diff_index[md+run1]] = 0.0;
2443  }
2444 
2445  nJacEvaluations++;
2446  jacComputation.stop();
2448 
2449  if( decomposeJacobian(M_index[stepnumber], *M[M_index[stepnumber]] ) != SUCCESSFUL_RETURN )
2451 
2453 
2454  JACOBIAN_COMPUTED = BT_TRUE;
2455  COMPUTE_JACOBIAN = BT_FALSE;
2456  }
2457 
2458  norm1 = applyNewtonStep( M_index[stepnumber],
2459  eta[newtonsteps+1],
2460  eta[newtonsteps],
2461  *M[M_index[stepnumber]],
2462  F );
2463 
2465  if( newtonsteps == nOfNewtonSteps[stepnumber] ){
2466  return SUCCESSFUL_RETURN;
2467  }
2468  }
2469 
2470 
2471  double subTOL = 1e-3*TOL;
2472 
2473  if( norm1 < subTOL ){
2474  nOfNewtonSteps[stepnumber] = newtonsteps+1;
2475  return SUCCESSFUL_RETURN;
2476  }
2477 
2478  if( newtonsteps == 0 ){
2479  norm2 = norm1;
2480  }
2481 
2482  if( newtonsteps == 1 && (norm1/norm2 > 0.33 || norm1/norm2 > sqrt(0.33*TOL/norm2)) ){
2483 
2484  if( JACOBIAN_COMPUTED == BT_FALSE ){
2485  COMPUTE_JACOBIAN = BT_TRUE;
2486  newtonsteps = -1 ;
2487  }
2488  }
2489 
2490  if( newtonsteps == 1 ){
2491  norm2 = norm1;
2492  }
2493 
2494  if( newtonsteps == 2 ){
2495 
2496  if( norm1 > 0.33*TOL ){
2497 
2498  if( JACOBIAN_COMPUTED == BT_FALSE ){
2499  COMPUTE_JACOBIAN = BT_TRUE;
2500  newtonsteps = -1 ;
2501  }
2502  else{
2503  return RET_INFO_UNDEFINED;
2504  }
2505  }
2506  else{
2507  nOfNewtonSteps[stepnumber] = newtonsteps+1;
2508  return SUCCESSFUL_RETURN;
2509  }
2510  }
2511 
2512  newtonsteps++;
2513  nOfNewtonSteps[stepnumber] = newtonsteps;
2514  }
2515 
2516  return RET_INFO_UNDEFINED;
2517 }
2518 
2519 
2521 
2522  int run1, run2, run3;
2523  double E = EPS;
2524 
2526  rel_time_scale = h[0];
2527  h[0] = h[1];
2528  }
2529 
2530  // MEMORY REALLOCATION:
2531  // --------------------
2533 
2534  int oldAlloc = maxAlloc;
2535 
2536  if( maxAlloc < 8 ){
2537  maxAlloc = 8;
2538 
2539  psi = (double**)realloc(psi,maxAlloc*sizeof(double*));
2540  gamma = (double**)realloc(gamma,maxAlloc*sizeof(double*));
2541  nOfNewtonSteps = (int*)realloc(nOfNewtonSteps,(maxAlloc)*sizeof(int));
2542 
2543  for( run1 = oldAlloc; run1 < maxAlloc; run1++ ){
2544  nOfNewtonSteps[run1] = 0;
2545  }
2546  for( run1 = oldAlloc; run1 < maxAlloc; run1++ ){
2547 
2548  psi [run1] = (double*)calloc(nstep,sizeof(double));
2549  gamma[run1] = (double*)calloc(nstep,sizeof(double));
2550  }
2551  }
2552 
2553  M_index = (int*)realloc(M_index,maxAlloc*sizeof(int));
2554  h = (double*)realloc(h,maxAlloc*sizeof(double));
2555  }
2556 
2557 
2558  if( soa != SOA_EVERYTHING_FROZEN ){
2559 
2560  returnValue returnvalue;
2561  int step_rejections = 0;
2562 
2563  while( step_rejections < 1 ){
2564 
2565  // determine k:
2566  // ------------
2567  run1 = 0;
2568 
2569  for( run2 = 0; run2 < m; run2++ ){
2570  k[0][run1][run2] = initial_guess[run2];
2571  }
2572 
2573  while( run1 < dim ){
2574 
2575  if( run1 > 0 ){
2576  for( run2 = 0; run2 < m; run2++ ){
2577  k[0][run1][run2] = k[nOfNewtonSteps[run1-1]][run1-1][run2];
2578  }
2579  }
2580 
2581  returnvalue = rk_start_solve(run1);
2582 
2583  if( returnvalue != SUCCESSFUL_RETURN ){
2584 
2585  if( PrintLevel == HIGH ){
2586  cout << "RUNGE-KUTTA STARTER: \n" << scientific
2587  << "STEP REJECTED: error estimate = " << E << endl
2588  << " required local tolerance = " << TOL << endl;
2589  count3++;
2590  }
2591 
2594  }
2595 
2596  if( h[0] <= hmin + EPS ){
2598  }
2599  h[0] = 0.2*h[0];
2600  if( h[0] < hmin ){
2601  h[0] = hmin;
2602  }
2603  run1 = -1;
2604  }
2605 
2606  run1++;
2607  }
2608 
2609 
2610  // save previous eta4:
2611  // ----------------------------------------------
2612 
2613  for( run1 = 0; run1 < m; run1++ ){
2614  eta4_[run1] = eta4[run1];
2615  eta5 [run1] = eta4[run1];
2616  }
2617 
2618  // determine eta4 and eta5:
2619  // ----------------------------------------------
2620 
2621  for( run1 = 0; run1 < dim; run1++ ){
2622  for( run2 = 0; run2 < md; run2++ ){
2623  eta4[run2] = eta4[run2] + b4[run1]*h[0]*k[nOfNewtonSteps[run1]][run1][run2];
2624  eta5[run2] = eta5[run2] + b5[run1]*h[0]*k[nOfNewtonSteps[run1]][run1][run2];
2625  }
2626  }
2627  for( run1 = 0; run1 < ma; run1++ ){
2628  eta4[md+run1] = k[nOfNewtonSteps[dim-1]][dim-1][md+run1];
2629  }
2630 
2631  // determine local error estimate E:
2632  // ----------------------------------------------
2633 
2634  E = EPS;
2635  for( run2 = 0; run2 < md; run2++ ){
2636  if( (eta4[run2]-eta5[run2])/diff_scale(run2) >= E ){
2637  E = (eta4[run2]-eta5[run2])/diff_scale(run2);
2638  }
2639  if( (eta4[run2]-eta5[run2])/diff_scale(run2) <= -E ){
2640  E = (-eta4[run2]+eta5[run2])/diff_scale(run2);
2641  }
2642  }
2643 
2644  if( E < TOL*h[0] || soa == SOA_EVERYTHING_FROZEN || soa == SOA_MESH_FROZEN ){
2645  break;
2646  }
2647  else{
2648 
2649  if( PrintLevel == HIGH ){
2650  cout << "RUNGE-KUTTA STARTER: \n" << scientific
2651  << "STEP REJECTED: error estimate = " << E << endl
2652  << " required local tolerance = " << TOL*h[0] << endl;
2653  count3++;
2654  }
2655 
2656  for( run1 = 0; run1 < m; run1++ ){
2657  eta4[run1] = eta4_[run1];
2658  }
2659  if( h[0] <= hmin + EPS ){
2661  }
2662  h[0] = 0.2*h[0];
2663  if( h[0] < hmin ){
2664  h[0] = hmin;
2665  }
2666  }
2667 
2668  step_rejections++;
2669  }
2670 
2671  }
2672 
2673 
2674  // PROCEED IF THE STEP IS ACCEPTED:
2675  // --------------------------------
2676 
2677  // plan the first step of the BDF method:
2678  // --------------------------------------
2679 
2680  if( soa != SOA_EVERYTHING_FROZEN ){
2681 
2682  for( run1 = 0; run1 < nstep-1; run1++ ){
2683  for( run2 = 0; run2 < md; run2++ ){
2684  nablaY(nstep-2-run1,run2) = eta4_[run2];
2685  for( run3 = 0; run3 <= run1+3; run3++ ){
2686  nablaY(nstep-2-run1,run2) = nablaY(nstep-2-run1,run2) +
2687  h[0]*A[run1+3][run3]*k[nOfNewtonSteps[run3]][run3][run2];
2688  }
2689  }
2690  for( run2 = 0; run2 < ma; run2++ ){
2691  nablaY(nstep-2-run1,md+run2) = k[nOfNewtonSteps[run1+3]][run1+3][md+run2];
2692  }
2693  }
2694 
2695  }
2696 
2697 
2698  // compute forward derivatives if requested:
2699  // ------------------------------------------
2700 
2701  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ){
2702 
2703  if( nBDirs != 0 ){
2705  }
2706 
2708  }
2709 
2710  if( nBDirs > 0 ){
2711 
2712  if( soa != SOA_EVERYTHING_FROZEN ){
2713  return ACADOERROR(RET_NOT_FROZEN);
2714  }
2715  if( nFDirs != 0 || nBDirs2 != 0 || nFDirs2 != 0 ){
2717  }
2719  }
2720  if( nFDirs2 > 0 ){
2721 
2722  if( soa != SOA_EVERYTHING_FROZEN ){
2723  return ACADOERROR(RET_NOT_FROZEN);
2724  }
2725  if( nBDirs != 0 || nBDirs2 != 0 || nFDirs != 1 ){
2727  }
2729  }
2730  if( nBDirs2 > 0 ){
2731 
2732  if( soa != SOA_EVERYTHING_FROZEN ){
2733  return ACADOERROR(RET_NOT_FROZEN);
2734  }
2735  if( nBDirs != 0 || nFDirs2 != 0 || nFDirs != 1 ){
2737  }
2738 
2740  }
2741 
2742  // Printing:
2743  // ---------
2745 
2746  const double hhh = c[3]*h[0];
2747 
2748 
2749  // STORAGE:
2750  // --------
2751  if( soa == SOA_FREEZING_MESH || soa == SOA_FREEZING_ALL ){
2752 
2753  h[1] = h[0];
2754  for( run3 = 3; run3 >= 0; run3-- )
2755  h[2+run3] = hhh;
2756  }
2757 
2758 
2759  int i1 = timeInterval.getFloorIndex( t );
2760  int i2 = timeInterval.getFloorIndex( t + c[6]*h[0] );
2761  int jj;
2762 
2763  for( jj = i1+1; jj <= i2; jj++ ){
2764 
2765  for( run1 = 0; run1 < m; run1++ ){
2766  if( nFDirs == 0 && nBDirs == 0 && nFDirs2 == 0 && nBDirs == 0 ) xStore( jj, run1 ) = nablaY (3,run1);
2767  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ) dxStore( jj, run1 ) = nablaG (3,run1);
2768  if( nFDirs2 > 0 ) ddxStore( jj, run1 ) = nablaG3(3,run1);
2769  }
2770  for( run1 = 0; run1 < mn; run1++ )
2771  iStore( jj, run1 ) = x[rhs->index( VT_INTERMEDIATE_STATE, run1 )];
2772  }
2773 
2774 
2775  // PREPARE MODIFIED DIVIDED DIFFERENCES:
2776  // -------------------------------------
2777  if( soa != SOA_EVERYTHING_FROZEN )
2779 
2780  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 )
2782 
2783  if( nFDirs2 > 0 ){
2786  }
2787 
2788 
2789  if( soa != SOA_EVERYTHING_FROZEN ){
2790 
2791  if( soa != SOA_FREEZING_ALL ){
2792 
2793  psi[7][0] = hhh ;
2794  psi[7][1] = 2.0*hhh;
2795  psi[7][2] = 3.0*hhh;
2796  psi[7][3] = 4.0*hhh;
2797  }
2798  else{
2799 
2800  psi[6][0] = hhh ;
2801  psi[6][1] = 2.0*hhh;
2802  psi[6][2] = 3.0*hhh;
2803  psi[6][3] = 4.0*hhh;
2804  }
2805  }
2806 
2807  // increase the time:
2808  // ----------------------------------------------
2809 
2810  t = t + c[6]*h[0];
2811 
2813 
2814  // determine the new step size:
2815  // ----------------------------------------------
2816 
2817  rel_time_scale = 0.2*h[0];
2818  h[0] = 0.2*h[0]*pow( tune*(TOL*h[0]/E) , 1.0/3.0 );
2819 
2820  if( h[0] > hmax ){
2821  h[0] = hmax;
2822  }
2823  if( h[0] < hmin ){
2824  h[0] = hmin;
2825  }
2826 
2827  if( t + h[0] >= timeInterval.getLastTime() ){
2828  h[0] = timeInterval.getLastTime()-t;
2829  }
2830  }
2831  else{
2832  h[0] = 0.2*h[0];
2833  }
2834 
2835  return SUCCESSFUL_RETURN;
2836 }
2837 
2838 
2840 
2841  int run1, run2, run3;
2842  int newtonsteps;
2843 
2844  double norm1 = 0.0;
2845  double norm2 = 0.0;
2846 
2847  BooleanType COMPUTE_JACOBIAN;
2848  BooleanType JACOBIAN_COMPUTED = BT_FALSE;
2849 
2850  if( stepnumber == 0 && soa != SOA_MESH_FROZEN && soa != SOA_EVERYTHING_FROZEN ){
2851  COMPUTE_JACOBIAN = BT_TRUE;
2852  }
2853  else{
2854  COMPUTE_JACOBIAN = BT_FALSE;
2855  }
2856 
2858  nOfNewtonSteps[stepnumber] = 0;
2859  }
2860 
2861  if( stepnumber > 0 ){
2862  M_index[stepnumber] = M_index[stepnumber-1];
2863  }
2864 
2865  newtonsteps = 0;
2866 
2867  while( newtonsteps < 3 ){
2868 
2869  // evaluate F:
2870  // -----------
2871  x[time_index] = t + c[stepnumber]*h[0];
2872  for( run2 = 0; run2 < md; run2++ ){
2873  x[diff_index[run2]] = eta4[run2];
2874  for( run3 = 0; run3 < stepnumber+1; run3++ ){
2875  x[diff_index[run2]] = x[diff_index[run2]] +
2876  A[stepnumber][run3]*h[0]*k[nOfNewtonSteps[run3]][run3][run2];
2877  }
2878  }
2879  for( run2 = 0; run2 < ma; run2++ ){
2880  x[diff_index[md+run2]] = k[newtonsteps][stepnumber][md+run2];
2881  }
2882  for( run2 = 0; run2 < md; run2++ ){
2883  x[ddiff_index[run2]] = k[newtonsteps][stepnumber][run2];
2884  }
2885 
2887 
2888  if( rhs[0].evaluate( 3*stepnumber+newtonsteps, x, F ) != SUCCESSFUL_RETURN ){
2890  }
2891 
2893 
2894  nFcnEvaluations++;
2896 
2897 
2898  if( COMPUTE_JACOBIAN == BT_TRUE ){
2899 
2901 
2902  if( PrintLevel == HIGH ){
2903  cout << "(RE-) COMPUTE-JACOBIAN... \n";
2904  }
2905 
2906  const double ise = h[0]*A[stepnumber][stepnumber];
2907 
2908  if( soa == SOA_FREEZING_MESH || soa == SOA_FREEZING_ALL ){
2909 
2910  if( nOfM >= maxNM ){
2911  int oldMN = maxNM;
2912  maxNM += maxNM;
2913  M = (DMatrix**)realloc(M, maxNM*sizeof(DMatrix*));
2914  for( run1 = oldMN; run1 < maxNM; run1++ )
2915  M[run1] = 0;
2916  }
2917  M_index[stepnumber] = nOfM;
2918  M[nOfM] = new DMatrix(m,m);
2919  nOfM++;
2920  }
2921  else{
2922  if( M[0] == 0 ) M[0] = new DMatrix(m,m);
2923  M_index[stepnumber] = 0;
2924  M[0]->init(m,m);
2925  }
2926 
2927  for( run1 = 0; run1 < md; run1++ ){
2928  iseed[ddiff_index[run1]] = 1.0;
2929  iseed[ diff_index[run1]] = ise;
2930  if( rhs[0].AD_forward( 3*stepnumber+newtonsteps, iseed,
2931  k2[0][0] ) != SUCCESSFUL_RETURN ){
2933  }
2934 
2935  for( run2 = 0; run2 < m; run2++ )
2936  M[M_index[stepnumber]]->operator()(run2,run1) = k2[0][0][run2];
2937 
2938  iseed[ddiff_index[run1]] = 0.0;
2939  iseed[ diff_index[run1]] = 0.0;
2940  }
2941  for( run1 = 0; run1 < ma; run1++ ){
2942  iseed[ diff_index[md+run1]] = 1.0;
2943  if( rhs[0].AD_forward( 3*stepnumber+newtonsteps, iseed,
2944  k2[0][0] ) != SUCCESSFUL_RETURN ){
2946  }
2947 
2948  for( run2 = 0; run2 < m; run2++ )
2949  M[M_index[stepnumber]]->operator()(run2,md+run1) = k2[0][0][run2];
2950 
2951  iseed[diff_index[md+run1]] = 0.0;
2952  }
2953 
2954  nJacEvaluations++;
2955  jacComputation.stop();
2957 
2958  if( decomposeJacobian(M_index[stepnumber], *M[M_index[stepnumber]] ) != SUCCESSFUL_RETURN )
2960 
2962 
2963  JACOBIAN_COMPUTED = BT_TRUE;
2964  COMPUTE_JACOBIAN = BT_FALSE;
2965  }
2966 
2967  norm1 = applyNewtonStep( M_index[stepnumber],
2968  k[newtonsteps+1][stepnumber],
2969  k[newtonsteps][stepnumber] ,
2970  *M[M_index[stepnumber]],
2971  F );
2972 
2974  if( newtonsteps == nOfNewtonSteps[stepnumber] ){
2975  return SUCCESSFUL_RETURN;
2976  }
2977  }
2978 
2979  double subTOL = 1e-3*TOL;
2980 
2981  if( norm1 < subTOL ){
2982 
2983  nOfNewtonSteps[stepnumber] = newtonsteps+1;
2984  return SUCCESSFUL_RETURN;
2985  }
2986 
2987  if( newtonsteps == 0 ){
2988  norm2 = norm1;
2989  }
2990 
2991  if( newtonsteps == 1 && (norm1/norm2 > 0.33 || norm1/norm2 > sqrt(0.33*TOL/norm2)) ){
2992 
2993  if( JACOBIAN_COMPUTED == BT_FALSE ){
2994  COMPUTE_JACOBIAN = BT_TRUE;
2995  newtonsteps = -1 ;
2996  }
2997  }
2998 
2999  if( newtonsteps == 1 ){
3000  norm2 = norm1;
3001  }
3002 
3003  if( newtonsteps == 2 ){
3004 
3005  if( JACOBIAN_COMPUTED == BT_FALSE ){
3006  COMPUTE_JACOBIAN = BT_TRUE;
3007  newtonsteps = -1 ;
3008  }
3009  else{
3010 
3011  if( norm1 > 0.33*TOL ){
3012  // printf("RK - REJECTION !!!!!! norm1 = %.16e \n", norm1 );
3013  return RET_INFO_UNDEFINED;
3014  }
3015 
3016  nOfNewtonSteps[stepnumber] = newtonsteps+1;
3017  return SUCCESSFUL_RETURN;
3018  }
3019  }
3020 
3021  newtonsteps++;
3022  nOfNewtonSteps[stepnumber] = newtonsteps;
3023  }
3024 
3025  return RET_INFO_UNDEFINED;
3026 }
3027 
3028 
3030 
3031  int run1, run2, run3, newtonsteps;
3032 
3033 
3034  for( run2 = 0; run2 < m; run2++ ){
3035  k[0][0][run2] = 0.0;
3036  }
3037 
3038 
3039  // determine k:
3040  // -----------------------------------------------
3041  for( run1 = 0; run1 < dim; run1++ ){
3042 
3043  if( run1 > 0 ){
3044  for( run2 = 0; run2 < m; run2++ ){
3045  k[0][run1][run2] = k[nOfNewtonSteps[run1-1]][run1-1][run2];
3046  }
3047  }
3048 
3049  newtonsteps = 0;
3050  while( newtonsteps < nOfNewtonSteps[run1] ){
3051 
3052  for( run2 = 0; run2 < md; run2++ ){
3053  G[diff_index[run2]] = etaG[run2];
3054  for( run3 = 0; run3 < run1; run3++ ){
3055  G[diff_index[run2]] = G[diff_index[run2]] +
3056  A[run1][run3]*h[0]*k[nOfNewtonSteps[run3]][run3][run2];
3057  }
3058  G[diff_index[run2]] = G[diff_index[run2]] +
3059  A[run1][run1]*h[0]*k[newtonsteps][run1][run2];
3060  }
3061  for( run2 = 0; run2 < ma; run2++ ){
3062  G[diff_index[md+run2]] = k[newtonsteps][run1][md+run2];
3063  }
3064  for( run2 = 0; run2 < md; run2++ ){
3065  G[ddiff_index[run2]] = k[newtonsteps][run1][run2];
3066  }
3067 
3068  if( rhs[0].AD_forward( 3*run1+newtonsteps, G, F )
3069  != SUCCESSFUL_RETURN ){
3071  return;
3072  }
3073 
3074 
3075  applyNewtonStep( M_index[run1],
3076  k[newtonsteps+1][run1],
3077  k[newtonsteps][run1] ,
3078  *M[M_index[run1]],
3079  F );
3080 
3081  newtonsteps++;
3082  }
3083  }
3084 
3085  // determine nablaG:
3086  // ----------------------------------------------
3087 
3088  for( run1 = 0; run1 < nstep-1; run1++ ){
3089  for( run2 = 0; run2 < md; run2++ ){
3090  nablaG(nstep-2-run1,run2) = etaG[run2];
3091  for( run3 = 0; run3 <= run1+3; run3++ ){
3092  nablaG(nstep-2-run1,run2) = nablaG(nstep-2-run1,run2) +
3093  h[0]*A[run1+3][run3]*k[nOfNewtonSteps[run3]][run3][run2];
3094  }
3095  }
3096  for( run2 = 0; run2 < ma; run2++ ){
3097  nablaG(nstep-2-run1,md+run2) = k[nOfNewtonSteps[run1+3]][run1+3][md+run2];
3098  }
3099  }
3100 }
3101 
3102 
3104 
3105  int run1, run2, run3, newtonsteps;
3106  const double hhh = c[3]*h[0];
3107  int number_;
3108  double *Hstore = new double[ndir];
3109 
3110  const double scalT = rel_time_scale/hhh;
3111 
3112  for( run3 = 0; run3 < 4; run3++ ){
3113  for( run1 = 0; run1 < dim; run1++ ){
3114  for( run2 = 0; run2 < ndir; run2++ ){
3115  l[run3][run1][run2] = 0.0;
3116  }
3117  }
3118  }
3119 
3120  for( run1 = 0; run1 < ndir; run1++ ){
3121  Hstore[run1] = nablaH(0,run1);
3122  }
3123 
3124  for( run1 = 0; run1 < ndir; run1++ ){
3125 
3126  zH[5][run1] = nablaH(3,run1)*scalT*scalT;
3127  zH[4][run1] = nablaH(2,run1)*scalT + zH[5][run1]/(3.0);
3128  zH[3][run1] = - zH[5][run1]/(3.0);
3129  zH[2][run1] = nablaH(1,run1) + zH[4][run1]/(2.0);
3130  zH[1][run1] = (zH[3][run1]-zH[4][run1])/(2.0);
3131  zH[0][run1] = - zH[3][run1]/(2.0);
3132 
3133  nablaH(3,run1) = nablaH(0,run1)*h[0] + zH[2][run1]*(h[0]/hhh);
3134  nablaH(2,run1) = (zH[1][run1]-zH[2][run1])*(h[0]/hhh);
3135  nablaH(1,run1) = (zH[0][run1]-zH[1][run1])*(h[0]/hhh);
3136  nablaH(0,run1) = -zH[0][run1]*(h[0]/hhh);
3137  }
3138 
3139  for( run1 = 0; run1 < ndir; run1++ ){
3140  kH[6][nOfNewtonSteps[6]][run1] = nablaH(3,run1)/h[0];
3141  }
3142  for( run1 = 0; run1 < md; run1++ ){
3143  kH[6][nOfNewtonSteps[6]][diff_index[run1]] =
3144  kH[6][nOfNewtonSteps[6]][diff_index[run1]]*h[0]*A[6][6];
3145  }
3146 
3147  newtonsteps = nOfNewtonSteps[6];
3148  newtonsteps--;
3149 
3150  number_ = 6;
3151 
3152  while( newtonsteps >= 0 ){
3153 
3154  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3155  *M[M_index[number_]],
3156  H );
3157 
3158  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3159  != SUCCESSFUL_RETURN ){
3161  }
3162 
3163 
3164  for( run1 = 0; run1 < ndir; run1++ ){
3165  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3166  - l[newtonsteps][number_][run1];
3167  }
3168  for( run1 = 0; run1 < md; run1++ ){
3169  kH[number_][newtonsteps][diff_index[run1]] =
3170  kH[number_][newtonsteps+1][diff_index[run1]]
3171  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3172  - l[newtonsteps][number_][ddiff_index[run1]];
3173  }
3174 
3175  newtonsteps--;
3176  }
3177 
3178 
3179  for( run1 = 0; run1 < ndir; run1++ ){
3180  kH[5][nOfNewtonSteps[5]][run1] = kH[6][0][run1] + nablaH(2,run1)/h[0];
3181  }
3182  for( run1 = 0; run1 < md; run1++ ){
3183  kH[5][nOfNewtonSteps[5]][diff_index[run1]] = kH[6][0][diff_index[run1]]
3184  + nablaH(3,diff_index[run1])*A[6][5]
3185  + nablaH(2,diff_index[run1])*A[5][5];
3186  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3187  kH[5][nOfNewtonSteps[5]][diff_index[run1]] =
3188  kH[5][nOfNewtonSteps[5]][diff_index[run1]]
3189  -l[run3][number_][diff_index[run1]]*h[0]*A[6][5];
3190  }
3191  }
3192 
3193 
3194  newtonsteps = nOfNewtonSteps[5];
3195  newtonsteps--;
3196 
3197  number_ = 5;
3198 
3199  while( newtonsteps >= 0 ){
3200 
3201  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3202  *M[M_index[number_]],
3203  H );
3204 
3205  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3206  != SUCCESSFUL_RETURN ){
3208  }
3209 
3210 
3211  for( run1 = 0; run1 < ndir; run1++ ){
3212  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3213  - l[newtonsteps][number_][run1];
3214  }
3215  for( run1 = 0; run1 < md; run1++ ){
3216  kH[number_][newtonsteps][diff_index[run1]] =
3217  kH[number_][newtonsteps+1][diff_index[run1]]
3218  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3219  - l[newtonsteps][number_][ddiff_index[run1]];
3220  }
3221 
3222  newtonsteps--;
3223  }
3224 
3225 
3226  for( run1 = 0; run1 < ndir; run1++ ){
3227  kH[4][nOfNewtonSteps[4]][run1] = kH[5][0][run1] + nablaH(1,run1)/h[0];
3228  }
3229  for( run1 = 0; run1 < md; run1++ ){
3230  kH[4][nOfNewtonSteps[4]][diff_index[run1]] = kH[5][0][diff_index[run1]]
3231  + nablaH(3,diff_index[run1])*A[6][4]
3232  + nablaH(2,diff_index[run1])*A[5][4]
3233  + nablaH(1,diff_index[run1])*A[4][4];
3234  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3235  kH[4][nOfNewtonSteps[4]][diff_index[run1]] =
3236  kH[4][nOfNewtonSteps[4]][diff_index[run1]]
3237  -l[run3][5][diff_index[run1]]*h[0]*A[5][4];
3238  }
3239  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3240  kH[4][nOfNewtonSteps[4]][diff_index[run1]] =
3241  kH[4][nOfNewtonSteps[4]][diff_index[run1]]
3242  -l[run3][6][diff_index[run1]]*h[0]*A[6][4];
3243  }
3244  }
3245 
3246 
3247  newtonsteps = nOfNewtonSteps[4];
3248  newtonsteps--;
3249 
3250  number_ = 4;
3251 
3252  while( newtonsteps >= 0 ){
3253 
3254  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3255  *M[M_index[number_]],
3256  H );
3257 
3258  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3259  != SUCCESSFUL_RETURN ){
3261  }
3262 
3263 
3264  for( run1 = 0; run1 < ndir; run1++ ){
3265  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3266  - l[newtonsteps][number_][run1];
3267  }
3268  for( run1 = 0; run1 < md; run1++ ){
3269  kH[number_][newtonsteps][diff_index[run1]] =
3270  kH[number_][newtonsteps+1][diff_index[run1]]
3271  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3272  - l[newtonsteps][number_][ddiff_index[run1]];
3273  }
3274 
3275  newtonsteps--;
3276  }
3277 
3278 
3279  for( run1 = 0; run1 < ndir; run1++ ){
3280  kH[3][nOfNewtonSteps[3]][run1] = kH[4][0][run1] + nablaH(0,run1)/h[0];
3281  }
3282  for( run1 = 0; run1 < md; run1++ ){
3283  kH[3][nOfNewtonSteps[3]][diff_index[run1]] = kH[4][0][diff_index[run1]]
3284  + nablaH(3,diff_index[run1])*A[6][3]
3285  + nablaH(2,diff_index[run1])*A[5][3]
3286  + nablaH(1,diff_index[run1])*A[4][3]
3287  + nablaH(0,diff_index[run1])*A[3][3];
3288  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3289  kH[3][nOfNewtonSteps[3]][diff_index[run1]] =
3290  kH[3][nOfNewtonSteps[3]][diff_index[run1]]
3291  -l[run3][4][diff_index[run1]]*h[0]*A[4][3];
3292  }
3293  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3294  kH[3][nOfNewtonSteps[3]][diff_index[run1]] =
3295  kH[3][nOfNewtonSteps[3]][diff_index[run1]]
3296  -l[run3][5][diff_index[run1]]*h[0]*A[5][3];
3297  }
3298  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3299  kH[3][nOfNewtonSteps[3]][diff_index[run1]] =
3300  kH[3][nOfNewtonSteps[3]][diff_index[run1]]
3301  -l[run3][6][diff_index[run1]]*h[0]*A[6][3];
3302  }
3303  }
3304 
3305 
3306  newtonsteps = nOfNewtonSteps[3];
3307  newtonsteps--;
3308 
3309  number_ = 3;
3310 
3311  while( newtonsteps >= 0 ){
3312 
3313  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3314  *M[M_index[number_]],
3315  H );
3316 
3317  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3318  != SUCCESSFUL_RETURN ){
3320  }
3321 
3322 
3323  for( run1 = 0; run1 < ndir; run1++ ){
3324  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3325  - l[newtonsteps][number_][run1];
3326  }
3327  for( run1 = 0; run1 < md; run1++ ){
3328  kH[number_][newtonsteps][diff_index[run1]] =
3329  kH[number_][newtonsteps+1][diff_index[run1]]
3330  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3331  - l[newtonsteps][number_][ddiff_index[run1]];
3332  }
3333 
3334  newtonsteps--;
3335  }
3336 
3337 
3338  for( run1 = 0; run1 < ndir; run1++ ){
3339  kH[2][nOfNewtonSteps[2]][run1] = kH[3][0][run1];
3340  }
3341  for( run1 = 0; run1 < md; run1++ ){
3342  kH[2][nOfNewtonSteps[2]][diff_index[run1]] = kH[3][0][diff_index[run1]]
3343  + nablaH(3,diff_index[run1])*A[6][2]
3344  + nablaH(2,diff_index[run1])*A[5][2]
3345  + nablaH(1,diff_index[run1])*A[4][2]
3346  + nablaH(0,diff_index[run1])*A[3][2];
3347  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
3348  kH[2][nOfNewtonSteps[2]][diff_index[run1]] =
3349  kH[2][nOfNewtonSteps[2]][diff_index[run1]]
3350  -l[run3][3][diff_index[run1]]*h[0]*A[3][2];
3351  }
3352  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3353  kH[2][nOfNewtonSteps[2]][diff_index[run1]] =
3354  kH[2][nOfNewtonSteps[2]][diff_index[run1]]
3355  -l[run3][4][diff_index[run1]]*h[0]*A[4][2];
3356  }
3357  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3358  kH[2][nOfNewtonSteps[2]][diff_index[run1]] =
3359  kH[2][nOfNewtonSteps[2]][diff_index[run1]]
3360  -l[run3][5][diff_index[run1]]*h[0]*A[5][2];
3361  }
3362  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3363  kH[2][nOfNewtonSteps[2]][diff_index[run1]] =
3364  kH[2][nOfNewtonSteps[2]][diff_index[run1]]
3365  -l[run3][6][diff_index[run1]]*h[0]*A[6][2];
3366  }
3367  }
3368 
3369 
3370  newtonsteps = nOfNewtonSteps[2];
3371  newtonsteps--;
3372 
3373  number_ = 2;
3374 
3375  while( newtonsteps >= 0 ){
3376 
3377  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3378  *M[M_index[number_]],
3379  H );
3380 
3381  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3382  != SUCCESSFUL_RETURN ){
3384  }
3385 
3386 
3387  for( run1 = 0; run1 < ndir; run1++ ){
3388  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3389  - l[newtonsteps][number_][run1];
3390  }
3391  for( run1 = 0; run1 < md; run1++ ){
3392  kH[number_][newtonsteps][diff_index[run1]] =
3393  kH[number_][newtonsteps+1][diff_index[run1]]
3394  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3395  - l[newtonsteps][number_][ddiff_index[run1]];
3396  }
3397 
3398  newtonsteps--;
3399  }
3400 
3401 
3402  for( run1 = 0; run1 < ndir; run1++ ){
3403  kH[1][nOfNewtonSteps[1]][run1] = kH[2][0][run1];
3404  }
3405  for( run1 = 0; run1 < md; run1++ ){
3406  kH[1][nOfNewtonSteps[1]][diff_index[run1]] = kH[2][0][diff_index[run1]]
3407  + nablaH(3,diff_index[run1])*A[6][1]
3408  + nablaH(2,diff_index[run1])*A[5][1]
3409  + nablaH(1,diff_index[run1])*A[4][1]
3410  + nablaH(0,diff_index[run1])*A[3][1];
3411  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
3412  kH[1][nOfNewtonSteps[1]][diff_index[run1]] =
3413  kH[1][nOfNewtonSteps[1]][diff_index[run1]]
3414  -l[run3][2][diff_index[run1]]*h[0]*A[2][1];
3415  }
3416  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
3417  kH[1][nOfNewtonSteps[1]][diff_index[run1]] =
3418  kH[1][nOfNewtonSteps[1]][diff_index[run1]]
3419  -l[run3][3][diff_index[run1]]*h[0]*A[3][1];
3420  }
3421  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3422  kH[1][nOfNewtonSteps[1]][diff_index[run1]] =
3423  kH[1][nOfNewtonSteps[1]][diff_index[run1]]
3424  -l[run3][4][diff_index[run1]]*h[0]*A[4][1];
3425  }
3426  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3427  kH[1][nOfNewtonSteps[1]][diff_index[run1]] =
3428  kH[1][nOfNewtonSteps[1]][diff_index[run1]]
3429  -l[run3][5][diff_index[run1]]*h[0]*A[5][1];
3430  }
3431  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3432  kH[1][nOfNewtonSteps[1]][diff_index[run1]] =
3433  kH[1][nOfNewtonSteps[1]][diff_index[run1]]
3434  -l[run3][6][diff_index[run1]]*h[0]*A[6][1];
3435  }
3436  }
3437 
3438 
3439  newtonsteps = nOfNewtonSteps[1];
3440  newtonsteps--;
3441 
3442  number_ = 1;
3443 
3444  while( newtonsteps >= 0 ){
3445 
3446  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3447  *M[M_index[number_]],
3448  H );
3449 
3450  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3451  != SUCCESSFUL_RETURN ){
3453  }
3454 
3455 
3456  for( run1 = 0; run1 < ndir; run1++ ){
3457  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3458  - l[newtonsteps][number_][run1];
3459  }
3460  for( run1 = 0; run1 < md; run1++ ){
3461  kH[number_][newtonsteps][diff_index[run1]] =
3462  kH[number_][newtonsteps+1][diff_index[run1]]
3463  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3464  - l[newtonsteps][number_][ddiff_index[run1]];
3465  }
3466 
3467  newtonsteps--;
3468  }
3469 
3470 
3471 
3472  for( run1 = 0; run1 < ndir; run1++ ){
3473  kH[0][nOfNewtonSteps[0]][run1] = kH[1][0][run1];
3474  }
3475  for( run1 = 0; run1 < md; run1++ ){
3476  kH[0][nOfNewtonSteps[0]][diff_index[run1]] = kH[1][0][diff_index[run1]]
3477  + nablaH(3,diff_index[run1])*A[6][0]
3478  + nablaH(2,diff_index[run1])*A[5][0]
3479  + nablaH(1,diff_index[run1])*A[4][0]
3480  + nablaH(0,diff_index[run1])*A[3][0];
3481  for( run3 = 0; run3 < nOfNewtonSteps[1]; run3++){
3482  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3483  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3484  -l[run3][1][diff_index[run1]]*h[0]*A[1][0];
3485  }
3486  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
3487  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3488  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3489  -l[run3][2][diff_index[run1]]*h[0]*A[2][0];
3490  }
3491  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
3492  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3493  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3494  -l[run3][3][diff_index[run1]]*h[0]*A[3][0];
3495  }
3496  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3497  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3498  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3499  -l[run3][4][diff_index[run1]]*h[0]*A[4][0];
3500  }
3501  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3502  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3503  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3504  -l[run3][5][diff_index[run1]]*h[0]*A[5][0];
3505  }
3506  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3507  kH[0][nOfNewtonSteps[0]][diff_index[run1]] =
3508  kH[0][nOfNewtonSteps[0]][diff_index[run1]]
3509  -l[run3][6][diff_index[run1]]*h[0]*A[6][0];
3510  }
3511  }
3512 
3513  newtonsteps = nOfNewtonSteps[0];
3514  newtonsteps--;
3515 
3516  number_ = 0;
3517 
3518  while( newtonsteps >= 0 ){
3519 
3520  applyMTranspose( M_index[number_], kH[number_][newtonsteps+1],
3521  *M[M_index[number_]],
3522  H );
3523 
3524  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][number_] )
3525  != SUCCESSFUL_RETURN ){
3527  }
3528 
3529  for( run1 = 0; run1 < ndir; run1++ ){
3530  kH[number_][newtonsteps][run1] = kH[number_][newtonsteps+1][run1]
3531  - l[newtonsteps][number_][run1];
3532  }
3533  for( run1 = 0; run1 < md; run1++ ){
3534  kH[number_][newtonsteps][diff_index[run1]] =
3535  kH[number_][newtonsteps+1][diff_index[run1]]
3536  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3537  - l[newtonsteps][number_][ddiff_index[run1]];
3538  }
3539 
3540  newtonsteps--;
3541  }
3542 
3543  for( run1 = 0; run1 < ndir; run1++ ){
3544  nablaH(0,run1) = Hstore[run1];
3545  }
3546  for( run1 = 0; run1 < ndir; run1++ ){
3547  for( run3 = 0; run3 < nOfNewtonSteps[0]; run3++)
3548  nablaH(0,run1) -= l[run3][0][run1];
3549  for( run3 = 0; run3 < nOfNewtonSteps[1]; run3++)
3550  nablaH(0,run1) -= l[run3][1][run1];
3551  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++)
3552  nablaH(0,run1) -= l[run3][2][run1];
3553  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++)
3554  nablaH(0,run1) -= l[run3][3][run1];
3555  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++)
3556  nablaH(0,run1) -= l[run3][4][run1];
3557  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++)
3558  nablaH(0,run1) -= l[run3][5][run1];
3559  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++)
3560  nablaH(0,run1) -= l[run3][6][run1];
3561  }
3562 
3563  delete[] Hstore;
3564 }
3565 
3566 
3568 
3569  int run1, run2, run3, newtonsteps;
3570 
3571 
3572  for( run2 = 0; run2 < m; run2++ ){
3573  k [0][0][run2] = 0.0;
3574  k2[0][0][run2] = 0.0;
3575  }
3576 
3577 
3578  // determine k:
3579  // -----------------------------------------------
3580  for( run1 = 0; run1 < dim; run1++ ){
3581 
3582  if( run1 > 0 ){
3583  for( run2 = 0; run2 < m; run2++ ){
3584  k [0][run1][run2] = k [nOfNewtonSteps[run1-1]][run1-1][run2];
3585  k2[0][run1][run2] = k2[nOfNewtonSteps[run1-1]][run1-1][run2];
3586  }
3587  }
3588 
3589  newtonsteps = 0;
3590  while( newtonsteps < nOfNewtonSteps[run1] ){
3591 
3592  for( run2 = 0; run2 < md; run2++ ){
3593  G2[diff_index[run2]] = etaG2[run2];
3594  G3[diff_index[run2]] = etaG3[run2];
3595  for( run3 = 0; run3 < run1; run3++ ){
3596  G2[diff_index[run2]] = G2[diff_index[run2]] +
3597  A[run1][run3]*h[0]*k [nOfNewtonSteps[run3]][run3][run2];
3598  G3[diff_index[run2]] = G3[diff_index[run2]] +
3599  A[run1][run3]*h[0]*k2[nOfNewtonSteps[run3]][run3][run2];
3600  }
3601  G2[diff_index[run2]] = G2[diff_index[run2]] +
3602  A[run1][run1]*h[0]*k [newtonsteps][run1][run2];
3603  G3[diff_index[run2]] = G3[diff_index[run2]] +
3604  A[run1][run1]*h[0]*k2[newtonsteps][run1][run2];
3605  }
3606  for( run2 = 0; run2 < ma; run2++ ){
3607  G2[diff_index[md+run2]] = k [newtonsteps][run1][md+run2];
3608  G3[diff_index[md+run2]] = k2[newtonsteps][run1][md+run2];
3609  }
3610  for( run2 = 0; run2 < md; run2++ ){
3611  G2[ddiff_index[run2]] = k [newtonsteps][run1][run2];
3612  G3[ddiff_index[run2]] = k2[newtonsteps][run1][run2];
3613  }
3614 
3615  if( rhs[0].AD_forward2( 3*run1+newtonsteps, G2, G3, F, F2 )
3616  != SUCCESSFUL_RETURN ){
3618  return;
3619  }
3620 
3621 
3622  applyNewtonStep( M_index[run1], k[newtonsteps+1][run1],
3623  k[newtonsteps][run1] ,
3624  *M[M_index[run1]],
3625  F );
3626  applyNewtonStep( M_index[run1], k2[newtonsteps+1][run1],
3627  k2[newtonsteps][run1] ,
3628  *M[M_index[run1]],
3629  F2 );
3630 
3631  newtonsteps++;
3632  }
3633  }
3634 
3635  // determine nablaG 2,3:
3636  // ----------------------------------------------
3637 
3638  for( run1 = 0; run1 < nstep-1; run1++ ){
3639  for( run2 = 0; run2 < md; run2++ ){
3640  nablaG2(nstep-2-run1,run2) = etaG2[run2];
3641  nablaG3(nstep-2-run1,run2) = etaG3[run2];
3642  for( run3 = 0; run3 <= run1+3; run3++ ){
3643  nablaG2(nstep-2-run1,run2) = nablaG2(nstep-2-run1,run2) +
3644  h[0]*A[run1+3][run3]*k [nOfNewtonSteps[run3]][run3][run2];
3645  nablaG3(nstep-2-run1,run2) = nablaG3(nstep-2-run1,run2) +
3646  h[0]*A[run1+3][run3]*k2[nOfNewtonSteps[run3]][run3][run2];
3647  }
3648  }
3649  for( run2 = 0; run2 < ma; run2++ ){
3650  nablaG2(nstep-2-run1,md+run2) = k [nOfNewtonSteps[run1+3]][run1+3][md+run2];
3651  nablaG3(nstep-2-run1,md+run2) = k2[nOfNewtonSteps[run1+3]][run1+3][md+run2];
3652  }
3653  }
3654 }
3655 
3656 
3658 
3659  int run1, run2, run3, run4, newtonsteps;
3660  const double hhh = c[3]*h[0];
3661  int number_;
3662 
3663  double *H1store = new double[ndir];
3664  double *H2store = new double[ndir];
3665 
3666  const double scalT = rel_time_scale/hhh;
3667 
3668  for( run4 = 0; run4 < nBDirs2; run4++ ){
3669 
3670  for( run3 = 0; run3 < 4; run3++ ){
3671  for( run1 = 0; run1 < dim; run1++ ){
3672  for( run2 = 0; run2 < ndir; run2++ ){
3673  l [run3][run1][run2] = 0.0;
3674  l2[run3][run1][run2] = 0.0;
3675  }
3676  }
3677  }
3678 
3679  for( run1 = 0; run1 < ndir; run1++ ){
3680  H1store[run1] = nablaH2(0,run1);
3681  H2store[run1] = nablaH3(0,run1);
3682  }
3683 
3684  for( run1 = 0; run1 < ndir; run1++ ){
3685 
3686  zH2[5][run1] = nablaH2(3,run1)*scalT*scalT;
3687  zH2[4][run1] = nablaH2(2,run1)*scalT + zH2[5][run1]/(3.0);
3688  zH2[3][run1] = - zH2[5][run1]/(3.0);
3689  zH2[2][run1] = nablaH2(1,run1) + zH2[4][run1]/(2.0);
3690  zH2[1][run1] = (zH2[3][run1]-zH2[4][run1])/(2.0);
3691  zH2[0][run1] = - zH2[3][run1]/(2.0);
3692 
3693  zH3[5][run1] = nablaH3(3,run1)*scalT*scalT;
3694  zH3[4][run1] = nablaH3(2,run1)*scalT + zH3[5][run1]/(3.0);
3695  zH3[3][run1] = - zH3[5][run1]/(3.0);
3696  zH3[2][run1] = nablaH3(1,run1) + zH3[4][run1]/(2.0);
3697  zH3[1][run1] = (zH3[3][run1]-zH3[4][run1])/(2.0);
3698  zH3[0][run1] = - zH3[3][run1]/(2.0);
3699 
3700  nablaH2(3,run1) = nablaH2(0,run1)*h[0] + zH2[2][run1]*(h[0]/hhh);
3701  nablaH2(2,run1) = (zH2[1][run1]-zH2[2][run1])*(h[0]/hhh);
3702  nablaH2(1,run1) = (zH2[0][run1]-zH2[1][run1])*(h[0]/hhh);
3703  nablaH2(0,run1) = -zH2[0][run1]*(h[0]/hhh);
3704 
3705  nablaH3(3,run1) = nablaH3(0,run1)*h[0] + zH3[2][run1]*(h[0]/hhh);
3706  nablaH3(2,run1) = (zH3[1][run1]-zH3[2][run1])*(h[0]/hhh);
3707  nablaH3(1,run1) = (zH3[0][run1]-zH3[1][run1])*(h[0]/hhh);
3708  nablaH3(0,run1) = -zH3[0][run1]*(h[0]/hhh);
3709  }
3710 
3711 
3712  for( run1 = 0; run1 < ndir; run1++ ){
3713  kH2[6][nOfNewtonSteps[6]][run1] = nablaH2(3,run1)/h[0];
3714  kH3[6][nOfNewtonSteps[6]][run1] = nablaH3(3,run1)/h[0];
3715  }
3716  for( run1 = 0; run1 < md; run1++ ){
3717  kH2[6][nOfNewtonSteps[6]][diff_index[run1]] =
3718  kH2[6][nOfNewtonSteps[6]][diff_index[run1]]*h[0]*A[6][6];
3719  kH3[6][nOfNewtonSteps[6]][diff_index[run1]] =
3720  kH3[6][nOfNewtonSteps[6]][diff_index[run1]]*h[0]*A[6][6];
3721  }
3722 
3723  newtonsteps = nOfNewtonSteps[6];
3724  newtonsteps--;
3725 
3726  number_ = 6;
3727 
3728  while( newtonsteps >= 0 ){
3729 
3730  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
3731  *M[M_index[number_]],
3732  H2 );
3733 
3734  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
3735  *M[M_index[number_]],
3736  H3 );
3737 
3738 
3739  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
3740  l[newtonsteps][number_], l2[newtonsteps][number_] )
3741  != SUCCESSFUL_RETURN ){
3743  }
3744 
3745 
3746  for( run1 = 0; run1 < ndir; run1++ ){
3747  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
3748  - l[newtonsteps][number_][run1];
3749  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
3750  - l2[newtonsteps][number_][run1];
3751  }
3752  for( run1 = 0; run1 < md; run1++ ){
3753  kH2[number_][newtonsteps][diff_index[run1]] =
3754  kH2[number_][newtonsteps+1][diff_index[run1]]
3755  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3756  - l[newtonsteps][number_][ddiff_index[run1]];
3757  kH3[number_][newtonsteps][diff_index[run1]] =
3758  kH3[number_][newtonsteps+1][diff_index[run1]]
3759  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3760  - l2[newtonsteps][number_][ddiff_index[run1]];
3761  }
3762 
3763  newtonsteps--;
3764  }
3765 
3766  for( run1 = 0; run1 < ndir; run1++ ){
3767  kH2[5][nOfNewtonSteps[5]][run1] = kH2[6][0][run1] + nablaH2(2,run1)/h[0];
3768  kH3[5][nOfNewtonSteps[5]][run1] = kH3[6][0][run1] + nablaH3(2,run1)/h[0];
3769  }
3770  for( run1 = 0; run1 < md; run1++ ){
3771  kH2[5][nOfNewtonSteps[5]][diff_index[run1]] = kH2[6][0][diff_index[run1]]
3772  + nablaH2(3,diff_index[run1])*A[6][5]
3773  + nablaH2(2,diff_index[run1])*A[5][5];
3774  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3775  kH2[5][nOfNewtonSteps[5]][diff_index[run1]] =
3776  kH2[5][nOfNewtonSteps[5]][diff_index[run1]]
3777  -l[run3][number_][diff_index[run1]]*h[0]*A[6][5];
3778  }
3779  kH3[5][nOfNewtonSteps[5]][diff_index[run1]] = kH3[6][0][diff_index[run1]]
3780  + nablaH3(3,diff_index[run1])*A[6][5]
3781  + nablaH3(2,diff_index[run1])*A[5][5];
3782  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3783  kH3[5][nOfNewtonSteps[5]][diff_index[run1]] =
3784  kH3[5][nOfNewtonSteps[5]][diff_index[run1]]
3785  -l2[run3][number_][diff_index[run1]]*h[0]*A[6][5];
3786  }
3787  }
3788 
3789 
3790  newtonsteps = nOfNewtonSteps[5];
3791  newtonsteps--;
3792 
3793  number_ = 5;
3794 
3795  while( newtonsteps >= 0 ){
3796 
3797  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
3798  *M[M_index[number_]],
3799  H2 );
3800  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
3801  *M[M_index[number_]],
3802  H3 );
3803 
3804  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
3805  l[newtonsteps][number_], l2[newtonsteps][number_] )
3806  != SUCCESSFUL_RETURN ){
3808  }
3809 
3810 
3811  for( run1 = 0; run1 < ndir; run1++ ){
3812  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
3813  - l[newtonsteps][number_][run1];
3814  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
3815  - l2[newtonsteps][number_][run1];
3816  }
3817  for( run1 = 0; run1 < md; run1++ ){
3818  kH2[number_][newtonsteps][diff_index[run1]] =
3819  kH2[number_][newtonsteps+1][diff_index[run1]]
3820  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3821  - l[newtonsteps][number_][ddiff_index[run1]];
3822  kH3[number_][newtonsteps][diff_index[run1]] =
3823  kH3[number_][newtonsteps+1][diff_index[run1]]
3824  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3825  - l2[newtonsteps][number_][ddiff_index[run1]];
3826  }
3827 
3828  newtonsteps--;
3829  }
3830 
3831 
3832  for( run1 = 0; run1 < ndir; run1++ ){
3833  kH2[4][nOfNewtonSteps[4]][run1] = kH2[5][0][run1] + nablaH2(1,run1)/h[0];
3834  kH3[4][nOfNewtonSteps[4]][run1] = kH3[5][0][run1] + nablaH3(1,run1)/h[0];
3835  }
3836  for( run1 = 0; run1 < md; run1++ ){
3837  kH2[4][nOfNewtonSteps[4]][diff_index[run1]] = kH2[5][0][diff_index[run1]]
3838  + nablaH2(3,diff_index[run1])*A[6][4]
3839  + nablaH2(2,diff_index[run1])*A[5][4]
3840  + nablaH2(1,diff_index[run1])*A[4][4];
3841  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3842  kH2[4][nOfNewtonSteps[4]][diff_index[run1]] =
3843  kH2[4][nOfNewtonSteps[4]][diff_index[run1]]
3844  -l[run3][5][diff_index[run1]]*h[0]*A[5][4];
3845  }
3846  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3847  kH2[4][nOfNewtonSteps[4]][diff_index[run1]] =
3848  kH2[4][nOfNewtonSteps[4]][diff_index[run1]]
3849  -l[run3][6][diff_index[run1]]*h[0]*A[6][4];
3850  }
3851  kH3[4][nOfNewtonSteps[4]][diff_index[run1]] = kH3[5][0][diff_index[run1]]
3852  + nablaH3(3,diff_index[run1])*A[6][4]
3853  + nablaH3(2,diff_index[run1])*A[5][4]
3854  + nablaH3(1,diff_index[run1])*A[4][4];
3855  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3856  kH3[4][nOfNewtonSteps[4]][diff_index[run1]] =
3857  kH3[4][nOfNewtonSteps[4]][diff_index[run1]]
3858  -l2[run3][5][diff_index[run1]]*h[0]*A[5][4];
3859  }
3860  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3861  kH3[4][nOfNewtonSteps[4]][diff_index[run1]] =
3862  kH3[4][nOfNewtonSteps[4]][diff_index[run1]]
3863  -l2[run3][6][diff_index[run1]]*h[0]*A[6][4];
3864  }
3865  }
3866 
3867 
3868  newtonsteps = nOfNewtonSteps[4];
3869  newtonsteps--;
3870 
3871  number_ = 4;
3872 
3873  while( newtonsteps >= 0 ){
3874 
3875  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
3876  *M[M_index[number_]],
3877  H2 );
3878  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
3879  *M[M_index[number_]],
3880  H3 );
3881 
3882  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
3883  l[newtonsteps][number_], l2[newtonsteps][number_] )
3884  != SUCCESSFUL_RETURN ){
3886  }
3887 
3888 
3889  for( run1 = 0; run1 < ndir; run1++ ){
3890  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
3891  - l[newtonsteps][number_][run1];
3892  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
3893  - l2[newtonsteps][number_][run1];
3894  }
3895  for( run1 = 0; run1 < md; run1++ ){
3896  kH2[number_][newtonsteps][diff_index[run1]] =
3897  kH2[number_][newtonsteps+1][diff_index[run1]]
3898  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3899  - l[newtonsteps][number_][ddiff_index[run1]];
3900  kH3[number_][newtonsteps][diff_index[run1]] =
3901  kH3[number_][newtonsteps+1][diff_index[run1]]
3902  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3903  - l2[newtonsteps][number_][ddiff_index[run1]];
3904  }
3905 
3906  newtonsteps--;
3907  }
3908 
3909 
3910  for( run1 = 0; run1 < ndir; run1++ ){
3911  kH2[3][nOfNewtonSteps[3]][run1] = kH2[4][0][run1] + nablaH2(0,run1)/h[0];
3912  kH3[3][nOfNewtonSteps[3]][run1] = kH3[4][0][run1] + nablaH3(0,run1)/h[0];
3913  }
3914  for( run1 = 0; run1 < md; run1++ ){
3915  kH2[3][nOfNewtonSteps[3]][diff_index[run1]] = kH2[4][0][diff_index[run1]]
3916  + nablaH2(3,diff_index[run1])*A[6][3]
3917  + nablaH2(2,diff_index[run1])*A[5][3]
3918  + nablaH2(1,diff_index[run1])*A[4][3]
3919  + nablaH2(0,diff_index[run1])*A[3][3];
3920  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3921  kH2[3][nOfNewtonSteps[3]][diff_index[run1]] =
3922  kH2[3][nOfNewtonSteps[3]][diff_index[run1]]
3923  -l[run3][4][diff_index[run1]]*h[0]*A[4][3];
3924  }
3925  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3926  kH2[3][nOfNewtonSteps[3]][diff_index[run1]] =
3927  kH2[3][nOfNewtonSteps[3]][diff_index[run1]]
3928  -l[run3][5][diff_index[run1]]*h[0]*A[5][3];
3929  }
3930  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3931  kH2[3][nOfNewtonSteps[3]][diff_index[run1]] =
3932  kH2[3][nOfNewtonSteps[3]][diff_index[run1]]
3933  -l[run3][6][diff_index[run1]]*h[0]*A[6][3];
3934  }
3935  kH3[3][nOfNewtonSteps[3]][diff_index[run1]] = kH3[4][0][diff_index[run1]]
3936  + nablaH3(3,diff_index[run1])*A[6][3]
3937  + nablaH3(2,diff_index[run1])*A[5][3]
3938  + nablaH3(1,diff_index[run1])*A[4][3]
3939  + nablaH3(0,diff_index[run1])*A[3][3];
3940  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
3941  kH3[3][nOfNewtonSteps[3]][diff_index[run1]] =
3942  kH3[3][nOfNewtonSteps[3]][diff_index[run1]]
3943  -l2[run3][4][diff_index[run1]]*h[0]*A[4][3];
3944  }
3945  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
3946  kH3[3][nOfNewtonSteps[3]][diff_index[run1]] =
3947  kH3[3][nOfNewtonSteps[3]][diff_index[run1]]
3948  -l2[run3][5][diff_index[run1]]*h[0]*A[5][3];
3949  }
3950  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
3951  kH3[3][nOfNewtonSteps[3]][diff_index[run1]] =
3952  kH3[3][nOfNewtonSteps[3]][diff_index[run1]]
3953  -l2[run3][6][diff_index[run1]]*h[0]*A[6][3];
3954  }
3955  }
3956 
3957 
3958  newtonsteps = nOfNewtonSteps[3];
3959  newtonsteps--;
3960 
3961  number_ = 3;
3962 
3963  while( newtonsteps >= 0 ){
3964 
3965  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
3966  *M[M_index[number_]],
3967  H2 );
3968  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
3969  *M[M_index[number_]],
3970  H3 );
3971 
3972  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
3973  l[newtonsteps][number_], l2[newtonsteps][number_] )
3974  != SUCCESSFUL_RETURN ){
3976  }
3977 
3978 
3979  for( run1 = 0; run1 < ndir; run1++ ){
3980  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
3981  - l[newtonsteps][number_][run1];
3982  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
3983  - l2[newtonsteps][number_][run1];
3984  }
3985  for( run1 = 0; run1 < md; run1++ ){
3986  kH2[number_][newtonsteps][diff_index[run1]] =
3987  kH2[number_][newtonsteps+1][diff_index[run1]]
3988  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3989  - l[newtonsteps][number_][ddiff_index[run1]];
3990  kH3[number_][newtonsteps][diff_index[run1]] =
3991  kH3[number_][newtonsteps+1][diff_index[run1]]
3992  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
3993  - l2[newtonsteps][number_][ddiff_index[run1]];
3994  }
3995 
3996  newtonsteps--;
3997  }
3998 
3999 
4000  for( run1 = 0; run1 < ndir; run1++ ){
4001  kH2[2][nOfNewtonSteps[2]][run1] = kH2[3][0][run1];
4002  kH3[2][nOfNewtonSteps[2]][run1] = kH3[3][0][run1];
4003  }
4004  for( run1 = 0; run1 < md; run1++ ){
4005  kH2[2][nOfNewtonSteps[2]][diff_index[run1]] = kH2[3][0][diff_index[run1]]
4006  + nablaH2(3,diff_index[run1])*A[6][2]
4007  + nablaH2(2,diff_index[run1])*A[5][2]
4008  + nablaH2(1,diff_index[run1])*A[4][2]
4009  + nablaH2(0,diff_index[run1])*A[3][2];
4010  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4011  kH2[2][nOfNewtonSteps[2]][diff_index[run1]] =
4012  kH2[2][nOfNewtonSteps[2]][diff_index[run1]]
4013  -l[run3][3][diff_index[run1]]*h[0]*A[3][2];
4014  }
4015  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4016  kH2[2][nOfNewtonSteps[2]][diff_index[run1]] =
4017  kH2[2][nOfNewtonSteps[2]][diff_index[run1]]
4018  -l[run3][4][diff_index[run1]]*h[0]*A[4][2];
4019  }
4020  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4021  kH2[2][nOfNewtonSteps[2]][diff_index[run1]] =
4022  kH2[2][nOfNewtonSteps[2]][diff_index[run1]]
4023  -l[run3][5][diff_index[run1]]*h[0]*A[5][2];
4024  }
4025  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4026  kH2[2][nOfNewtonSteps[2]][diff_index[run1]] =
4027  kH2[2][nOfNewtonSteps[2]][diff_index[run1]]
4028  -l[run3][6][diff_index[run1]]*h[0]*A[6][2];
4029  }
4030  kH3[2][nOfNewtonSteps[2]][diff_index[run1]] = kH3[3][0][diff_index[run1]]
4031  + nablaH3(3,diff_index[run1])*A[6][2]
4032  + nablaH3(2,diff_index[run1])*A[5][2]
4033  + nablaH3(1,diff_index[run1])*A[4][2]
4034  + nablaH3(0,diff_index[run1])*A[3][2];
4035  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4036  kH3[2][nOfNewtonSteps[2]][diff_index[run1]] =
4037  kH3[2][nOfNewtonSteps[2]][diff_index[run1]]
4038  -l2[run3][3][diff_index[run1]]*h[0]*A[3][2];
4039  }
4040  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4041  kH3[2][nOfNewtonSteps[2]][diff_index[run1]] =
4042  kH3[2][nOfNewtonSteps[2]][diff_index[run1]]
4043  -l2[run3][4][diff_index[run1]]*h[0]*A[4][2];
4044  }
4045  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4046  kH3[2][nOfNewtonSteps[2]][diff_index[run1]] =
4047  kH3[2][nOfNewtonSteps[2]][diff_index[run1]]
4048  -l2[run3][5][diff_index[run1]]*h[0]*A[5][2];
4049  }
4050  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4051  kH3[2][nOfNewtonSteps[2]][diff_index[run1]] =
4052  kH3[2][nOfNewtonSteps[2]][diff_index[run1]]
4053  -l2[run3][6][diff_index[run1]]*h[0]*A[6][2];
4054  }
4055  }
4056 
4057 
4058  newtonsteps = nOfNewtonSteps[2];
4059  newtonsteps--;
4060 
4061  number_ = 2;
4062 
4063  while( newtonsteps >= 0 ){
4064 
4065  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
4066  *M[M_index[number_]],
4067  H2 );
4068  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
4069  *M[M_index[number_]],
4070  H3 );
4071 
4072  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
4073  l[newtonsteps][number_], l2[newtonsteps][number_] )
4074  != SUCCESSFUL_RETURN ){
4076  }
4077 
4078 
4079  for( run1 = 0; run1 < ndir; run1++ ){
4080  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
4081  - l[newtonsteps][number_][run1];
4082  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
4083  - l2[newtonsteps][number_][run1];
4084  }
4085  for( run1 = 0; run1 < md; run1++ ){
4086  kH2[number_][newtonsteps][diff_index[run1]] =
4087  kH2[number_][newtonsteps+1][diff_index[run1]]
4088  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4089  - l[newtonsteps][number_][ddiff_index[run1]];
4090  kH3[number_][newtonsteps][diff_index[run1]] =
4091  kH3[number_][newtonsteps+1][diff_index[run1]]
4092  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4093  - l2[newtonsteps][number_][ddiff_index[run1]];
4094  }
4095 
4096  newtonsteps--;
4097  }
4098 
4099 
4100  for( run1 = 0; run1 < ndir; run1++ ){
4101  kH2[1][nOfNewtonSteps[1]][run1] = kH2[2][0][run1];
4102  kH3[1][nOfNewtonSteps[1]][run1] = kH3[2][0][run1];
4103  }
4104  for( run1 = 0; run1 < md; run1++ ){
4105  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] = kH2[2][0][diff_index[run1]]
4106  + nablaH2(3,diff_index[run1])*A[6][1]
4107  + nablaH2(2,diff_index[run1])*A[5][1]
4108  + nablaH2(1,diff_index[run1])*A[4][1]
4109  + nablaH2(0,diff_index[run1])*A[3][1];
4110  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
4111  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] =
4112  kH2[1][nOfNewtonSteps[1]][diff_index[run1]]
4113  -l[run3][2][diff_index[run1]]*h[0]*A[2][1];
4114  }
4115  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4116  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] =
4117  kH2[1][nOfNewtonSteps[1]][diff_index[run1]]
4118  -l[run3][3][diff_index[run1]]*h[0]*A[3][1];
4119  }
4120  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4121  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] =
4122  kH2[1][nOfNewtonSteps[1]][diff_index[run1]]
4123  -l[run3][4][diff_index[run1]]*h[0]*A[4][1];
4124  }
4125  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4126  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] =
4127  kH2[1][nOfNewtonSteps[1]][diff_index[run1]]
4128  -l[run3][5][diff_index[run1]]*h[0]*A[5][1];
4129  }
4130  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4131  kH2[1][nOfNewtonSteps[1]][diff_index[run1]] =
4132  kH2[1][nOfNewtonSteps[1]][diff_index[run1]]
4133  -l[run3][6][diff_index[run1]]*h[0]*A[6][1];
4134  }
4135  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] = kH3[2][0][diff_index[run1]]
4136  + nablaH3(3,diff_index[run1])*A[6][1]
4137  + nablaH3(2,diff_index[run1])*A[5][1]
4138  + nablaH3(1,diff_index[run1])*A[4][1]
4139  + nablaH3(0,diff_index[run1])*A[3][1];
4140  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
4141  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] =
4142  kH3[1][nOfNewtonSteps[1]][diff_index[run1]]
4143  -l2[run3][2][diff_index[run1]]*h[0]*A[2][1];
4144  }
4145  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4146  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] =
4147  kH3[1][nOfNewtonSteps[1]][diff_index[run1]]
4148  -l2[run3][3][diff_index[run1]]*h[0]*A[3][1];
4149  }
4150  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4151  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] =
4152  kH3[1][nOfNewtonSteps[1]][diff_index[run1]]
4153  -l2[run3][4][diff_index[run1]]*h[0]*A[4][1];
4154  }
4155  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4156  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] =
4157  kH3[1][nOfNewtonSteps[1]][diff_index[run1]]
4158  -l2[run3][5][diff_index[run1]]*h[0]*A[5][1];
4159  }
4160  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4161  kH3[1][nOfNewtonSteps[1]][diff_index[run1]] =
4162  kH3[1][nOfNewtonSteps[1]][diff_index[run1]]
4163  -l2[run3][6][diff_index[run1]]*h[0]*A[6][1];
4164  }
4165  }
4166 
4167 
4168  newtonsteps = nOfNewtonSteps[1];
4169  newtonsteps--;
4170 
4171  number_ = 1;
4172 
4173  while( newtonsteps >= 0 ){
4174 
4175  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
4176  *M[M_index[number_]],
4177  H2 );
4178  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
4179  *M[M_index[number_]],
4180  H3 );
4181 
4182  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
4183  l[newtonsteps][number_], l2[newtonsteps][number_] )
4184  != SUCCESSFUL_RETURN ){
4186  }
4187 
4188 
4189  for( run1 = 0; run1 < ndir; run1++ ){
4190  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
4191  - l[newtonsteps][number_][run1];
4192  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
4193  - l2[newtonsteps][number_][run1];
4194  }
4195  for( run1 = 0; run1 < md; run1++ ){
4196  kH2[number_][newtonsteps][diff_index[run1]] =
4197  kH2[number_][newtonsteps+1][diff_index[run1]]
4198  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4199  - l[newtonsteps][number_][ddiff_index[run1]];
4200  kH3[number_][newtonsteps][diff_index[run1]] =
4201  kH3[number_][newtonsteps+1][diff_index[run1]]
4202  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4203  - l2[newtonsteps][number_][ddiff_index[run1]];
4204  }
4205 
4206  newtonsteps--;
4207  }
4208 
4209 
4210 
4211  for( run1 = 0; run1 < ndir; run1++ ){
4212  kH2[0][nOfNewtonSteps[0]][run1] = kH2[1][0][run1];
4213  kH3[0][nOfNewtonSteps[0]][run1] = kH3[1][0][run1];
4214  }
4215  for( run1 = 0; run1 < md; run1++ ){
4216  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] = kH2[1][0][diff_index[run1]]
4217  + nablaH2(3,diff_index[run1])*A[6][0]
4218  + nablaH2(2,diff_index[run1])*A[5][0]
4219  + nablaH2(1,diff_index[run1])*A[4][0]
4220  + nablaH2(0,diff_index[run1])*A[3][0];
4221  for( run3 = 0; run3 < nOfNewtonSteps[1]; run3++){
4222  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4223  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4224  -l[run3][1][diff_index[run1]]*h[0]*A[1][0];
4225  }
4226  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
4227  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4228  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4229  -l[run3][2][diff_index[run1]]*h[0]*A[2][0];
4230  }
4231  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4232  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4233  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4234  -l[run3][3][diff_index[run1]]*h[0]*A[3][0];
4235  }
4236  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4237  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4238  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4239  -l[run3][4][diff_index[run1]]*h[0]*A[4][0];
4240  }
4241  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4242  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4243  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4244  -l[run3][5][diff_index[run1]]*h[0]*A[5][0];
4245  }
4246  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4247  kH2[0][nOfNewtonSteps[0]][diff_index[run1]] =
4248  kH2[0][nOfNewtonSteps[0]][diff_index[run1]]
4249  -l[run3][6][diff_index[run1]]*h[0]*A[6][0];
4250  }
4251  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] = kH3[1][0][diff_index[run1]]
4252  + nablaH3(3,diff_index[run1])*A[6][0]
4253  + nablaH3(2,diff_index[run1])*A[5][0]
4254  + nablaH3(1,diff_index[run1])*A[4][0]
4255  + nablaH3(0,diff_index[run1])*A[3][0];
4256  for( run3 = 0; run3 < nOfNewtonSteps[1]; run3++){
4257  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4258  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4259  -l2[run3][1][diff_index[run1]]*h[0]*A[1][0];
4260  }
4261  for( run3 = 0; run3 < nOfNewtonSteps[2]; run3++){
4262  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4263  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4264  -l2[run3][2][diff_index[run1]]*h[0]*A[2][0];
4265  }
4266  for( run3 = 0; run3 < nOfNewtonSteps[3]; run3++){
4267  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4268  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4269  -l2[run3][3][diff_index[run1]]*h[0]*A[3][0];
4270  }
4271  for( run3 = 0; run3 < nOfNewtonSteps[4]; run3++){
4272  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4273  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4274  -l2[run3][4][diff_index[run1]]*h[0]*A[4][0];
4275  }
4276  for( run3 = 0; run3 < nOfNewtonSteps[5]; run3++){
4277  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4278  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4279  -l2[run3][5][diff_index[run1]]*h[0]*A[5][0];
4280  }
4281  for( run3 = 0; run3 < nOfNewtonSteps[6]; run3++){
4282  kH3[0][nOfNewtonSteps[0]][diff_index[run1]] =
4283  kH3[0][nOfNewtonSteps[0]][diff_index[run1]]
4284  -l2[run3][6][diff_index[run1]]*h[0]*A[6][0];
4285  }
4286  }
4287 
4288  newtonsteps = nOfNewtonSteps[0];
4289  newtonsteps--;
4290 
4291  number_ = 0;
4292 
4293  while( newtonsteps >= 0 ){
4294 
4295  applyMTranspose( M_index[number_], kH2[number_][newtonsteps+1],
4296  *M[M_index[number_]],
4297  H2 );
4298  applyMTranspose( M_index[number_], kH3[number_][newtonsteps+1],
4299  *M[M_index[number_]],
4300  H3 );
4301 
4302  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
4303  l[newtonsteps][number_], l2[newtonsteps][number_] )
4304  != SUCCESSFUL_RETURN ){
4306  }
4307 
4308 
4309  for( run1 = 0; run1 < ndir; run1++ ){
4310  kH2[number_][newtonsteps][run1] = kH2[number_][newtonsteps+1][run1]
4311  - l[newtonsteps][number_][run1];
4312  kH3[number_][newtonsteps][run1] = kH3[number_][newtonsteps+1][run1]
4313  - l2[newtonsteps][number_][run1];
4314  }
4315  for( run1 = 0; run1 < md; run1++ ){
4316  kH2[number_][newtonsteps][diff_index[run1]] =
4317  kH2[number_][newtonsteps+1][diff_index[run1]]
4318  - l[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4319  - l[newtonsteps][number_][ddiff_index[run1]];
4320  kH3[number_][newtonsteps][diff_index[run1]] =
4321  kH3[number_][newtonsteps+1][diff_index[run1]]
4322  - l2[newtonsteps][number_][diff_index[run1]]*h[0]*A[number_][number_]
4323  - l2[newtonsteps][number_][ddiff_index[run1]];
4324  }
4325 
4326  newtonsteps--;
4327  }
4328 
4329 
4330  for( run1 = 0; run1 < ndir; run1++ ){
4331  nablaH2(0,run1) = H1store[run1];
4332  nablaH3(0,run1) = H2store[run1];
4333  }
4334 
4335  for( run1 = 0; run1 < ndir; run1++ ){
4336  for( run3 = 0; run3 < nOfNewtonSteps[0]; run3++){
4337  for( run2 = 0; run2 < dim; run2++ ){
4338  nablaH2(0,run1) = nablaH2(0,run1)
4339  -l[run3][run2][run1];
4340  nablaH3(0,run1) = nablaH3(0,run1)
4341  -l2[run3][run2][run1];
4342  }
4343  }
4344  }
4345 
4346  }
4347  delete[] H1store;
4348  delete[] H2store;
4349 }
4350 
4351 
4352 
4354 
4355  int run1, run2, run4;
4356  int newtonsteps;
4357  double pp;
4358 
4359  const double scalT = psi[number_][0]/rel_time_scale;
4360 
4361  for( run4 = 0; run4 < nFDirs; run4++ ){
4362 
4363  for( run1 = 0; run1 < m; run1++ ){
4364  phiG(1,run1) = nablaG(1,run1)*scalT;
4365  }
4366 
4367  pp = (psi[number_][1]/psi[number_][0]);
4368  for( run1 = 0; run1 < m; run1++ ){
4369  phiG(2,run1) = pp*nablaG(2,run1)*scalT*scalT;
4370  }
4371 
4372  pp = pp*(psi[number_][2]/psi[number_][0]);
4373  for( run1 = 0; run1 < m; run1++ ){
4374  phiG(3,run1) = pp*nablaG(3,run1)*scalT*scalT*scalT;
4375  }
4376 
4377  pp = pp*(psi[number_][3]/psi[number_][0]);
4378  for( run1 = 0; run1 < m; run1++ ){
4379  phiG(4,run1) = pp*nablaG(4,run1)*scalT*scalT*scalT*scalT;
4380  }
4381 
4382  for( run1 = 0; run1 < m; run1++ ){
4383  deltaG(1,run1) = nablaG(0,run1) + phiG(1,run1);
4384  }
4385 
4386  for( run1 = 0; run1 < m; run1++ ){
4387  deltaG(2,run1) = deltaG(1,run1) + phiG(2,run1);
4388  }
4389 
4390  for( run1 = 0; run1 < m; run1++ ){
4391  deltaG(3,run1) = deltaG(2,run1) + phiG(3,run1);
4392  }
4393 
4394  for( run1 = 0; run1 < m; run1++ ){
4395  eta[0][run1] = deltaG(3,run1) + phiG(4,run1);
4396  }
4397 
4398 
4399  for( run1 = 0; run1 < md; run1++ ){
4400 
4401  c2G(run1) = - nablaG (0,run1)/psi[number_][0]
4402  - deltaG (1,run1)/psi[number_][1]
4403  - deltaG (2,run1)/psi[number_][2]
4404  - deltaG (3,run1)/psi[number_][3];
4405  }
4406 
4407 
4408  newtonsteps = 0;
4409  while( newtonsteps < nOfNewtonSteps[number_] ){
4410  for( run2 = 0; run2 < m; run2++ ){
4411  G[diff_index[run2]] = eta[newtonsteps][run2];
4412  }
4413  for( run2 = 0; run2 < md; run2++ ){
4414  G[ddiff_index[run2]] =
4415  gamma[number_][4]*eta[newtonsteps][run2]+c2G(run2);
4416  }
4417 
4418  if( rhs[0].AD_forward( 3*number_+newtonsteps, G, F )
4419  != SUCCESSFUL_RETURN ){
4421  }
4422 
4423  applyNewtonStep( M_index[number_], eta[newtonsteps+1],
4424  eta[newtonsteps],
4425  *M[M_index[number_]],
4426  F );
4427 
4428  newtonsteps++;
4429  }
4430 
4431  for( run1 = 0; run1 < m; run1++ ){
4432  nablaY_(0,run1) = eta[nOfNewtonSteps[number_]][run1];
4433  nablaY_(1,run1) = nablaY_(0,run1) - nablaG(0,run1);
4434  nablaY_(2,run1) = (nablaY_(1,run1) - nablaG(1,run1)*scalT)*(psi[number_][0]/psi[number_][1]);
4435  nablaY_(3,run1) = (nablaY_(2,run1) - nablaG(2,run1)*scalT*scalT)*(psi[number_][0]/psi[number_][2]);
4436  nablaY_(4,run1) = (nablaY_(3,run1) - nablaG(3,run1)*scalT*scalT*scalT)*(psi[number_][0]/psi[number_][3]);
4437  }
4438 
4439  nablaG = nablaY_;
4440  }
4441 }
4442 
4443 
4445 
4446  int run1, run2;
4447  int newtonsteps;
4448  double pp;
4449 
4450  const double scalT = rel_time_scale/psi[number_][0];
4451 
4452  newtonsteps = nOfNewtonSteps[number_];
4453 
4454  for( run1 = 0; run1 < newtonsteps; run1++ ){
4455  for( run2 = 0; run2 < ndir; run2++ ){
4456  l[run1][0][run2] = 0.0;
4457  }
4458  }
4459 
4460  for( run1 = 0; run1 < ndir; run1++ ){
4461 
4462  nablaH_(4,run1) = nablaH(4,run1)*scalT*scalT*scalT;
4463  nablaH_(3,run1) = nablaH(3,run1)*scalT*scalT + nablaH_(4,run1)*(psi[number_][0]/psi[number_][3]);
4464  nablaH_(2,run1) = nablaH(2,run1)*scalT + nablaH_(3,run1)*(psi[number_][0]/psi[number_][2]);
4465  nablaH_(1,run1) = nablaH(1,run1) + nablaH_(2,run1)*(psi[number_][0]/psi[number_][1]);
4466  nablaH_(0,run1) = nablaH(0,run1) + nablaH_(1,run1)/psi[number_][0];
4467 
4468  etaH[newtonsteps][run1] = nablaH_(0,run1);
4469  c2H(run1) = 0.0;
4470  }
4471 
4472  newtonsteps--;
4473  while( newtonsteps >= 0 ){
4474 
4475  applyMTranspose( M_index[number_], etaH[newtonsteps+1], M[M_index[number_]][0], H );
4476 
4477  if( rhs[0].AD_backward( 3*number_+newtonsteps, H, l[newtonsteps][0] ) != SUCCESSFUL_RETURN )
4479 
4480  for( run1 = 0; run1 < ndir; run1++ ){
4481  etaH[newtonsteps][run1] = etaH[newtonsteps+1][run1] - l[newtonsteps][0][run1];
4482  }
4483 
4484  for( run1 = 0; run1 < md; run1++ )
4485  etaH[newtonsteps][diff_index[run1]] -= l[newtonsteps][0][ddiff_index[run1]]*gamma[number_][4];
4486 
4487  for( run1 = 0; run1 < md; run1++ )
4488  c2H(diff_index[run1]) -= l[newtonsteps][0][ddiff_index[run1]];
4489 
4490  newtonsteps--;
4491  }
4492 
4493 
4494  for( run1 = 0; run1 < ndir; run1++ ){
4495 
4496  deltaH(3,run1) = etaH[0][run1] - c2H(run1)/psi[number_][3];
4497  deltaH(2,run1) = deltaH(3,run1) - c2H(run1)/psi[number_][2];
4498  deltaH(1,run1) = deltaH(2,run1) - c2H(run1)/psi[number_][1];
4499  }
4500 
4501  for( run1 = 0; run1 < ndir; run1++ ){
4502 
4503  nablaH(0,run1) = - nablaH_(1,run1) /psi[number_][0]
4504  - c2H(run1) /psi[number_][0]
4505  + deltaH(1,run1);
4506  }
4507  pp = psi[number_][0];
4508  for( run1 = 0; run1 < ndir; run1++ ){
4509  nablaH(1,run1) = - nablaH_(2,run1)*(psi[number_][0]/psi[number_][1])
4510  + pp*deltaH(1,run1);
4511  }
4512  pp = pp*(psi[number_][1]/psi[number_][0]);
4513  for( run1 = 0; run1 < ndir; run1++ ){
4514  nablaH(2,run1) = - nablaH_(3,run1)*(psi[number_][0]/psi[number_][2])
4515  + pp*deltaH(2,run1);
4516  }
4517  pp = pp*(psi[number_][2]/psi[number_][0]);
4518 
4519  for( run1 = 0; run1 < ndir; run1++ ){
4520  nablaH(3,run1) = - nablaH_(4,run1)*(psi[number_][0]/psi[number_][3])
4521  + pp*deltaH(3,run1);
4522  }
4523  pp = pp*(psi[number_][3]/psi[number_][0]);
4524 
4525  for( run1 = 0; run1 < ndir; run1++ ){
4526  nablaH(4,run1) = pp*etaH[0][run1];
4527  }
4528 }
4529 
4530 
4531 
4533 
4534  int run1, run2, run4;
4535  int newtonsteps;
4536  double pp;
4537 
4538  const double scalT = psi[number_][0]/rel_time_scale;
4539 
4540  for( run4 = 0; run4 < nFDirs2; run4++ ){
4541 
4542  for( run1 = 0; run1 < m; run1++ ){
4543  phiG2(1,run1) = nablaG2(1,run1)*scalT;
4544  }
4545  for( run1 = 0; run1 < m; run1++ ){
4546  phiG3(1,run1) = nablaG3(1,run1)*scalT;
4547  }
4548 
4549  pp = (psi[number_][1]/psi[number_][0]);
4550  for( run1 = 0; run1 < m; run1++ ){
4551  phiG2(2,run1) = pp*nablaG2(2,run1)*scalT*scalT;
4552  }
4553  for( run1 = 0; run1 < m; run1++ ){
4554  phiG3(2,run1) = pp*nablaG3(2,run1)*scalT*scalT;
4555  }
4556 
4557  pp = pp*(psi[number_][2]/psi[number_][0]);
4558  for( run1 = 0; run1 < m; run1++ ){
4559  phiG2(3,run1) = pp*nablaG2(3,run1)*scalT*scalT*scalT;
4560  }
4561  for( run1 = 0; run1 < m; run1++ ){
4562  phiG3(3,run1) = pp*nablaG3(3,run1)*scalT*scalT*scalT;
4563  }
4564 
4565  pp = pp*(psi[number_][3]/psi[number_][0]);
4566  for( run1 = 0; run1 < m; run1++ ){
4567  phiG2(4,run1) = pp*nablaG2(4,run1)*scalT*scalT*scalT*scalT;
4568  }
4569  for( run1 = 0; run1 < m; run1++ ){
4570  phiG3(4,run1) = pp*nablaG3(4,run1)*scalT*scalT*scalT*scalT;
4571  }
4572 
4573  for( run1 = 0; run1 < m; run1++ ){
4574  deltaG2(1,run1) = nablaG2(0,run1) + phiG2(1,run1);
4575  }
4576  for( run1 = 0; run1 < m; run1++ ){
4577  deltaG3(1,run1) = nablaG3(0,run1) + phiG3(1,run1);
4578  }
4579 
4580  for( run1 = 0; run1 < m; run1++ ){
4581  deltaG2(2,run1) = deltaG2(1,run1) + phiG2(2,run1);
4582  }
4583  for( run1 = 0; run1 < m; run1++ ){
4584  deltaG3(2,run1) = deltaG3(1,run1) + phiG3(2,run1);
4585  }
4586 
4587  for( run1 = 0; run1 < m; run1++ ){
4588  deltaG2(3,run1) = deltaG2(2,run1) + phiG2(3,run1);
4589  }
4590  for( run1 = 0; run1 < m; run1++ ){
4591  deltaG3(3,run1) = deltaG3(2,run1) + phiG3(3,run1);
4592  }
4593 
4594  for( run1 = 0; run1 < m; run1++ ){
4595  eta [0][run1] = deltaG2(3,run1) + phiG2(4,run1);
4596  }
4597  for( run1 = 0; run1 < m; run1++ ){
4598  eta2[0][run1] = deltaG3(3,run1) + phiG3(4,run1);
4599  }
4600 
4601  for( run1 = 0; run1 < md; run1++ ){
4602 
4603  c2G2(run1) = - nablaG2 (0,run1)/psi[number_][0]
4604  - deltaG2 (1,run1)/psi[number_][1]
4605  - deltaG2 (2,run1)/psi[number_][2]
4606  - deltaG2 (3,run1)/psi[number_][3];
4607  }
4608  for( run1 = 0; run1 < md; run1++ ){
4609 
4610  c2G3(run1) = - nablaG3 (0,run1)/psi[number_][0]
4611  - deltaG3 (1,run1)/psi[number_][1]
4612  - deltaG3 (2,run1)/psi[number_][2]
4613  - deltaG3 (3,run1)/psi[number_][3];
4614  }
4615 
4616 
4617  newtonsteps = 0;
4618  while( newtonsteps < nOfNewtonSteps[number_] ){
4619  for( run2 = 0; run2 < m; run2++ ){
4620  G2[diff_index[run2]] = eta [newtonsteps][run2];
4621  G3[diff_index[run2]] = eta2[newtonsteps][run2];
4622  }
4623  for( run2 = 0; run2 < md; run2++ ){
4624  G2[ddiff_index[run2]] =
4625  gamma[number_][4]*eta [newtonsteps][run2]+c2G2(run2);
4626  G3[ddiff_index[run2]] =
4627  gamma[number_][4]*eta2[newtonsteps][run2]+c2G3(run2);
4628  }
4629 
4630  if( rhs[0].AD_forward2( 3*number_+newtonsteps, G2, G3, F, F2 )
4631  != SUCCESSFUL_RETURN ){
4633  }
4634 
4635  applyNewtonStep( M_index[number_], eta[newtonsteps+1],
4636  eta[newtonsteps],
4637  *M[M_index[number_]],
4638  F );
4639 
4640  applyNewtonStep( M_index[number_], eta2[newtonsteps+1],
4641  eta2[newtonsteps],
4642  *M[M_index[number_]],
4643  F2 );
4644 
4645  newtonsteps++;
4646  }
4647 
4648  for( run1 = 0; run1 < m; run1++ ){
4649  nablaY_(0,run1) = eta[nOfNewtonSteps[number_]][run1];
4650  nablaY_(1,run1) = nablaY_(0,run1) - nablaG2(0,run1);
4651  nablaY_(2,run1) = (nablaY_(1,run1) - nablaG2(1,run1)*scalT)*(psi[number_][0]/psi[number_][1]);
4652  nablaY_(3,run1) = (nablaY_(2,run1) - nablaG2(2,run1)*scalT*scalT)*(psi[number_][0]/psi[number_][2]);
4653  nablaY_(4,run1) = (nablaY_(3,run1) - nablaG2(3,run1)*scalT*scalT*scalT)*(psi[number_][0]/psi[number_][3]);
4654  }
4655  nablaG2 = nablaY_;
4656 
4657 
4658  for( run1 = 0; run1 < m; run1++ ){
4659  nablaY_(0,run1) = eta2[nOfNewtonSteps[number_]][run1];
4660  nablaY_(1,run1) = nablaY_(0,run1) - nablaG3(0,run1);
4661  nablaY_(2,run1) = (nablaY_(1,run1) - nablaG3(1,run1)*scalT)*(psi[number_][0]/psi[number_][1]);
4662  nablaY_(3,run1) = (nablaY_(2,run1) - nablaG3(2,run1)*scalT*scalT)*(psi[number_][0]/psi[number_][2]);
4663  nablaY_(4,run1) = (nablaY_(3,run1) - nablaG3(3,run1)*scalT*scalT*scalT)*(psi[number_][0]/psi[number_][3]);
4664  }
4665  nablaG3 = nablaY_;
4666  }
4667 }
4668 
4669 
4670 
4672 
4673  int run1, run2, run4;
4674  int newtonsteps;
4675  double pp;
4676 
4677  const double scalT = rel_time_scale/psi[number_][0];
4678 
4679  for( run4 = 0; run4 < nBDirs2; run4++ ){
4680 
4681  newtonsteps = nOfNewtonSteps[number_];
4682 
4683  for( run1 = 0; run1 < newtonsteps; run1++ ){
4684  for( run2 = 0; run2 < ndir; run2++ ){
4685  l [run1][0][run2] = 0.0;
4686  l2[run1][0][run2] = 0.0;
4687  }
4688  }
4689 
4690  for( run1 = 0; run1 < ndir; run1++ ){
4691 
4692  nablaH2_(4,run1) = nablaH2(4,run1)*scalT*scalT*scalT;
4693  nablaH2_(3,run1) = nablaH2(3,run1)*scalT*scalT + nablaH2_(4,run1)*(psi[number_][0]/psi[number_][3]);
4694  nablaH2_(2,run1) = nablaH2(2,run1)*scalT + nablaH2_(3,run1)*(psi[number_][0]/psi[number_][2]);
4695  nablaH2_(1,run1) = nablaH2(1,run1) + nablaH2_(2,run1)*(psi[number_][0]/psi[number_][1]);
4696  nablaH2_(0,run1) = nablaH2(0,run1) + nablaH2_(1,run1)/psi[number_][0];
4697 
4698  nablaH3_(4,run1) = nablaH3(4,run1)*scalT*scalT*scalT;
4699  nablaH3_(3,run1) = nablaH3(3,run1)*scalT*scalT + nablaH3_(4,run1)*(psi[number_][0]/psi[number_][3]);
4700  nablaH3_(2,run1) = nablaH3(2,run1)*scalT + nablaH3_(3,run1)*(psi[number_][0]/psi[number_][2]);
4701  nablaH3_(1,run1) = nablaH3(1,run1) + nablaH3_(2,run1)*(psi[number_][0]/psi[number_][1]);
4702  nablaH3_(0,run1) = nablaH3(0,run1) + nablaH3_(1,run1)/psi[number_][0];
4703 
4704  etaH2[newtonsteps][run1] = nablaH2_(0,run1);
4705  etaH3[newtonsteps][run1] = nablaH3_(0,run1);
4706  c2H2 (run1) = 0.0;
4707  c2H3 (run1) = 0.0;
4708  }
4709 
4710  newtonsteps--;
4711  while( newtonsteps >= 0 ){
4712 
4713  applyMTranspose( M_index[number_], etaH2[newtonsteps+1],
4714  *M[M_index[number_]],
4715  H2 );
4716 
4717  applyMTranspose( M_index[number_], etaH3[newtonsteps+1],
4718  *M[M_index[number_]],
4719  H3 );
4720 
4721  if( rhs[0].AD_backward2( 3*number_+newtonsteps, H2, H3,
4722  l[newtonsteps][0], l2[newtonsteps][0] )
4723  != SUCCESSFUL_RETURN ){
4725  }
4726 
4727  for( run1 = 0; run1 < ndir; run1++ ){
4728  etaH2[newtonsteps][run1] = etaH2[newtonsteps+1][run1] -
4729  l [newtonsteps][0][run1];
4730  etaH3[newtonsteps][run1] = etaH3[newtonsteps+1][run1] -
4731  l2[newtonsteps][0][run1];
4732  }
4733 
4734  for( run1 = 0; run1 < md; run1++ ){
4735  etaH2[newtonsteps][diff_index[run1]] =
4736  etaH2[newtonsteps][diff_index[run1]] -
4737  l[newtonsteps][0][ddiff_index[run1]]*gamma[number_][4];
4738  etaH3[newtonsteps][diff_index[run1]] =
4739  etaH3[newtonsteps][diff_index[run1]] -
4740  l2[newtonsteps][0][ddiff_index[run1]]*gamma[number_][4];
4741  }
4742 
4743  for( run1 = 0; run1 < md; run1++ ){
4744  c2H2(diff_index[run1]) -= l[newtonsteps][0][ddiff_index[run1]];
4745  c2H3(diff_index[run1]) -= l2[newtonsteps][0][ddiff_index[run1]];
4746  }
4747 
4748  newtonsteps--;
4749  }
4750 
4751  for( run1 = 0; run1 < ndir; run1++ ){
4752 
4753  deltaH2(3,run1) = etaH2[0][run1] - c2H2(run1)/psi[number_][3];
4754  deltaH2(2,run1) = deltaH2(3,run1) - c2H2(run1)/psi[number_][2];
4755  deltaH2(1,run1) = deltaH2(2,run1) - c2H2(run1)/psi[number_][1];
4756 
4757  deltaH3(3,run1) = etaH3[0][run1] - c2H3(run1)/psi[number_][3];
4758  deltaH3(2,run1) = deltaH3(3,run1) - c2H3(run1)/psi[number_][2];
4759  deltaH3(1,run1) = deltaH3(2,run1) - c2H3(run1)/psi[number_][1];
4760  }
4761 
4762  for( run1 = 0; run1 < ndir; run1++ ){
4763  nablaH2(0,run1) = - nablaH2_(1,run1) /psi[number_][0]
4764  - c2H2(run1) /psi[number_][0]
4765  + deltaH2(1,run1);
4766  nablaH3(0,run1) = - nablaH3_(1,run1) /psi[number_][0]
4767  - c2H3(run1) /psi[number_][0]
4768  + deltaH3(1,run1);
4769  }
4770 
4771  pp = psi[number_][0];
4772  for( run1 = 0; run1 < ndir; run1++ ){
4773  nablaH2(1,run1) = - nablaH2_(2,run1)*(psi[number_][0]/psi[number_][1])
4774  + pp*deltaH2(1,run1);
4775  nablaH3(1,run1) = - nablaH3_(2,run1)*(psi[number_][0]/psi[number_][1])
4776  + pp*deltaH3(1,run1);
4777  }
4778  pp = pp*(psi[number_][1]/psi[number_][0]);
4779  for( run1 = 0; run1 < ndir; run1++ ){
4780  nablaH2(2,run1) = - nablaH2_(3,run1)*(psi[number_][0]/psi[number_][2])
4781  + pp*deltaH2(2,run1);
4782  nablaH3(2,run1) = - nablaH3_(3,run1)*(psi[number_][0]/psi[number_][2])
4783  + pp*deltaH3(2,run1);
4784  }
4785  pp = pp*(psi[number_][2]/psi[number_][0]);
4786  for( run1 = 0; run1 < ndir; run1++ ){
4787  nablaH2(3,run1) = - nablaH2_(4,run1)*(psi[number_][0]/psi[number_][3])
4788  + pp*deltaH2(3,run1);
4789  nablaH3(3,run1) = - nablaH3_(4,run1)*(psi[number_][0]/psi[number_][3])
4790  + pp*deltaH3(3,run1);
4791  }
4792  pp = pp*(psi[number_][3]/psi[number_][0]);
4793  for( run1 = 0; run1 < ndir; run1++ ){
4794  nablaH2(4,run1) = pp*etaH2[0][run1];
4795  nablaH3(4,run1) = pp*etaH3[0][run1];
4796  }
4797  }
4798 }
4799 
4800 
4801 
4803 
4804 
4805  A[0][0] = 0.096 ;
4806  A[0][1] = 0.0 ;
4807  A[0][2] = 0.0 ;
4808  A[0][3] = 0.0 ;
4809  A[0][4] = 0.0 ;
4810  A[0][5] = 0.0 ;
4811  A[0][6] = 0.0 ;
4812 
4813  A[1][0] = 0.104 ;
4814  A[1][1] = 0.096 ;
4815  A[1][2] = 0.0 ;
4816  A[1][3] = 0.0 ;
4817  A[1][4] = 0.0 ;
4818  A[1][5] = 0.0 ;
4819  A[1][6] = 0.0 ;
4820 
4821  A[2][0] = 0.1310450349650284 ;
4822  A[2][1] = 0.07295496503497158102;
4823  A[2][2] = 0.096 ;
4824  A[2][3] = 0.0 ;
4825  A[2][4] = 0.0 ;
4826  A[2][5] = 0.0 ;
4827  A[2][6] = 0.0 ;
4828 
4829  A[3][0] = 0.2199597787833081951;
4830  A[3][1] = -0.1447179487179487179;
4831  A[3][2] = 0.02875816993464052288;
4832  A[3][3] = 0.096 ;
4833  A[3][4] = 0.0 ;
4834  A[3][5] = 0.0 ;
4835  A[3][6] = 0.0 ;
4836 
4837  A[4][0] = 0.1608848667672197084;
4838  A[4][1] = -0.0512400094214750;
4839  A[4][2] = -0.02467973856209150327;
4840  A[4][3] = 0.2190348812163467684;
4841  A[4][4] = 0.096 ;
4842  A[4][5] = 0.0 ;
4843  A[4][6] = 0.0 ;
4844 
4845  A[5][0] = 0.1761704161863276;
4846  A[5][1] = -0.2376951929172075;
4847  A[5][2] = 0.1249803878146932;
4848  A[5][3] = 0.3034259664066430296;
4849  A[5][4] = 0.1371184225095437181;
4850  A[5][5] = 0.096 ;
4851  A[5][6] = 0.0 ;
4852 
4853  A[6][0] = 0.1822523881347410759;
4854  A[6][1] = -0.3465441147470548;
4855  A[6][2] = 0.213542483660130719;
4856  A[6][3] = 0.3547492429521829614;
4857  A[6][4] = 0.1 ;
4858  A[6][5] = 0.2 ;
4859  A[6][6] = 0.096 ;
4860 
4861 
4862  b4[0] = 0.0;
4863  b4[1] = 0.0;
4864  b4[2] = 0.0;
4865  b4[3] = 0.4583333333333333333;
4866  b4[4] = 0.04166666666666666667;
4867  b4[5] = 0.04166666666666666667;
4868  b4[6] = 0.4583333333333333333;
4869 
4870  b5[0] = -0.3413924474339141;
4871  b5[1] = 0.8554210048117954;
4872  b5[2] = -0.5726796951403962;
4873  b5[3] = 0.4498353628579520;
4874  b5[4] = 0.0888157749045627;
4875  b5[5] = 0.0400000000000000;
4876  b5[6] = 0.4800000000000000;
4877 
4878  c[0] = 0.096;
4879  c[1] = 0.2;
4880  c[2] = 0.3;
4881  c[3] = 0.2;
4882  c[4] = 0.4;
4883  c[5] = 0.6;
4884  c[6] = 0.8;
4885 }
4886 
4887 
4888 
4890 
4891  int run2;
4892 
4893  cout <<"w.r.t. the states:\n" << scientific;
4894  for( run2 = 0; run2 < md; run2++ ){
4895  cout << div(0,diff_index[run2]) << " ";
4896  }
4897  cout <<"\n";
4898 
4899  if( mu > 0 ){
4900  cout <<"w.r.t. the controls:\n" << scientific;
4901  for( run2 = 0; run2 < mu; run2++ ){
4902  cout << div(0,control_index[run2]) << " ";
4903  }
4904  cout <<"\n";
4905  }
4906  if( mp > 0 ){
4907  cout <<"w.r.t. the parameters:\n" << scientific;
4908  for( run2 = 0; run2 < mp; run2++ ){
4909  cout << div(0,parameter_index[run2]) << " ";
4910  }
4911  cout <<"\n";
4912  }
4913  if( mw > 0 ){
4914  cout <<"w.r.t. the diturbances:\n" << scientific;
4915  for( run2 = 0; run2 < mw; run2++ ){
4916  cout << div(0,disturbance_index[run2]) << " ";
4917  }
4918  cout <<"\n";
4919  }
4920 }
4921 
4922 
4923 
4925 
4926  cout << scientific;
4927 
4928  int run1;
4929 
4930  // Forward Sensitivities:
4931  // ----------------------
4932 
4933  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ){
4934  cout <<"BDF: Forward Sensitivities:\n";
4935  for( run1 = 0; run1 < m; run1++ ){
4936 
4937  cout << nablaG(0, run1);
4938  }
4939  cout <<"\n";
4940  }
4941 
4942  if( nFDirs2 > 0 ){
4943 
4944  cout <<"BDF: First Order Forward Sensitivities:\n";
4945  for( run1 = 0; run1 < m; run1++ ){
4946  cout << nablaG2(0, run1);
4947  }
4948  cout <<"\n";
4949 
4950  cout <<"BDF: Second Order Forward Sensitivities:\n";
4951  for( run1 = 0; run1 < m; run1++ ){
4952  cout << nablaG3(0, run1);
4953  }
4954  cout <<"\n";
4955  }
4956 
4957  // Backward Sensitivities:
4958  // -----------------------
4959 
4960  if( nBDirs > 0 ){
4961  cout <<"BDF: t = " << t-c[6]*h[0] << " h = " << h[0] << endl;
4962  cout <<"BDF: Backward Sensitivities:\n";
4964  }
4965 
4966  // 2nd order Backward Sensitivities:
4967  // ---------------------------------
4968 
4969  if( nBDirs2 > 0 ){
4970  cout <<"BDF: t = " << t-c[6]*h[0] << " h = " << h[0] << endl;
4971 
4972  cout << "BDF: Backward Sensitivities:\n";
4974  cout <<"BDF: 2nd order Backward Sensitivities:\n";
4976  }
4977 }
4978 
4979 
4980 
4982 
4983  cout << scientific;
4984 
4985  int run1;
4986 
4987  if( PrintLevel == HIGH ){
4988 
4989  if( nBDirs == 0 && nBDirs2 == 0 )
4990  cout <<"BDF: t = " << t-c[6]*h[0] << " h = " << h[0] << endl;
4991 
4992  if( soa != SOA_EVERYTHING_FROZEN ){
4993 
4994  for( run1 = 0; run1 < md; run1++ ){
4995  cout << "x[" << run1 << "] = " << nablaY(0,run1) << " ";
4996  }
4997  for( run1 = 0; run1 < ma; run1++ ){
4998  cout << "xa[" << run1 << "] = " << nablaY(0,md + run1) << " ";
4999  }
5000  cout <<"\n";
5001  }
5002  else{
5003  if( nBDirs == 0 && nBDirs2 == 0 )
5004  cout <<"\n";
5005  }
5006 
5007  // Forward Sensitivities:
5008  // ----------------------
5009 
5010  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ){
5011  cout <<"BDF: Forward Sensitivities:\n";
5012  for( run1 = 0; run1 < m; run1++ ){
5013  cout << nablaG(0,run1) << " ";
5014  }
5015  cout <<"\n";
5016  }
5017 
5018  if( nFDirs2 > 0 ){
5019 
5020  cout <<"BDF: First Order Forward Sensitivities:\n";
5021  for( run1 = 0; run1 < m; run1++ ){
5022  cout << nablaG2(0,run1) << " ";
5023  }
5024  cout <<"\n";
5025 
5026  cout <<"BDF: Second Order Forward Sensitivities:\n";
5027  for( run1 = 0; run1 < m; run1++ ){
5028  cout << nablaG3(0,run1) << " ";
5029  }
5030  cout <<"\n";
5031  }
5032 
5033  }
5034 }
5035 
5036 
5038 
5039  int run1, run3;
5040 
5041  cout << scientific;
5042 
5043  if( PrintLevel == HIGH ){
5044 
5045  for( run3 = 0; run3 < 4; run3++ ){
5046  if( nBDirs == 0 && nBDirs2 == 0 )
5047  cout <<"BDF: t = " << t+h[0]*c[3+run3] << " h = " << h[0]*c[3];
5048  if( soa != SOA_EVERYTHING_FROZEN ){
5049  for( run1 = 0; run1 < md; run1++ ){
5050  cout << "x[" << run1 << "] = " << nablaY(3 - run3, run1) << " ";
5051  }
5052  for( run1 = 0; run1 < ma; run1++ ){
5053  cout << "xa[" << run1 << "] = " << nablaY(3-run3,run1+md) << " ";
5054  }
5055  cout <<"\n";
5056  }
5057  else{
5058  if( nBDirs == 0 && nBDirs2 == 0 )
5059  cout <<"\n";
5060  }
5061 
5062  // Forward Sensitivities:
5063  // ----------------------
5064 
5065  if( nFDirs > 0 && nBDirs2 == 0 && nFDirs2 == 0 ){
5066  cout <<"BDF: Forward Sensitivities:\n";
5067  for( run1 = 0; run1 < m; run1++ ){
5068  cout << nablaG(3-run3,run1) << " ";
5069  }
5070  cout <<"\n";
5071  }
5072 
5073  if( nFDirs2 > 0 ){
5074 
5075  cout <<"BDF: First Order Forward Sensitivities:\n";
5076  for( run1 = 0; run1 < m; run1++ ){
5077  cout << nablaG2(3-run3,run1) << " ";
5078  }
5079  cout <<"\n";
5080 
5081  cout <<"BDF: Second Order Forward Sensitivities:\n";
5082  for( run1 = 0; run1 < m; run1++ ){
5083  cout << nablaG3(3-run3,run1) << " ";
5084  }
5085  cout <<"\n";
5086  }
5087  }
5088 
5089  // Backward Sensitivities:
5090  // -----------------------
5091 
5092  if( nBDirs > 0 ){
5093  cout << scientific
5094  << "BDF: t = " << t-c[6]*h[0] << " h_RK = " << h[0] << endl
5095  << "BDF: Backward Sensitivities:\n";
5097  }
5098 
5099  // 2nd order Backward Sensitivities:
5100  // ---------------------------------
5101 
5102  if( nBDirs2 > 0 ){
5103  cout << scientific
5104  << "BDF: t = " << t-c[6]*h[0] << " " << "h_RK = " << h[0] << endl
5105  << "BDF: Backward Sensitivities:\n";
5107  cout << "BDF: 2nd order Backward Sensitivities:\n";
5109  }
5110  }
5111 }
5112 
5113 
5115 
5116 // ACADOFATAL( RET_NOT_IMPLEMENTED_YET );
5117 
5118  switch( las ){
5119 
5120  case HOUSEHOLDER_METHOD:
5121 // ACADOFATAL( RET_NOT_IMPLEMENTED_YET );
5122  break;
5123 // ASSERT( index < qr.size() );
5124 // qr[ index ] = Eigen::HouseholderQR< DMatrix::Base >( J );
5125 // return SUCCESSFUL_RETURN;
5126 
5127  case SPARSE_LU:
5129  break;
5130 // return J.computeSparseLUdecomposition();
5131 
5132  default:
5134  }
5135 
5136  return SUCCESSFUL_RETURN;
5137 }
5138 
5139 
5140 double IntegratorBDF::applyNewtonStep( int index, double *etakplus1, const double *etak, const DMatrix &J, const double *FFF ){
5141 
5142  int run1;
5143  DVector bb(m,FFF);
5144  DVector deltaX;
5145 
5146  switch (las)
5147  {
5148  case HOUSEHOLDER_METHOD:
5149  deltaX = J.householderQr().solve( bb );
5150 // deltaX = qr[ index ].solve(bb);
5151  break;
5152  case SPARSE_LU:
5154 // deltaX = J.solveSparseLU(bb);
5155  break;
5156  default:
5157  deltaX.setZero();
5158  break;
5159  }
5160 
5161  for( run1 = 0; run1 < m; run1++ )
5162  etakplus1[run1] = etak[run1] - deltaX(run1);
5163 
5164  return deltaX.getNorm( VN_LINF, diff_scale );
5165 }
5166 
5167 
5168 void IntegratorBDF::applyMTranspose( int index, double *seed1, DMatrix &J, double *seed2 ){
5169 
5170  int run1;
5171  DVector bb(m);
5172 
5173  for( run1 = 0; run1 < m; run1++ )
5174  bb(run1) = seed1[diff_index[run1]];
5175 
5176  DVector deltaX;
5177 
5178  switch (las)
5179  {
5180  case HOUSEHOLDER_METHOD:
5181  // Dodgy
5182  deltaX = J.transpose().householderQr().solve( bb );
5183 // deltaX = J.solveTransposeQR(bb);
5184 // deltaX = qr[ index ].matrixQR().block(0, 0, J.cols(), J.cols()).
5185 // triangularView<Eigen::Upper>().transpose().solve( bb );
5186  break;
5187  case SPARSE_LU:
5189 // deltaX = J.solveTransposeSparseLU(bb);
5190  break;
5191  default:
5193  deltaX.setZero();
5194  break;
5195  }
5196 
5197  for( run1 = 0; run1 < m; run1++ )
5198  seed2[run1] = deltaX(run1);
5199 }
5200 
5201 
5202 
5203 void IntegratorBDF::relaxAlgebraic( double *residuum, double timePoint ){
5204 
5205  int relaxationType ;
5206  double relaxationPar ;
5207 
5208  get( RELAXATION_PARAMETER, relaxationPar );
5209 
5210  const double a = relaxationPar*(timeInterval.getIntervalLength());
5211  const double b = 1.0;
5212  double damping = 1.0;
5213  int run1 ;
5214  double normRES;
5215 
5216  get( ALGEBRAIC_RELAXATION, relaxationType );
5217 
5218  switch( relaxationType ){
5219 
5220  case ART_EXPONENTIAL:
5222  for( run1 = 0; run1 < ma; run1++ )
5223  residuum[md+run1] -= initialAlgebraicResiduum[md+run1]*damping;
5224  break;
5225 
5227  normRES = 0.0;
5228  for( run1 = 0; run1 < ma; run1++ )
5229  if( fabs(initialAlgebraicResiduum[md+run1]) > normRES )
5230  normRES = fabs(initialAlgebraicResiduum[md+run1]);
5231 
5232  damping = pow( 1.0 - ( timePoint-timeInterval.getFirstTime())/( timeInterval.getIntervalLength() ) ,
5233  b + a/(normRES+100.0*EPS) );
5234 
5235  break;
5236 
5237  case ART_UNKNOWN:
5238  damping = 1.0;
5239  break;
5240  }
5241 
5242  for( run1 = 0; run1 < ma; run1++ )
5243  residuum[md+run1] -= initialAlgebraicResiduum[md+run1]*damping;
5244 
5245  return;
5246 }
5247 
5248 
5249 
5251 
5252  return md+ma;
5253 }
5254 
5256 
5257  return md;
5258 }
5259 
5260 
5262 
5263  int run1;
5264 
5265  for( run1 = 0; run1 < m; run1++ ){
5266 
5267  div(3,run1) = div(2,run1) - div(3,run1);
5268 
5269  div(2,run1) = div(1,run1) - div(2,run1);
5270  div(3,run1) = ( div(2,run1) - div(3,run1) )/(2.0);
5271 
5272  div(1,run1) = div(0,run1) - div(1,run1);
5273  div(2,run1) = ( div(1,run1) - div(2,run1) )/(2.0);
5274  div(3,run1) = ( div(2,run1) - div(3,run1) )/(3.0);
5275 
5276  div(4,run1) = 0.0; // the RK-starter has order 3 only.
5277  }
5278  return;
5279 }
5280 
5281 
5283  DVector &Dx_p ,
5284  DVector &Dx_u ,
5285  DVector &Dx_w ,
5286  const DMatrix &div ) const{
5287 
5288  int run1;
5289 
5290  if( Dx_x0.getDim() != 0 )
5291  for( run1 = 0; run1 < m; run1++ )
5292  Dx_x0(run1)= div(0,diff_index[run1]);
5293 
5294  if( Dx_u.getDim() != 0 )
5295  for( run1 = 0; run1 < mu; run1++ )
5296  Dx_u(run1) = div(0,control_index[run1]);
5297 
5298  if( Dx_p.getDim() != 0 )
5299  for( run1 = 0; run1 < mp; run1++ )
5300  Dx_p(run1) = div(0,parameter_index[run1]);
5301 
5302  if( Dx_w.getDim() != 0 )
5303  for( run1 = 0; run1 < mw; run1++ )
5304  Dx_w(run1) = div(0,disturbance_index[run1]);
5305 
5306  return;
5307 }
5308 
5309 
5310 void IntegratorBDF::interpolate( int number_, DMatrix &div, VariablesGrid &poly ){
5311 
5312  int i1 = timeInterval.getFloorIndex( t-h[0] );
5313  int i2 = timeInterval.getFloorIndex( t );
5314  int jj, run1;
5315 
5316  for( jj = i1+1; jj <= i2; jj++ ){
5317 
5318  for( run1 = 0; run1 < m; run1++ )
5319  poly( jj, run1 ) = div(0,run1);
5320 
5321  double pp1 = (timeInterval.getTime(jj) - t)/h[0];
5322  double pp2 = pp1*(pp1*h[0] + h[0])/h[0];
5323  double pp3 = pp2*(pp1*h[0] + psi[number_][1])/h[0];
5324  double pp4 = pp3*(pp1*h[0] + psi[number_][2])/h[0];
5325 
5326  for( run1 = 0; run1 < m; run1++ )
5327  poly( jj, run1 ) += pp1*div(1,run1);
5328 
5329  for( run1 = 0; run1 < m; run1++ )
5330  poly( jj, run1 ) += pp2*div(2,run1);
5331 
5332  for( run1 = 0; run1 < m; run1++ )
5333  poly( jj, run1 ) += pp3*div(3,run1);
5334 
5335  for( run1 = 0; run1 < m; run1++ )
5336  poly( jj, run1 ) += pp4*div(4,run1);
5337  }
5338 }
5339 
5340 
5342  const DVector& currentXA
5343  )
5344 {
5345  return;
5346 
5347  int run1;
5348 
5349  // log differential states
5350  if ( currentX.isEmpty( ) == BT_TRUE )
5351  {
5352  DVector currentDiffStates(md);
5353  for( run1 = 0; run1 < md; run1++ )
5354  currentDiffStates( run1 ) = nablaY( 0,run1 );
5355  setLast( LOG_DIFFERENTIAL_STATES,currentDiffStates,t );
5356  }
5357  else
5358  {
5359  setLast( LOG_DIFFERENTIAL_STATES,currentX,t );
5360  }
5361 
5362  // log algebraic states
5363  if ( currentX.isEmpty( ) == BT_TRUE )
5364  {
5365  DVector currentAlgStates(ma);
5366  for( run1 = 0; run1 < ma; run1++ )
5367  currentAlgStates( run1 ) = nablaY( 0,md+run1 );
5368  setLast( LOG_ALGEBRAIC_STATES,currentAlgStates,t );
5369  }
5370  else
5371  {
5372  setLast( LOG_ALGEBRAIC_STATES,currentXA,t );
5373  }
5374 }
5375 
5376 
5378 
5379 // end of file.
returnValue setLast(LogName _name, int lastValue, double time=-INFTY)
int getStateEnumerationIndex(int index_)
double * initial_guess
virtual returnValue getProtectedBackwardSensitivities(DVector &Dx_x0, DVector &Dx_p, DVector &Dx_u, DVector &Dx_w, int order) const
double TOL
Definition: integrator.hpp:645
void determineRKEtaGForward()
IntermediateState sqrt(const Expression &arg)
void printBDFfinalResults()
double tune
Definition: integrator.hpp:644
virtual returnValue getProtectedForwardSensitivities(DMatrix *Dx, int order) const
void constructAll(const IntegratorBDF &arg)
int getNPI() const
Definition: function.cpp:239
virtual int getNumberOfRejectedSteps() const
void init(unsigned _nRows=0, unsigned _nCols=0)
Definition: matrix.hpp:135
double applyNewtonStep(int index, double *etakplus1, const double *etak, const DMatrix &J, const double *FFF)
returnValue setForwardSeed2(const DVector &xSeed, const DVector &pSeed, const DVector &uSeed, const DVector &wSeed)
double getTime(uint pointIdx) const
int getNumDynamicEquations() const
int * int_parameter_index
Definition: integrator.hpp:659
double determinePredictor(int number, BooleanType ini)
double getFirstTime() const
int maxNumberOfSteps
Definition: integrator.hpp:666
VariablesGrid dxStore
Definition: integrator.hpp:712
const double INFTY
Implements the backward-differentiation formula for integrating DAEs.
RealClock jacComputation
virtual returnValue freezeAll()
short int m
Definition: integrator.hpp:620
void initializeOptions()
Definition: integrator.cpp:587
virtual returnValue stop()
void determineBDFEtaHBackward(int number)
BEGIN_NAMESPACE_ACADO const double EPS
int getNDX() const
Definition: function.cpp:217
void logCurrentIntegratorStep(const DVector &currentX=emptyConstVector, const DVector &currentXA=emptyConstVector)
int * diff_index
Definition: integrator.hpp:653
void init(unsigned _dim=0)
Definition: vector.hpp:155
void determineRKEtaGForward2()
Provides a time grid consisting of vector-valued optimization variables at each grid point...
virtual IntegratorBDF & operator=(const IntegratorBDF &arg)
Allows to pass back messages to the calling function.
DVector evaluate(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:520
RealClock correctorTime
double hmin
Definition: integrator.hpp:642
void determineBDFEtaGForward(int number)
AlgorithmicBase & operator=(const AlgorithmicBase &rhs)
int getNUI() const
Definition: function.cpp:227
short int mdx
Definition: integrator.hpp:622
IntermediateState pow(const Expression &arg1, const Expression &arg2)
int getNU() const
Definition: function.cpp:222
returnValue decomposeJacobian(int index, DMatrix &J)
Allows to conveniently handle (one-dimensional) grids consisting of time points.
Definition: grid.hpp:58
RealClock functionEvaluation
Definition: integrator.hpp:695
virtual int getDim() const
void determineRKEtaHBackward()
virtual returnValue evaluate(const DVector &x0, const DVector &xa, const DVector &p, const DVector &u, const DVector &w, const Grid &t_)
void printRKIntermediateResults()
short int md
Definition: integrator.hpp:629
virtual ~IntegratorBDF()
Grid timeInterval
Definition: integrator.hpp:648
returnValue rk_start_solve(int stepnumber)
#define CLOSE_NAMESPACE_ACADO
virtual Integrator * clone() const
int * int_control_index
Definition: integrator.hpp:658
bool isEmpty() const
Definition: vector.hpp:176
int getNXA() const
Definition: function.cpp:212
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
VariablesGrid ddxStore
Definition: integrator.hpp:713
Abstract base class for all kinds of algorithms for integrating differential equations (ODEs or DAEs)...
Definition: integrator.hpp:61
VariablesGrid xStore
Definition: integrator.hpp:711
uint getFloorIndex(double time) const
Definition: grid.cpp:593
#define ACADOFATAL(retval)
double * initialAlgebraicResiduum
void determineBDFEtaHBackward2(int number)
virtual returnValue evaluateSensitivities()
returnValue determineCorrector(int number, BooleanType ini)
int * alg_index
Definition: integrator.hpp:655
int getNW() const
Definition: function.cpp:245
StateOfAggregation soa
Definition: integrator.hpp:688
double relaxationConstant
int nFcnEvaluations
Definition: integrator.hpp:696
BooleanType acadoIsNaN(double x)
double getIntervalLength() const
int getNP() const
Definition: function.cpp:233
short int mu
Definition: integrator.hpp:624
virtual int getDimX() const
virtual returnValue step(int number)
virtual returnValue unfreeze()
void determineRKEtaHBackward2()
unsigned getDim() const
Definition: vector.hpp:172
returnValue rk_start()
int getDim() const
short int mw
Definition: integrator.hpp:628
double * h
Definition: integrator.hpp:640
double scale(VariableType variableType_, int index_) const
Definition: function.cpp:182
#define E
RealClock totalTime
Definition: integrator.hpp:694
void printBDFIntermediateResults()
virtual returnValue setProtectedForwardSeed(const DVector &xSeed, const DVector &pSeed, const DVector &uSeed, const DVector &wSeed, const int &order)
returnValue init()
int getN() const
RealClock jacDecomposition
void initializeVariables()
returnValue acadoPrintCopyrightNotice(const std::string &subpackage)
short int mui
Definition: integrator.hpp:625
int * control_index
Definition: integrator.hpp:656
Derived & setZero(Index size)
int * disturbance_index
Definition: integrator.hpp:660
void interpolate(int number_, DMatrix &div, VariablesGrid &poly)
int index(VariableType variableType_, int index_) const
Definition: function.cpp:176
void relaxAlgebraic(double *residuum, double timePoint)
short int mp
Definition: integrator.hpp:626
virtual returnValue setProtectedBackwardSeed(const DVector &seed, const int &order)
PrintLevel
virtual BooleanType makeImplicit()
virtual returnValue start()
Definition: real_clock.cpp:78
void printBDFfinalResults2(DMatrix &div)
VariablesGrid iStore
Definition: integrator.hpp:714
virtual returnValue getProtectedX(DVector *xEnd) const
#define BT_TRUE
Definition: acado_types.hpp:47
void prepareDividedDifferences(DMatrix &div)
short int mn
Definition: integrator.hpp:623
virtual returnValue setBackwardSeed2(const DVector &seed)
void applyMTranspose(int index, double *seed1, DMatrix &J, double *seed2)
int * parameter_index
Definition: integrator.hpp:657
void initializeButcherTableau()
int * ddiff_index
Definition: integrator.hpp:654
virtual returnValue stop()
Definition: real_clock.cpp:107
virtual int getNumberOfSteps() const
double getLastTime() const
#define ACADOWARNING(retval)
virtual returnValue init(const DifferentialEquation &rhs_)
#define BEGIN_NAMESPACE_ACADO
virtual returnValue freezeMesh()
virtual returnValue setDxInitialization(double *dx0)
#define BT_FALSE
Definition: acado_types.hpp:49
IntermediateState exp(const Expression &arg)
DVector diff_scale
Definition: integrator.hpp:670
short int ma
Definition: integrator.hpp:621
void determineBDFEtaGForward2(int number)
short int mpi
Definition: integrator.hpp:627
double hmax
Definition: integrator.hpp:643
virtual returnValue printRunTimeProfile() const
Definition: integrator.cpp:624
double hini
Definition: integrator.hpp:641
void copyBackward(DVector &Dx_x0, DVector &Dx_p, DVector &Dx_u, DVector &Dx_w, const DMatrix &div) const
virtual double getStepSize() const
int getNumberOfVariables() const
Definition: function.cpp:264
#define ACADOERROR(retval)
T getNorm(VectorNorm _norm) const
Definition: vector.cpp:67
virtual returnValue getTime(double &_elapsedTime)
Definition: clock.cpp:92
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.
DifferentialEquation * rhs
Definition: integrator.hpp:619


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:42