- #1
member 428835
Hi PF!
I'm wanting to run a function in parallel, which I've denoted integrate below, on line 33.
I'd like to run the integrate function over a given range, in this case range(len(fL)) = [0,1,2,3,4,5,6,7,8]. Notice I'm trying to store the results of integrate(i) as K. But on my output, K is seemingly a list of zeros. However, if I add print(K) immediately before line 75 I get the correct value. Something seems to be overwriting K, Any ideas?
I've attached the necessary .txt files so you can run it too.
Thanks so much!
I'm wanting to run a function in parallel, which I've denoted integrate below, on line 33.
Python:
from math import *
from random import *
import math, scipy.special
import statistics
import multiprocessing as mp
# READ MATHEAMTICA FUNCTIONS
with open("funcL.txt") as fileL:
fL = fileL.readlines()
with open("funcR.txt") as fileR:
fR = fileR.readlines()
# DEFINE MATH REQUIRED
Pi = math.pi
def Sqrt(x):
return math.sqrt(x)
def Power(a,b):
return a**b
def BesselJ(z,v):
return scipy.special.jv(z,v)
def Cosh(x):
return math.cosh(x)
def Cos(x):
return math.cos(x)
def Csc(x):
return 1/math.sin(x)
def Cot(x):
return 1/math.tan(x)
# DEFINE ALPHA
with open("alpha.txt") as alpha:
alpha1 = alpha.readlines()
alpha = eval(alpha1[0])
# MCI INTEGRATION
K = [0] * len(fL)
def integrate(i):
def funcL(x,y):
# RETURN INTEGRAND AS FUNCTION
return (eval(fL[i]))
def funcR(x,y):
# RETURN INTEGRAND AS FUNCTION
return (eval(fR[i]))
# DEFINE DOMAINS
def testRegion(pt):
return (pt[0] > pt[1])
def genpoint():
# GENERATE COORDINATES IN A SQUARE
x = (1 - math.sin(alpha))*random() + math.sin(alpha)
y = (1 - math.sin(alpha))*random() + math.sin(alpha)
return (x,y)
# INITIALIZE
SumL = 0.0
SumR = 0.0
Area = (1 - math.sin(alpha))**2
NL = 0
NR = 0
# PARAMETERS OF MCI
samp_pts = 100
int_dist = []
iterations = 100
# INTEGRATION
for _ in range(iterations):
for _ in range(samp_pts):
pt = genpoint()
if testRegion(pt):
SumR += funcR(pt[0],pt[1])
NR += 1
else:
SumL += funcL(pt[0],pt[1])
NL += 1
solL = SumL*Area/NL
solR = SumR*Area/NR
sol = solL + solR
int_dist.append(sol)
K[i] = statistics.mean(int_dist)
return K[i]
def main():
pool = mp.Pool(mp.cpu_count())
result = pool.map(integrate, range(len(fL)) )
if __name__ == "__main__":
main()
print(K)
I've attached the necessary .txt files so you can run it too.
Thanks so much!