TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Read_unsupported_ASCII_file_from_ICEM.cpp
1/****************************************************************************
2* Copyright (c) 2024, 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 <Read_unsupported_ASCII_file_from_ICEM.h>
17#include <Lire_Fichier.h>
18#include <SFichierBin.h>
19#include <EFichierBin.h>
20#include <Interprete.h>
21#include <TRUSTTab.h>
22#include <Objet_U.h>
23#include <Motcle.h>
24
25
26// PLEASE DO NOT REMOVE Read_unsupported_ASCII_file_from_ICEM KEYWORD FROM TRUST
27// IT IS STILL USEFUL (in v1.9.7) TO READ OLD ICEM FILES (ND, 07/11/2025)
28
29Implemente_instanciable(Read_unsupported_ASCII_file_from_ICEM,"Read_unsupported_ASCII_file_from_ICEM",Lire_Fichier);
30// XD read_unsupported_ascii_file_from_icem read_file read_unsupported_ascii_file_from_icem INHERITS_BRACE not_set
31
33{
34 return os;
35}
36
38{
39 return is;
40}
41
42// Used function
43inline char read_write_string_from(EFichierBin& s,SFichierBin& o, const char* first_caracter)
44{
45 // The rule is:
46 // Read up to first_caracter if it is defined
47 // Pad with the separator 00
48 // Return the last character (before the space)
49 char octet;
50 char zero=0;
51 char last_caracter;
52 // If first_caracter is defined, read up to it
53 if (*first_caracter!=0)
54 {
55#ifndef NDEBUG
56 Cerr << " -> Look for the byte:";
57 fprintf(stderr," %02x <=> ",*first_caracter);
58 Cerr << (Nom)*first_caracter << finl;
59 Cerr << " -> Read the bytes :";
60#endif
61 //while (s.good() && s.get_istream().read(&octet,1)!=0 && octet!=*first_caracter)
62 while (s.good() && s.get_istream().read(&octet,1) && octet!=*first_caracter)
63 {
64#ifndef NDEBUG
65 fprintf(stderr," %02x",octet);
66#endif
67 };
68 }
69 else
70 {
71 // If first_caracter is not defined, simply skip
72 // spaces (32, 20 in hex) and separators (00) and delete (127, 7F in hex)
73#ifndef NDEBUG
74 Cerr << " -> Read the bytes :";
75#endif
76 //while (s.good() && s.get_istream().read(&octet,1)!=0 && (octet==32 || octet==0 || octet==127) )
77 while (s.good() && s.get_istream().read(&octet,1) && (octet==32 || octet==0 || octet==127) )
78 {
79#ifndef NDEBUG
80 fprintf(stderr," %02x",octet);
81#endif
82 };
83 }
84#ifndef NDEBUG
85 fprintf(stderr," %02x",octet);
86#endif
87 o.get_ostream().write(&octet,1);
88 last_caracter = octet;
89 Nom chaine(octet);
90 // Read up to the space:
91 //while (s.good() && s.get_istream().read(&octet,1)!=0 && octet!=32)
92 while (s.good() && s.get_istream().read(&octet,1) && octet!=32)
93 {
94#ifndef NDEBUG
95 fprintf(stderr," %02x",octet);
96#endif
97 o.get_ostream().write(&octet,1);
98 last_caracter = octet;
99 chaine+=octet;
100 }
101#ifndef NDEBUG
102 fprintf(stderr," %02x",octet);
103#endif
104 o.get_ostream().write(&zero,1);
105#ifndef NDEBUG
106 Cerr << finl;
107 Cerr << " -> Read the string: " << chaine << finl;
108#endif
109 return last_caracter;
110}
111// Function that checks if we are reading an ICEM binary file and if so
112// creates a TRUST-compatible file
113void check_ICEM_binary_file(Nom& filename, const Nom& nom_objet_lu)
114{
115 // Check that the read object is a domain
116 Objet_U& objet_lu = Interprete::objet(nom_objet_lu);
117 // [ABN] hmmmm ... hopefully the only place where we have this sort of things....:
118 if (!(objet_lu.que_suis_je()=="Domaine" || objet_lu.que_suis_je()=="Domaine")) return;
119
120 EFichierBin tmp(filename);
121 // An ASCII binary file is recognizable by the fact that the domain name
122 // is followed by a space, so the byte is 32 (= 20 in hex = space)
123 // In a standard binary file the domain name is followed by 0
124 char octet=(char)-1;
125 while(octet!=32 && octet!=0)
126 tmp.get_istream().read(&octet,1);
127 tmp.close();
128 if (octet==32) // This is an ICEM binary file
129 {
130 char zero=0;
131 Cerr << "==============================================" << finl;
132 Cerr << filename << " is an ICEM binary file." << finl;
133 Cerr << "To save space, you can now delete this file and use" << finl;
134 // Removal of certain spaces from the binary file
135 Nom new_filename(filename);
136 new_filename+=".cleaned";
137 Cerr << "instead, the newly created " << new_filename << " file." << finl;
138 Cerr << "==============================================" << finl;
139 EFichierBin s(filename);
140 SFichierBin o(new_filename);
141 s.get_istream().read(&octet,1);
142 while(octet!=32)
143 {
144 o.get_ofstream().write(&octet,1);
145 s.get_istream().read(&octet,1);
146 }
147 // Write 00 in place of 32
148 o.get_ostream().write(&zero,1);
149 // Then go up to integer 2
150 s.get_istream().read(&octet,1);
151 while(octet!=2)
152 s.get_istream().read(&octet,1);
153 // Go back and start reading the DoubleTab
154 s.get_istream().unget();
155 // Read nodes
156 DoubleTab sommets;
157 s >> sommets;
158 o << sommets;
159 Cerr << "End of the read of the nodes." << finl;
160 // "{"
161 if (read_write_string_from(s,o,"{")!=123)
162 {
163 Cerr << "Error in is_a_ICEM_binary_file : { is waited." << finl;
165 }
166 // Domain name
167 read_write_string_from(s,o,"");
168 // Read element type
169 read_write_string_from(s,o,"T"); // TETRAEDRE
170 // Read elements
171 IntTab elems;
172 s >> elems;
173 o << elems;
174 Cerr << "End of the read of the cells." << finl;
175 // Loop over boundaries
176 // { : 7b in hex, 123 in decimal
177 // , : 2C in hex, 44 in decimal
178 // } : 7D in hex, 125 in decimal
179 const char* sep="{";
180 while(read_write_string_from(s,o,sep)!=125)
181 {
182 // Boundary name
183 read_write_string_from(s,o,"");
184 // Element type
185 read_write_string_from(s,o,"T"); // TRIANGLE_3D
186 // Boundary elements
187 s >> elems;
188 o << elems;
189 // Neighbor faces
190 IntTab faces_voisins;
191 s >> faces_voisins;
192 o << faces_voisins;
193 Cerr << "End of the read of a boundary." << finl;
194 sep=""; // , or } cannot be known in advance...
195 }
196 // Read the 3 "vide" entries
197 for (int i=0; i<3; i++)
198 {
199 if (read_write_string_from(s,o,"v")!=101)
200 {
201 Cerr << "Error in is_a_ICEM_binary_file : 'vide' is waited." << finl;
203 }
204 }
205 // Read }
206 if (read_write_string_from(s,o,"}")!=125)
207 {
208 Cerr << "Error in is_a_ICEM_binary_file : } is waited." << finl;
210 }
211 // Read the last "vide" entry
212 read_write_string_from(s,o,"v");
213 o.close();
214 s.close();
215 // Point to the new file
216 filename=new_filename;
217 Cerr << "=============================================" << finl;
218 Cerr << "You can use now: " << finl;
219 Cerr << "Lire_fichier " << nom_objet_lu << " "<< filename << finl;
220 Cerr << "=============================================" << finl;
221 }
222}
223
224void check_ICEM_ascii_file(const Nom& filename, const Lire_Fichier& keyword)
225{
226 Nom ICEM(filename);
227 if (ICEM.finit_par(".asc") && !sub_type(Read_unsupported_ASCII_file_from_ICEM,keyword))
228 {
229 Cerr << "Since the 1.6.7 version, the read of an ASCII file from ICEM mesh tool is not" << finl;
230 Cerr << "supported anymore cause insufficient number of digits to define the node coordinates." <<finl;
231 Cerr << "Please, use the TRUST binary export from ICEM." << finl;
233 }
234 else if (sub_type(Read_unsupported_ASCII_file_from_ICEM,keyword))
235 {
236 Cerr << "====================================================" << finl;
237 Cerr << "Warning: You are using an obsolete feature of TRUST" << finl;
238 Cerr << "with Unsupported_ASCII_file_read_from_ICEM keyword." << finl;
239 Cerr << "Use binary format for ICEM file!" << finl;
240 Cerr << "====================================================" << finl;
241 }
242}
Reading from a file of objects written in binary format.
Definition EFichierBin.h:30
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
virtual istream & get_istream()
Definition Entree.cpp:41
static Objet_U & objet(const Nom &)
See Interprete_bloc::objet_global(). BM: the Interprete class is not the best place for this.
class Lire_Fichier: reads a file.
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
const Nom & que_suis_je() const
Returns the string identifying the class.
Definition Objet_U.cpp:104
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
Writes objects to a file in binary format.
Definition SFichierBin.h:26
Base class for output streams.
Definition Sortie.h:52
ostream & get_ostream()
Definition Sortie.h:64