TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Domaine_dis_cache.cpp
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Domaine_dis_cache.h>
17#include <Domaine_dis_base.h>
18#include <Discretisation_base.h>
19#include <Interprete_bloc.h>
20#include <Domaine.h>
21#include <sstream>
22
23/*! @brief Get the unique instance of the cache.
24 */
26{
27 static Domaine_dis_cache instance_;
28 static bool is_first_time = true; // will remain true only once!
29
30 // Register unique instance for clearing when TRUST will exit:
31 if (is_first_time)
33
34 is_first_time = false;
35 return instance_;
36}
37
38/*! @brief Clear the content of the cache. Useful to exit nicely making sure everything is deallocated.
39 */
41{
42 Domaine_dis_cache& ddc = Get_instance();
43 ddc.cache_.clear();
44}
45
46/*! @brief Get a discretized domain from the cache, building it and recording it if not
47 * there yet.
48 *
49 * Process is as follows: type might start with "NO_FACE_" indicating that we just need a simplified discretisation.
50 * (see Domaine_dis_base::discretiser_no_face())
51 * Otherwise we want the full discretisation. The full discr can be used in place of a "NO_FACE", but not the inverse.
52 * In the cache, we save both the full and the NO_FACE if the full was requested. Otherwise just the NO_FACE is saved.
53 * The cache key also uses the current interpretor address to handle weird cases like:
54 * domaine dom
55 * probleme pb
56 * Discretize pb dis
57 * {
58 * Domaine dom
59 * Probleme pb
60 * Discretize pb dis
61 * Lire pb { ... }
62 * }
63 * Lire pb { ... }
64 */
65Domaine_dis_base& Domaine_dis_cache::build_or_get(const Nom& type, const Domaine& dom, const Discretisation_base * disc)
66{
67 // Do we have the prefix "NO_FACE_" at the begining of type:
68 Nom typ_short(type);
69 typ_short.suffix("NO_FACE_");
70
71 // Build a key of the form: "(@x07f8e63)_dom_NO_FACE_Domaine_PolyMAC_HFV"
72 auto make_key = [&](const std::string& t)
73 {
74 std::ostringstream oss;
75 oss << "(@" << &(Interprete_bloc::interprete_courant()) << ")_" << dom.le_nom().getString() << "_" << t;
76 return oss.str();
77 };
78
79 // Query the cache:
80 std::string key = make_key(type.getString());
81 if (cache_.count(key)) return cache_[key].valeur();
82 // We can return the fully discretized domain even if NO_FACE_ was asked, since the former encompasses the latter:
83 std::string key_no_face = make_key(typ_short.getString());
84 if (cache_.count(key_no_face)) return cache_[key_no_face].valeur();
85
86 // OK, not in cache, we really have to discretize:
87
88 cache_[key] = OWN_PTR(Domaine_dis_base)();
89 OWN_PTR(Domaine_dis_base)& ddp = cache_[key];
90 ddp.typer(typ_short);
91 ddp->associer_domaine(dom);
92 if(disc != nullptr)
93 ddp->completer(*disc);
94 ddp->discretiser_root(type);
95
96 // If a full discretisation was requested, we can also register the NO_FACE_ version:
97 /*
98 bool no_face = typ_short != type;
99 if (!no_face)
100 {
101 std::string key_face = make_key("NO_FACE_" + typ_short.getString());
102 cache_[key_face] = ddp; // shared_ptr copy
103 }
104 */
105 return cache_[key].valeur();
106}
107
109{
110 std::string new_type = dom.le_nom().getString() + "_" + type.getString();
111 for (auto &itr : cache_)
112 if (itr.first.find(new_type) != std::string::npos)
113 return cache_[itr.first].valeur();
114
115 Cerr << "Domaine_dis_cache::build_or_get_poly_post ... Trying to build_or_get Domaine_dis for domaine " << dom.le_nom() << " with type " << type << finl;
116 return build_or_get(type, dom);
117}
118
119Domaine_dis_base& Domaine_dis_cache::Build_or_get(const Nom& type, const Domaine& dom, const Discretisation_base * disc)
120{
121 return Get_instance().build_or_get(type, dom, disc);
122}
123
125{
126 return Get_instance().build_or_get_poly_post(type, dom);
127}
classe Discretisation_base Cette classe represente un schema de discretisation en espace,...
const Nom & le_nom() const override
Donne le nom de l'Objet_U Methode a surcharger : renvoie "neant" dans cette implementation.
classe Domaine_dis_base Cette classe est la base de la hierarchie des domaines discretisees.
static Domaine_dis_cache & Get_instance()
Get the unique instance of the cache.
static Domaine_dis_base & Build_or_get_poly_post(const Nom &type, const Domaine &dom)
static Domaine_dis_base & Build_or_get(const Nom &type, const Domaine &dom, const Discretisation_base *disc=nullptr)
Domaine_dis_base & build_or_get_poly_post(const Nom &type, const Domaine &dom)
static void Clear()
Clear the content of the cache. Useful to exit nicely making sure everything is deallocated.
Domaine_dis_base & build_or_get(const Nom &type, const Domaine &dom, const Discretisation_base *disc=nullptr)
Get a discretized domain from the cache, building it and recording it if not there yet.
static Interprete_bloc & interprete_courant()
renvoie l'interprete_bloc en train d'etre lu dans le jeu de donnees.
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
Nom & suffix(const char *const)
Extraction de suffixe : Nom x("azerty");.
Definition Nom.cpp:271
const std::string & getString() const
Definition Nom.h:92
static void Register_clearable(TClearable *c)
Definition TClearable.h:35