【发布时间】:2015-02-14 07:57:44
【问题描述】:
我的函数类似于使用蒙特卡洛积分法确定 pi 值。该函数基本上是在随机位置插入一个分子并估计能量。我将我的 for 循环转换为一个函数,以使用多处理模块在多个内核上运行。但是我从随机数生成器和所有过程的能量值中得到了相似的值。看起来它多次运行该函数,但报告的结果相似。
用户定义函数列表。为简单起见,没有给出详细信息。
def createRandomValuesforRotationTranslation(boxSpace):
def rotateTranslateMolec(randomValues,molec):
def makemolgroups(newMolec,peg):
def steric_closmol_clashes_vdw2(boxMolecs,ResID):
运行一个for循环;
nReplicates = 100
count = 0
throws = 0
for i in range(nReplicates):
throws += 1
randomvalues = createRandomValuesforRotationTranslation(boxSpace)
newMolec = rotateTranslateMolec(randomvalues,rna_mol)
boxMolecs = makemolgroups(newMolec,peg)
output = steric_closmol_clashes_vdw2(boxMolecs,ResID)
count += output
ratio = count /throws
# binomial distribution method V_free = (number of accepted/total)Vbox
V_free = (count/throws)*output_vol
p = count/throws
std_binom = sqrt(throws*p*(1-p))
error_binom = (output_vol/throws)*std_binom
error_binom_fraction = error_binom/V_free
if i % 1 == 0:
print("STEPS %d: BINOMIAL ERROR T.VOLUME %s: ERROR F.VOLUME %s: ESTIMATED VOLUME %s:" %(i, error_binom,error_binom_fraction,ratio))
然后我将 for 循环转换为 ;
def paralle_distances(nReplicates):
count = 0
throws = 0
for i in range(nReplicates):
throws += 1
randomvalues = createRandomValuesforRotationTranslation(boxSpace)
newMolec = rotateTranslateMolec(randomvalues,rna_mol)
boxMolecs = makemolgroups(newMolec,peg)
output = steric_closmol_clashes_vdw2(boxMolecs,ResID)
count += output
ratio = count /throws
# binomial distribution method V_free = (number of accepted/total)Vbox
V_free = (count/throws)*output_vol
p = count/throws
std_binom = sqrt(throws*p*(1-p))
error_binom = (output_vol/throws)*std_binom
error_binom_fraction = error_binom/V_free
if i % 1 == 0:
print("STEPS %d: BINOMIAL ERROR T.VOLUME %s: ERROR F.VOLUME %s: ESTIMATED VOLUME %s:" %(i, error_binom,error_binom_fraction,ratio))
return
import multiprocessing as mp
pool = mp.Pool(processes=4)
results = [pool.apply_async(paralle_distances, args=(x,)) for x in range(1,5)]
我只在这里打印了随机位置值。
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 157.376, 67.453, -132.227 ), ( 0.0216526, 0.765258, 0.64336 ), 16.5297 degree)
(( 157.376, 67.453, -132.227 ), ( 0.0216526, 0.765258, 0.64336 ), 16.5297 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
非常感谢!
【问题讨论】:
-
您确定您的功能实现正确吗?
-
这就是我在这里问的原因。寻找更好的方法来运行脚本。有什么建议吗?
标签: python python-2.7 ipython python-multiprocessing ipython-parallel