Plugins
Loading...
Searching...
No Matches
value_by_id_impl.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Gesellschaft fuer Anlagen- und Reaktorsicherheit gGmbH
3 * SPDX-License-Identifier: EUPL-1.2
4 * SPDX-FileContributor: Dmitry Logashenko
5 * SPDX-FileContributor: Goethe Universität Frankfurt
6 * SPDX-FileType: SOURCE
7 *
8 * This file is part of d3f++.
9 * d3f++ is an extension for UG4. Licensing information and citation requirements of UG4 are provided in LICENSES/UG4-LGPL_2.1
10 */
11
12
13
15
16namespace ug {
17namespace d3f {
18
22template <typename TIdManager>
24(
25 number x, number y,
26 number t
27) const
28{
29 if (m_numCols < 1)
30 UG_THROW ("ValueById: No data read yet!");
31
32 int ident;
33 if (! this->get_id_by_pnt (x, y, ident))
34 return m_default_value;
35 if (ident < 0 || ident >= (int) m_data.size () || ! m_assigned[ident])
36 UG_THROW ("ValueById: Inquired id " << ident << " not assigned!");
37
38 size_t col = size_t (floor (t / m_interval)) % m_numCols;
39 return m_data [ident * m_numCols + col];
40}
41
45template <typename TIdManager>
47(
48 long ident,
49 std::vector<number>& val
50)
51{
52 if (ident < 0)
53 UG_THROW ("ValueById: Id < 0 not supported!");
54
55 if (m_numCols < 1) // still no data in the object
56 {
57 m_data.clear (); m_assigned.clear ();
58 m_numCols = val.size ();
59 size_t init_size = ident + 1;
60 m_data.resize (init_size * m_numCols);
61 m_assigned.resize (init_size, false);
62 }
63 else
64 {
65 if (val.size () != m_numCols)
66 UG_THROW ("ValueById: Data length should be equal for all Id's!");
67
68 if ((size_t) ident >= m_assigned.size ())
69 {
70 size_t new_size = ident + 1;
71 m_data.resize (new_size * m_numCols);
72 m_assigned.resize (new_size, false);
73 }
74 else if (m_assigned[ident])
75 UG_THROW ("ValueById: Redefining value for id " << ident);
76 }
77
78 for (size_t col = 0; col < m_numCols; col++)
79 m_data[ident * m_numCols + col] = val[col];
80 m_assigned[ident] = true;
81}
82
86template <typename TIdManager>
87void ValueById<TIdManager>::load_values_from (std::istream & input)
88{
89 size_t num_lines = 0;
90 size_t num_data_lines = 0;
91 std::string input_line;
92 std::vector<number> values;
93
94 m_numCols = 0; // to clear the data
95
96 try
97 {
98 while (! input.eof ())
99 {
100 long ident;
101
102 num_lines++;
103 if (input.fail ())
104 UG_THROW ("ValueById: Could not load the points from the file!");
105 std::getline (input, input_line);
106 if (input_line.length () == 0)
107 continue;
108
109 const char * str = input_line.c_str ();
110 char * end_ptr;
111 while (*str && std::isspace (*str)) str++;
112 if (*str == '#')
113 continue; // this is a comment
114 ident = std::strtol (str, &end_ptr, 10);
115 if (end_ptr == str)
116 UG_THROW ("ValueById: Failed to parse the identifier.\n");
117 str = end_ptr;
118
119 while (*str && std::isspace (*str)) str++;
120 values.clear ();
121 while (*str && *str != '#')
122 {
123 number v = std::strtod (str, &end_ptr);
124 if (end_ptr == str)
125 UG_THROW ("ValueById: Failed to parse a value.\n");
126 values.push_back (v);
127 str = end_ptr;
128 while (*str && std::isspace (*str)) str++;
129 }
130
131 append_value (ident, values);
132 num_data_lines++;
133 }
134 }
135 UG_CATCH_THROW ("ValueById: Error in line no. " << num_lines << ": '" << input_line << "'\n");
136
137 UG_LOG
138 (
139 "ValueById: " << num_lines << " lines read from the file"
140 " (" << num_data_lines << " data lines with " << m_numCols << " values per line).\n"
141 );
142}
143
147template <typename TIdManager>
148void ValueById<TIdManager>::load_values_from (const char * file_name)
149{
150 std::string fullFileName = FindFileInStandardPaths(file_name);
151 UG_COND_THROW (fullFileName.empty(), "ValueById: Coulnd't locate file '" << file_name);
152
153 std::ifstream input (fullFileName.c_str(), std::ifstream::in);
154
155 if (input.fail ())
156 UG_THROW ("ValueById: Failed to open data file '" << fullFileName << "' for input!");
157 UG_LOG ("ValueById: Reading data from '" << fullFileName << "'\n");
158 load_values_from (input);
159}
160
161} // namespace d3f
162} // end namespace ug
163
164/* End of File */
void append_value(long ident, std::vector< number > &val)
appends a value
Definition value_by_id_impl.h:47
void load_values_from(const char *file_name)
loads values from a file
Definition value_by_id_impl.h:148
number interpolate(number x, number y, number t) const
gets the value for a given point
Definition value_by_id_impl.h:24
std::string FindFileInStandardPaths(const char *filename)
#define UG_CATCH_THROW(msg)
#define UG_THROW(msg)
#define UG_LOG(msg)
#define UG_COND_THROW(cond, msg)
double number