medcoupling 9.11.0__cp310-cp310-win_amd64.whl → 9.15.0__cp310-cp310-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
CaseWriter.py CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- coding: iso-8859-1 -*-
2
- # Copyright (C) 2007-2023 CEA, EDF
2
+ # Copyright (C) 2007-2025 CEA, EDF
3
3
  #
4
4
  # This library is free software; you can redistribute it and/or
5
5
  # modify it under the terms of the GNU Lesser General Public
@@ -22,22 +22,23 @@
22
22
  import numpy as np
23
23
  from CaseIO import CaseIO
24
24
  from MEDLoader import *
25
- import sys,re,os,mmap
25
+ import sys, re, os, mmap
26
26
 
27
27
  ### www-vis.lbl.gov/NERSC/Software/ensight/doc/OnlineHelp/UM-C11.pdf
28
28
 
29
+
29
30
  class CaseWriter(CaseIO):
30
- """ Converting MED file format in memory to a the Case file format (Ensight).
31
+ """Converting MED file format in memory to a the Case file format (Ensight).
31
32
  A new file with the same base name and the .case extension is created with its dependencies (.geo ...).
32
33
  """
33
34
 
34
- header="""FORMAT
35
+ header = """FORMAT
35
36
  type: ensight gold
36
37
  GEOMETRY
37
38
  model: %(geofilewithoutpath)s
38
39
  """
39
- header_varpart="""VARIABLE"""
40
- header_timepart="""TIME
40
+ header_varpart = """VARIABLE"""
41
+ header_timepart = """TIME
41
42
  time set: 1
42
43
  number of steps: %(NbTimeSteps)i
43
44
  filename start number: 0
@@ -48,302 +49,473 @@ time values:
48
49
 
49
50
  @classmethod
50
51
  def New(cls):
51
- """ Static constructor. """
52
+ """Static constructor."""
52
53
  return CaseWriter()
53
54
  pass
54
55
 
55
56
  def __init__(self):
56
- """ Constructor """
57
- self.__export_groups=False
57
+ """Constructor"""
58
+ self.__export_groups = False
58
59
  pass
59
60
 
60
- def setMEDFileDS(self,medData):
61
- """ Input should be MEDFileData instance """
62
- self._med_data=medData
61
+ def setMEDFileDS(self, medData):
62
+ """Input should be MEDFileData instance"""
63
+ self._med_data = medData
63
64
  pass
64
65
 
65
66
  def isExportingGroups(self):
66
- """ return the status of exporting groups policy """
67
+ """return the status of exporting groups policy"""
67
68
  return self.__export_groups
68
69
 
69
- def setExportingGroups(self,status):
70
- assert(isinstance(status,bool))
71
- self.__export_groups=status
70
+ def setExportingGroups(self, status):
71
+ assert isinstance(status, bool)
72
+ self.__export_groups = status
72
73
  pass
73
74
 
74
-
75
- def write(self,fileName):
76
- """ Write into the specified fileName series the result """
77
- self._file_name=fileName
78
- self._base_name_without_dir=os.path.splitext(os.path.basename(self._file_name))[0]
79
- self._l=self._file_name.split(os.path.sep) ; self._l[-1]=os.path.splitext(self._l[-1])[0]
80
- self._base_name_with_dir=os.path.sep.join(self._l)
81
- self._real_written_file_name=[]
82
- self._dico={}
75
+ def write(self, fileName):
76
+ """Write into the specified fileName series the result"""
77
+ self._file_name = fileName
78
+ self._base_name_without_dir = os.path.splitext(
79
+ os.path.basename(self._file_name)
80
+ )[0]
81
+ self._l = self._file_name.split(os.path.sep)
82
+ self._l[-1] = os.path.splitext(self._l[-1])[0]
83
+ self._base_name_with_dir = os.path.sep.join(self._l)
84
+ self._real_written_file_name = []
85
+ self._dico = {}
83
86
  for mesh in self._med_data.getMeshes():
84
- additionnalFileNamePart=""
85
- if len(self._med_data.getMeshes())!=1:
86
- additionnalFileNamePart="_%s"%(mesh.getName())
87
+ additionnalFileNamePart = ""
88
+ if len(self._med_data.getMeshes()) != 1:
89
+ additionnalFileNamePart = "_%s" % (mesh.getName())
87
90
  pass
88
- self._dico["geofilewithoutpath"]="%s%s.geo"%(self._base_name_without_dir,additionnalFileNamePart)
89
- h0=self.header%self._dico
90
- self.__writeMeshesPart(mesh,"%s%s.geo"%(self._base_name_with_dir,additionnalFileNamePart))
91
+ self._dico["geofilewithoutpath"] = "%s%s.geo" % (
92
+ self._base_name_without_dir,
93
+ additionnalFileNamePart,
94
+ )
95
+ h0 = self.header % self._dico
96
+ self.__writeMeshesPart(
97
+ mesh, "%s%s.geo" % (self._base_name_with_dir, additionnalFileNamePart)
98
+ )
91
99
  #
92
- h2=self.__writeFieldsPart(self._med_data.getFields().partOfThisLyingOnSpecifiedMeshName(mesh.getName()))
93
- realWrittenCaseFileNameForCurMesh="%s%s.case"%(self._base_name_with_dir,additionnalFileNamePart)
94
- fheader=open(realWrittenCaseFileNameForCurMesh,"w") ; fheader.write((h0+h2)%self._dico)
100
+ h2 = self.__writeFieldsPart(
101
+ self._med_data.getFields().partOfThisLyingOnSpecifiedMeshName(
102
+ mesh.getName()
103
+ )
104
+ )
105
+ realWrittenCaseFileNameForCurMesh = "%s%s.case" % (
106
+ self._base_name_with_dir,
107
+ additionnalFileNamePart,
108
+ )
109
+ fheader = open(realWrittenCaseFileNameForCurMesh, "w")
110
+ fheader.write((h0 + h2) % self._dico)
95
111
  self._real_written_file_name.append(realWrittenCaseFileNameForCurMesh)
96
112
  pass
97
113
  return self._real_written_file_name
98
114
 
99
- def __writeMeshesPart(self,mdm,meshfn):
115
+ def __writeMeshesPart(self, mdm, meshfn):
100
116
  try:
101
117
  os.remove(meshfn)
102
118
  except:
103
119
  pass
104
- f=open(meshfn,"w+b")
105
- sz=5*80
120
+ f = open(meshfn, "w+b")
121
+ sz = 5 * 80
106
122
  #
107
- assert(isinstance(mdm,MEDFileUMesh))
108
- ms2=[[mdm.getMeshAtLevel(lev) for lev in mdm.getNonEmptyLevels()[:1]]]
123
+ assert isinstance(mdm, MEDFileUMesh)
124
+ ms2 = [[mdm.getMeshAtLevel(lev) for lev in mdm.getNonEmptyLevels()[:1]]]
109
125
  if self.__export_groups:
110
126
  for grpnm in mdm.getGroupsNames():
111
- ms3=[]
127
+ ms3 = []
112
128
  for lev in mdm.getGrpNonEmptyLevels(grpnm)[:1]:
113
- m=mdm.getGroup(lev,grpnm) ; m.zipCoords()
129
+ m = mdm.getGroup(lev, grpnm)
130
+ m.zipCoords()
114
131
  ms3.append(m)
115
132
  pass
116
133
  ms2.append(ms3)
117
134
  pass
118
135
  pass
119
136
  for ms in ms2:
120
- nn=ms[0].getNumberOfNodes()
121
- sz+=self.__computeSizeOfGeoFile(ms,nn)
137
+ nn = ms[0].getNumberOfNodes()
138
+ sz += self.__computeSizeOfGeoFile(ms, nn)
122
139
  pass
123
140
  pass
124
- a=np.memmap(f,dtype='byte',mode='w+',offset=0,shape=(sz,)) ; a.flush() # truncate to set the size of the file
125
- mm=mmap.mmap(f.fileno(),offset=0,length=0)
141
+ a = np.memmap(f, dtype="byte", mode="w+", offset=0, shape=(sz,))
142
+ a.flush() # truncate to set the size of the file
143
+ mm = mmap.mmap(f.fileno(), offset=0, length=0)
126
144
  mm.write(self.__str80("C Binary"))
127
- mm.write(self.__str80("Exported from MEDCoupling/MEDLoader version %s"%(MEDCouplingVersionStr())))
145
+ mm.write(
146
+ self.__str80(
147
+ "Exported from MEDCoupling/MEDLoader version %s"
148
+ % (MEDCouplingVersionStr())
149
+ )
150
+ )
128
151
  mm.write(self.__str80("Conversion using CaseWriter class"))
129
152
  mm.write(self.__str80("node id off"))
130
153
  mm.write(self.__str80("element id off"))
131
- for iii,ms in enumerate(ms2):
132
- nn=ms[0].getNumberOfNodes()
154
+ for iii, ms in enumerate(ms2):
155
+ nn = ms[0].getNumberOfNodes()
133
156
  mm.write(self.__str80("part"))
134
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(1,))
135
- a[0]=iii+1 ; a.flush() ; mm.seek(mm.tell()+4) # part number maybe to change ?
136
- name=ms[0].getName()
137
- if iii>0:
138
- name="%s_%s"%(ms2[0][0].getName(),name)
157
+ a = np.memmap(f, dtype="int32", mode="w+", offset=mm.tell(), shape=(1,))
158
+ a[0] = iii + 1
159
+ a.flush()
160
+ mm.seek(mm.tell() + 4) # part number maybe to change ?
161
+ name = ms[0].getName()
162
+ if iii > 0:
163
+ name = "%s_%s" % (ms2[0][0].getName(), name)
139
164
  pass
140
165
  mm.write(self.__str80(name))
141
166
  mm.write(self.__str80("coordinates"))
142
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(1,))
143
- a[0]=nn ; a.flush() # number of nodes
144
- mm.seek(mm.tell()+4)
145
- coo=ms[0].getCoords()
146
- spaceDim=coo.getNumberOfComponents()
147
- if spaceDim!=3:
148
- coo=coo.changeNbOfComponents(3,0.)
167
+ a = np.memmap(f, dtype="int32", mode="w+", offset=mm.tell(), shape=(1,))
168
+ a[0] = nn
169
+ a.flush() # number of nodes
170
+ mm.seek(mm.tell() + 4)
171
+ coo = ms[0].getCoords()
172
+ spaceDim = coo.getNumberOfComponents()
173
+ if spaceDim != 3:
174
+ coo = coo.changeNbOfComponents(3, 0.0)
149
175
  pass
150
- a=np.memmap(f,dtype='float32',mode='w+',offset=mm.tell(),shape=(3,nn))
151
- c=coo.toNoInterlace() ; c.rearrange(1) ; cnp=c.toNumPyArray() ; cnp=cnp.reshape(3,nn)
152
- a[:]=cnp ; a.flush() ; mm.seek(mm.tell()+3*nn*4)
176
+ a = np.memmap(
177
+ f, dtype="float32", mode="w+", offset=mm.tell(), shape=(3, nn)
178
+ )
179
+ c = coo.toNoInterlace()
180
+ c.rearrange(1)
181
+ cnp = c.toNumPyArray()
182
+ cnp = cnp.reshape(3, nn)
183
+ a[:] = cnp
184
+ a.flush()
185
+ mm.seek(mm.tell() + 3 * nn * 4)
153
186
  for m in ms:
154
- i=0
155
- for typ2,nbelem,dummy in m.getDistributionOfTypes():
156
- typ=typ2
187
+ i = 0
188
+ for typ2, nbelem, dummy in m.getDistributionOfTypes():
189
+ typ = typ2
157
190
  if typ not in self.dictMCTyp:
158
- typ=MEDCouplingMesh.GetCorrespondingPolyType(typ)
191
+ typ = MEDCouplingMesh.GetCorrespondingPolyType(typ)
159
192
  pass
160
- mp=m[i:i+nbelem]
193
+ mp = m[i : i + nbelem]
161
194
  mm.write(self.__str80(self.dictMCTyp_str[typ]))
162
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(1,))
163
- a[0]=nbelem ; a.flush() ; mm.seek(mm.tell()+4)
164
- if typ!=NORM_POLYHED and typ!=NORM_POLYGON:
165
- nbNodesPerElem=MEDCouplingMesh.GetNumberOfNodesOfGeometricType(typ)
166
- c=mp.getNodalConnectivity() ; c.rearrange(nbNodesPerElem+1) ; c=c[:,1:] ; c.rearrange(1) ; c+=1
167
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(nbNodesPerElem*nbelem,))
168
- a[:]=c.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+nbNodesPerElem*nbelem*4)
195
+ a = np.memmap(
196
+ f, dtype="int32", mode="w+", offset=mm.tell(), shape=(1,)
197
+ )
198
+ a[0] = nbelem
199
+ a.flush()
200
+ mm.seek(mm.tell() + 4)
201
+ if typ != NORM_POLYHED and typ != NORM_POLYGON:
202
+ nbNodesPerElem = (
203
+ MEDCouplingMesh.GetNumberOfNodesOfGeometricType(typ)
204
+ )
205
+ c = mp.getNodalConnectivity()
206
+ c.rearrange(nbNodesPerElem + 1)
207
+ c = c[:, 1:]
208
+ c.rearrange(1)
209
+ c += 1
210
+ a = np.memmap(
211
+ f,
212
+ dtype="int32",
213
+ mode="w+",
214
+ offset=mm.tell(),
215
+ shape=(nbNodesPerElem * nbelem,),
216
+ )
217
+ a[:] = c.toNumPyArray()
218
+ a.flush()
219
+ mm.seek(mm.tell() + nbNodesPerElem * nbelem * 4)
169
220
  pass
170
- elif typ==NORM_POLYHED:
221
+ elif typ == NORM_POLYHED:
171
222
  mp.orientCorrectlyPolyhedrons()
172
- c=mp.computeNbOfFacesPerCell()
173
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(nbelem,))
174
- a[:]=c.toNumPyArray(); a.flush() ; mm.seek(mm.tell()+nbelem*4)
175
- c=mp.getNodalConnectivity()[:] ; c.pushBackSilent(-1) ; c[mp.getNodalConnectivityIndex()[:-1]]=-1 ; ids=c.findIdsEqual(-1) ; nbOfNodesPerFace=ids.deltaShiftIndex()-1
176
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(len(nbOfNodesPerFace),))
177
- a[:]=nbOfNodesPerFace.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+len(nbOfNodesPerFace)*4)
178
- ids2=ids.buildComplement(ids.back()+1)
179
- c2=mp.getNodalConnectivity()[ids2]+1
180
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(len(c2),))
181
- a[:]=c2.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+len(c2)*4)
223
+ c = mp.computeNbOfFacesPerCell()
224
+ a = np.memmap(
225
+ f,
226
+ dtype="int32",
227
+ mode="w+",
228
+ offset=mm.tell(),
229
+ shape=(nbelem,),
230
+ )
231
+ a[:] = c.toNumPyArray()
232
+ a.flush()
233
+ mm.seek(mm.tell() + nbelem * 4)
234
+ c = mp.getNodalConnectivity()[:]
235
+ c.pushBackSilent(-1)
236
+ c[mp.getNodalConnectivityIndex()[:-1]] = -1
237
+ ids = c.findIdsEqual(-1)
238
+ nbOfNodesPerFace = ids.deltaShiftIndex() - 1
239
+ a = np.memmap(
240
+ f,
241
+ dtype="int32",
242
+ mode="w+",
243
+ offset=mm.tell(),
244
+ shape=(len(nbOfNodesPerFace),),
245
+ )
246
+ a[:] = nbOfNodesPerFace.toNumPyArray()
247
+ a.flush()
248
+ mm.seek(mm.tell() + len(nbOfNodesPerFace) * 4)
249
+ ids2 = ids.buildComplement(ids.back() + 1)
250
+ c2 = mp.getNodalConnectivity()[ids2] + 1
251
+ a = np.memmap(
252
+ f,
253
+ dtype="int32",
254
+ mode="w+",
255
+ offset=mm.tell(),
256
+ shape=(len(c2),),
257
+ )
258
+ a[:] = c2.toNumPyArray()
259
+ a.flush()
260
+ mm.seek(mm.tell() + len(c2) * 4)
182
261
  pass
183
262
  else:
184
- nbOfNodesPerCell=mp.getNodalConnectivityIndex().deltaShiftIndex()-1
185
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(len(nbOfNodesPerCell),))
186
- a[:]=nbOfNodesPerCell.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+len(nbOfNodesPerCell)*4)
187
- ids2=mp.getNodalConnectivityIndex().buildComplement(mp.getNodalConnectivityIndex().back()+1)
188
- c2=mp.getNodalConnectivity()[ids2]+1
189
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(len(c2),))
190
- a[:]=c2.toNumPyArray() ; a.flush() ; mm.seek(mm.tell()+len(c2)*4)
263
+ nbOfNodesPerCell = (
264
+ mp.getNodalConnectivityIndex().deltaShiftIndex() - 1
265
+ )
266
+ a = np.memmap(
267
+ f,
268
+ dtype="int32",
269
+ mode="w+",
270
+ offset=mm.tell(),
271
+ shape=(len(nbOfNodesPerCell),),
272
+ )
273
+ a[:] = nbOfNodesPerCell.toNumPyArray()
274
+ a.flush()
275
+ mm.seek(mm.tell() + len(nbOfNodesPerCell) * 4)
276
+ ids2 = mp.getNodalConnectivityIndex().buildComplement(
277
+ mp.getNodalConnectivityIndex().back() + 1
278
+ )
279
+ c2 = mp.getNodalConnectivity()[ids2] + 1
280
+ a = np.memmap(
281
+ f,
282
+ dtype="int32",
283
+ mode="w+",
284
+ offset=mm.tell(),
285
+ shape=(len(c2),),
286
+ )
287
+ a[:] = c2.toNumPyArray()
288
+ a.flush()
289
+ mm.seek(mm.tell() + len(c2) * 4)
191
290
  pass
192
- i+=nbelem
291
+ i += nbelem
193
292
  pass
194
293
  pass
195
294
  pass
196
295
  pass
197
296
 
198
- def __writeFieldsPart(self,mdfs):
297
+ def __writeFieldsPart(self, mdfs):
199
298
  if not mdfs:
200
299
  return ""
201
- self._ze_top_dict={}
202
- its,areForgottenTS=mdfs.getCommonIterations()
300
+ self._ze_top_dict = {}
301
+ its, areForgottenTS = mdfs.getCommonIterations()
203
302
  if areForgottenTS:
204
- print(("WARNING : some iterations are NOT present in all fields ! Kept iterations are : %s !"%(str(its))))
303
+ print(
304
+ (
305
+ "WARNING : some iterations are NOT present in all fields ! Kept iterations are : %s !"
306
+ % (str(its))
307
+ )
308
+ )
205
309
  pass
206
- TimeValues=""
310
+ TimeValues = ""
207
311
  for it in its:
208
- TimeValues+="%s\n"%(str(mdfs[0][it].getTime()[-1]))
312
+ TimeValues += "%s\n" % (str(mdfs[0][it].getTime()[-1]))
209
313
  pass
210
- dictVars={}
314
+ dictVars = {}
211
315
  for mdf in mdfs:
212
- nbCompo=mdf.getNumberOfComponents()
316
+ nbCompo = mdf.getNumberOfComponents()
213
317
  if nbCompo not in self.dictCompo:
214
318
  l = [x for x in self.dictCompo if x - nbCompo > 0]
215
- if len(l)==0:
216
- print(("Field \"%s\" will be ignored because number of components (%i) is too big to be %s supported by case files !"%(mdf.getName(),nbCompo,str(list(self.dictCompo.keys())))))
319
+ if len(l) == 0:
320
+ print(
321
+ (
322
+ 'Field "%s" will be ignored because number of components (%i) is too big to be %s supported by case files !'
323
+ % (mdf.getName(), nbCompo, str(list(self.dictCompo.keys())))
324
+ )
325
+ )
217
326
  continue
218
327
  pass
219
- print(("WARNING : Field \"%s\" will have its number of components (%i) set to %i, in order to be supported by case files (must be in %s) !"%(mdf.getName(),nbCompo,l[0],str(list(self.dictCompo.keys())))))
220
- nbCompo=l[0]
328
+ print(
329
+ (
330
+ 'WARNING : Field "%s" will have its number of components (%i) set to %i, in order to be supported by case files (must be in %s) !'
331
+ % (
332
+ mdf.getName(),
333
+ nbCompo,
334
+ l[0],
335
+ str(list(self.dictCompo.keys())),
336
+ )
337
+ )
338
+ )
339
+ nbCompo = l[0]
221
340
  pass
222
341
  if nbCompo in dictVars:
223
342
  dictVars[nbCompo].append(mdf)
224
343
  pass
225
344
  else:
226
- dictVars[nbCompo]=[mdf]
345
+ dictVars[nbCompo] = [mdf]
227
346
  pass
228
347
  pass
229
348
  for mdf in mdfs:
230
- nbCompo=mdf.getNumberOfComponents()
349
+ nbCompo = mdf.getNumberOfComponents()
231
350
  if nbCompo not in self.dictCompo:
232
351
  l = [x for x in self.dictCompo if x - nbCompo > 0]
233
- if len(l)==0:
234
- continue;
235
- nbCompo=l[0]
352
+ if len(l) == 0:
353
+ continue
354
+ nbCompo = l[0]
236
355
  pass
237
- for iii,it in enumerate(its):
238
- ff=mdf[it]
239
- isMultiDisc=len(ff.getTypesOfFieldAvailable())>1
356
+ for iii, it in enumerate(its):
357
+ ff = mdf[it]
358
+ isMultiDisc = len(ff.getTypesOfFieldAvailable()) > 1
240
359
  for typ in ff.getTypesOfFieldAvailable():
241
- l=self._l[:] ; l[-1]="%s%s.%s"%(self._base_name_without_dir,str(iii).rjust(4,"0"),ff.getName())
360
+ l = self._l[:]
361
+ l[-1] = "%s%s.%s" % (
362
+ self._base_name_without_dir,
363
+ str(iii).rjust(4, "0"),
364
+ ff.getName(),
365
+ )
242
366
  if isMultiDisc:
243
- l[-1]="%s_%s"(l[-1],MEDCouplingFieldDiscretization.New(typ).getStringRepr())
367
+ l[-1] = "%s_%s"(
368
+ l[-1],
369
+ MEDCouplingFieldDiscretization.New(typ).getStringRepr(),
370
+ )
244
371
  pass
245
- fffn=l[-1]
372
+ fffn = l[-1]
246
373
  try:
247
374
  os.remove(os.path.sep.join(l))
248
375
  except:
249
376
  pass
250
- f=open(os.path.sep.join(l),"w+b")
251
- summ=0
252
- for geo,[(curTyp,(bg,end),pfl,loc)] in ff.getFieldSplitedByType():
253
- if typ==curTyp:
254
- summ+=4*nbCompo*(end-bg)+80
377
+ f = open(os.path.sep.join(l), "w+b")
378
+ summ = 0
379
+ for geo, [
380
+ (curTyp, (bg, end), pfl, loc)
381
+ ] in ff.getFieldSplitedByType():
382
+ if typ == curTyp:
383
+ summ += 4 * nbCompo * (end - bg) + 80
255
384
  pass
256
385
  pass
257
- a=np.memmap(f,dtype='byte',mode='w+',offset=0,shape=(2*80+4+summ,)) ; a.flush() # truncate to set the size of the file
258
- mm=mmap.mmap(f.fileno(),offset=0,length=0)
259
- k1=ff.getName()
386
+ a = np.memmap(
387
+ f, dtype="byte", mode="w+", offset=0, shape=(2 * 80 + 4 + summ,)
388
+ )
389
+ a.flush() # truncate to set the size of the file
390
+ mm = mmap.mmap(f.fileno(), offset=0, length=0)
391
+ k1 = ff.getName()
260
392
  if isMultiDisc:
261
- k1="%s_%s"%(k1,MEDCouplingFieldDiscretization.New(typ).getStringRepr())
393
+ k1 = "%s_%s" % (
394
+ k1,
395
+ MEDCouplingFieldDiscretization.New(typ).getStringRepr(),
396
+ )
262
397
  pass
263
398
  mm.write(self.__str80(k1))
264
399
  mm.write(self.__str80("part"))
265
- a=np.memmap(f,dtype='int32',mode='w+',offset=mm.tell(),shape=(1,))
266
- a[0]=1 ; a.flush() ; mm.seek(mm.tell()+4) # part number maybe to change ?
267
- for geo,[(curTyp,(bg,end),pfl,loc)] in ff.getFieldSplitedByType():
268
- if pfl!="":
269
- raise Exception("Field \"%s\" contains profiles ! Profiles are not supported yet !"%(mdf.getName()))
270
- if typ==curTyp:
271
- arr=ff.getUndergroundDataArray()[bg:end].changeNbOfComponents(nbCompo,0.) ; arr=arr.toNoInterlace()
272
- if typ==ON_CELLS:
400
+ a = np.memmap(
401
+ f, dtype="int32", mode="w+", offset=mm.tell(), shape=(1,)
402
+ )
403
+ a[0] = 1
404
+ a.flush()
405
+ mm.seek(mm.tell() + 4) # part number maybe to change ?
406
+ for geo, [
407
+ (curTyp, (bg, end), pfl, loc)
408
+ ] in ff.getFieldSplitedByType():
409
+ if pfl != "":
410
+ raise Exception(
411
+ 'Field "%s" contains profiles ! Profiles are not supported yet !'
412
+ % (mdf.getName())
413
+ )
414
+ if typ == curTyp:
415
+ arr = ff.getUndergroundDataArray()[
416
+ bg:end
417
+ ].changeNbOfComponents(nbCompo, 0.0)
418
+ arr = arr.toNoInterlace()
419
+ if typ == ON_CELLS:
273
420
  mm.write(self.__str80(self.dictMCTyp_str[geo]))
274
421
  pass
275
- elif typ==ON_NODES:
422
+ elif typ == ON_NODES:
276
423
  mm.write(self.__str80("coordinates"))
277
424
  pass
278
425
  else:
279
- print(("UnManaged type of field for field \"%s\" !"%(mdf.getName())))
426
+ print(
427
+ (
428
+ 'UnManaged type of field for field "%s" !'
429
+ % (mdf.getName())
430
+ )
431
+ )
280
432
  pass
281
- a=np.memmap(f,dtype='float32',mode='w+',offset=mm.tell(),shape=(nbCompo,end-bg))
282
- b=arr.toNumPyArray() ; b=b.reshape(nbCompo,end-bg)
283
- a[:]=b
284
- a.flush() ; mm.seek(mm.tell()+nbCompo*(end-bg)*4)
433
+ a = np.memmap(
434
+ f,
435
+ dtype="float32",
436
+ mode="w+",
437
+ offset=mm.tell(),
438
+ shape=(nbCompo, end - bg),
439
+ )
440
+ b = arr.toNumPyArray()
441
+ b = b.reshape(nbCompo, end - bg)
442
+ a[:] = b
443
+ a.flush()
444
+ mm.seek(mm.tell() + nbCompo * (end - bg) * 4)
285
445
  pass
286
446
  pass
287
- k="%s per %s"%(self.dictCompo[nbCompo],self.discSpatial[typ])
447
+ k = "%s per %s" % (self.dictCompo[nbCompo], self.discSpatial[typ])
288
448
  if k in self._ze_top_dict:
289
449
  if k1 in self._ze_top_dict[k]:
290
450
  self._ze_top_dict[k][k1].append(fffn)
291
451
  pass
292
452
  else:
293
- self._ze_top_dict[k][k1]=[fffn]
453
+ self._ze_top_dict[k][k1] = [fffn]
294
454
  pass
295
455
  else:
296
- self._ze_top_dict[k]={k1:[fffn]}
456
+ self._ze_top_dict[k] = {k1: [fffn]}
297
457
  pass
298
458
  pass
299
459
  pass
300
460
  pass
301
- headerPart=""
302
- if len(self._ze_top_dict)!=0:
303
- hvp=self.header_varpart[:]
461
+ headerPart = ""
462
+ if len(self._ze_top_dict) != 0:
463
+ hvp = self.header_varpart[:]
304
464
  for k in self._ze_top_dict:
305
465
  for k1 in self._ze_top_dict[k]:
306
- hvp+="\n%s: %s %s"%(k,k1,re.sub("([\d]{4})",4*"*",self._ze_top_dict[k][k1][0]))
466
+ hvp += "\n%s: %s %s" % (
467
+ k,
468
+ k1,
469
+ re.sub("([\d]{4})", 4 * "*", self._ze_top_dict[k][k1][0]),
470
+ )
307
471
  pass
308
472
  pass
309
- hvp+="\n"
310
- headerPart+=hvp
473
+ hvp += "\n"
474
+ headerPart += hvp
311
475
  #
312
- ddd={"NbTimeSteps":len(its),"TimeValues":TimeValues}
313
- htp=self.header_timepart%ddd
314
- headerPart+=htp
476
+ ddd = {"NbTimeSteps": len(its), "TimeValues": TimeValues}
477
+ htp = self.header_timepart % ddd
478
+ headerPart += htp
315
479
  pass
316
480
  return headerPart
317
481
 
318
482
  @classmethod
319
- def __str80(cls,st):
320
- if len(st)>79:
321
- raise Exception("String \"%s\" is too long (>79) !"%(st))
322
- return bytes(str(st).ljust(79)+"\n", "ascii")
483
+ def __str80(cls, st):
484
+ if len(st) > 79:
485
+ raise Exception('String "%s" is too long (>79) !' % (st))
486
+ return bytes(str(st).ljust(79) + "\n", "ascii")
323
487
 
324
- def __computeSizeOfGeoFile(self,listOfMeshes,nn):
325
- sz=0
488
+ def __computeSizeOfGeoFile(self, listOfMeshes, nn):
489
+ sz = 0
326
490
  for m in listOfMeshes:
327
- distribTypes=m.getDistributionOfTypes()
328
- sz+=80+4+2*80+4+nn*3*4
329
- i=0
330
- for typ2,nbelem,dummy in distribTypes:
331
- typ=typ2
491
+ distribTypes = m.getDistributionOfTypes()
492
+ sz += 80 + 4 + 2 * 80 + 4 + nn * 3 * 4
493
+ i = 0
494
+ for typ2, nbelem, dummy in distribTypes:
495
+ typ = typ2
332
496
  if typ not in self.dictMCTyp:
333
- typ=MEDCouplingMesh.GetCorrespondingPolyType()
497
+ typ = MEDCouplingMesh.GetCorrespondingPolyType()
334
498
  pass
335
- if typ!=NORM_POLYHED and typ!=NORM_POLYGON:
336
- sz+=80+4+MEDCouplingMesh.GetNumberOfNodesOfGeometricType(typ)*nbelem*4
499
+ if typ != NORM_POLYHED and typ != NORM_POLYGON:
500
+ sz += (
501
+ 80
502
+ + 4
503
+ + MEDCouplingMesh.GetNumberOfNodesOfGeometricType(typ)
504
+ * nbelem
505
+ * 4
506
+ )
337
507
  pass
338
- elif typ==NORM_POLYHED:
339
- mplh=m[i:i+nbelem] ; delta=len(mplh.getNodalConnectivity())+nbelem
340
- sz+=80+4+delta*4
508
+ elif typ == NORM_POLYHED:
509
+ mplh = m[i : i + nbelem]
510
+ delta = len(mplh.getNodalConnectivity()) + nbelem
511
+ sz += 80 + 4 + delta * 4
341
512
  pass
342
513
  else:
343
- mplh=m[i:i+nbelem] ; delta=len(mplh.getNodalConnectivity())
344
- sz+=80+4+delta*4
514
+ mplh = m[i : i + nbelem]
515
+ delta = len(mplh.getNodalConnectivity())
516
+ sz += 80 + 4 + delta * 4
345
517
  pass
346
- i+=nbelem
518
+ i += nbelem
347
519
  pass
348
520
  pass
349
521
  return sz