0

The problem I am having is in putting data I have created into a 2D mesh (list of lists) I am creating the data properly, however in trying to put it into the list of lists format the previous lists seems to be overwritten. in my function 'getDistnRechargeGrid', there is a check line, that highlights the problem.

DDgrid[x][y] = DD # <---------------- THIS DOES NOT STORE THE UNIQUE VALUES ????? The last calculate value replaces the previous for x

I would be very grateful to understand why the above does not place the unique values into the list of lists ???

How to you place the correctly calculated value in the list of list and create a unique 2D grid ? The focus here is how to get the value in form where it can plot a 2D grid.

"""
------------------------------- PYTHON 3.xx ------------------------------------------

This script aims to plot the results of pumping at a location on the draw down of the ground water surface

The script initially set up the data of the ground water surface

Then based on Pump & Soil Data, calculates the radial draw down over a grid.


Input parameters:
Transmissivity T =  119 m2/day
Storativity S = 0.002
Pumping rate Q = 1000m3/day
Initial head in aquifer (LHS)* =100 m
Initial head (RHS)* ==90 m
delta t =1day
delta x =50 m
delta y =50 m
Alpha = 1


"""
#-------------------------------------------------------------------------------
def getDistnRechargeGrid(xn,yn,PumpCellX,PumpCellY,Cell_Size,Q,DDgrid,RRgrid):
    """
    This calculates a 2D grid of the distance from each cell to a target cell
    Based on that distance a DrawDown/Recharge Surface is Created as a 2D mesh
    
    """
    chkx = 5
    chky = 5
    
    for x in range(0,xn+1,1):
        for y in range(0,yn+1,1):
            Xd = abs(x-PumpCellX)*Cell_Size
            Yd = abs(y-PumpCellY)*Cell_Size
            DD = (Xd**2+Yd**2)**0.5
            if DD == 0: DD = 1.0 
            RR = Q/DD**2
            if x == chkx and y == chky: chkVal = DD
            # ------- Here is my problem, 
            """
            When i print out the calaculted values below they are correct...
            However when I check the values in the list os lists... all the lists have been replaced with the last list created
            How is this occurring ??
            How do I ensure the lists are ALL retained as I produced them ?
            """
            DDgrid[x][y] = DD # <---------------- THIS DOES NOT STORE THE UNIQUE VALUES ????? The last calculate value replaces the previous for x
            RRgrid[x][y] = RR
            print ('x,y:%i,%i:Xd,Yd:D; %.2f,%.2f;%.2f:RR=%.2f'%(x,y,Xd,Yd,DD,RR))
            #input ('Check')
    print ('Compare Calc Val to Value placed in the grid')
    print ('Location: %i,%i: CalcVal: %.2f, Value in Grid: %.2f' %(chkx,chky,chkVal,DDgrid[chkx][chky]))
    input ('Check')
    # Put it into a numpy  array ??  array(DDgrid)
    #print (DDgrid)
    print (len(DDgrid))
    return(DDgrid,RRgrid)
#------------------------------------------------------------------------------------------    
def setupGroundwaterSurface(xn,yn,LeftH,RighH,PumpCellX,PumpCellY,Q):
    """
    
    """
    XL =[]
    Data = []
    R = []
    RR = []
    DDrow = []
    RRrow = []
    DDgrid = []
    RRgrid = []    
    for x in range(0,xn+1,1): # This will be i
        Value = LeftH - (LeftH-RighH)/xn*x
        print (Value)
        XL.append(Value) # Level of Ground water Surface
        DDrow.append(0.0) #--- Create Gridrow of Zero for Dist
        RRrow.append(0.0) #--- Create Gridrow of Zero for RR
        if x == PumpCellX:
            R.append(Q) # Set the Pump Rate
        else: R.append(0.0)
    for y in range(0,yn+1,1): # This will be j
        Data.append(XL)
        DDgrid.append(DDrow) #Create the 2D grid as Zeroes
        RRgrid.append(RRrow) #Create the 2D grid as Zeroes
        if y == PumpCellY:
            RR.append(R) # Leave only the 1 Specific Cell as the Pump/Recharge Rate
        else: RR.append([0.0 if wd == Q else wd for wd in R]) # Set the Rest to Zero
    print (Data)    
    return(Data,RR,DDgrid,RRgrid)
#-------------------------------------------------------------------------------    
def plot2D_data(data,Suptitle):
    """
    https://matplotlib.org/stable/tutorials/colors/colormaps.html
    """
    import matplotlib.pyplot as plt
    
    im = plt.imshow(data, cmap="rainbow")
    #ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')  # 3D Meshed Surface
    plt.colorbar(im)
    plt.suptitle(Suptitle)
    plt.show()
    return()
#-----------------------------------------------

#------------------ SET UP DATA -------------------
T = 119.0 #m2/day
S = 0.002
Q = -10000.0 # m3/day
PumpCellX = 8
PumpCellY = 8
xn = 15
yn = 15
Cell_Size = 50.0 #(m)
LeftH = 100.0
RighH = 85.0
TimeSteps = 20
alpha = 1
dt = 1.0
dx = 50.0

#--------------- SET UP 2D Grid in LISTS -------------------------

#----- First Create the Water Table Surface based on Levels -------------
Data,RR,DDgrid,RRgrid = setupGroundwaterSurface(xn,yn,LeftH,RighH,PumpCellX,PumpCellY,Q)
plot2D_data(Data,'GroundWater Surface')
print (RR)
plot2D_data(RR,'Well Location Drawdown Rate')
#------- Now Create the Distance Grid and the Recharge Grid----------------
DDgrid,RRgrid = getDistnRechargeGrid(xn,yn,PumpCellX,PumpCellY,Cell_Size,Q,DDgrid,RRgrid)
plot2D_data(DDgrid,'Distance Grid to Well Location')
#print (RRgrid)
plot2D_data(RRgrid,'DrawDown Surface based on Distance to Well')
print (RR)

Rudy Van Drie
  • 91
  • 2
  • 11
  • Short answer: `DDgrid.append(DDrow)` appends the same _object_ to `DDgrid`. When you mutate one you mutate all references of `DDRow`. – Selcuk Jun 11 '21 at 02:35
  • Ok, so how do I avoid that, and write the correct values ? – Rudy Van Drie Jun 11 '21 at 04:47
  • The duplicate question discusses in detail. Copy/deepcopy would be one way to fix it. – Selcuk Jun 11 '21 at 05:31
  • I have looked at that answer, and it states to create the list using Range, which I have ? So why does my attempt not work ? .......................................................... for x in range(0,xn+1,1): for y in range(0,yn+1,1): But then setting a value fails ? DDgrid[x][y] = DD – Rudy Van Drie Jun 14 '21 at 02:34
  • It doesn't say that, you should read the question and answer more carefully. If you need further help please post it as a new question as you cannot format code properly in comments. – Selcuk Jun 14 '21 at 03:11
  • Seeking Clarification Please... (A solution) if DDgrid[x][y] = DD does not assign the value uniquely to Arr[x][y].... how then do I assign a unique value to a unique reference in a list of lists ? why does DDgrid[x][y] = DD not work ?? The list were create with Range as suggested ?? – Rudy Van Drie Jun 14 '21 at 04:38
  • Your problem is in the `DDgrid.append(DDrow)` as I mentioned above. Since you construct a list of the same `DDRow` instances `DDgrid[1][y]` is the same thing as `DDgrid[2][y]`, both when reading and writing. – Selcuk Jun 14 '21 at 04:54
  • Yes ok, I can see the problem, so what is the solution ?? – Rudy Van Drie Jun 14 '21 at 05:06

0 Answers0