Commit 6f4c52dd authored by Jayant Khatkar's avatar Jayant Khatkar
Browse files

simplify interface

parent 6df6246d
include("utils.jl")
import Plots: histogram
### ABS material properties
α_ABS = 63e-6
......@@ -18,45 +19,26 @@ tdfast = tempdecay(215, 25, 0.08)
add = "/Users/jayant/phd/tempaware/models/"
#stress_multiplier!(vd.voxels, 10)
#rl = random_rollout(cdata)
# single local search
#rl = random_rollout(cdata)
#cost_f(rl)
#@time local_search!(rl, max_iterations)
### MAIN LOOP
results = JSON.parse(open(add * "results.json"))
n_local_searches = 10
n_models = 10
max_iterations = 50
n_local_searches = 100
max_iterations = 250
k = 150
i=1 # ith model
obj = "M" * string(i)
# if obj in keys(results) || obj == "M4" || obj == "M9"
# if obj == "M4" || obj == "M9"
# continue # skip is already done
# end
println("Starting " * obj)
obj = "tempaware"
# Load data
f_name = Symbol("cost_" * obj)
println("Loading data...")
obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 20) # contour data
try
cost_func = getfield(Main, f_name)
catch
vd = voxdata(obj_add * "_voxels.csv", cdata)
println("Constructing Cost function...")
construct_cost(cdata, vd, ABS, td, f_name)
end
cdata = contourdata(contours, 40, 2) # contour data
@time vd = voxdata(obj_add * "_voxels.csv", cdata)
println("Constructing Cost function...")
construct_cost(cdata, vd, ABS, td, f_name)
construct_cost_hist(cdata, vd, ABS, td)
cost_func = getfield(Main, f_name)
construct_best_neighbor(cdata, cost_func, k)
......@@ -68,16 +50,17 @@ update_result(results, obj, rl_d, c, :default)
# greedy rollout cost
rl_g = greedy_rollout(cdata)
c = cost_func(rl_g)
#update_result(results, obj, rl_g, c, :greedy)
update_result(results, obj, rl_g, c, :greedy)
plot(rl_g)
# local search
println("Doing local search " * string(n_local_searches) * " times")
Threads.@threads for i in 1:n_local_searches
rl = greedish_rollout(cdata)
greedish_cost = cost_func(rl) # change to random
println("GREEDISH COST: " * string(greedish_cost))
update_result(results, obj, rl, greedish_cost, :greedish)
#Threads.@threads
for i in 1:1#n_local_searches
rl = greedy_rollout(cdata)
random_cost = cost_func(rl) # change to random
println("RANDOM COST: " * string(random_cost))
update_result(results, obj, rl, random_cost, :random)
local_cost = local_search!(rl, max_iterations)
println("LOCAL COST: " * string(local_cost))
update_result(results, obj, rl, local_cost, :local)
......@@ -91,10 +74,10 @@ data_stress = .√( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
data_strain = .(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2)
data = data_stress
histogram(data) # vast majority of voxels near 0 stress - can ignore
histogram!(sort(data, rev=true)[1:4000]) # vast majority of voxels near 0 stress - can ignore
histogram!(sort(data, rev=true)[1:1000]) # vast majority of voxels near 0 stress - can ignore
# voxmap sanity check
plot(vd, 1500, cdata)
plot(vd, 1000, cdata)
# rollouts
rl_d = Vector(1:length(cdata.contours))
......@@ -102,36 +85,32 @@ rl_r = random_rollout(cdata)
rl_g = greedy_rollout(cdata)
rl_l = Vector{Int}(results[obj]["best_rollout"])
plot(rl_d) # plot rollout line graph
plot(rl_l) # plot rollout line graph
plot(rl_d, cdata) # plot rollout 3d graph
plot(rl_r, cdata)
plot(rl_g, cdata)
plot(rl_l, cdata)
# plot rollout animated
@time plot_animate(rl_g, cdata, rate=5)
@time plot_animate(rl_l, cdata, rate=1)
# plot results
plot(results)
# plot before and after histograms
import Plots: histogram
i = 8
obj = "M" * string(i)
obj_add = add * obj
contours = clean_contour.(contour.(JSON.parse(open(obj_add * "contours.json"))))
cdata = contourdata(contours, 40, 20) # contour data
vd = voxdata(obj_add * "_voxels.csv", cdata)
construct_cost_hist(cdata, vd, ABS, td, :cost_hist)
histogram(
cost_hist(Vector(1:length(cdata.contours))),
cost_hist(rl_d),
bins=50,
label="Default " * string(sum(cost_hist(rl_d))),
title=obj,
opacity=0.5)
histogram!(
cost_hist(rl_g),
bins=50,
label="Default " * string(sum(cost_hist(Vector(1:length(cdata.contours))))),
label="greedy " * string(sum(cost_hist(rl_g))),
opacity=0.5)
histogram!(
cost_hist(Vector{Int}(results[obj]["best_rollout"])),
cost_hist(rl_l),
bins=50,
label="Opt " * string(sum(cost_hist(Vector{Int}(results[obj]["best_rollout"])))),
label="local_search " * string(sum(cost_hist(rl_l))),
opacity=0.5)
\ No newline at end of file
......@@ -671,6 +671,7 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp
relbelows = vd.below[valid_voxels]
rel_voxels = vd.voxels[considered_voxels,:]
relmaps = vd.maps
vox_area_scaling = min.(abs.(1 ./ rel_voxels.AreaRatio),1)
# voxtimes vectorize
max_contours_per_voxel = maximum([length(r.seglen) for r in relmaps])
......@@ -709,10 +710,10 @@ function construct_cost(cdata::contourdata, vd::voxdata, mat::material, td::temp
σ12 = rel_v.Txy
σ23 = rel_v.Tyz + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT
σ31 = rel_v.Txy + $((cdata.layer_height/vd.width)*mat.E*mat.α)*ΔT
return sum($F * (σ11 - σ22).^2 +
return sum($vox_area_scaling .* ($F * (σ11 - σ22).^2 +
$G * ((σ33 - σ11).^2 + (σ33 - σ22).^2) +
$(2 * L) * (σ12).^2 +
$(2 * M) * (σ23 + σ31).^2)
$(2 * M) * (σ23 + σ31).^2))
end
end
return eval(a)
......@@ -723,13 +724,19 @@ function construct_cost_hist(cdata::contourdata, vd::voxdata, mat::material, td:
contour_times = [cdata.contours[c].time[end] for c in 1:length(cdata.contours)]
# considered voxels
println("n total voxels:")
println(length(vd.maps))
not_empty_voxels = length.([m.seglen for m in vd.maps]) .>0
valid_voxels = (1:length(vd.below))[(vd.below.!=0) .& not_empty_voxels]
valid_voxels = valid_voxels[not_empty_voxels[vd.below[valid_voxels]]]
println("n empty voxels:")
println(sum(not_empty_voxels))
considered_voxels = valid_voxels
relbelows = vd.below[valid_voxels]
rel_voxels = vd.voxels[considered_voxels,:]
println("n valid voxels:")
println(size(rel_voxels)[1])
relmaps = vd.maps
# voxtimes vectorize
......@@ -881,7 +888,7 @@ end
function plot(rl::Vector{Int}, cdata::contourdata)
n = length(rl)
cols = range(HSV(720,1,1), stop=HSV(-720,1,1), length=n)
cols = range(HSV(180,1,1), stop=HSV(-180,1,1), length=n)
traces = Vector{GenericTrace}()
layout = Layout(
scene_aspect_ratio="data",
......
......@@ -105,6 +105,7 @@ def check_data(mesh_file_name, csv_fname, from_inches=False):
tu.transform_to_z0(mesh)
data = pd.read_csv(csv_fname, header=4)
locs = data.iloc[:,1:4].to_numpy()
print(locs)
if from_inches:
locs = locs*25.4
locs_pv = pv.PolyData(locs)
......@@ -194,19 +195,24 @@ def vox_section_area(voxels, mesh_file_name, vox_dim=3):
print("Fixed {} of those".format(fixed))
visualise_voxels(voxels, stresses=SecArea)
return SecArea/(vox_dim**2)
if __name__ == '__main__':
obj = 'models/M9'
#check_data(obj + '.stl', obj + '_raw.csv', from_inches=True)
#out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv', from_inches=True)
obj = 'models/tempaware'
#check_data(obj + '.stl', obj + '_raw.csv', from_inches=False)
#out = voxelised_csv(obj + '.stl', obj + '_raw.csv', voxels=obj+'_vox_centers.csv', from_inches=False)
#out.to_csv(obj + '_voxels.csv')
#visualise_voxels(obj + '_voxels.csv')
voxels = pd.read_csv(obj + '_voxels.csv')
vox_section_area(voxels, obj + '.stl')
#voxels = pd.read_csv(obj + '_voxels.csv')
#area_ratio = vox_section_area(voxels, obj + '.stl')
#voxels["AreaRatio"] = area_ratio
#voxels.to_csv(obj + '_voxels.csv', index=False)
#contours = gc.decode_gcode(obj + '.gcode')
#outfile = open(obj + 'contours.json','w')
#json.dump([contour2dict(c) for c in contours], outfile)
#outfile.close()
contours = gc.decode_gcode(obj + '.gcode')
outfile = open(obj + 'contours.json','w')
json.dump([contour2dict(c) for c in contours], outfile)
outfile.close()
Markdown is supported
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