LCOV - code coverage report
Current view: top level - src/solvers - memory_history_data.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4229 (6a9aeb) with base 727f46 Lines: 44 49 89.8 %
Date: 2025-08-19 19:27:09 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : #include "libmesh/memory_history_data.h"
      19             : 
      20             : namespace libMesh
      21             : {
      22         560 :     void MemoryHistoryData::store_initial_solution()
      23             :     {
      24             :      // The initial data should only be stored once.
      25          16 :      libmesh_assert(previously_stored == false);
      26             : 
      27         560 :      time_stamp = 0;
      28             : 
      29         560 :      deltat_at = std::numeric_limits<double>::signaling_NaN();
      30             : 
      31         560 :      store_vectors();
      32             : 
      33         560 :      previously_stored = true;
      34         560 :     }
      35             : 
      36        7980 :     void MemoryHistoryData::store_primal_solution(stored_data_iterator stored_datum)
      37             :     {
      38        7980 :      stored_data_iterator stored_datum_last = stored_datum;
      39         228 :      stored_datum_last--;
      40             : 
      41        7980 :      time_stamp = (stored_datum_last->second)->get_time_stamp() + 1;
      42             : 
      43             :      // For the current time instant, we dont know yet what timestep the solver might decide, so a placeholder NaN for now.
      44        7980 :      deltat_at = std::numeric_limits<double>::signaling_NaN();
      45             : 
      46        8208 :      (stored_datum_last->second)->set_deltat_at(_system.time_solver->TimeSolver::last_completed_timestep_size());
      47             : 
      48        7980 :      store_vectors();
      49             : 
      50        7980 :      previously_stored = true;
      51        7980 :     }
      52             : 
      53           0 :     void MemoryHistoryData::store_adjoint_solution()
      54             :     {
      55           0 :      libmesh_error_msg("For MemorySolutionHistory, primal and adjoints are stored in the same container.");
      56             :     }
      57             : 
      58        8540 :     void MemoryHistoryData::rewrite_stored_solution()
      59             :     {
      60             :       // We are rewriting.
      61         244 :       libmesh_assert(previously_stored == true);
      62             : 
      63        8540 :       store_vectors();
      64        8540 :     }
      65             : 
      66       23240 :     void MemoryHistoryData::retrieve_primal_solution()
      67             :     {
      68       23240 :      retrieve_vectors();
      69       23240 :     }
      70             : 
      71           0 :     void MemoryHistoryData::retrieve_adjoint_solution()
      72             :     {
      73           0 :      retrieve_vectors();
      74           0 :     }
      75             : 
      76       17080 :     void MemoryHistoryData::store_vectors()
      77             :     {
      78             :      // Now save all the preserved vectors in stored_datum
      79             :      // Loop over all the system vectors
      80       20672 :      for (System::vectors_iterator vec = _system.vectors_begin(),
      81      146392 :           vec_end = _system.vectors_end(); vec != vec_end; ++vec)
      82             :      {
      83             :       // The name of this vector
      84      125720 :       const std::string & vec_name = vec->first;
      85             : 
      86             :       // Store the vector if it is to be preserved
      87      129312 :       if (_system.vector_preservation(vec_name))
      88             :         {
      89       93840 :          stored_vecs[vec_name] = vec->second->clone();
      90             :         }
      91             :      }
      92             : 
      93             :      // Of course, we will usually save the actual solution
      94       17568 :      std::string _solution("_solution");
      95       17080 :      if (_system.project_solution_on_reinit())
      96             :      {
      97       33672 :       stored_vecs[_solution] = _system.solution->clone();
      98             :      }
      99       17080 :     }
     100             : 
     101       23240 :     void MemoryHistoryData::retrieve_vectors()
     102             :     {
     103             :       // We are reading, hopefully something has been written before
     104         664 :       libmesh_assert(previously_stored == true);
     105             : 
     106         664 :       map_type::iterator vec = stored_vecs.begin();
     107         664 :       map_type::iterator vec_end = stored_vecs.end();
     108             : 
     109             :       // Loop over all the saved vectors
     110      100240 :       for (; vec != vec_end; ++vec)
     111             :       {
     112             :        // The name of this vector
     113       77000 :        const std::string & vec_name = vec->first;
     114             : 
     115             :        // Get the vec_name entry in the saved vectors map and set the
     116             :        // current system vec[vec_name] entry to it
     117       77000 :        if (vec_name != "_solution")
     118       55296 :          _system.get_vector(vec_name) = *(vec->second);
     119             :       }
     120             : 
     121       23904 :       std::string _solution("_solution");
     122       23904 :       *(_system.solution) = *(stored_vecs[_solution]);
     123             : 
     124       23240 :     }
     125             : }

Generated by: LCOV version 1.14