Skip to content

Commit

Permalink
Finalized optimization and table generation code, optimizing for h_e=…
Browse files Browse the repository at this point in the history
…10mm by default and automating the generation (with bold figures) of various design tables
  • Loading branch information
tbvanderwoude committed Jun 29, 2024
1 parent 9280426 commit cc669ab
Showing 1 changed file with 78 additions and 35 deletions.
113 changes: 78 additions & 35 deletions optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"metadata": {},
"outputs": [],
"source": [
"def optimize_all_designs(detector_size=11e-3, theta_a_max = 15e-3):\n",
"def optimize_all_designs(detector_size=10e-3, theta_a_max = 15e-3):\n",
" instrs = [] \n",
" for type in ['foil', 'wsp', 'iso','foil2',]:\n",
" print(f\"==========Best {type} instrument compatible with pyroletic graphite monochromator==========\")\n",
Expand All @@ -155,6 +155,38 @@
"instrs = optimize_all_designs()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"detector_size = 30e-3\n",
"instrs_2 = optimize_all_designs(detector_size)\n",
"for instr in instrs_2:\n",
" instr[0].name+= ' B'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import pickle\n",
"# with open('optimized_instruments_1.pkl', 'wb') as file:\n",
"# pickle.dump(instrs, file)\n",
"# with open('optimized_instruments_2.pkl', 'wb') as file:\n",
"# pickle.dump(instrs_2, file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pretty print $\\LaTeX$ tables"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -214,19 +246,13 @@
"metadata": {},
"outputs": [],
"source": [
"print_optimized_design_tables(instrs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"detector_size = 30e-3\n",
"instrs_2 = optimize_all_designs(detector_size)\n",
"for instr in instrs_2:\n",
" instr[0].name+= ' B'"
"def seperate_FOIL2(instrs):\n",
" has_foil2 = lambda x: \"FOIL2\" in x[0].name\n",
" return list(filter(has_foil2, instrs)), list(filter(lambda x: not has_foil2(x), instrs))\n",
"instr_1_foil2, instr_1_normal = seperate_FOIL2(instrs)\n",
"print_optimized_design_tables(instr_1_normal)\n",
"print_optimized_design_tables(instr_1_foil2)\n",
"# print(seperate_FOIL2(instrs))"
]
},
{
Expand All @@ -235,20 +261,9 @@
"metadata": {},
"outputs": [],
"source": [
"print_optimized_design_tables(instrs_2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import pickle\n",
"# with open('optimized_instruments_1.pkl', 'wb') as file:\n",
"# pickle.dump(instrs, file)\n",
"# with open('optimized_instruments_2.pkl', 'wb') as file:\n",
"# pickle.dump(instrs_2, file)"
"instr_2_foil2, instr_2_normal = seperate_FOIL2(instrs_2)\n",
"print_optimized_design_tables(instr_2_normal)\n",
"print_optimized_design_tables(instr_2_foil2)"
]
},
{
Expand All @@ -259,8 +274,8 @@
"source": [
"def print_latex_tables(instrs):\n",
" for i in [0,1]:\n",
" print(f\"====TABLE {i}======\")\n",
" for instr in instrs[3:-6]:\n",
" print(instr)\n",
" delta_max_field = instr.delta_max_B_field()\n",
" delta_max_env = instr.delta_max_envelope()\n",
" delta_max_ten_samples = instr.delta_max_sampling()\n",
Expand All @@ -273,17 +288,45 @@
" (delta_min, min_name) = max(mins)\n",
" # print(min_max)\n",
" Q_max = instr.Q_max()\n",
" L_s_min, L_s_max = instr.L_s_range()\n",
" delta_min_var, delta_max_var = instr.delta_range_var_L_s()\n",
"\n",
" # print(f\"{a}\")\n",
" r = lambda x: round(x * 1e9,2)\n",
" num = lambda x: r\"\\num{\" + f\"{x:#.3g}\" + \"}\"\n",
" bold = lambda x: r\"\\textbf{\" + f\"{x:#.3g}\" + \"}\"\n",
"\n",
" r = lambda x: num(round(x * 1e9,1)) \n",
" r_bold = lambda x: bold(round(x * 1e9,1)) \n",
" r2 = lambda x: num(round(x * 1e6,2)) \n",
" r2_bold = lambda x: bold(round(x * 1e6,2))\n",
" def r_dynamic(x):\n",
" if round(x * 1e9,1) == round(delta_min * 1e9,1):\n",
" return r_bold(x)\n",
" else:\n",
" return r(x)\n",
" def r2_dynamic(x):\n",
" if round(x * 1e6,2) == round(delta_max * 1e6,2):\n",
" return r2_bold(x)\n",
" else:\n",
" return r2(x)\n",
" if i==0:\n",
" print(f\"{instr.name} & {r(delta_min_single_period)} & {r(delta_min_field)} & {r(delta_max_ten_samples)} & {r(delta_max_env)} & {r(delta_max_field)} \\\\\\\\\")\n",
" else:\n",
" print(f\"{instr.name} & {round(Q_max * 1e-10, 5)} & {r(delta_min)} & {r(delta_max)} \\\\\\\\\")\n",
" print(f\"{instr.name} & {r_dynamic(delta_min_single_period)} & {r_dynamic(delta_min_field)} & {r2_dynamic(delta_max_ten_samples)} & {r2_dynamic(delta_max_env)} & {r2_dynamic(delta_max_field)} \\\\\\\\\")\n",
" elif i==1:\n",
" print(f\"{instr.name} & {num(round(Q_max * 1e-10, 5))} & {num(round(instr.theta_a() * 1e3,2))} & {r(delta_min)} & {r2(delta_max)} & {r(delta_min_var)} & {r2(delta_max_var)} \\\\\\\\\")\n",
"if __name__ == '__main__':\n",
" import util\n",
" instrs = util.load_instruments('instruments.csv')\n",
" print_latex_tables(instrs)"
" instrs_sheet = util.load_instruments('instruments.csv')\n",
" for instr in instrs_sheet:\n",
" print(instr)\n",
" print_latex_tables(instrs_sheet)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit cc669ab

Please sign in to comment.