asdf
This commit is contained in:
@ -175,6 +175,86 @@ def READcheckpoint(MESH, mode, output_path, checkpoint_path, filename, outname,
|
||||
for bb in bnds:
|
||||
QQ[bb] = []
|
||||
|
||||
if mode == 'perturbation':
|
||||
u = Function(W)
|
||||
unew = Function(W)
|
||||
u.rename('velocity', outname)
|
||||
unew.rename('velocity', outname)
|
||||
if options['Perturbation']['xdmf']:
|
||||
xdmf_u = XDMFFile(output_path+'u.xdmf')
|
||||
|
||||
if not options['Perturbation']['type']['SNR']=='inf':
|
||||
Noise = True
|
||||
def Add_Noise(signal,SNR):
|
||||
Psignal = signal**2
|
||||
Psignal_av = np.mean(Psignal)
|
||||
Psignal_av_db = 10*np.log10(Psignal_av)
|
||||
Pnoise_av_db = Psignal_av_db - SNR
|
||||
Pnoise_av = 10**(Pnoise_av_db/10)
|
||||
noise_std = np.sqrt(Pnoise_av)
|
||||
noise = np.random.normal(0,noise_std,len(signal))
|
||||
return signal + noise
|
||||
else:
|
||||
Noise = False
|
||||
|
||||
if not options['Perturbation']['type']['phase_contrast']==0:
|
||||
Phase_Contrast = True
|
||||
else:
|
||||
Phase_Contrast = False
|
||||
|
||||
noise_in_coil = options['Perturbation']['type']['coil']
|
||||
|
||||
for k in indexes:
|
||||
path = checkpoint_path + str(k) + '/'+filename+'.h5'
|
||||
hdf = HDF5File(MESH['mesh'].mpi_comm(), path, 'r')
|
||||
hdf.read(u, 'u/vector_0')
|
||||
time = hdf.attributes('u/vector_0').to_dict()['timestamp']
|
||||
hdf.close()
|
||||
uvec = u.vector().get_local()
|
||||
|
||||
if Phase_Contrast:
|
||||
ufactor = options['Perturbation']['type']['phase_contrast']/100
|
||||
VENC = np.max(np.abs(uvec))*ufactor
|
||||
gamma = 267.513e6 # rad/Tesla/sec Gyromagnetic ratio for H nuclei
|
||||
#B0 = 1.5 # Tesla Magnetic Field Strenght
|
||||
TE = 5e-3 # Echo-time
|
||||
Phi1 = gamma*10*TE + 0*uvec
|
||||
Phi2 = gamma*10*TE + np.pi*uvec/VENC
|
||||
M1 = np.cos(Phi1) + 1j*np.sin(Phi1)
|
||||
M2 = np.cos(Phi2) + 1j*np.sin(Phi2)
|
||||
|
||||
if noise_in_coil:
|
||||
SNR = options['Perturbation']['type']['SNR']
|
||||
m1x_new = Add_Noise(np.real(M1),SNR)
|
||||
m1y_new = Add_Noise(np.imag(M1),SNR)
|
||||
m2x_new = Add_Noise(np.real(M2),SNR)
|
||||
m2y_new = Add_Noise(np.imag(M2),SNR)
|
||||
M1_new = m1x_new + 1j*m1y_new
|
||||
M2_new = m2x_new + 1j*m2y_new
|
||||
uvec = (np.angle(M2_new) - np.angle(M1_new))*VENC/np.pi
|
||||
unew.vector()[:] = uvec
|
||||
else:
|
||||
uvec = (np.angle(M2) - np.angle(M1))*VENC/np.pi
|
||||
else:
|
||||
if noise_in_coil:
|
||||
raise Exception('In order to perturb in coils some PC should be selected')
|
||||
|
||||
|
||||
if not noise_in_coil:
|
||||
if Noise:
|
||||
SNR = options['Perturbation']['type']['SNR']
|
||||
unew.vector()[:] = Add_Noise(uvec,SNR)
|
||||
else:
|
||||
unew.vector()[:] = uvec
|
||||
|
||||
print('Writing checkpoint number ',k)
|
||||
write_path = output_path + 'checkpoint/{i}/'.format(i=k)
|
||||
hdf2 = HDF5File(MESH['mesh'].mpi_comm(), write_path + 'u.h5', 'w')
|
||||
hdf2.write(unew, '/u', time)
|
||||
hdf2.close()
|
||||
|
||||
if options['Perturbation']['xdmf']:
|
||||
xdmf_u.write(unew, time)
|
||||
|
||||
if mode == 'interpolation':
|
||||
dt_new = options['Temporal-Interpolation']['dt_new']
|
||||
@ -239,7 +319,6 @@ def READcheckpoint(MESH, mode, output_path, checkpoint_path, filename, outname,
|
||||
if options['Temporal-Interpolation']['xdmf']:
|
||||
xdmf_u.write(unew, (k-1)*dt_new)
|
||||
|
||||
|
||||
if mode == 'average':
|
||||
N_av = options['Temporal-Average']['subsampling_rate']
|
||||
dt = options['Temporal-Average']['dt']
|
||||
@ -1047,7 +1126,16 @@ def ROUTINE(options):
|
||||
ref_check = options['Temporal-Interpolation']['original_check'] + 'checkpoint/'
|
||||
out_check = options['Temporal-Interpolation']['out_check']
|
||||
READcheckpoint(MESH,'interpolation', out_check,ref_check,'u','u',options)
|
||||
|
||||
|
||||
if 'Perturbation' in options:
|
||||
if options['Perturbation']['apply']:
|
||||
if rank==0:
|
||||
print('--- Perturbation in measurements ---')
|
||||
|
||||
MESH = LOADmesh(options['Perturbation']['meshpath'])
|
||||
checkpath = options['Perturbation']['checkpath'] + 'checkpoint/'
|
||||
out_check = options['Perturbation']['checkpath'] + 'Perturbation/'
|
||||
READcheckpoint(MESH,'perturbation', out_check,checkpath,'u','u',options)
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user