-
Notifications
You must be signed in to change notification settings - Fork 120
/
iterators.py
126 lines (102 loc) · 3.42 KB
/
iterators.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
from abc import ABC
from abc import abstractmethod
from parcels.tools.statuscodes import OperationCode, StateCode
class BaseParticleCollectionIterable(ABC):
_pcoll_immutable = None
_reverse = None
_subset = None
def __init__(self, pcoll, reverse=False, subset=None):
self._pcoll_immutable = pcoll
self._reverse = reverse
self._subset = subset
@abstractmethod
def __iter__(self):
pass
class BaseParticleCollectionIterator(ABC):
"""Interface for the ParticleCollection iterator. Provides the
ability to iterate over the particles in the ParticleCollection.
"""
def ___init___(self):
self._head = None
self._tail = None
self._current = None
@abstractmethod
def __next__(self):
"""Returns a ParticleAccessor for the next particle in the
ParticleSet.
"""
pass
@property
def head(self):
"""Returns a ParticleAccessor for the first particle in the
ParticleSet.
"""
return self._head
@property
def tail(self):
"""Returns a ParticleAccessor for the last particle in the
ParticleSet.
"""
return self._tail
@property
def current(self):
"""Returns a ParticleAccessor for the particle that the iteration
is currently at.
"""
return self._current
@abstractmethod
def __repr__(self):
"""Represents the current position in the iteration.
"""
pass
class BaseParticleAccessor(ABC):
"""Interface for the ParticleAccessor. Implements a wrapper around
particles to provide easy access."""
_pcoll = None
def __init__(self, pcoll):
"""Initialize the ParticleAccessor object with at least a
reference to the ParticleSet it encapsulates.
"""
self._pcoll = pcoll
@abstractmethod
def update_next_dt(self, next_dt=None):
pass
def delete(self):
"""Signal the underlying particle for deletion."""
self.state = OperationCode.Delete
def set_state(self, state):
"""Syntactic sugar for changing the state of the underlying
particle.
"""
self.state = state
def succeeded(self):
self.state = StateCode.Success
def isComputed(self):
return self.state == StateCode.Success
def reset_state(self):
self.state = StateCode.Evaluate
@abstractmethod
def getPType(self):
return None
def __getattr__(self, name):
"""The ParticleAccessor should provide an implementation of this
built-in function to allow accessing particle attributes in its
corresponding ParticleSet datastructure.
"""
if name in ['_pcoll', ]:
return super(BaseParticleAccessor, self).__getattribute__(name)
return None
def __setattr__(self, name, value):
"""The ParticleAccessor should provide an implementation of this
built-in function to allow setting particle attributes in its
corresponding ParticleSet datastructure.
"""
if name in ['_pcoll', ]:
super(BaseParticleAccessor, self).__setattr__(name, value)
@abstractmethod
def __repr__(self):
"""The ParticleAccessor should provide an implementation of this
built-in function that returns a string representation of the
Particle that it currently provides access to.
"""
pass