plot sample
This commit is contained in:
@ -1,9 +1,16 @@
|
||||
from math import pi
|
||||
|
||||
from io import BytesIO
|
||||
import base64
|
||||
from django.shortcuts import render
|
||||
from django.utils.safestring import mark_safe
|
||||
import re
|
||||
import numpy as np
|
||||
import matplotlib
|
||||
from setuptools.command.rotate import rotate
|
||||
|
||||
matplotlib.use('Agg')
|
||||
from matplotlib.patches import Wedge, FancyArrowPatch
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from isotopapp.models import Isotope
|
||||
# Create your views here.
|
||||
@ -42,9 +49,10 @@ def result(request):
|
||||
|
||||
field_T = freq / isotope1.gamma
|
||||
|
||||
close_isotopes = []
|
||||
|
||||
if request.GET.get('search') == "":
|
||||
figure=None
|
||||
print(request.GET)
|
||||
if request.GET.get('range_search') == "":
|
||||
close_isotopes = [isotope_info(isotope1, field_T)]
|
||||
freq_range = float(request.GET.get('freq_range'))
|
||||
Isotope.objects.filter()
|
||||
# calculate the frequency for all isotopes and compile a list of close by isotopes
|
||||
@ -56,21 +64,49 @@ def result(request):
|
||||
close_isotopes.append(isotope_info(isotope, field_T))
|
||||
ans = sorted(close_isotopes, key=lambda x: -float(x[3]))
|
||||
|
||||
elif request.GET.get('calculate') == "":
|
||||
elif request.GET.get('gradient_search') == "":
|
||||
fig, ax = plt.subplots(1, 1)
|
||||
theta1, theta2 = 0, 360
|
||||
radius = 2.5
|
||||
center = (0, 0)
|
||||
w = Wedge(center, radius, theta1, theta2, fc='#D5D9FFB2', edgecolor='black')
|
||||
ax.add_patch(w)
|
||||
|
||||
sample_diameter = 5e-3
|
||||
gradient = float(request.GET.get('gradient'))
|
||||
freq_range = sample_diameter/2 * gradient * isotope1.gamma
|
||||
close_isotopes = []
|
||||
for isotope in Isotope.objects.all():
|
||||
if isotope.gamma == 0: continue
|
||||
if not isotope.stable: continue
|
||||
f_Larmor = field_T*isotope.gamma
|
||||
if abs(f_Larmor - freq) <= freq_range:
|
||||
close_isotopes.append(isotope_info(isotope, field_T))
|
||||
ans = sorted(close_isotopes, key=lambda x: x[3])
|
||||
z = (freq/isotope.gamma-field_T)/gradient
|
||||
if abs(z) <= sample_diameter/2:
|
||||
i_info = isotope_info(isotope, field_T)
|
||||
#i_info[3] = f"{z*1e3:.1f} mm"
|
||||
close_isotopes.append(i_info)
|
||||
|
||||
arr = FancyArrowPatch((np.sqrt(2.5**2-(z*1e3)**2),z*1e3), (-np.sqrt(2.5**2-(z*1e3)**2), z*1e3),
|
||||
arrowstyle='<->,head_width=.1', mutation_scale=20)
|
||||
ax.add_patch(arr)
|
||||
ax.annotate(f"{isotope.n_nucleons}{isotope.symbol}", (1.05, 0), xycoords=arr, ha='left', va='center',fontsize=7)
|
||||
|
||||
ax.set_aspect('equal')
|
||||
ax.set_xlim([-4, 4])
|
||||
ax.set_ylim([-4, 4])
|
||||
ax.set_ylabel('z (mm)')
|
||||
ax.grid(True)
|
||||
buf = BytesIO()
|
||||
plt.savefig(buf, format='png')
|
||||
figure = base64.b64encode(buf.getvalue()).decode('utf-8').replace('\n', '')
|
||||
buf.close()
|
||||
ans = sorted(close_isotopes, key=lambda x: float(x[3]))
|
||||
|
||||
elif request.GET.get('transform') == "":
|
||||
n2, element2 = extract_isotope_parts(request.GET.get('isotope2'))
|
||||
isotope2 = Isotope.objects.filter(symbol=element2, n_nucleons=n2).get()
|
||||
#isotope_info(isotope2, field_T)
|
||||
ans = [isotope_info(isotope2, field_T)]
|
||||
|
||||
return render(request, 'result.html', {'ans': ans})
|
||||
else:
|
||||
ans = []
|
||||
return render(request, 'result.html', {'ans': ans, 'figure': figure})
|
||||
|
Reference in New Issue
Block a user