forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.py
More file actions
852 lines (609 loc) · 24.5 KB
/
Copy pathpatch.py
File metadata and controls
852 lines (609 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
"""
The patch module defines the classes necessary to describe finite-volume
data and the grid that it lives on.
Typical usage:
-- create the grid
grid = Grid2d(nx, ny)
-- create the data that lives on that grid
data = CellCenterData2d(grid)
bc = BC(xlb="reflect", xrb="reflect",
ylb="outflow", yrb="outflow")
data.register_var("density", bc)
...
data.create()
-- initialize some data
dens = data.get_var("density")
dens[:,:] = ...
-- fill the ghost cells
data.fill_BC("density")
"""
from __future__ import print_function
import numpy as np
import pickle
from util import msg
import mesh.boundary as bnd
import mesh.array_indexer as ai
class Grid2d(object):
"""
the 2-d grid class. The grid object will contain the coordinate
information (at various centerings).
A basic (1-d) representation of the layout is:
| | | X | | | | X | | |
+--*--+- // -+--*--X--*--+--*--+- // -+--*--+--*--X--*--+- // -+--*--+
0 ng-1 ng ng+1 ... ng+nx-1 ng+nx 2ng+nx-1
ilo ihi
|<- ng guardcells->|<---- nx interior zones ----->|<- ng guardcells->|
The '*' marks the data locations.
"""
def __init__(self, nx, ny, ng=1, \
xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0):
"""
Create a Grid2d object.
The only data that we require is the number of points that
make up the mesh in each direction. Optionally we take the
extrema of the domain (default is [0,1]x[0,1]) and number of
ghost cells (default is 1).
Note that the Grid2d object only defines the discretization,
it does not know about the boundary conditions, as these can
vary depending on the variable.
Parameters
----------
nx : int
Number of zones in the x-direction
ny : int
Number of zones in the y-direction
ng : int, optional
Number of ghost cells
xmin : float, optional
Physical coordinate at the lower x boundary
xmax : float, optional
Physical coordinate at the upper x boundary
ymin : float, optional
Physical coordinate at the lower y boundary
ymax : float, optional
Physical coordinate at the upper y boundary
"""
# size of grid
self.nx = int(nx)
self.ny = int(ny)
self.ng = int(ng)
self.qx = int(2*ng + nx)
self.qy = int(2*ng + ny)
# domain extrema
self.xmin = xmin
self.xmax = xmax
self.ymin = ymin
self.ymax = ymax
# compute the indices of the block interior (excluding guardcells)
self.ilo = self.ng
self.ihi = self.ng + self.nx-1
self.jlo = self.ng
self.jhi = self.ng + self.ny-1
# center of the grid (for convenience)
self.ic = self.ilo + self.nx//2 - 1
self.jc = self.jlo + self.ny//2 - 1
# define the coordinate information at the left, center, and right
# zone coordinates
self.dx = (xmax - xmin)/nx
self.xl = (np.arange(self.qx) - ng)*self.dx + xmin
self.xr = (np.arange(self.qx) + 1.0 - ng)*self.dx + xmin
self.x = 0.5*(self.xl + self.xr)
self.dy = (ymax - ymin)/ny
self.yl = (np.arange(self.qy) - ng)*self.dy + ymin
self.yr = (np.arange(self.qy) + 1.0 - ng)*self.dy + ymin
self.y = 0.5*(self.yl + self.yr)
# 2-d versions of the zone coordinates (replace with meshgrid?)
x2d = np.repeat(self.x, self.qy)
x2d.shape = (self.qx, self.qy)
self.x2d = x2d
y2d = np.repeat(self.y, self.qx)
y2d.shape = (self.qy, self.qx)
y2d = np.transpose(y2d)
self.y2d = y2d
def scratch_array(self, nvar=1):
"""
return a standard numpy array dimensioned to have the size
and number of ghostcells as the parent grid
"""
if nvar == 1:
_tmp = np.zeros((self.qx, self.qy), dtype=np.float64)
else:
_tmp = np.zeros((self.qx, self.qy, nvar), dtype=np.float64)
return ai.ArrayIndexer(d=_tmp, grid=self)
def norm(self, d):
"""
find the norm of the quantity d defined on the same grid, in the
domain's valid region
"""
return np.sqrt(self.dx*self.dy*
np.sum((d[self.ilo:self.ihi+1,self.jlo:self.jhi+1]**2).flat))
def coarse_like(self, N):
"""
return a new grid object coarsened by a factor n, but with
all the other properties the same
"""
return Grid2d(self.nx//N, self.ny//N, ng=self.ng,
xmin=self.xmin, xmax=self.xmax,
ymin=self.ymin, ymax=self.ymax)
def fine_like(self, N):
"""
return a new grid object finer by a factor n, but with
all the other properties the same
"""
return Grid2d(self.nx*N, self.ny*N, ng=self.ng,
xmin=self.xmin, xmax=self.xmax,
ymin=self.ymin, ymax=self.ymax)
def __str__(self):
""" print out some basic information about the grid object """
return "2-d grid: nx = {}, ny = {}, ng = {}".format(
self.nx, self.ny, self.ng)
def __eq__(self, other):
""" are two grids equivalent? """
result = (self.nx == other.nx and self.ny == other.ny and
self.ng == other.ng and
self.xmin == other.xmin and self.xmax == other.xmax and
self.ymin == other.ymin and self.ymax == other.ymax)
return result
class CellCenterData2d(object):
"""
A class to define cell-centered data that lives on a grid. A
CellCenterData2d object is built in a multi-step process before
it can be used.
-- Create the object. We pass in a grid object to describe where
the data lives:
my_data = patch.CellCenterData2d(myGrid)
-- Register any variables that we expect to live on this patch.
Here BC describes the boundary conditions for that variable.
my_data.register_var('density', BC)
my_data.register_var('x-momentum', BC)
...
-- Register any auxillary data -- these are any parameters that are
needed to interpret the data outside of the simulation (for
example, the gamma for the equation of state).
my_data.set_aux(keyword, value)
-- Finish the initialization of the patch
my_data.create()
This last step actually allocates the storage for the state
variables. Once this is done, the patch is considered to be
locked. New variables cannot be added.
"""
def __init__(self, grid, dtype=np.float64):
"""
Initialize the CellCenterData2d object.
Parameters
----------
grid : Grid2d object
The grid upon which the data will live
dtype : NumPy data type, optional
The datatype of the data we wish to create (defaults to
np.float64
runtime_parameters : RuntimeParameters object, optional
The runtime parameters that go along with this data
"""
self.grid = grid
self.dtype = dtype
self.data = None
self.vars = []
self.nvar = 0
self.aux = {}
# derived variables will have a callback function
self.derives = []
self.BCs = {}
# time
self.t = -1.0
self.initialized = 0
def register_var(self, name, bc):
"""
Register a variable with CellCenterData2d object.
Parameters
----------
name : str
The variable name
bc : BC object
The boundary conditions that describe the actions to take
for this variable at the physical domain boundaries.
"""
if self.initialized == 1:
msg.fail("ERROR: grid already initialized")
self.vars.append(name)
self.nvar += 1
self.BCs[name] = bc
def set_aux(self, keyword, value):
"""
Set any auxillary (scalar) data. This data is simply carried
along with the CellCenterData2d object
Parameters
----------
keyword : str
The name of the datum
value : any time
The value to associate with the keyword
"""
self.aux[keyword] = value
def add_derived(self, func):
"""
Register a function to compute derived variable
Parameters
----------
func : function
A function to call to derive the variable. This function
should take two arguments, a CellCenterData2d object and a
string variable name (or list of variables)
"""
self.derives.append(func)
def create(self):
"""
Called after all the variables are registered and allocates
the storage for the state data.
"""
if self.initialized == 1:
msg.fail("ERROR: grid already initialized")
self.data = np.zeros((self.grid.qx, self.grid.qy, self.nvar),
dtype=self.dtype)
self.initialized = 1
def __str__(self):
""" print out some basic information about the CellCenterData2d
object """
if self.initialized == 0:
my_str = "CellCenterData2d object not yet initialized"
return my_str
my_str = "cc data: nx = {}, ny = {}, ng = {}\n".format(
self.grid.nx, self.grid.ny, self.grid.ng)
my_str += " nvars = {}\n".format(self.nvar)
my_str += " variables:\n"
ilo = self.grid.ilo
ihi = self.grid.ihi
jlo = self.grid.jlo
jhi = self.grid.jhi
for n in range(self.nvar):
my_str += "%16s: min: %15.10f max: %15.10f\n" % \
(self.vars[n], self.min(self.names[n]), self.max(self.names[n]))
my_str += "%16s BCs: -x: %-12s +x: %-12s -y: %-12s +y: %-12s\n" %\
(" " , self.BCs[self.vars[n]].xlb,
self.BCs[self.vars[n]].xrb,
self.BCs[self.vars[n]].ylb,
self.BCs[self.vars[n]].yrb)
return my_str
def get_var(self, name):
"""
Return a data array for the variable described by name. Stored
variables will be checked first, and then any derived variables
will be checked.
For a stored variable, changes made to this are automatically
reflected in the CellCenterData2d object.
Parameters
----------
name : str
The name of the variable to access
Returns
-------
out : ndarray
The array of data corresponding to the variable name
"""
try:
n = self.vars.index(name)
except:
for f in self.derives:
var = f(self, name)
if len(var) > 0:
return var
raise KeyError("name {} is not valid".format(name))
else:
return ai.ArrayIndexer(d=self.data[:,:,n], grid=self.grid)
def get_var_by_index(self, n):
"""
Return a data array for the variable with index n in the
data array. Any changes made to this are automatically
reflected in the CellCenterData2d object.
Parameters
----------
n : int
The index of the variable to access
Returns
-------
out : ndarray
The array of data corresponding to the index
"""
return ai.ArrayIndexer(d=self.data[:,:,n], grid=self.grid)
def get_vars(self):
"""
Return the entire data array. Any changes made to this
are automatically reflected in the CellCenterData2d object.
Returns
-------
out : ndarray
The array of data
"""
return ai.ArrayIndexer(d=self.data, grid=self.grid)
def get_aux(self, keyword):
"""
Get the auxillary data associated with keyword
Parameters
----------
keyword : str
The name of the auxillary data to access
Returns
-------
out : variable type
The value corresponding to the keyword
"""
if keyword in self.aux.keys():
return self.aux[keyword]
else:
return None
def zero(self, name):
"""
Zero out the data array associated with variable name.
Parameters
----------
name : str
The name of the variable to zero
"""
n = self.vars.index(name)
self.data[:,:,n] = 0.0
def fill_BC_all(self):
"""
Fill boundary conditions on all variables.
"""
for name in self.vars:
self.fill_BC(name)
def fill_BC(self, name):
"""
Fill the boundary conditions. This operates on a single state
variable at a time, to allow for maximum flexibility.
We do periodic, reflect-even, reflect-odd, and outflow
Each variable name has a corresponding BC stored in the
CellCenterData2d object -- we refer to this to figure out the
action to take at each boundary.
Parameters
----------
name : str
The name of the variable for which to fill the BCs.
"""
# there is only a single grid, so every boundary is on
# a physical boundary (except if we are periodic)
# Note: we piggy-back on outflow and reflect-odd for
# Neumann and Dirichlet homogeneous BCs respectively, but
# this only works for a single ghost cell
n = self.vars.index(name)
# -x boundary
if self.BCs[name].xlb in ["outflow", "neumann"]:
if self.BCs[name].xl_value is None:
for i in range(self.grid.ilo):
self.data[i,:,n] = self.data[self.grid.ilo,:,n]
else:
self.data[self.grid.ilo-1,:,n] = \
self.data[self.grid.ilo,:,n] - self.grid.dx*self.BCs[name].xl_value[:]
elif self.BCs[name].xlb == "reflect-even":
for i in range(self.grid.ilo):
self.data[i,:,n] = self.data[2*self.grid.ng-i-1,:,n]
elif self.BCs[name].xlb in ["reflect-odd", "dirichlet"]:
if self.BCs[name].xl_value is None:
for i in range(self.grid.ilo):
self.data[i,:,n] = -self.data[2*self.grid.ng-i-1,:,n]
else:
self.data[self.grid.ilo-1,:,n] = \
2*self.BCs[name].xl_value[:] - self.data[self.grid.ilo,:,n]
elif self.BCs[name].xlb == "periodic":
for i in range(self.grid.ilo):
self.data[i,:,n] = self.data[self.grid.ihi-self.grid.ng+i+1,:,n]
# +x boundary
if self.BCs[name].xrb in ["outflow", "neumann"]:
if self.BCs[name].xr_value is None:
for i in range(self.grid.ihi+1, self.grid.nx+2*self.grid.ng):
self.data[i,:,n] = self.data[self.grid.ihi,:,n]
else:
self.data[self.grid.ihi+1,:,n] = \
self.data[self.grid.ihi,:,n] + self.grid.dx*self.BCs[name].xr_value[:]
elif self.BCs[name].xrb == "reflect-even":
for i in range(self.grid.ng):
i_bnd = self.grid.ihi+1+i
i_src = self.grid.ihi-i
self.data[i_bnd,:,n] = self.data[i_src,:,n]
elif self.BCs[name].xrb in ["reflect-odd", "dirichlet"]:
if self.BCs[name].xr_value is None:
for i in range(self.grid.ng):
i_bnd = self.grid.ihi+1+i
i_src = self.grid.ihi-i
self.data[i_bnd,:,n] = -self.data[i_src,:,n]
else:
self.data[self.grid.ihi+1,:,n] = \
2*self.BCs[name].xr_value[:] - self.data[self.grid.ihi,:,n]
elif self.BCs[name].xrb == "periodic":
for i in range(self.grid.ihi+1, 2*self.grid.ng + self.grid.nx):
self.data[i,:,n] = self.data[i-self.grid.ihi-1+self.grid.ng,:,n]
# -y boundary
if self.BCs[name].ylb in ["outflow", "neumann"]:
if self.BCs[name].yl_value is None:
for j in range(self.grid.jlo):
self.data[:,j,n] = self.data[:,self.grid.jlo,n]
else:
self.data[:,self.grid.jlo-1,n] = \
self.data[:,self.grid.jlo,n] - self.grid.dy*self.BCs[name].yl_value[:]
elif self.BCs[name].ylb == "reflect-even":
for j in range(self.grid.jlo):
self.data[:,j,n] = self.data[:,2*self.grid.ng-j-1,n]
elif self.BCs[name].ylb in ["reflect-odd", "dirichlet"]:
if self.BCs[name].yl_value is None:
for j in range(self.grid.jlo):
self.data[:,j,n] = -self.data[:,2*self.grid.ng-j-1,n]
else:
self.data[:,self.grid.jlo-1,n] = \
2*self.BCs[name].yl_value[:] - self.data[:,self.grid.jlo,n]
elif self.BCs[name].ylb == "periodic":
for j in range(self.grid.jlo):
self.data[:,j,n] = self.data[:,self.grid.jhi-self.grid.ng+j+1,n]
else:
if self.BCs[name].ylb in bnd.ext_bcs.keys():
bnd.ext_bcs[self.BCs[name].ylb](self.BCs[name].ylb, "ylb", name, self)
# +y boundary
if self.BCs[name].yrb in ["outflow", "neumann"]:
if self.BCs[name].yr_value is None:
for j in range(self.grid.jhi+1, self.grid.ny+2*self.grid.ng):
self.data[:,j,n] = self.data[:,self.grid.jhi,n]
else:
self.data[:,self.grid.jhi+1,n] = \
self.data[:,self.grid.jhi,n] + self.grid.dy*self.BCs[name].yr_value[:]
elif self.BCs[name].yrb == "reflect-even":
for j in range(self.grid.ng):
j_bnd = self.grid.jhi+1+j
j_src = self.grid.jhi-j
self.data[:,j_bnd,n] = self.data[:,j_src,n]
elif self.BCs[name].yrb in ["reflect-odd", "dirichlet"]:
if self.BCs[name].yr_value is None:
for j in range(self.grid.ng):
j_bnd = self.grid.jhi+1+j
j_src = self.grid.jhi-j
self.data[:,j_bnd,n] = -self.data[:,j_src,n]
else:
self.data[:,self.grid.jhi+1,n] = \
2*self.BCs[name].yr_value[:] - self.data[:,self.grid.jhi,n]
elif self.BCs[name].yrb == "periodic":
for j in range(self.grid.jhi+1, 2*self.grid.ng + self.grid.ny):
self.data[:,j,n] = self.data[:,j-self.grid.jhi-1+self.grid.ng,n]
else:
if self.BCs[name].yrb in bnd.ext_bcs.keys():
bnd.ext_bcs[self.BCs[name].yrb](self.BCs[name].yrb, "yrb", name, self)
def min(self, name, ng=0):
"""
return the minimum of the variable name in the domain's valid region
"""
n = self.vars.index(name)
g = self.grid
return np.min(self.data[g.ilo-ng:g.ihi+1+ng,g.jlo-ng:g.jhi+1+ng,n])
def max(self, name, ng=0):
"""
return the maximum of the variable name in the domain's valid region
"""
n = self.vars.index(name)
g = self.grid
return np.max(self.data[g.ilo-ng:g.ihi+1+ng,g.jlo-ng:g.jhi+1+ng,n])
def restrict(self, varname):
"""
Restrict the variable varname to a coarser grid (factor of 2
coarser) and return an array with the resulting data (and same
number of ghostcells)
"""
fine_grid = self.grid
fdata = self.get_var(varname)
# allocate an array for the coarsely gridded data
coarse_grid = fine_grid.coarse_like(2)
cdata = coarse_grid.scratch_array()
# fill the coarse array with the restricted data -- just
# average the 4 fine cells into the corresponding coarse cell
# that encompasses them.
cdata.v()[:,:] = \
0.25*(fdata.v(s=2) + fdata.ip(1, s=2) +
fdata.jp(1, s=2) + fdata.ip_jp(1, 1, s=2))
return cdata
def prolong(self, varname):
"""
Prolong the data in the current (coarse) grid to a finer
(factor of 2 finer) grid. Return an array with the resulting
data (and same number of ghostcells). Only the data for the
variable varname will be operated upon.
We will reconstruct the data in the zone from the
zone-averaged variables using the same limited slopes as in
the advection routine. Getting a good multidimensional
reconstruction polynomial is hard -- we want it to be bilinear
and monotonic -- we settle for having each slope be
independently monotonic:
(x) (y)
f(x,y) = m x/dx + m y/dy + <f>
where the m's are the limited differences in each direction.
When averaged over the parent cell, this reproduces <f>.
Each zone's reconstrution will be averaged over 4 children.
+-----------+ +-----+-----+
| | | | |
| | | 3 | 4 |
| <f> | --> +-----+-----+
| | | | |
| | | 1 | 2 |
+-----------+ +-----+-----+
We will fill each of the finer resolution zones by filling all
the 1's together, using a stride 2 into the fine array. Then
the 2's and ..., this allows us to operate in a vector
fashion. All operations will use the same slopes for their
respective parents.
"""
coarse_grid = self.grid
cdata = self.get_var(varname)
# allocate an array for the finely gridded data
fine_grid = coarse_grid.fine_like(2)
fdata = fine_grid.scratch_array()
# slopes for the coarse data
m_x = coarse_grid.scratch_array()
m_x.v()[:,:] = 0.5*(cdata.ip(1) - cdata.ip(-1))
m_y = coarse_grid.scratch_array()
m_y.v()[:,:] = 0.5*(cdata.jp(1) - cdata.jp(-1))
# fill the children
fdata.v(s=2)[:,:] = cdata.v() - 0.25*m_x.v() - 0.25*m_y.v() # 1 child
fdata.ip(1, s=2)[:,:] = cdata.v() + 0.25*m_x.v() - 0.25*m_y.v() # 2
fdata.jp(1, s=2)[:,:] = cdata.v() - 0.25*m_x.v() + 0.25*m_y.v() # 3
fdata.ip_jp(1, 1, s=2)[:,:] = cdata.v() + 0.25*m_x.v() + 0.25*m_y.v() # 4
return fdata
def write(self, filename):
"""
write out the CellCenterData2d object to disk, stored in the
file filename. We use a python binary format (via pickle).
This stores a representation of the entire object.
"""
pF = open(filename + ".pyro", "wb")
pickle.dump(self, pF, pickle.HIGHEST_PROTOCOL)
pF.close()
def pretty_print(self, var, fmt=None):
a = self.get_var(var)
a.pretty_print(fmt=fmt)
def read(filename):
"""
Read a CellCenterData object from a file and return it and the grid
info and data.
"""
# if we come in with .pyro, we don't need to add it again
if filename.find(".pyro") < 0:
filename += ".pyro"
pF = open(filename, "rb")
data = pickle.load(pF)
pF.close()
return data.grid, data
def cell_center_data_clone(old):
"""
Create a new CellCenterData2d object that is a copy of an existing
one
Parameters
----------
old : CellCenterData2d object
The CellCenterData2d object we wish to copy
Note
----
It may be that this whole thing can be replaced with a copy.deepcopy()
"""
if not isinstance(old, CellCenterData2d):
msg.fail("Can't clone object")
new = CellCenterData2d(old.grid, dtype=old.dtype)
for n in range(old.nvar):
new.register_var(old.vars[n], old.BCs[old.vars[n]])
new.create()
new.aux = old.aux.copy()
new.data = old.data.copy()
new.derives = old.derives.copy()
return new
def do_demo():
# illustrate basic mesh operations
myg = Grid2d(8, 16, xmax=1.0, ymax=2.0)
mydata = CellCenterData2d(myg)
bc = bnd.BC()
mydata.register_var("a", bc)
mydata.create()
a = mydata.get_var("a")
a[:,:] = np.exp(-(myg.x2d - 0.5)**2 - (myg.y2d - 1.0)**2)
print(mydata)
# output
print("writing\n")
mydata.write("mesh_test")
print("reading\n")
_, myd2 = read("mesh_test")
print(myd2)
mydata.pretty_print("a")
if __name__ == "__main__":
do_demo()