Commit e60aa5c7 authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

add 5 models

parent ac17a341
......@@ -83,18 +83,12 @@ threads = zeros(n_starts)
start_costs[i] = cost_f(rl)
opt_costs[i] = local_search!(rl, max_iterations)
threads[i] = Threads.threadid()
end # 12 seconds for 10
# k_temp = 0.08 (215C -> 50C in 25s), average improvement=1.8%
# k_temp = 0.04 (215C -> 50C in 50s), average improvement=4.5%
# k_temp = 0.01 (215C -> 50C in 200s), average improvement=21.7%
# Note: improvement decreases as original stress increases
# (ie proportional of original stress vs thermal strain/stress)
end
@time for i in 1:n_starts
my_costs[i] = local_search!(random_rollout(cdata), 5)
threads[i] = Threads.threadid()
end # 22 seconds
end
### Visualise distribution of stresses
data_stress = .( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
......
This diff is collapsed.
......@@ -10,90 +10,12 @@ from scipy.interpolate import LinearNDInterpolator
import pandas as pd
import json
import utils as tu
class Voxelizer:
"""
Track all relevant mesh objects for pin insertion
"""
def __init__(self,
mesh_file_name, # vtk file as generated by FreeCAD
voxel_dim=1,
layer_height=0.2
):
# record params
s = time.time()
self.voxel_dim = voxel_dim
self.lheight = layer_height
print("Reading mesh")
self.mesh = pv.read(mesh_file_name)
self.boundary = self.mesh.decimate_boundary()
v = time.time()
print(v-s)
print("Voxelizing mesh")
hwratio = voxel_dim/self.lheight
m = self.boundary.copy()
m.points[:,2] *= hwratio
self.grid = pv.voxelize(m, density=self.voxel_dim)
self.grid.points[:,2] *= 1/hwratio
c = time.time()
print(c-s)
print("Getting Voxel centers")
points_to_add = []
temptree = KDTree(self.grid.points, leafsize=5) #kd tree for fast lookup of adjacent grid points
# TODO This logic is not necessarily accurate - voxel could still be empty
# but should be fine for our case
d1, ii = temptree.query(self.grid.points + [voxel_dim, voxel_dim, layer_height])
d2, ii = temptree.query(self.grid.points + [voxel_dim, 0, layer_height])
d3, ii = temptree.query(self.grid.points + [0, voxel_dim, 0])
self.centers = self.grid.points[d1 + d2 + d3 < 1e-4] + [voxel_dim/2, voxel_dim/2, layer_height/2]
# move grid points to center of each voxel and remove grid points which are not contained in a voxel
gs = time.time()
print(gs-c)
print("Interpolating pointcloud to grid stresses")
#self.interpolator = LinearNDInterpolator(self.mesh.points, self.mesh['StressValues'])
print(time.time()-gs) # up to 100s for mesh with 300k points
print("Performing interpolations")
#self.cstress = self.interpolator(self.centers)
def check_interp1s(self, node_is):
"""
Works my removing nodes in list node_is from mehs and linear interpolating their stresses
and then comparing to true values
return predicted values and true values
# called with node_is = [i for i in range(0,308539, 1000)]
"""
s = time.time()
except_i = [i for i in np.arange(self.mesh.points.shape[0]) if i not in node_is]
interpolator = LinearNDInterpolator(self.mesh.points[except_i,:], self.mesh['StressValues'][except_i])
node_i_pred_stress = interpolator(self.mesh.points[node_is,:])
node_i_stress = self.mesh['StressValues'][node_is]
e = time.time()
print(e-s)
return node_i_pred_stress, node_i_stress
def export_voxels(self, fname):
"""
Export voxel center locations and voxel stress to csv
"""
df = pd.DataFrame(np.hstack((self.centers,self.cstress.reshape(-1,1))), columns=['x','y','z','stress'])
csv_name = "{}-{}-{}.csv".format(fname, self.voxel_dim, self.lheight)
df.to_csv(csv_name, index=False)
return csv_name
from os.path import exists
def voxelised_csv(mesh_file_name,
csv_fname,
voxels=None,
voxels='',
skip_lines=4,
voxel_dim=3,
layer_height=1
......@@ -115,7 +37,7 @@ def voxelised_csv(mesh_file_name,
print("Voxelizing mesh")
hwratio = voxel_dim/layer_height
if voxels is None:
if not exists(voxels):
m = mesh.copy()
m.points[:,2] *= hwratio
grid = pv.voxelize(m, density=voxel_dim, check_surface=False)
......@@ -133,13 +55,17 @@ def voxelised_csv(mesh_file_name,
c = time.time()
print(c-s)
v2 = pd.DataFrame(centers, columns=['x','y','z'])
v2.to_csv('temp_vox_centers.csv', index=False)
#v2.to_csv('temp_vox_centers.csv', index=False)
else:
centers= pd.read_csv(voxels).to_numpy()
data = pd.read_csv(csv_fname, header=skip_lines)
locs = data.iloc[:,1:4].to_numpy()
locs_pv = pv.PolyData(locs)
if 'M3' in mesh_file_name:
locs_pv.rotate_x(-90)
elif 'M5' in mesh_file_name:
locs_pv.rotate_x(90)
tu.transform_to_bed_centre(locs_pv, bed_centre)
tu.transform_to_z0(locs_pv)
locs = locs_pv.points
......@@ -149,25 +75,48 @@ def voxelised_csv(mesh_file_name,
stress = interpolator(centers)
# output values
out = pd.DataFrame(np.hstack((centers, stress)), columns=['x', 'y', 'z', 'Sx', 'Sy', 'Sz', 'Txy', 'Tyz', 'Txz'])
out = pd.DataFrame(np.hstack((centers, stress)), columns=['x', 'y', 'z', 'Sx', 'Sy', 'Sz', 'Txy', 'Txz', 'Tyz'])
out = out.dropna()
return out
def check_data(mesh_file_name, csv_fname):
mesh = pv.read(mesh_file_name)
bed_centre = [110, 110, 0]
tu.transform_to_bed_centre(mesh, bed_centre)
tu.transform_to_z0(mesh)
data = pd.read_csv(csv_fname, header=4)
locs = data.iloc[:,1:4].to_numpy()
locs_pv = pv.PolyData(locs)
#locs_pv.rotate_x(-90) # M3
#locs_pv.rotate_x(90) # M5
tu.transform_to_bed_centre(locs_pv, bed_centre)
tu.transform_to_z0(locs_pv)
pl = pv.Plotter()
pl.add_mesh(mesh, opacity=0.3)
pl.add_mesh(locs_pv)
pl.show()
def visualise_voxels(fname):
data = pd.read_csv(fname)
locs = data.iloc[:,1:4].to_numpy()
locs_pv = pv.PolyData(locs)
locs_pv.plot()
def contour2dict(c):
return {'pos':c.pos, 'time':c.time}
if __name__ == '__main__':
obj = 'M1'
obj = 'models/M1'
#check_data(obj + '.stl', obj + '_raw.csv')
out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv')
out.to_csv(obj + 'voxels.csv')
#pl = Voxelizer(obj + '.vtk', 1, 1) #0.25)
#pl.export_voxels(obj)
out.to_csv(obj + '_voxels.csv')
visualise_voxels(obj + '_voxels.csv')
#contours = gc.decode_gcode(obj + '.gcode')
#outfile = open(obj + 'contours.json','w')
#json.dump([contour2dict(c) for c in contours], outfile)
#outfile.close()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment