Page MenuHomec4science

analytical_solution_final.py
No OneTemporary

File Metadata

Created
Tue, Nov 19, 19:38

analytical_solution_final.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 26 17:33:53 2022
@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
def parse_args():
"""
Function to handle arguments
"""
parser = argparse.ArgumentParser(description='Run FEM of single scan on a simple box')
parser.add_argument("rho", type=float, help='density in kg/m^3')
parser.add_argument("c_p", type=float, help='specific heat in J/kgK')
parser.add_argument("k", type=float, help='thermal conductivity in W/Km')
parser.add_argument("time", type=float, help='total simulation time in s')
parser.add_argument("t_off", type=float, help='time when the scan is turned off in s')
parser.add_argument("power", type=float, help='laser power in W')
parser.add_argument("vel_mag", type=float, help='velocity magnitude in m/s')
parser.add_argument("num_steps", type=int, help='total number of steps')
args = parser.parse_args()
return args
def main():
#get the arguments
args = parse_args()
#define simulation parameters
rho = args.rho #kg/m^3
c_p = args.c_p #J/kgK
k = args.k #W/mK
time = args.time #s
t_off = args.t_off #s
power = args.power #W
velocity_mag = args.vel_mag
num_steps = args.num_steps
alpha = k/(rho*c_p) #m^2/s
dt = time/num_steps #s
tol = 1e-14
velocity = velocity_mag*np.array([1, 0, 0]) #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
#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('analytic')
except FileExistsError:
for file in os.scandir('analytic'):
os.remove(file.path)
#define vtk file to save the data at each iteration
vtkfile_analytical = File('analytic/analytic_temperature.pvd')
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
factor = power/(4*rho*c_p*(np.pi*alpha)**1.5)
layer = Layer(0.008)
scan = Scan(tol, t_off, source_loc, velocity)
#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_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)
#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")
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))
if __name__=='__main__':
main()

Event Timeline