-
Notifications
You must be signed in to change notification settings - Fork 303
/
mp7particledata.py
1025 lines (897 loc) · 35.8 KB
/
mp7particledata.py
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
mp7particledata module. Contains the ParticleData, CellDataType,
FaceDataType, and NodeParticleData classes.
"""
import os
import numpy as np
from ..utils.util_array import Util2d
from ..utils.recarray_utils import create_empty_recarray
class ParticleData(object):
"""
Class to create the most basic particle data type (starting location
input style 1). Input style 1 is the most general input style and
provides the most flexibility in customizing starting locations.
Parameters
----------
partlocs : list/tuple of int, list/tuple of list/tuple, or np.ndarray
Particle locations (zero-based) that are either layer, row, column
locations or nodes.
structured : bool
Boolean defining if a structured (True) or unstructured
particle recarray will be created (default is True).
particleids : list, tuple, or np.ndarray
Particle ids for the defined particle locations. If particleids
is None, MODPATH 7 will define the particle ids to each particle
location. If particleids is provided a particle
id must be provided for each partloc (default is None).
localx : float, list, tuple, or np.ndarray
Local x-location of the particle in the cell. If a single value is
provided all particles will have the same localx position. If
a list, tuple, or np.ndarray is provided a localx position must
be provided for each partloc. If localx is None, a value of
0.5 (center of the cell) will be used (default is None).
localy : float, list, tuple, or np.ndarray
Local y-location of the particle in the cell. If a single value is
provided all particles will have the same localy position. If
a list, tuple, or np.ndarray is provided a localy position must
be provided for each partloc. If localy is None, a value of
0.5 (center of the cell) will be used (default is None).
localz : float, list, tuple, or np.ndarray
Local z-location of the particle in the cell. If a single value is
provided all particles will have the same localz position. If
a list, tuple, or np.ndarray is provided a localz position must
be provided for each partloc. If localy is None, a value of
0.5 (center of the cell) will be used (default is None).
timeoffset : float, list, tuple, or np.ndarray
Timeoffset of the particle relative to the release time. If a
single value is provided all particles will have the same
timeoffset. If a list, tuple, or np.ndarray is provided a
timeoffset must be provided for each partloc. If timeoffset is
None, a value of 0. (equal to the release time) will be used
(default is None).
drape : int, list, tuple, or np.ndarray
Drape indicates how particles are treated when starting locations
are specified for cells that are dry. If drape is 0, Particles are
placed in the specified cell. If the cell is dry at the time of
release, the status of the particle is set to unreleased and
removed from the simulation. If drape is 1, particles are placed
in the upper most active grid cell directly beneath the specified
layer, row, column or node location. If a single value is provided
all particles will have the same drape value. If a list, tuple, or
np.ndarray is provided a drape value must be provided for each
partloc. If drape is None, a value of 0 will be used (default
is None).
Examples
--------
>>> import flopy
>>> locs = [(0, 0, 0), (1, 0, 0), (2, 0, 0)]
>>> pd = flopy.modpath.ParticleData(locs, structured=True, drape=0,
... localx=0.5, localy=0.5, localz=1)
"""
def __init__(
self,
partlocs=None,
structured=False,
particleids=None,
localx=None,
localy=None,
localz=None,
timeoffset=None,
drape=None,
):
"""
Class constructor
"""
self.name = "ParticleData"
if structured:
locationstyle = 1
else:
locationstyle = 2
if partlocs is None:
if structured:
partlocs = [(0, 0, 0)]
else:
partlocs = [(0,)]
# create dtype
dtype = []
if structured:
dtype.append(("k", np.int32))
dtype.append(("i", np.int32))
dtype.append(("j", np.int32))
else:
dtype.append(("node", np.int32))
dtype = np.dtype(dtype)
if isinstance(partlocs, (list, tuple)):
# determine if the list or tuple contains lists or tuples
alllsttup = all(isinstance(el, (list, tuple)) for el in partlocs)
if structured:
if alllsttup:
alllen3 = all(len(el) == 3 for el in partlocs)
if not alllen3:
msg = (
"{}: all partlocs entries ".format(self.name)
+ " must have 3 items for structured particle data"
)
raise ValueError(msg)
else:
msg = (
"{}: partlocs list or tuple ".format(self.name)
+ "for structured particle data should "
+ "contain list or tuple entries"
)
raise ValueError(msg)
else:
allint = all(
isinstance(el, (int, np.int32, np.int64))
for el in partlocs
)
# convert to a list of tuples
if allint:
t = []
for el in partlocs:
t.append((el,))
partlocs = t
alllsttup = all(
isinstance(el, (list, tuple)) for el in partlocs
)
if alllsttup:
alllen1 = all(len(el) == 1 for el in partlocs)
if not alllen1:
msg = (
"{}: all entries of ".format(self.name)
+ "partlocs must have 1 items "
+ "for unstructured particle data"
)
raise ValueError(msg)
else:
msg = (
"{}: partlocs list or tuple ".format(self.name)
+ "for unstructured particle data should "
+ "contain integers or a list or tuple with one entry"
)
raise ValueError(msg)
# convert partlocs composed of a lists/tuples of lists/tuples
# to a numpy array
partlocs = np.array(partlocs, dtype=dtype)
elif isinstance(partlocs, np.ndarray):
dtypein = partlocs.dtype
if dtypein != dtype:
partlocs = np.array(partlocs, dtype=dtype)
else:
msg = (
"{}: partlocs must be a list or ".format(self.name)
+ "tuple with lists or tuples"
)
raise ValueError(msg)
# localx
if localx is None:
localx = 0.5
else:
if isinstance(localx, (float, int)):
localx = np.ones(partlocs.shape[0], dtype=np.float32) * localx
elif isinstance(localx, (list, tuple)):
localx = np.array(localx, dtype=np.float32)
if isinstance(localx, np.ndarray):
if localx.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of localx ({}) ".format(localx.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# localy
if localy is None:
localy = 0.5
else:
if isinstance(localy, (float, int)):
localy = np.ones(partlocs.shape[0], dtype=np.float32) * localy
elif isinstance(localy, (list, tuple)):
localy = np.array(localy, dtype=np.float32)
if isinstance(localy, np.ndarray):
if localy.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of localy ({}) ".format(localy.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# localz
if localz is None:
localz = 0.5
else:
if isinstance(localz, (float, int)):
localz = np.ones(partlocs.shape[0], dtype=np.float32) * localz
elif isinstance(localz, (list, tuple)):
localz = np.array(localz, dtype=np.float32)
if isinstance(localz, np.ndarray):
if localz.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of localz ({}) ".format(localz.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# timeoffset
if timeoffset is None:
timeoffset = 0.0
else:
if isinstance(timeoffset, (float, int)):
timeoffset = (
np.ones(partlocs.shape[0], dtype=np.float32) * timeoffset
)
elif isinstance(timeoffset, (list, tuple)):
timeoffset = np.array(timeoffset, dtype=np.float32)
if isinstance(timeoffset, np.ndarray):
if timeoffset.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of timeoffset "
+ "({}) ".format(timeoffset.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# drape
if drape is None:
drape = 0
else:
if isinstance(drape, (float, int)):
drape = np.ones(partlocs.shape[0], dtype=np.float32) * drape
elif isinstance(drape, (list, tuple)):
drape = np.array(drape, dtype=np.int32)
if isinstance(drape, np.ndarray):
if drape.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of drape ({}) ".format(drape.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# particleids
if particleids is None:
particleid = False
particleidoption = 0
else:
particleid = True
particleidoption = 1
if isinstance(particleids, (int, float)):
msg = (
"{}:".format(self.name)
+ "A particleid must be provided for each partloc "
+ "as a list/tuple/np.ndarray of size "
+ "{}. ".format(partlocs.shape[0])
+ "A single particleid has been provided."
)
raise TypeError(msg)
elif isinstance(particleids, (list, tuple)):
particleids = np.array(particleids, dtype=np.int32)
if isinstance(particleids, np.ndarray):
if particleids.shape[0] != partlocs.shape[0]:
msg = (
"{}:".format(self.name)
+ "shape of particleids "
+ "({}) ".format(particleids.shape[0])
+ "is not equal to the shape "
+ "of partlocs ({}).".format(partlocs.shape[0])
)
raise ValueError(msg)
# create empty particle
ncells = partlocs.shape[0]
self.dtype = self._get_dtype(structured, particleid)
particledata = create_empty_recarray(
ncells, self.dtype, default_value=0
)
# fill particle
if structured:
particledata["k"] = partlocs["k"]
particledata["i"] = partlocs["i"]
particledata["j"] = partlocs["j"]
else:
particledata["node"] = partlocs["node"]
particledata["localx"] = localx
particledata["localy"] = localy
particledata["localz"] = localz
particledata["timeoffset"] = timeoffset
particledata["drape"] = drape
if particleid:
particledata["id"] = particleids
self.particlecount = particledata.shape[0]
self.particleidoption = particleidoption
self.locationstyle = locationstyle
self.particledata = particledata
return
def write(self, f=None):
"""
Parameters
----------
f : fileobject
Fileobject that is open with write access
Returns
-------
"""
# validate that a valid file object was passed
if not hasattr(f, "write"):
msg = (
"{}: cannot write data for template ".format(self.name)
+ "without passing a valid file object ({}) ".format(f)
+ "open for writing"
)
raise ValueError(msg)
# particle data item 4 and 5
d = np.recarray.copy(self.particledata)
lnames = [name.lower() for name in d.dtype.names]
# Add one to the kij and node indices
for idx in ["k", "i", "j", "node", "id"]:
if idx in lnames:
d[idx] += 1
# write the particle data
fmt = self._fmt_string + "\n"
for v in d:
f.write(fmt.format(*v))
return
def _get_dtype(self, structured, particleid):
"""
define the dtype for a structured or unstructured
particledata recarray. Optionally, include a particleid column in
the dtype.
Parameters
----------
structured : bool
Boolean defining if a structured (True) or unstructured
particle dtype will be created.
particleid : bool
Boolean defining if the dtype will include a particle id
column.
Returns
-------
dtype : numpy dtype
Examples
--------
>>> import flopy.modpath as fmp
>>> dtype = fmp.ParticleGroup.get_particledata_dtype(structured=True,
... particleid=True)
"""
dtype = []
if particleid:
dtype.append(("id", np.int32))
if structured:
dtype.append(("k", np.int32))
dtype.append(("i", np.int32))
dtype.append(("j", np.int32))
else:
dtype.append(("node", np.int32))
dtype.append(("localx", np.float32))
dtype.append(("localy", np.float32))
dtype.append(("localz", np.float32))
dtype.append(("timeoffset", np.float32))
dtype.append(("drape", np.int32))
return np.dtype(dtype)
@property
def _fmt_string(self):
"""
Returns a python-style fmt string to write particle data
that corresponds to the dtype
Parameters
----------
Returns
-------
fmt : str
python format string with space delimited entries
"""
fmts = []
for field in self.particledata.dtype.descr:
vtype = field[1][1].lower()
if vtype == "i" or vtype == "b":
fmts.append("{:9d}")
elif vtype == "f":
if field[1][2] == 8:
fmts.append("{:23.16g}")
else:
fmts.append("{:15.7g}")
elif vtype == "o":
fmts.append("{:9s}")
elif vtype == "s":
msg = (
"Particles.fmt_string error: 'str' "
+ "type found in dtype. This gives unpredictable "
+ "results when recarray to file - change to 'object' type"
)
raise TypeError(msg)
else:
msg = (
"MfList.fmt_string error: unknown vtype in "
+ "field: {}".format(field)
)
raise TypeError(msg)
return " " + " ".join(fmts)
class FaceDataType(object):
"""
Face data type class to create a MODPATH 7 particle location template for
input style 2, 3, and 4 on cell faces (templatesubdivisiontype = 2).
Parameters
----------
drape : int
Drape indicates how particles are treated when starting locations
are specified for cells that are dry. If drape is 0, Particles are
placed in the specified cell. If the cell is dry at the time of
release, the status of the particle is set to unreleased and
removed from the simulation. If drape is 1, particles are placed
in the upper most active grid cell directly beneath the specified
layer, row, column or node location (default is 0).
verticaldivisions1 : int
The number of vertical subdivisions that define the two-dimensional
array of particles on cell face 1 (default is 3).
horizontaldivisions1 : int
The number of horizontal subdivisions that define the two-dimensional
array of particles on cell face 1 (default is 3).
verticaldivisions2 : int
The number of vertical subdivisions that define the two-dimensional
array of particles on cell face 2 (default is 3).
horizontaldivisions2 : int
The number of horizontal subdivisions that define the two-dimensional
array of particles on cell face 2 (default is 3).
verticaldivisions3 : int
The number of vertical subdivisions that define the two-dimensional
array of particles on cell face 3 (default is 3).
horizontaldivisions3 : int
The number of horizontal subdivisions that define the two-dimensional
array of particles on cell face 3 (default is 3).
verticaldivisions4 : int
The number of vertical subdivisions that define the two-dimensional
array of particles on cell face 4 (default is 3).
horizontaldivisions4 : int
The number of horizontal subdivisions that define the two-dimensional
array of particles on cell face 4 (default is 3).
rowdivisions5 : int
The number of row subdivisions that define the two-dimensional array
of particles on the bottom cell face (face 5) (default is 3).
columndivisions5 : int
The number of column subdivisions that define the two-dimensional array
of particles on the bottom cell face (face 5) (default is 3).
rowdivisions6 : int
The number of row subdivisions that define the two-dimensional array
of particles on the top cell face (face 6) (default is 3).
columndivisions6 : int
The number of column subdivisions that define the two-dimensional array
of particles on the top cell face (face 6) (default is 3).
Examples
--------
>>> import flopy
>>> fd = flopy.modpath.FaceDataType()
"""
def __init__(
self,
drape=0,
verticaldivisions1=3,
horizontaldivisions1=3,
verticaldivisions2=3,
horizontaldivisions2=3,
verticaldivisions3=3,
horizontaldivisions3=3,
verticaldivisions4=3,
horizontaldivisions4=3,
rowdivisions5=3,
columndivisions5=3,
rowdivisions6=3,
columndivisions6=3,
):
"""
Class constructor
"""
self.name = "FaceDataType"
# assign attributes
self.templatesubdivisiontype = 1
self.drape = drape
self.verticaldivisions1 = verticaldivisions1
self.horizontaldivisions1 = horizontaldivisions1
self.verticaldivisions2 = verticaldivisions2
self.horizontaldivisions2 = horizontaldivisions2
self.verticaldivisions3 = verticaldivisions3
self.horizontaldivisions3 = horizontaldivisions3
self.verticaldivisions4 = verticaldivisions4
self.horizontaldivisions4 = horizontaldivisions4
self.rowdivisions5 = rowdivisions5
self.columndivisions5 = columndivisions5
self.rowdivisions6 = rowdivisions6
self.columndivisions6 = columndivisions6
return
def write(self, f=None):
"""
Parameters
----------
f : fileobject
Fileobject that is open with write access
Returns
-------
"""
# validate that a valid file object was passed
if not hasattr(f, "write"):
msg = (
"{}: cannot write data for template ".format(self.name)
+ "without passing a valid file object ({}) ".format(f)
+ "open for writing"
)
raise ValueError(msg)
# item 4
fmt = 12 * " {}" + "\n"
line = fmt.format(
self.verticaldivisions1,
self.horizontaldivisions1,
self.verticaldivisions2,
self.horizontaldivisions2,
self.verticaldivisions3,
self.horizontaldivisions3,
self.verticaldivisions4,
self.horizontaldivisions4,
self.rowdivisions5,
self.columndivisions5,
self.rowdivisions6,
self.columndivisions6,
)
f.write(line)
return
class CellDataType(object):
"""
Cell data type class to create a MODPATH 7 particle location template for
input style 2, 3, and 4 in cells (templatesubdivisiontype = 2).
Parameters
----------
drape : int
Drape indicates how particles are treated when starting locations
are specified for cells that are dry. If drape is 0, Particles are
placed in the specified cell. If the cell is dry at the time of
release, the status of the particle is set to unreleased and
removed from the simulation. If drape is 1, particles are placed
in the upper most active grid cell directly beneath the specified
layer, row, column or node location (default is 0).
columncelldivisions : int
Number of particles in a cell in the column (x-coordinate)
direction (default is 3).
rowcelldivisions : int
Number of particles in a cell in the row (y-coordinate)
direction (default is 3).
layercelldivisions : int
Number of particles in a cell in the layer (z-coordinate)
direction (default is 3).
Examples
--------
>>> import flopy
>>> cd = flopy.modpath.CellDataType()
"""
def __init__(
self,
drape=0,
columncelldivisions=3,
rowcelldivisions=3,
layercelldivisions=3,
):
"""
Class constructor
"""
self.name = "CellDataType"
# assign attributes
self.templatesubdivisiontype = 2
self.drape = drape
self.columncelldivisions = columncelldivisions
self.rowcelldivisions = rowcelldivisions
self.layercelldivisions = layercelldivisions
return
def write(self, f=None):
"""
Parameters
----------
f : fileobject
Fileobject that is open with write access
Returns
-------
"""
# validate that a valid file object was passed
if not hasattr(f, "write"):
msg = (
"{}: cannot write data for template ".format(self.name)
+ "without passing a valid file object ({}) ".format(f)
+ "open for writing"
)
raise ValueError(msg)
# item 5
fmt = " {} {} {}\n"
line = fmt.format(
self.columncelldivisions,
self.rowcelldivisions,
self.layercelldivisions,
)
f.write(line)
return
class LRCParticleData(object):
"""
Layer, row, column particle data template class to create MODPATH 7
particle location input style 2 on cell faces (templatesubdivisiontype = 1)
and/or in cells (templatesubdivisiontype = 2). Particle locations for this
template are specified by layer, row, column regions.
Parameters
----------
subdivisiondata : FaceDataType, CellDataType or list of FaceDataType
and/or CellDataType types
FaceDataType, CellDataType, or a list of FaceDataType and/or
CellDataTypes that are used to create one or more particle templates
in a particle group. If subdivisiondata is None, a default CellDataType
with 27 particles per cell will be created (default is None).
lrcregions : list of lists tuples or np.ndarrays
Layer, row, column (zero-based) regions with particles created using
the specified template parameters. A region is defined as a list/tuple
of minlayer, minrow, mincolumn, maxlayer, maxrow, maxcolumn values.
If subdivisiondata is a list, a list/tuple or array of layer, row,
column regions with the same length as subdivision data must be
provided. If lrcregions is None, particles will be placed in
the first model cell (default is None).
Examples
--------
>>> import flopy
>>> pg = flopy.modpath.LRCParticleData(lrcregions=[0, 0, 0, 3, 10, 10])
"""
def __init__(self, subdivisiondata=None, lrcregions=None):
"""
Class constructor
"""
self.name = "LRCParticleData"
if subdivisiondata is None:
subdivisiondata = CellDataType()
if lrcregions is None:
lrcregions = [[0, 0, 0, 0, 0, 0]]
if isinstance(subdivisiondata, (CellDataType, FaceDataType)):
subdivisiondata = [subdivisiondata]
for idx, fd in enumerate(subdivisiondata):
if not isinstance(fd, (CellDataType, FaceDataType)):
msg = (
"{}: facedata item {} ".format(self.name, idx)
+ "is of type {} ".format(type(fd))
+ "instead of an instance of CellDataType or FaceDataType"
)
raise TypeError(msg)
# validate lrcregions data
if isinstance(lrcregions, (list, tuple)):
# determine if the list or tuple contains lists or tuples
alllsttup = all(
isinstance(el, (list, tuple, np.ndarray)) for el in lrcregions
)
if not alllsttup:
msg = (
"{}: lrcregions should be ".format(self.name)
+ "a list with lists, tuples, or arrays"
)
raise TypeError(msg)
t = []
for lrcregion in lrcregions:
t.append(np.array(lrcregion, dtype=np.int32))
lrcregions = t
else:
msg = (
"{}: lrcregions should be ".format(self.name)
+ "a list of lists, tuples, or arrays "
+ "not a {}.".format(type(lrcregions))
)
raise TypeError(msg)
# validate size of nodes relative to subdivisiondata
shape = len(subdivisiondata)
if len(lrcregions) != shape:
msg = (
"{}: lrcregions data must have ".format(self.name)
+ "{} rows but a total of ".format(shape)
+ "{} rows were provided.".format(lrcregions.shape[0])
)
raise ValueError(msg)
# validate that there are 6 columns in each lrcregions entry
for idx, lrcregion in enumerate(lrcregions):
shapel = lrcregion.shape
if len(shapel) == 1:
lrcregions[idx] = lrcregion.reshape(1, shapel)
shapel = lrcregion[idx].shape
if shapel[1] != 6:
msg = (
"{}: Each lrcregions entry must ".format(self.name)
+ "have 6 columns passed lrcregions has "
+ "{} columns".format(shapel[1])
)
raise ValueError(msg)
#
totalcellregioncount = 0
for lrcregion in lrcregions:
totalcellregioncount += lrcregion.shape[0]
# assign attributes
self.particletemplatecount = shape
self.totalcellregioncount = totalcellregioncount
self.subdivisiondata = subdivisiondata
self.lrcregions = lrcregions
return
def write(self, f=None):
"""
Parameters
----------
f : fileobject
Fileobject that is open with write access
Returns
-------
"""
# validate that a valid file object was passed
if not hasattr(f, "write"):
msg = (
"{}: cannot write data for template ".format(self.name)
+ "without passing a valid file object ({}) ".format(f)
+ "open for writing"
)
raise ValueError(msg)
# item 2
f.write(
"{} {}\n".format(
self.particletemplatecount, self.totalcellregioncount
)
)
for sd, lrcregion in zip(self.subdivisiondata, self.lrcregions):
# item 3
f.write(
"{} {} {}\n".format(
sd.templatesubdivisiontype, lrcregion.shape[0], sd.drape
)
)
# item 4 or 5
sd.write(f)
# item 6
for row in lrcregion:
line = ""
for lrc in row:
line += "{} ".format(lrc + 1)
line += "\n"
f.write(line)
return
class NodeParticleData(object):
"""
Node particle data template class to create MODPATH 7 particle location
input style 3 on cell faces (templatesubdivisiontype = 1) and/or in cells
(templatesubdivisiontype = 2). Particle locations for this template are
specified by nodes.
Parameters
----------
subdivisiondata : FaceDataType, CellDataType or list of FaceDataType
and/or CellDataType types
FaceDataType, CellDataType, or a list of FaceDataType and/or
CellDataTypes that are used to create one or more particle templates
in a particle group. If subdivisiondata is None, a default CellDataType
with 27 particles per cell will be created (default is None).
nodes : int, list of ints, tuple of ints, or np.ndarray
Nodes (zero-based) with particles created using the specified template
parameters. If subdivisiondata is a list, a list of nodes with the same
length as subdivision data must be provided. If nodes is None,
particles will be placed in the first model cell (default is None).
Examples
--------
>>> import flopy
>>> pg = flopy.modpath.NodeParticleData(nodes=[100, 101])
"""
def __init__(self, subdivisiondata=None, nodes=None):
"""
Class constructor
"""
self.name = "NodeParticleData"
if subdivisiondata is None:
subdivisiondata = CellDataType()
if nodes is None:
nodes = 0
if isinstance(subdivisiondata, (CellDataType, FaceDataType)):
subdivisiondata = [subdivisiondata]
if isinstance(nodes, (int, np.int32, np.int64)):
nodes = [(nodes,)]
elif isinstance(nodes, (float, np.float32, np.float64)):
msg = (
"{}: nodes is of type {} ".format(self.name, type(nodes))
+ "but must be an int if a single value is passed"
)
raise TypeError(msg)
for idx, fd in enumerate(subdivisiondata):
if not isinstance(fd, (CellDataType, FaceDataType)):
msg = (
"{}: facedata item {} ".format(self.name, idx)
+ "is of type {} ".format(type(fd))
+ "instead of an instance of CellDataType or FaceDataType"
)
raise TypeError(msg)
# validate nodes data
if isinstance(nodes, np.ndarray):
if len(nodes.shape) == 1:
nodes = nodes.reshape(1, nodes.shape[0])
# convert to a list of numpy arrays
t = []
for idx in range(nodes.shape[0]):
t.append(np.array(nodes[idx, :], dtype=np.int32))
nodes = t
elif isinstance(nodes, (list, tuple)):
# convert a single list/tuple to a list of tuples if only one
# entry in subdivisiondata
if len(subdivisiondata) == 1:
if len(nodes) > 1:
nodes = [tuple(nodes)]
# determine if the list or tuple contains lists or tuples
alllsttup = all(
isinstance(el, (list, tuple, np.ndarray)) for el in nodes
)
if not alllsttup:
msg = (
"{}: nodes should be ".format(self.name)
+ "a list or tuple with lists or tuple if a single "
+ "int or numpy array is not provided"
)
raise TypeError(msg)
t = []
for idx in range(len(nodes)):
t.append(np.array(nodes[idx], dtype=np.int32))
nodes = t
else:
msg = (
"{}: nodes should be ".format(self.name)
+ "a single integer, a numpy array, or a "
+ "list/tuple or lists/tuples."
)
raise TypeError(msg)
# validate size of nodes relative to subdivisiondata
shape = len(subdivisiondata)
if len(nodes) != shape:
msg = (
"{}: node data must have ".format(self.name)
+ "{} rows but a total of ".format(shape)
+ "{} rows were provided.".format(nodes.shape[0])
)
raise ValueError(msg)
totalcellcount = 0
for t in nodes:
totalcellcount += t.shape[0]
# assign attributes
self.particletemplatecount = shape
self.totalcellcount = totalcellcount
self.subdivisiondata = subdivisiondata
self.nodedata = nodes
return
def write(self, f=None):
"""
Parameters
----------
f : fileobject
Fileobject that is open with write access
Returns
-------
"""
# validate that a valid file object was passed
if not hasattr(f, "write"):
msg = (
"{}: cannot write data for template ".format(self.name)
+ "without passing a valid file object ({}) ".format(f)
+ "open for writing"
)
raise ValueError(msg)
# item 2
f.write(
"{} {}\n".format(self.particletemplatecount, self.totalcellcount)
)