This source file includes following definitions.
- step_
- prefix
- ie_heat_algorithm
- bdf_algorithm
- nonlinear_algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #ifndef DUNE_HEAT_ALGORITHM_HPP
17 #define DUNE_HEAT_ALGORITHM_HPP
18
19
20 #include "dune_typedef_heat.hpp"
21 #include "/Users/christianpower/cpp/ODE_Solver/implicit_euler.h"
22 #include "/Users/christianpower/cpp/ODE_Solver/bdf.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 struct DataOutputParameters:
38 public Dune::Fem::
39 LocalParameter<Dune::Fem::DataOutputParameters, DataOutputParameters> {
40 DataOutputParameters(const std::string name, const int step)
41 : name_(name), step_( step )
42 {}
43 DataOutputParameters(const DataOutputParameters& other)
44 : step_( other.step_ )
45 {}
46 std::string prefix() const {
47 std::stringstream s;
48
49 s << name_ << step_ << "-";
50 return s.str();
51 }
52 private:
53 std::string name_;
54 int step_;
55 };
56
57
58 void BDF::ie_heat_algorithm(){
59 double t_0 =
60 Dune::Fem::Parameter::getValue<double>("heat.starttime",0.0);
61 double dT =
62 Dune::Fem::Parameter::getValue<double>("heat.timestep",0.1);
63 double t_end =
64 Dune::Fem::Parameter::getValue<double>("heat.endtime",0.6);
65 const int itno = (t_end - t_0)/dT + .1;
66
67
68 const std::string gridkey =
69 Dune::Fem::IOInterface::defaultGridKey( GridType::dimension );
70 const std::string gridfile =
71 Dune::Fem::Parameter::getValue< std::string >( gridkey );
72 if( Dune::Fem::MPIManager::rank() == 0 )
73 std::cout << "Loading macro grid: " << gridfile << std::endl;
74 Dune::GridPtr< HostGridType > hostGrid {gridfile};
75 hostGrid ->loadBalance();
76
77
78 DeformationCoordFunction deformation {};
79 GridType grid(*hostGrid, deformation);
80 GridPartType gridPart(grid);
81 DiscreteFunctionSpaceType dfSpace(gridPart);
82
83 L2NormType l2norm(gridPart);
84 H1NormType h1norm(gridPart);
85 std::ofstream l2h1error_ofs
86 {Dune::Fem::Parameter::getValue<std::string>("fem.io.errorFile",
87 "../output/l2h1error")};
88
89 Dune::Fem::GridTimeProvider<GridType> timeProvider(t_0, grid);
90 timeProvider.init(dT);
91
92 deformation.set_time_provider(timeProvider);
93
94
95 DiscreteFunctionType U_n("U_n",dfSpace), rhs("rhs", dfSpace),
96 load_vector("load_vector", dfSpace), exactSolution("exactSolution",dfSpace);
97
98 InitialDataType initialData {timeProvider};
99 InterpolationType::interpolateFunction( initialData, U_n );
100 InterpolationType::interpolateFunction( initialData, exactSolution );
101
102
103
104
105
106
107
108
109 RHSFunctionType f {timeProvider};
110
111
112
113 IOTupleType ioTuple(&U_n);
114 const int step = 0;
115 DataOutputType
116 dataOutput(grid, ioTuple,
117 DataOutputParameters(Dune::Fem::Parameter::getValue<std::string>
118 ("fem.io.outputName",
119 "../output/ALE_LiteratureExample-"),
120 step) );
121
122
123 auto write_error = [&](std::ostream& os){
124 os << std::defaultfloat << timeProvider.time() << ' '
125 << std::scientific
126 << l2norm.distance(exactSolution, U_n) << ' '
127 << h1norm.distance(exactSolution, U_n) << std::endl;
128 };
129
130
131
132 NonlinearModel model {timeProvider};
133 NonlinearOperator ellipticOp {U_n, model};
134 const double solverEps =
135 Dune::Fem::Parameter::getValue<double>( "heat.solvereps", 1e-8 );
136 LinearInverseOperatorType solver(ellipticOp, solverEps, solverEps);
137
138
139
140
141
142 ellipticOp.get_xi(U_n);
143 ellipticOp.mass_matrix(U_n, rhs);
144
145
146
147 std::cout << std::scientific << "\n==========\n"
148 << "U_n.dbegin() = " << *(U_n.dbegin()) << std::endl;
149 std::cout << "rhs.dbegin() = " << *(rhs.dbegin()) << std::endl;
150
151 int iter = 0;
152 for(timeProvider.next(dT); iter < itno; timeProvider.next(dT), ++iter)
153
154 {
155 std::cout << std::defaultfloat << "==========\n"
156 << "In for loop: iter = "<< iter << ", "
157 << "time = " << timeProvider.time() << std::endl;
158
159
160
161
162
163
164
165 assembleRHS(f, load_vector);
166 std::cout << "assembleRHS(f, load_vector) = " << *(load_vector.dbegin()) << std::endl;
167
168
169
170
171 rhs.axpy(timeProvider.deltaT(), load_vector);
172
173
174
175 std::cout << "rhs.axpy = " << *(rhs.dbegin()) << std::endl;
176
177
178 solver(rhs, U_n);
179
180
181
182 std::cout << "solver(rhs, U_n) = " << *(U_n.dbegin()) << std::endl;
183
184
185
186 InterpolationType::interpolateFunction(initialData, exactSolution);
187
188
189 ellipticOp.mass_matrix(U_n, rhs);
190 std::cout << "mass_matrix(U_n, rhs) = " << *(rhs.dbegin()) << std::endl;
191
192
193 ellipticOp.get_xi(U_n);
194
195
196 std::cout << std::scientific
197 << "U_n.dbegin() = " << *(U_n.dbegin()) << std::endl;
198 std::cout << "rhs.dbegin() = " << *(rhs.dbegin()) << std::endl;
199 std::cout << "load_vector.dbegin() = " << *(load_vector.dbegin())
200 << std::endl;
201 }
202 l2h1error_ofs.close();
203 }
204
205 void BDF::bdf_algorithm(const int bdf_no){
206 if( bdf_no < 1 || bdf_no > 6)
207 throw std::runtime_error("ERROR in BDF::bdf_algorithm():"
208 " Bad bdf_no = " + std::to_string(bdf_no) + "\n"
209 "Enter an integer between 1 and 6");
210
211 double t_0 =
212 Dune::Fem::Parameter::getValue<double>("heat.starttime",0.0);
213 double dT =
214 Dune::Fem::Parameter::getValue<double>("heat.timestep",0.1);
215 double t_end =
216 Dune::Fem::Parameter::getValue<double>("heat.endtime",0.6);
217 const int time_step_no_max = (t_end - t_0)/dT + .1;
218
219
220 const std::string gridkey =
221 Dune::Fem::IOInterface::defaultGridKey(GridType::dimension);
222 const std::string gridfile =
223 Dune::Fem::Parameter::getValue<std::string>(gridkey);
224 if(Dune::Fem::MPIManager::rank() == 0)
225 std::cout << "Loading macro grid: " << gridfile << std::endl;
226 Dune::GridPtr<HostGridType> hostGrid {gridfile};
227 hostGrid->loadBalance();
228
229
230 DeformationCoordFunction deformation {};
231 GridType grid(*hostGrid, deformation);
232 GridPartType gridPart(grid);
233 DiscreteFunctionSpaceType dfSpace(gridPart);
234
235 L2NormType l2norm(gridPart);
236 H1NormType h1norm(gridPart);
237 std::ofstream l2h1error_ofs
238 {Dune::Fem::Parameter::getValue<std::string>("fem.io.errorFile",
239 "../output/l2h1error")};
240
241 Dune::Fem::GridTimeProvider<GridType> timeProvider(t_0, grid);
242 timeProvider.init(dT);
243
244 deformation.set_time_provider(timeProvider);
245
246
247 DiscreteFunctionType U_n("U_n",dfSpace), rhs("rhs", dfSpace),
248 load_vector("load_vector", dfSpace), exactSolution("exactSolution",dfSpace);
249 DiscreteFunctionType ie_U_n("ie_U_n", dfSpace);
250 DiscreteFunctionType ie_rhs("ie_rhs", dfSpace);
251
252 std::vector<DiscreteFunctionType> prev_M_n_U_n;
253 for(size_t i = 0; i < bdf_no; ++i)
254 prev_M_n_U_n.push_back(
255 DiscreteFunctionType {"U_n-" + std::to_string(i+1), dfSpace});
256
257
258 IOTupleType ioTuple(&U_n);
259 const int step = 0;
260 DataOutputType
261 dataOutput(grid, ioTuple,
262 DataOutputParameters(Dune::Fem::Parameter::getValue<std::string>
263 ("fem.io.outputName",
264 "../output/ALE_LiteratureExample-"),
265 step) );
266
267
268 auto write_error = [&](std::ostream& os){
269 os << std::defaultfloat << timeProvider.time() << ' '
270 << std::scientific
271 << l2norm.distance(exactSolution, U_n) << ' '
272 << h1norm.distance(exactSolution, U_n) << std::endl;
273 };
274
275 InitialDataType initialData {timeProvider};
276 RHSFunctionType f {timeProvider};
277 NonlinearModel model {timeProvider};
278 NonlinearOperator ellipticOp {U_n, model};
279 const double solverEps =
280 Dune::Fem::Parameter::getValue<double>( "heat.solvereps", 1e-8 );
281 LinearInverseOperatorType solver(ellipticOp, solverEps, solverEps);
282
283
284 int time_step_no = 0;
285
286
287 InterpolationType::interpolateFunction(initialData, U_n);
288 InterpolationType::interpolateFunction(initialData, exactSolution);
289 InterpolationType::interpolateFunction(initialData, ie_U_n);
290
291
292
293
294 ellipticOp.get_xi(U_n);
295 ellipticOp.mass_matrix(ie_U_n, ie_rhs);
296 ellipticOp.mass_matrix(ie_U_n, prev_M_n_U_n.at(0) );
297
298
299
300 std::cout << std::scientific << "\n==========\n"
301 << "ie_U_n = " << *(ie_U_n.dbegin()) << '\t'
302 << "ie_rhs = " << *(ie_rhs.dbegin()) << '\n'
303 << "U_n = " << *(U_n.dbegin()) << '\t'
304 << "prev_M_n_U_n.at(0) = " << *(prev_M_n_U_n.at(0).dbegin()) << std::endl;
305
306
307 for(timeProvider.next(dT); time_step_no < time_step_no_max; timeProvider.next(dT), ++time_step_no)
308
309 {
310 std::cout << std::defaultfloat << "==========\n"
311 << "In for loop: time_step_no = "<< time_step_no << ", "
312 << "time = " << timeProvider.time() << std::endl;
313
314
315
316
317
318
319
320 assembleRHS(f, load_vector);
321 std::cout << "assembleRHS(f, load_vector) = " << *(load_vector.dbegin()) << std::endl;
322
323
324
325
326 rhs.axpy(timeProvider.deltaT(), load_vector);
327
328
329
330 std::cout << "rhs.axpy = " << *(rhs.dbegin()) << std::endl;
331
332
333 solver(rhs, U_n);
334
335
336
337 std::cout << "solver(rhs, U_n) = " << *(U_n.dbegin()) << std::endl;
338
339
340
341 InterpolationType::interpolateFunction(initialData, exactSolution);
342
343
344 ellipticOp.mass_matrix(U_n, rhs);
345 std::cout << "mass_matrix(U_n, rhs) = " << *(rhs.dbegin()) << std::endl;
346
347
348 ellipticOp.get_xi(U_n);
349
350
351 std::cout << std::scientific
352 << "U_n.dbegin() = " << *(U_n.dbegin()) << std::endl;
353 std::cout << "rhs.dbegin() = " << *(rhs.dbegin()) << std::endl;
354 std::cout << "load_vector.dbegin() = " << *(load_vector.dbegin())
355 << std::endl;
356 }
357 l2h1error_ofs.close();
358 }
359
360
361
362 void BDF::nonlinear_algorithm(){
363 double t_0 =
364 Dune::Fem::Parameter::getValue<double>("heat.starttime",0.0);
365 double dT =
366 Dune::Fem::Parameter::getValue<double>("heat.timestep",0.1);
367 double t_end =
368 Dune::Fem::Parameter::getValue<double>("heat.endtime",0.6);
369 const int itno = (t_end - t_0)/dT + .1;
370
371
372 const int bdf_no =
373 Dune::Fem::Parameter::getValue<double>("heat.bdf", 1);
374 const std::vector<double> bdf_alpha_coeff { NUMERIK::bdf_alpha_coeff(bdf_no) };
375 const std::vector<double> bdf_gamma_coeff { NUMERIK::bdf_gamma_coeff(bdf_no) };
376
377
378 const std::string gridkey =
379 Dune::Fem::IOInterface::defaultGridKey( GridType::dimension );
380 const std::string gridfile =
381 Dune::Fem::Parameter::getValue< std::string >( gridkey );
382 if( Dune::Fem::MPIManager::rank() == 0 )
383 std::cout << "Loading macro grid: " << gridfile << std::endl;
384 Dune::GridPtr< HostGridType > hostGrid {gridfile};
385 hostGrid ->loadBalance();
386
387
388 DeformationCoordFunction deformation {};
389 GridType grid(*hostGrid, deformation);
390 GridPartType gridPart(grid);
391 DiscreteFunctionSpaceType dfSpace(gridPart);
392
393 L2NormType l2norm(gridPart);
394 H1NormType h1norm(gridPart);
395 std::ofstream l2h1error_ofs
396 {Dune::Fem::Parameter::getValue<std::string>("fem.io.errorFile",
397 "../output/l2h1error")};
398
399 Dune::Fem::GridTimeProvider<GridType> timeProvider(t_0, grid);
400 timeProvider.init(dT);
401
402 deformation.set_time_provider(timeProvider);
403
404
405
406 DiscreteFunctionType U_n("U_n", dfSpace);
407 DiscreteFunctionType rhs("rhs", dfSpace);
408 DiscreteFunctionType bdf_tmp("bdf_tmp", dfSpace);
409
410
411
412 DiscreteFunctionType load_vector("load_vector", dfSpace);
413 DiscreteFunctionType exact_solution("exact_solution", dfSpace);
414 DiscreteFunctionType err_vec("U_n_minus_exact_solution", dfSpace);
415
416
417 auto calc_err_vec = [&]
418
419 {
420 err_vec.assign(U_n);
421 err_vec.axpy((-1.), exact_solution);
422 };
423
424 std::vector<DiscreteFunctionType> prev_steps_vec;
425 for(int i = 0; i < bdf_no; ++i)
426 prev_steps_vec.push_back(
427 DiscreteFunctionType {"U_n" + std::to_string(i+1), dfSpace} );
428
429
430 DiscreteFunctionType ie_U_n("ie_U_n", dfSpace);
431 DiscreteFunctionType ie_rhs("ie_rhs", dfSpace);
432 DiscreteFunctionType ie_load_vector("ie_load_vector", dfSpace);
433
434
435 InitialDataType initialData {timeProvider};
436 InterpolationType::interpolateFunction( initialData, ie_U_n );
437
438 RHSFunctionType f {timeProvider};
439 NonlinearModel model {timeProvider};
440 NonlinearOperator ellipticOp {ie_U_n, model};
441 const double solverEps =
442 Dune::Fem::Parameter::getValue<double>( "heat.solvereps", 1e-8 );
443 LinearInverseOperatorType solver(ellipticOp, solverEps, solverEps);
444
445
446 IOTupleType ioTuple(&U_n);
447 const int step = 0;
448 DataOutputType
449 dataOutput(grid, ioTuple,
450 DataOutputParameters(Dune::Fem::Parameter::getValue<std::string>
451 ("fem.io.outputName",
452 "../output/ALE_LiteratureExample-"),
453 step) );
454
455 auto write_error = [&](std::ostream& os){
456 os << std::defaultfloat << timeProvider.time() << ' '
457 << std::scientific
458 << l2norm.distance(exact_solution, U_n) << ' '
459 << h1norm.distance(exact_solution, U_n) << std::endl;
460 };
461
462
463
464 std::cout << '\n' << std::endl;
465
466 int iter = (-1) * bdf_no;
467 for(int i =0;
468 (i < prev_steps_vec.size()) && (iter < itno);
469 ++i, ++iter, timeProvider.next(dT)){
470 std::cout << std::defaultfloat << "==========\n"
471 << "In initial steps: iter = "<< iter << ", "
472 << "time = " << timeProvider.time() << std::endl;
473
474 InterpolationType::interpolateFunction(initialData, bdf_tmp);
475 ellipticOp.mass_matrix(bdf_tmp, prev_steps_vec.at(i));
476 InterpolationType::interpolateFunction(initialData, exact_solution);
477 U_n.assign(bdf_tmp);
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497 std::cout << std::scientific
498 << "U_n.dbegin() = " << *(U_n.dbegin()) << '\t'
499 << "ie_U_n.dbegin() = " << *(ie_U_n.dbegin()) << '\n'
500 << "ie_rhs.dbegin() = " << *(ie_rhs.dbegin()) << std::endl;
501 }
502
503 ellipticOp.get_xi(U_n);
504 ellipticOp.mass_matrix(U_n, rhs);
505
506
507
508 auto bdf_rhs = [&]
509
510
511
512 {
513
514 rhs.clear();
515 bdf_tmp.clear();
516
517 for(size_t i=0; i < prev_steps_vec.size(); ++i){
518 rhs.axpy(bdf_alpha_coeff.at(i), prev_steps_vec.at(i));
519 bdf_tmp.axpy(bdf_gamma_coeff.at(i), prev_steps_vec.at(i));
520 }
521 rhs *= (-1);
522 ellipticOp.get_xi(bdf_tmp);
523 };
524
525 auto bdf_cycle = [&]
526
527 {
528
529
530 for(size_t i=0; i < prev_steps_vec.size() - 1; ++i)
531 prev_steps_vec.at(i).assign(prev_steps_vec.at(i+1));
532 ellipticOp.mass_matrix(U_n, prev_steps_vec.back());
533 };
534
535
536 for(;
537 iter < itno;
538 timeProvider.next(dT), ++iter)
539
540 {
541 std::cout << std::defaultfloat << "==========\n"
542 << "In for loop: iter = "<< iter << ", "
543 << "time = " << timeProvider.time() << std::endl;
544
545 bdf_rhs();
546 assembleRHS(f, load_vector);
547
548
549
550 rhs.axpy(timeProvider.deltaT(), load_vector);
551
552
553
554 rhs /= bdf_alpha_coeff.back();
555
556
557 solver(rhs, U_n);
558
559
560
561 bdf_cycle();
562 InterpolationType::interpolateFunction(initialData, exact_solution);
563 calc_err_vec();
564
565
566
567
568
569 assembleRHS(f, ie_load_vector);
570 std::cout << "assembleRHS(f, ie_load_vector) = " << *(ie_load_vector.dbegin()) << std::endl;
571 ie_rhs.axpy(timeProvider.deltaT(), ie_load_vector);
572 std::cout << "ie_rhs.axpy = " << *(ie_rhs.dbegin()) << std::endl;
573 solver(ie_rhs, ie_U_n);
574 std::cout << "solver(ie_rhs, ie_U_n) = " << *(ie_U_n.dbegin()) << std::endl;
575 ellipticOp.mass_matrix(ie_U_n, ie_rhs);
576 std::cout << "mass_matrix(ie_U_n, ie_rhs) = " << *(ie_rhs.dbegin()) << std::endl;
577 ellipticOp.get_xi(ie_U_n);
578
579
580 std::cout << std::scientific
581 << "U_n.dbegin() = " << *(U_n.dbegin()) << "\t\t"
582 << "ie_U_n.dbegin() = " << *(ie_U_n.dbegin()) << std::endl;
583 std::cout << "rhs.dbegin() = " << *(rhs.dbegin()) << "\t\t"
584 << "ie_rhs.dbegin() = " << *(ie_rhs.dbegin()) << std::endl;
585 std::cout << "load_vector.dbegin() = " << *(load_vector.dbegin()) << '\t'
586 << "ie_load_vector.dbegin() = " << *(ie_load_vector.dbegin())
587 << std::endl;
588 std::cout << "bdf_tmp.dbegin() = " << *(bdf_tmp.dbegin()) << std::endl;
589
590 for(size_t i=0; i < prev_steps_vec.size(); ++i){
591 std::cout << "prev_steps_vec.at(" << i << ").dbegin() = "
592 << *(prev_steps_vec.at(i).dbegin()) << std::endl;
593 }
594
595 }
596 l2h1error_ofs.close();
597 }
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825 #endif