ikarus-project / ikarus

Ikarus is a C++-based library that uses DUNE modules to solve partial differential equations with the finite element method and more.
https://ikarus-project.github.io/
Other
4 stars 2 forks source link

refactor observer patterns and unify logging information #260

Open tarun-mitruka opened 5 months ago

tarun-mitruka commented 5 months ago

This PR heavily refactors the interface of the observer patterns. By the addition of struct ControlLoggerInformation and struct NonLinearSolverLoggingInformation, the function updateImpl of the class IObserver can be simplified by just passing these struct and thus removes the overloading of this function. This simplifies the whole interface of the class IObservable as well. Moreover, the switch statements in controllogger.cpp and nonlinearsolverlogger.cpp can be combined in one function which increases code readability and extensibility. The information of the load factor lambda is now also moved from a solver logger to a control logger as it is expected to be. This leads to small fixes in the class LoadControl which now then shows the same output as by the class PathFollowing as confirmed by an additional test added in testpathfollowing.cpp.

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 57.55396% with 59 lines in your changes missing coverage. Please review.

Project coverage is 84.53%. Comparing base (fa0c806) to head (f37f798).

:exclamation: Current head f37f798 differs from pull request most recent head 26a8620

Please upload reports for the commit 26a8620 to get more accurate results.

Files Patch % Lines
ikarus/controlroutines/pathfollowing.inl 0.00% 23 Missing :warning:
...olver/newtonraphsonwithscalarsubsidiaryfunction.hh 0.00% 20 Missing :warning:
ikarus/controlroutines/pathfollowing.hh 0.00% 7 Missing :warning:
ikarus/controlroutines/adaptivestepsizing.hh 0.00% 3 Missing :warning:
ikarus/controlroutines/loadcontrol.hh 66.66% 3 Missing :warning:
ikarus/controlroutines/loadcontrol.inl 93.33% 1 Missing :warning:
ikarus/utils/observer/controllogger.hh 0.00% 1 Missing :warning:
ikarus/utils/observer/observer.hh 66.66% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #260 +/- ## ========================================== - Coverage 91.23% 84.53% -6.70% ========================================== Files 63 62 -1 Lines 2053 1953 -100 ========================================== - Hits 1873 1651 -222 - Misses 180 302 +122 ``` | [Flag](https://app.codecov.io/gh/ikarus-project/ikarus/pull/260/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ikarus-project) | Coverage Δ | | |---|---|---| | [tests](https://app.codecov.io/gh/ikarus-project/ikarus/pull/260/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ikarus-project) | `84.53% <57.55%> (-6.70%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=ikarus-project#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

tarun-mitruka commented 3 months ago

!!! UPDATED THRICE!!!

Here is a part of an output as example obtained from testpathfollowing.cpp. The below code uses ArcLength along with the class PathFollowing.

==========================================================================================
Started Path following with Arc length
Load step:    0                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm      SubsidiaryFunction      LoadFactor
------------------------------------------------------------------------------------------
   1                 6.452e-10           8.237e-05              -1.388e-17       9.927e-02
Number of iterations by the non-linear solver: 1
Load factor: 9.927e-02
==========================================================================================
Load step:    1                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm      SubsidiaryFunction      LoadFactor
------------------------------------------------------------------------------------------
   1                 5.272e-08           4.993e-04               2.316e-08       1.981e-01
   2                 8.504e-07           1.038e-06               8.504e-07       1.981e-01
   3                 2.439e-16           3.608e-14               2.359e-16       1.981e-01
Number of iterations by the non-linear solver: 3
Load factor: 1.981e-01
==========================================================================================
Load step:    2                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm      SubsidiaryFunction      LoadFactor
------------------------------------------------------------------------------------------
   1                 2.971e-07           1.025e-03              -1.388e-17       2.962e-01
   2                 3.575e-06           4.358e-06               3.575e-06       2.962e-01
   3                 8.230e-15           9.900e-13               8.230e-15       2.962e-01
Number of iterations by the non-linear solver: 3
Load factor: 2.962e-01
==========================================================================================
Load step:    3                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm      SubsidiaryFunction      LoadFactor
------------------------------------------------------------------------------------------
   1                 9.574e-07           1.609e-03               0.000e+00       3.929e-01
   2                 8.751e-06           1.063e-05               8.751e-06       3.929e-01
   3                 8.078e-14           8.346e-12               8.078e-14       3.929e-01
Number of iterations by the non-linear solver: 3
Load factor: 3.929e-01
==========================================================================================
Load step:    4                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm      SubsidiaryFunction      LoadFactor
------------------------------------------------------------------------------------------
   1                 2.349e-06           2.288e-03               0.000e+00       4.878e-01
   2                 1.754e-05           2.120e-05               1.754e-05       4.878e-01
   3                 4.538e-13           4.522e-11               4.538e-13       4.878e-01
Number of iterations by the non-linear solver: 3
Load factor: 4.878e-01
==========================================================================================
End of Path following with Arc length
Total number of iterations:  13
Elapsed time: 15 ms

The below code uses the class LoadControl. It is to note that the line which shows the displacement being printed explicitly is just an outcome of a user-defined observer included in the tests and is not part of the ControlLogger itself.

==========================================================================================
Started Load Control Method
The displacement is     0 0
Load step:    0                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm
------------------------------------------------------------------------------------------
   1                 4.110e-04           7.118e-02
   2                 2.462e-09           9.726e-05
Number of iterations by the non-linear solver: 2
The displacement is     0.01715872957844366 0.06918063748415847
Load factor: 1.000e-01
==========================================================================================
Load step:    0                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm
------------------------------------------------------------------------------------------
   1                 1.661e-03           7.147e-02
   2                 8.313e-08           3.990e-04
   3                 2.559e-16           2.253e-08
Number of iterations by the non-linear solver: 3
The displacement is     0.03454644287301918  0.1389097864303651
Load factor: 2.000e-01
==========================================================================================
Load step:    1                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm
------------------------------------------------------------------------------------------
   1                 2.999e-03           7.237e-02
   2                 4.312e-07           7.396e-04
   3                 1.101e-14           1.196e-07
Number of iterations by the non-linear solver: 3
The displacement is     0.05241261128656169  0.2097895325120464
Load factor: 3.000e-01
==========================================================================================
Load step:    2                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm
------------------------------------------------------------------------------------------
   1                 4.520e-03           7.396e-02
   2                 1.428e-06           1.160e-03
   3                 1.757e-13           4.098e-07
Number of iterations by the non-linear solver: 3
The displacement is     0.07105346894026039  0.2825443193976918
Load factor: 4.000e-01
==========================================================================================
Load step:    3                                                     Step size =  1.000e-01
------------------------------------------------------------------------------------------
Non-linear solver started:
 Ite      FirstOrderOptimality      CorrectionNorm
------------------------------------------------------------------------------------------
   1                 6.366e-03           7.642e-02
   2                 4.017e-06           1.728e-03
   3                 1.946e-12           1.210e-06
Number of iterations by the non-linear solver: 3
The displacement is     0.09085338848350603  0.3581294588381903
Load factor: 5.000e-01
==========================================================================================
End of Load Control Method
Total number of iterations:  14
Elapsed time: 1 ms
rath3t commented 3 months ago

Ok I refactored the whole observer observable pattern such that it is much more scalable: https://godbolt.org/z/b9cM5e714 Can you have a look and try to understand and refactor the existing code? Ignore the thread-safe parts.

tarun-mitruka commented 3 months ago

Further things to do: