Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
SimpleCsvComparatorService.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* SimpleCsvComparatorService.cc (C) 2000-2022 */
9/* */
10/* Service allowing comparison of an ISimpleTableOutput with a reference */
11/* file in CSV format. */
12/*---------------------------------------------------------------------------*/
13/*---------------------------------------------------------------------------*/
14
15#include "arcane/std/SimpleCsvComparatorService.h"
16
17#include "arcane/utils/Iostream.h"
18
19#include "arcane/core/Directory.h"
20#include "arcane/core/IMesh.h"
21#include "arcane/core/IParallelMng.h"
22
23#include <optional>
24#include <regex>
25#include <string>
26
27/*---------------------------------------------------------------------------*/
28/*---------------------------------------------------------------------------*/
29
30namespace Arcane
31{
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
37init(ISimpleTableOutput* simple_table_output_ptr)
38{
39 // We store the provided pointer.
40 ARCANE_CHECK_PTR(simple_table_output_ptr);
41 m_simple_table_output_ptr = simple_table_output_ptr;
42
43 if (!m_is_already_init) {
44 m_is_already_init = true;
45
46 m_simple_table_internal_reference = makeRef(new SimpleTableInternal(mesh()->parallelMng()));
47
48 m_simple_table_internal_comparator.setInternalRef(m_simple_table_internal_reference);
49 m_simple_csv_reader_writer.setInternal(m_simple_table_internal_reference);
50 }
51
52 m_simple_table_internal_to_compare = m_simple_table_output_ptr->internal();
53 m_simple_table_internal_comparator.setInternalToCompare(m_simple_table_internal_to_compare);
54
55 // We deduce the location of the reference files.
56 m_output_directory = m_simple_table_output_ptr->outputDirectory();
57 m_root_path = Directory(subDomain()->exportDirectory(), m_simple_table_output_ptr->fileType() + "_refs");
58 m_reference_path = Directory(m_root_path, m_output_directory);
59 m_table_name = m_simple_table_output_ptr->tableName();
60 m_file_name = m_table_name + "." + m_simple_table_output_ptr->fileType();
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
67clear()
68{
69 m_simple_table_internal_comparator.clearComparator();
70 m_is_file_read = false;
71
72 if (m_is_already_init) {
73 m_simple_table_internal_reference->clear();
74 }
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
81editRootDirectory(const Directory& root_directory)
82{
83 m_root_path = root_directory;
84 m_reference_path = Directory(m_root_path, m_output_directory);
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
91print(Integer rank)
92{
93 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank)
94 return;
95 m_simple_csv_reader_writer.print();
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
106{
107 ARCANE_CHECK_PTR(m_simple_table_output_ptr);
108 // We save the original parameters.
109 Integer save_preci = m_simple_table_output_ptr->precision();
110 bool save_scientific = m_simple_table_output_ptr->isForcedToUseScientificNotation();
111 bool save_fixed = m_simple_table_output_ptr->isFixed();
112
113 // We set the max precision (+2 for rounding errors).
114 m_simple_table_output_ptr->setPrecision(std::numeric_limits<Real>::max_digits10);
115 m_simple_table_output_ptr->setForcedToUseScientificNotation(true);
116 m_simple_table_output_ptr->setFixed(false);
117
118 // We write our reference files.
119 bool fin = m_simple_table_output_ptr->writeFile(m_root_path, rank);
120
121 // We restore the default parameters.
122 m_simple_table_output_ptr->setPrecision(save_preci);
123 m_simple_table_output_ptr->setForcedToUseScientificNotation(save_scientific);
124 m_simple_table_output_ptr->setFixed(save_fixed);
125
126 return fin;
127}
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
131
134{
135 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank)
136 return true;
137
138 m_is_file_read = m_simple_csv_reader_writer.readTable(m_reference_path, m_table_name);
139
140 return m_is_file_read;
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
148{
149 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank)
150 return true;
151
152 return SimpleTableReaderWriterUtils::isFileExist(m_reference_path, m_file_name);
153}
154
155/*---------------------------------------------------------------------------*/
156/*---------------------------------------------------------------------------*/
157
159compareWithReference(Integer rank, bool compare_dimension_too)
160{
161 // If the calling process should not read.
162 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank) {
163 return true;
164 }
165 // If the file cannot be read.
166 if (!m_is_file_read && !readReferenceFile(rank)) {
167 error() << "Error with the file reader: invalid file";
168 return false;
169 }
170
171 return m_simple_table_internal_comparator.compare(compare_dimension_too);
172}
173
174/*---------------------------------------------------------------------------*/
175/*---------------------------------------------------------------------------*/
176
178compareElemWithReference(const String& column_name, const String& row_name, Integer rank)
179{
180 // If the calling process should not read.
181 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank) {
182 return true;
183 }
184 // If the file cannot be read.
185 if (!m_is_file_read && !readReferenceFile(rank)) {
186 error() << "Error with the file reader: invalid file";
187 return false;
188 }
189
190 return m_simple_table_internal_comparator.compareElem(column_name, row_name);
191}
192
193/*---------------------------------------------------------------------------*/
194/*---------------------------------------------------------------------------*/
195
197compareElemWithReference(Real elem, const String& column_name, const String& row_name, Integer rank)
198{
199 // If the calling process should not read.
200 if (rank != -1 && subDomain()->parallelMng()->commRank() != rank) {
201 return true;
202 }
203 // If the file cannot be read.
204 if (!m_is_file_read && !readReferenceFile(rank)) {
205 error() << "Error with the file reader: invalid file";
206 return false;
207 }
208
209 return m_simple_table_internal_comparator.compareElem(elem, column_name, row_name);
210}
211
212/*---------------------------------------------------------------------------*/
213/*---------------------------------------------------------------------------*/
214
216addColumnForComparing(const String& column_name)
217{
218 return m_simple_table_internal_comparator.addColumnForComparing(column_name);
219}
220
221/*---------------------------------------------------------------------------*/
222/*---------------------------------------------------------------------------*/
223
225addRowForComparing(const String& row_name)
226{
227 return m_simple_table_internal_comparator.addRowForComparing(row_name);
228}
229
230/*---------------------------------------------------------------------------*/
231/*---------------------------------------------------------------------------*/
232
234isAnArrayExclusiveColumns(bool is_exclusive)
235{
236 m_simple_table_internal_comparator.isAnArrayExclusiveColumns(is_exclusive);
237}
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
243isAnArrayExclusiveRows(bool is_exclusive)
244{
245 m_simple_table_internal_comparator.isAnArrayExclusiveRows(is_exclusive);
246}
247
248/*---------------------------------------------------------------------------*/
249/*---------------------------------------------------------------------------*/
250
252editRegexColumns(const String& regex_column)
253{
254 m_simple_table_internal_comparator.editRegexColumns(regex_column);
255}
256
257/*---------------------------------------------------------------------------*/
258/*---------------------------------------------------------------------------*/
259
261editRegexRows(const String& regex_row)
262{
263 m_simple_table_internal_comparator.editRegexRows(regex_row);
264}
265
266/*---------------------------------------------------------------------------*/
267/*---------------------------------------------------------------------------*/
268
270isARegexExclusiveColumns(bool is_exclusive)
271{
272 m_simple_table_internal_comparator.isARegexExclusiveColumns(is_exclusive);
273}
274
275/*---------------------------------------------------------------------------*/
276/*---------------------------------------------------------------------------*/
277
279isARegexExclusiveRows(bool is_exclusive)
280{
281 m_simple_table_internal_comparator.isARegexExclusiveRows(is_exclusive);
282}
283
284/*---------------------------------------------------------------------------*/
285/*---------------------------------------------------------------------------*/
286
288addEpsilonColumn(const String& column_name, Real epsilon)
289{
290 return m_simple_table_internal_comparator.addEpsilonColumn(column_name, epsilon);
291}
292
293/*---------------------------------------------------------------------------*/
294/*---------------------------------------------------------------------------*/
295
297addEpsilonRow(const String& row_name, Real epsilon)
298{
299 return m_simple_table_internal_comparator.addEpsilonRow(row_name, epsilon);
300}
301
302/*---------------------------------------------------------------------------*/
303/*---------------------------------------------------------------------------*/
304
305ARCANE_REGISTER_SERVICE_SIMPLECSVCOMPARATOR(SimpleCsvComparator, SimpleCsvComparatorService);
306
307/*---------------------------------------------------------------------------*/
308/*---------------------------------------------------------------------------*/
309
310} // End namespace Arcane
311
312/*---------------------------------------------------------------------------*/
313/*---------------------------------------------------------------------------*/
Class managing a directory.
Definition Directory.h:36
Interface representing a simple table output.
virtual Ref< SimpleTableInternal > internal()=0
Method allowing retrieval of a reference to the SimpleTableInternal object used.
void init(ISimpleTableOutput *simple_table_output_ptr) override
Method allowing the service to be initialized.
bool addColumnForComparing(const String &column_name) override
Method allowing a column to be added to the list of columns to be compared.
void editRegexColumns(const String &regex_column) override
Method allowing a regular expression to be added to determine the columns to compare.
bool addRowForComparing(const String &row_name) override
Method allowing a row to be added to the list of rows to be compared.
void isAnArrayExclusiveColumns(bool is_exclusive) override
Method allowing definition whether the array of columns represents the columns to include in the comp...
bool addEpsilonRow(const String &row_name, Real epsilon) override
Method allowing an epsilon to be defined for a given row. This epsilon must be positive to be taken i...
void isARegexExclusiveColumns(bool is_exclusive) override
Method allowing to request that the regular expression excludes columns instead of including them.
void clear() override
Method allowing the data read by readReferenceFile() to be cleared.
void print(Integer rank) override
Method allowing the read table to be displayed.
bool isReferenceExist(Integer rank) override
Method allowing to check if the reference files exist.
void isAnArrayExclusiveRows(bool is_exclusive) override
Method allowing definition whether the array of rows represents the rows to include in the comparison...
bool compareWithReference(Integer rank, bool compare_dimension_too) override
Method allowing the ISimpleTableOutput object to be compared to the reference files.
void editRegexRows(const String &regex_row) override
Method allowing a regular expression to be added to determine the rows to compare.
bool readReferenceFile(Integer rank) override
Method allowing reference files to be read.
bool addEpsilonColumn(const String &column_name, Real epsilon) override
Method allowing an epsilon to be defined for a given column. This epsilon must be positive to be take...
void isARegexExclusiveRows(bool is_exclusive) override
Method allowing to request that the regular expression excludes rows instead of including them.
bool compareElemWithReference(const String &column_name, const String &row_name, Integer rank) override
Method allowing only an element to be compared. Both SimpleTableInternals are represented by Refs,...
void editRootDirectory(const Directory &root_directory) override
Method allowing the root directory to be modified. This allows writing or searching for reference fil...
static bool isFileExist(const Directory &directory, const String &file)
Static method allowing verification of file existence.
TraceMessage error() const
Flow for an error message.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
Structure representing a simple table.