0

i have the following code:

from math import *
from scipy.special import *
import matplotlib.pyplot as plt
import csv


## Definition der Parameter für Druckgleichung nach Rudnicki (1986) ##
q = 6.0/1000                                   # Fluidmenge pro Fläche und Zeit [l/s]
rhof = 1000                                     # Dichte Flüssigkeit [kg/m³]
lameu = 11.2*10**9                              # Lamé-Parameter, undrained [Pa]
lame = 8.4*10**9                                # Lamé-Parameter, drained [Pa]
pi                                              # durch Pythonmodul "math" gegeben
alpha = 0.65                                    # Biot-Willis-Koeffizient
G = 8.4*10**9                                   # Schermodul [Pa]
k = 1.0e-15                                     # Permeabilität [m²] bzw. [Darcy] 
eta = 0.001                                     # Viskosität des Fluids [Pa*s]            
t = 1000*24*3600                                # Zeit in [s]

## Beziehungen der Parameter untereinander ##
kappa = k/eta                                                   # Berechnung der Permeabilität nach Rudnicki (1986), [m³*s/kg]
print "kappa ist:",kappa                                        # Ausgabe Permabilität
c = (kappa*(lameu-lame)*(lame+2*G))/((alpha**2)*(lameu+2*G))    # Berechnung der Diffusivität
print "c ist:",c                                                # Ausgabe der Diffusivität

## Bereiche der Achsen in [m] ##
#def drange(start, stop, step):
#    r = start
#    while r <= stop:
#            yield r
#            r += step    
xmin = 1
xmax = 20
ymin = 1
ymax = 20

## Druckberechnung um Bohrung ##
for x in range (xmin,xmax,1):
    for y in range (ymin,ymax,1):
            r = sqrt(x**2+y**2)
            P = (q/(rhof*4*pi*kappa))*(expn(1,r**2/(4*c*t)))
            z = P/1e6
            #print x, "  ", y, "  ", z

For my ouput, i want to write my values of x,y and z in a csv file. It should look like e.g. each x-value in a cell within a single row and each y-value in a cell within the same column, so that one z-value can be shown for each x/y combination. It should look like this:

   x    x1        ...

y z(x,y) z(x1,y) ...

y1 z(x,y1) z(x1,y1) ...

... ... ...

I'm new to the csv module and not really getting used to it, especially how to set the values in the right places and when to use it (e.g. inside or after loops). Is this possible? Or can it only look like this (similar to my outcommented print):

x y z(x,y)

x y1 z(x,y1)

x y2 z(x,y2) ... ...

Sorry for this quite strange and ugly formatting, but i think you get my idea :)

Any help would - as always - be greatly appreciated!

2
  • what is 'scv' module and what is pi variable? it seems not initialized. Commented Dec 19, 2014 at 1:27
  • sorry Marcin, i forgot to correct these two issues. Thanks for for hint! Commented Dec 19, 2014 at 12:00

1 Answer 1

1

With the csv module you generally have to write entire rows at a time. I believe this code (replacing your final for loop) will print out the output you desire:

def your_function(x,y):
            r = sqrt(x**2+y**2)
            P = (q/(rhof*4*pi*kappa))*(expn(1,r**2/(4*c*t)))
            z = P/1e6

x_range = range(xmin,xmax,1)
y_range = range(ymin,ymax,1)

print(x_range)
for y in y_range:
    this_row = [y] + [your_function(x,y) for x in x_range]
    print(this_row)

If that does what you want, it should be straightforward to replace the print functions with writing to CSV.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.