Skip to content

Commit

Permalink
Fix(ModflowDis): zero based get_node() and get_lrc()… (#847)
Browse files Browse the repository at this point in the history
* Fix(ModflowDis): zero based get_node() and get_lrc() for input parameters and output

* added test_get_lrc_get_node()
  • Loading branch information
jlarsen-usgs committed Apr 10, 2020
1 parent f514a22 commit 52ecd98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 13 additions & 0 deletions autotest/t007_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,19 @@ def test_get_vertices():
assert np.array_equal(a1, a2)


def test_get_lrc_get_node():
node = 50
ml = flopy.modflow.Modflow()
dis = flopy.modflow.ModflowDis(ml, nlay=1, nrow=1, ncol=201, delr=10,
delc=1, top=50, botm=0)
lrc = dis.get_lrc([node, ])
if lrc[0] != (0, 0, 50):
raise AssertionError("get_lrc() is not returning zero based (k, i, j)")
nodes = dis.get_node(lrc)
if nodes[0] != node:
raise AssertionError('get_node() is not returning zero based node')


def test_vertex_model_dot_plot():
import matplotlib.pyplot as plt
# load up the vertex example problem
Expand Down
10 changes: 6 additions & 4 deletions flopy/modflow/mfdis.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ def get_rc_from_node_coordinates(self, x, y, local=True):

def get_lrc(self, nodes):
"""
Get layer, row, column from a list of MODFLOW node numbers.
Get layer, row, column from a list of zero based
MODFLOW node numbers.
Returns
-------
Expand All @@ -491,20 +492,21 @@ def get_lrc(self, nodes):
nrc = self.nrow * self.ncol
v = []
for node in nodes:
k = int(node / nrc)
k = int((node + 1) / nrc)
if (k * nrc) < node:
k += 1
ij = int(node - (k - 1) * nrc)
i = int(ij / self.ncol)
if (i * self.ncol) < ij:
i += 1
j = ij - (i - 1) * self.ncol
v.append((k, i, j))
v.append((k - 1, i - 1, j))
return v

def get_node(self, lrc_list):
"""
Get node number from a list of MODFLOW layer, row, column tuples.
Get node number from a list of zero based MODFLOW
layer, row, column tuples.
Returns
-------
Expand Down

4 comments on commit 52ecd98

@andyrich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't node 0 have layer, row, column = 0? currently it returns (-1, 9, 10). am i wrong, or is the rest of the behavior also incorrect as shown below?

image

@andyrich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shows the problem more clearly:
image

@langevin-usgs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an open issue: #1040.

@andyrich
Copy link

@andyrich andyrich commented on 52ecd98 Jan 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def get_lrc(ml, node):
    '''
    Get zero-based layer, row, column from a list of zero based
    MODFLOW node numbers.

    Returns
    -------
    v : list of tuples containing the layer (k), row (i),
    and column (j) for each node in the input list
    '''
    k,i,j = np.indices((ml.dis.nlay,ml.dis.nrow, ml.dis.ncol))
    k = k.ravel()
    i = i.ravel()
    j = j.ravel()
    
    nodes = np.arange(j.shape[0])
    
    d = dict(list(zip(nodes,list(zip(k,i,j)))))
    
    allnodes = []
    for node_i in node:
        allnodes.append(d[node_i])
    
    return allnodes
def get_node(ml, lrc):  
    '''
    Get node number from a list of zero based MODFLOW
    layer, row, column tuples.

    Returns
    -------
    v : list of MODFLOW nodes for each layer (k), row (i),
        and column (j) tuple in the input list
    '''
    
    allnodes = np.arange(ml.dis.nrow*ml.dis.ncol*ml.dis.nlay)
    allnodes = allnodes.reshape((ml.dis.nlay,ml.dis.nrow, ml.dis.ncol))
    
    f = []
    for kij in lrc:
        lay = kij[0]
        row = kij[1]
        col = kij[2]

        f.append( allnodes[lay,row,col])

    return  f

Please sign in to comment.