Finish web implementation to enable hosting on janbergen.de

This commit is contained in:
2024-06-04 20:58:58 +02:00
parent 521113cd7c
commit 86416e3566
7 changed files with 300 additions and 13 deletions

View File

@@ -6,6 +6,11 @@ use std::collections::HashMap;
use std::io;
use std::rc::Rc;
use std::{
fs::File,
io::BufReader,
};
use crate::gq_storage; // This should be moved to main.rs after new archtitecture (see main.rs TODO)
pub type Complex = num::complex::Complex<f64>;
@@ -32,8 +37,21 @@ pub struct Cache {
impl Cache {
pub fn from_file(filepath: &str) -> io::Result<Cache> {
let file = File::open(filepath)?;
let mut file = BufReader::new(file);
let me = Cache{
gauss_quad_lut: gq_storage::deserialize(filepath, |_| true)?.into_iter()
gauss_quad_lut: gq_storage::deserialize(&mut file, |_| true)?.into_iter()
.map(|(roots, weights)| (u32::try_from(roots.len()).unwrap(), (Rc::from(roots), Rc::from(weights)))).collect(),
delta_lut: HashMap::with_hasher(Default::default()),
omnes_lut: HashMap::with_hasher(Default::default())
};
Ok(me)
}
pub fn from_slice(data: &[u8]) -> io::Result<Cache> {
let mut cursor = std::io::Cursor::new(data);
let me = Cache{
gauss_quad_lut: gq_storage::deserialize(&mut cursor, |_| true)?.into_iter()
.map(|(roots, weights)| (u32::try_from(roots.len()).unwrap(), (Rc::from(roots), Rc::from(weights)))).collect(),
delta_lut: HashMap::with_hasher(Default::default()),
omnes_lut: HashMap::with_hasher(Default::default())

View File

@@ -28,15 +28,12 @@ pub fn serialize(filepath: &str, params: Vec<(Vec<f64>, Vec<f64>)>) -> io::Resul
Ok(())
}
pub fn deserialize(filepath: &str, filter: impl Fn(u64) -> bool) -> io::Result<Vec<(Vec<f64>, Vec<f64>)>> {
let file = File::open(filepath)?;
let mut file = BufReader::new(file);
pub fn deserialize<R:std::io::Read + std::io::Seek>(reader: &mut R, filter: impl Fn(u64) -> bool) -> io::Result<Vec<(Vec<f64>, Vec<f64>)>> {
let mut buf = [0u8; 8];
let mut values = Vec::new();
'outer: loop {
let res = file.read(&mut buf)?;
let res = reader.read(&mut buf)?;
if res < 8 {
break;
}
@@ -44,7 +41,7 @@ pub fn deserialize(filepath: &str, filter: impl Fn(u64) -> bool) -> io::Result<V
// skips stored floats for n that should not be stored
if !filter(n) {
file.seek(SeekFrom::Current(i64::try_from(2*n*8).unwrap())).unwrap();
reader.seek(SeekFrom::Current(i64::try_from(2*n*8).unwrap())).unwrap();
continue;
}
@@ -52,7 +49,7 @@ pub fn deserialize(filepath: &str, filter: impl Fn(u64) -> bool) -> io::Result<V
let mut roots = Vec::new();
for _ in 0..n {
let res = file.read(&mut buf)?;
let res = reader.read(&mut buf)?;
if res < 8 {
break 'outer;
}
@@ -61,7 +58,7 @@ pub fn deserialize(filepath: &str, filter: impl Fn(u64) -> bool) -> io::Result<V
let mut weights = Vec::new();
for _ in 0..n {
let res = file.read(&mut buf)?;
let res = reader.read(&mut buf)?;
if res < 8 {
break 'outer;
}
@@ -72,13 +69,16 @@ pub fn deserialize(filepath: &str, filter: impl Fn(u64) -> bool) -> io::Result<V
Ok(values)
}
pub fn fill_with_gauss_quad(n_values: Vec<u64>) {
pub fn fill_with_gauss_quad(n_values: Vec<u64>) -> std::io::Result<()> {
let mut params = Vec::new();
for i in n_values {
let gq = GaussLegendre::init(usize::try_from(i).unwrap());
params.push((gq.nodes, gq.weights));
}
serialize("./gauss_quad_lut.morello", params).unwrap();
let res = deserialize("./gauss_quad_lut.morello", |_| true);
let file = File::open("./gauss_quad_lut.morello")?;
let mut file = BufReader::new(file);
let res = deserialize(&mut file, |_| true);
println!("{:?}", res);
Ok(())
}

View File

@@ -119,7 +119,7 @@ impl eframe::App for App {
ui.label("You can calculate the following functions:");
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
for i in 0..self.plots_available.len() {
let button = ui.button( format!("{}", self.plots_available[i].clone()));
if button.clicked() {
@@ -257,6 +257,7 @@ impl eframe::App for App {
}
}
#[cfg(not(target_arch = "wasm32"))]
fn main() {
let mut app = App::default();
app.calc_cache = calc::Cache::from_file("./gauss_quad_lut.morello").unwrap();
@@ -266,9 +267,35 @@ fn main() {
// std::hint::black_box(&y_values_phi0);
// println!("{:?}", t0.elapsed());
eframe::run_native("Morellus", eframe::NativeOptions::default(), Box::new(|_cc| Box::new(app))).unwrap();
}
#[cfg(target_arch = "wasm32")]
fn main() {
let mut app = App::default();
wasm_bindgen_futures::spawn_local(async {
let res = gloo_net::http::RequestBuilder::new("/lut/gauss_quad_lut.morello")
.method(gloo_net::http::Method::POST)
.send()
.await
.unwrap();
app.calc_cache = calc::Cache::from_slice(&res.binary().await.unwrap()).unwrap();
let web_options = eframe::WebOptions::default();
eframe::WebRunner::new()
.start(
"morellus_canvas_id",
web_options,
Box::new(|_cc| Box::new(app)),
)
.await
.expect("failed to start eframe");
});
}
fn log_map(x:f64) -> f64 {
x.floor() + (1.0 + (x - x.floor()) * 10.0).log10()
}