File indexing completed on 2025-01-18 09:17:04
0001 import sys
0002 import struct
0003 import math
0004 import numpy as np
0005
0006
0007 vol_work = []
0008 vol_result = []
0009
0010
0011
0012
0013
0014
0015 sizex = 128
0016 sizey = 128
0017 sizez = 128
0018 resolx = 3.90625
0019 resoly = 3.90625
0020 resolz = 3.90625
0021 superres = 8
0022
0023
0024 r1 = 196
0025
0026
0027 r2 = 171
0028
0029
0030 type = 2
0031
0032
0033 value1 = 0
0034 value2 = 0
0035
0036 if type == 1:
0037 value1 = 108
0038 value2 = 0
0039 elif type == 2:
0040 value1 = 54000
0041 value2 = 0
0042
0043
0044 x0 = 0.0
0045 y0 = 0.0
0046 z0 = -1.953125
0047
0048
0049 super_sizex = sizex * superres
0050 super_sizey = sizey * superres
0051 super_sizez = sizez * superres
0052 center_shift = sizex / 2
0053
0054
0055 def make_sphere_center(r, x, y, z, value):
0056 rsample = (r / resolx) * superres
0057 xsample = (x / resolx) * superres
0058 ysample = (y / resoly) * superres
0059 zsample = (z / resolz) * superres
0060 center_shift_sample = center_shift * superres
0061
0062 print(rsample, xsample, ysample, zsample)
0063 number = 0
0064
0065 for k in range(0, super_sizez):
0066 for j in range(0, super_sizey):
0067 for i in range(0, super_sizex):
0068 ii = i + 0.5
0069 jj = j + 0.5
0070 kk = k + 0.5
0071 res = pow((ii - xsample - center_shift_sample), 2) / (rsample * rsample) + \
0072 pow((jj - ysample - center_shift_sample), 2) / (rsample * rsample) + \
0073 pow((kk - zsample - center_shift_sample), 2) / (rsample * rsample)
0074
0075 if (res <= 1.0):
0076 vol_work[i + j * super_sizex + k * super_sizex * super_sizey] = value
0077 number += 1
0078
0079 print(number)
0080
0081
0082
0083 def initialize():
0084 for k in range(0, sizez):
0085 for j in range(0, sizey):
0086 for i in range(0, sizex):
0087 vol_result.append(0.0)
0088 for k in range(0, super_sizez):
0089 for j in range(0, super_sizey):
0090 for i in range(0, super_sizex):
0091 vol_work.append(0.0)
0092
0093
0094
0095 def undersample():
0096 x = 0
0097 y = 0
0098 z = 0
0099 for k in range(0, super_sizez, superres):
0100 for j in range(0, super_sizey, superres):
0101 for i in range(0, super_sizex, superres):
0102
0103 total = 0
0104 for kk in range(0, superres):
0105 for jj in range(0, superres):
0106 for ii in range(0, superres):
0107 total = total + vol_work[
0108 i + ii + (j + jj) * super_sizex + (k + kk) * (super_sizex * super_sizey)]
0109 vol_result[x + y * sizex + z * (sizex * sizey)] = total / (superres * superres * superres)
0110
0111 x += 1
0112 y += 1
0113 x = 0
0114 z += 1
0115 y = 0
0116
0117
0118
0119 def save_whole_work(file):
0120 fd = open(file, "wb")
0121 for i in range(0, len(vol_work)):
0122 fd.write(struct.pack("f", vol_work[i]))
0123 fd.close()
0124
0125
0126
0127
0128 def save_workslice(file, slice):
0129 fd = open(file, "wb")
0130 for j in range(0, super_sizey):
0131 for i in range(0, super_sizex):
0132 fd.write(struct.pack("f", vol_work[i + j * super_sizex + slice * super_sizex * super_sizey]))
0133 fd.close()
0134
0135
0136
0137 def save_whole_result(file):
0138 fd = open(file, "wb")
0139 for i in range(0, len(vol_result)):
0140 fd.write(struct.pack("f", vol_result[i]))
0141 fd.close()
0142
0143
0144
0145
0146 def save_slice(file, slice):
0147 fd = open(file, "wb")
0148 for j in range(0, sizey):
0149 for i in range(0, sizex):
0150 fd.write(struct.pack("f", vol_result[i + j * sizex + slice * sizex * sizey]))
0151
0152 fd.close()
0153
0154
0155 print("------intialisation------")
0156 initialize()
0157 print("------end intialisation------")
0158
0159 print("------begin sphere 1------")
0160 make_sphere_center(r1, x0, y0, z0, value1)
0161 print("------end sphere 1------")
0162
0163 print("------begin sphere 2------")
0164 make_sphere_center(r2, x0, y0, z0, value2)
0165 print("------end sphere 2------")
0166
0167 print("------begin undersample------")
0168 undersample()
0169 print("------end undersample------")
0170
0171 print("The length of vol_work: ", len(vol_work))
0172 print("The length of vol_result: ", len(vol_result))
0173
0174
0175
0176
0177
0178 save_whole_result("./vol_result.dat")
0179 save_whole_work("./vol_work.dat")
0180 print("------end save work------")