Page MenuHomec4science

superposition.py
No OneTemporary

File Metadata

Created
Tue, Nov 19, 18:32

superposition.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 22 16:08:08 2021
@author: kubilay
"""
from fenics import *
import os
import numpy as np
from helpers import *
import time as timer
import matplotlib.pyplot as plt
from functools import partial
import argparse
import numexpr as ne
#define simulation parameters
rho = 4420 #kg/m^3
c_p = 750 #J/kgK
k = 27 #W/mK
alpha = k/(rho*c_p) #m^2/s
time = 0.008 #s
t_off = 0.002984
num_steps = 20
dt = time/num_steps #s
power = 175 #W
tol = 1e-14
velocity = 0.8*np.array([1, 0, 0]) #m/s
velocity_mag = np.linalg.norm(velocity) #m/s
source_loc = np.array([0, -0.225, 2])*1e-3-np.array([0, 0, 1])*1e-6 #m
#read the mesh
#mesh = Mesh('../Part_geometry/mesh/layer_005.xml') #in mm
#MeshTransformation.rescale(mesh, 1e-3, Point(0,0,0)) #in m
mesh = Mesh()
filename = '../Part_geometry/mesh/layer_005.xdmf'
f = XDMFFile(MPI.comm_world, filename)
f.read(mesh)
MeshTransformation.rescale(mesh, 1e-3, Point(0,0,0)) #in m
f.close()
#define function space for v
V = FunctionSpace(mesh, "CG", 1)
#define the function and time
T = interpolate(Constant(0.0), V)
t = 0.00001
#create directory to save files
try:
os.mkdir('results/analytic')
except FileExistsError:
for file in os.scandir('results/analytic'):
os.remove(file.path)
#define vtk file to save the data at each iteration
#vtkfile_analytical = File('results/analytic/analytic_temperature.pvd')
xdmf_file = XDMFFile(MPI.comm_world, "results/analytic/analytic.xdmf")
xdmf_file.parameters["flush_output"] = True
xdmf_file.parameters["functions_share_mesh"] = True
xdmf_file.parameters["rewrite_function_mesh"] = False
start = timer.time()
#T_final = Function(V)
#define arrays to store data on the points of interest
T_start = np.zeros(num_steps)
T_middle = np.zeros(num_steps)
T_end = np.zeros(num_steps)
start_point = np.array([0, -0.225, 2])*1e-3-np.array([0, 0, 300])*1e-6
mid_point = np.array([1.19375, -0.225, 2])*1e-3-np.array([0, 0, 300])*1e-6
end_point = np.array([2.3875, -0.225, 2])*1e-3-np.array([0, 0, 300])*1e-6
scan_limits = get_interpolation_limits(0.00377/2, velocity_mag, alpha)
factor = power/(4*rho*c_p*(np.pi*alpha)**1.5)
layer = Layer()
scan = Scan(tol, t_off, source_loc, velocity, scan_limits)
#layer.addScan([scan, Scan(0.0031, 0.006084, source_loc + t_off*velocity + np.array([0,0.00007, 0]), -velocity)])
layer.addScan([scan])
#calculate anaytical temperature at each time step
for n in range(num_steps):
#T.vector()[:] = run_analytical_fields(mesh.coordinates()[dof_to_vertex_map(V)], layer, t, alpha, factor)
T.vector().set_local(run_analytical_fields(mesh.coordinates()[dof_to_vertex_map(V)], layer, t, alpha, factor))
as_backend_type(T.vector()).vec().ghostUpdate()
T_start[n] = T(start_point)
T_middle[n] = T(mid_point)
T_end[n] = T(end_point)
T.rename("Temperature", "Analytical Temperature")
#vtkfile_analytical << (T,t)
xdmf_file.write(T, t)
#increment time and recalculate time dependent functions
t += dt
print('{}% Complete'.format(round(100*n/num_steps,2)))
print('it took:', timer.time()-start)
#print temperature evalution at points of interest
plt.plot(np.linspace(0,time,num_steps), T_start/power, label='start point', marker='.')
plt.plot(np.linspace(0,time,num_steps), T_middle/power, label='middle point', marker='.')
plt.plot(np.linspace(0,time,num_steps), T_end/power, label='end point', marker='.')
plt.xlabel(r'$time (s)$')
plt.ylabel("$\Delta T/P (K/W)$")
plt.legend()
plt.savefig("Figures/analytical_temperature_evolution.jpg")
plt.show()
np.savetxt('Figures/analytic_start.txt', T_start/power)
np.savetxt('Figures/analytic_middle.txt', T_middle/power)
np.savetxt('Figures/analytic_end.txt', T_end/power)
np.savetxt('Figures/analytic_time.txt', np.linspace(0,t,num_steps))

Event Timeline