-
Notifications
You must be signed in to change notification settings - Fork 2
Version: 0.2.0 - Multiple Models #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
acosta-leandro
wants to merge
4
commits into
martinjrobins:main
Choose a base branch
from
acosta-leandro:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec36018
refactor: update logistic model adding two equations and test cases
acosta-leandro 7339aeb
refactor: multiple compilations, options handling in index, options, …
acosta-leandro 141a1b8
test: Update all tests for compatibility with older versions and new …
acosta-leandro 0ab15b5
docs: updated v0.2.0
acosta-leandro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@martinjrobins/diffeq-js", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/martinjrobins/diffeq-js", | ||
"description": "A library for solving differential equations in JavaScript", | ||
"author": "Martin Robinson <[email protected]> (https://github.com/martinjrobins)", | ||
|
@@ -13,41 +13,37 @@ | |
"url": "https://github.com/martinjrobins/diffeq-js" | ||
}, | ||
"source": "src/index.ts", | ||
"main": "dist/main.js", | ||
"types": "dist/types.d.ts", | ||
"main": "./dist/diffeq-js.umd.js", | ||
"module": "./dist/diffeq-js.es.js", | ||
"types": "./dist/types.d.ts", | ||
"license": "MIT", | ||
"browser": "dist/browser.js", | ||
"browser": "./dist/diffeq-js.es.js", | ||
"type": "module", | ||
"targets": { | ||
"browser": { | ||
"context": "browser", | ||
"optimize": true, | ||
"includeNodeModules": true, | ||
"sourceMap": true, | ||
"outputFormat": "esmodule" | ||
"exports": { | ||
".": { | ||
"import": "./dist/diffeq-js.es.js", | ||
"require": "./dist/diffeq-js.umd.js", | ||
"types": "./dist/types.d.ts" | ||
} | ||
}, | ||
"@parcel/resolver-default": { | ||
"packageExports": true | ||
}, | ||
"scripts": { | ||
"watch": "parcel watch", | ||
"build": "parcel build", | ||
"dev": "vite", | ||
"build": "vite build", | ||
"test": "mocha -r ts-node/register" | ||
}, | ||
"devDependencies": { | ||
"@parcel/packager-ts": "2.12.0", | ||
"@parcel/transformer-typescript-types": "2.12.0", | ||
"@types/chai": "^4.3.5", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.5.9", | ||
"@vitejs/plugin-vue": "^5.0.0", | ||
"chai": "^4.3.8", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"parcel": "^2.12.0", | ||
"ts-node": "^10.9.1", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^5.2.2" | ||
"typescript": "^5.2.2", | ||
"vite": "^5.0.0", | ||
"vue-tsc": "^1.8.0" | ||
}, | ||
"dependencies": { | ||
"@bjorn3/browser_wasi_shim": "^0.2.14" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,12 @@ | ||
import { WASI, File, OpenFile, PreopenDirectory } from "@bjorn3/browser_wasi_shim"; | ||
import { extract_vector_functions } from "./vector"; | ||
import { extract_options_functions } from "./options"; | ||
import { extract_solver_functions } from "./solver"; | ||
import base from "/node_modules/base-x/src/index"; | ||
import {File, OpenFile, WASI} from "@bjorn3/browser_wasi_shim"; | ||
import {extract_vector_functions} from "./vector"; | ||
import {extract_options_functions} from "./options"; | ||
import {extract_solver_functions} from "./solver"; | ||
|
||
export { default as Vector } from "./vector"; | ||
export { default as Options, OptionsJacobian, OptionsLinearSolver, OptionsPreconditioner } from "./options"; | ||
export { default as Solver } from "./solver"; | ||
|
||
|
||
let args: string[] = []; | ||
let env: string[] = []; | ||
let fds = [ | ||
new OpenFile(new File([])), // stdin | ||
new OpenFile(new File([])), // stdout | ||
new OpenFile(new File([])), // stderr | ||
]; | ||
let wasi = new WASI(args, env, fds); | ||
let inst: WebAssembly.WebAssemblyInstantiatedSource | undefined = undefined; | ||
|
||
class SimpleOpenFile { | ||
file: File; | ||
file_pos: number; | ||
|
@@ -32,29 +20,83 @@ class SimpleOpenFile { | |
const end = data.byteLength; | ||
const slice = data.slice(start, end); | ||
this.file_pos = end; | ||
const string = new TextDecoder().decode(slice); | ||
return string; | ||
return new TextDecoder().decode(slice); | ||
} | ||
} | ||
|
||
// @ts-expect-error | ||
let stderr = new SimpleOpenFile(wasi.fds[2].file); | ||
const defaultBaseUrl = "https://compbio.fhs.um.edu.mo/diffeq"; | ||
|
||
// @ts-expect-error | ||
let stdout = new SimpleOpenFile(wasi.fds[1].file); | ||
function createWasi() { | ||
let args: string[] = []; | ||
let env: string[] = []; | ||
let fds = [ | ||
new OpenFile(new File([])), // stdin | ||
new OpenFile(new File([])), // stdout | ||
new OpenFile(new File([])), // stderr | ||
]; | ||
return new WASI(args, env, fds); | ||
} | ||
|
||
function getWasmMemory() { | ||
if (inst === undefined) { | ||
throw new Error("WASM module not loaded"); | ||
class ModelRegistry { | ||
private static models: Map<string, any> = new Map(); | ||
private static defaultModel: any = null; | ||
|
||
static register(id: string | undefined, model: any) { | ||
if (id) { | ||
this.models.set(id, model); | ||
} else { | ||
this.defaultModel = model; | ||
} | ||
} | ||
|
||
static get(id?: string) { | ||
if (id) { | ||
return this.models.get(id); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if an |
||
return this.defaultModel; | ||
} | ||
return inst.instance.exports.memory as WebAssembly.Memory; | ||
} | ||
|
||
const defaultBaseUrl = "https://diffeq-backend.fly.dev"; | ||
function compileResponse(response: Promise<Response>) { | ||
const wasi = createWasi(); | ||
const importObject = { | ||
"wasi_snapshot_preview1": wasi.wasiImport, | ||
}; | ||
return WebAssembly.instantiateStreaming(response, importObject).then( | ||
(obj) => { | ||
wasi.initialize({ | ||
exports: { | ||
memory: obj.instance.exports.memory as WebAssembly.Memory, | ||
}, | ||
}); | ||
const stderr = new SimpleOpenFile((wasi.fds[2] as OpenFile).file); | ||
const stdout = new SimpleOpenFile((wasi.fds[1] as OpenFile).file); | ||
const vectorFunctions = extract_vector_functions(obj); | ||
const optionsFunctions = extract_options_functions(obj); | ||
const solverFunctions = extract_solver_functions(obj); | ||
|
||
// Set global functions | ||
global.vectorFunctions = vectorFunctions; | ||
global.optionsFunctions = optionsFunctions; | ||
global.solverFunctions = solverFunctions; | ||
global.stderr = stderr; | ||
global.stdout = stdout; | ||
|
||
return { | ||
instance: obj, | ||
stderr, | ||
stdout, | ||
vectorFunctions, | ||
optionsFunctions, | ||
solverFunctions | ||
}; | ||
}, | ||
); | ||
} | ||
|
||
function compileModel(text: string, baseUrl: string = defaultBaseUrl) { | ||
export async function compileModel(code: string, id?: string) { | ||
const data = { | ||
text, | ||
text: code, | ||
name: "unknown", | ||
}; | ||
const options: RequestInit = { | ||
|
@@ -65,35 +107,23 @@ function compileModel(text: string, baseUrl: string = defaultBaseUrl) { | |
}, | ||
body: JSON.stringify(data), | ||
}; | ||
const response = fetch(`${baseUrl}/compile`, options).then((response) => { | ||
const response = fetch(`${defaultBaseUrl}/compile`, options).then((response) => { | ||
if (!response.ok) { | ||
return response.text().then((text) => { | ||
throw text; | ||
}); | ||
} | ||
return response; | ||
}); | ||
return compileResponse(response); | ||
const model = await compileResponse(response); | ||
ModelRegistry.register(id, model); | ||
return model; | ||
} | ||
|
||
function compileResponse(response: Promise<Response>) { | ||
const importObject = { | ||
"wasi_snapshot_preview1": wasi.wasiImport, | ||
}; | ||
return WebAssembly.instantiateStreaming(response, importObject).then( | ||
(obj) => { | ||
extract_vector_functions(obj); | ||
extract_options_functions(obj); | ||
extract_solver_functions(obj); | ||
inst = obj | ||
// @ts-expect-error | ||
wasi.initialize(inst.instance); | ||
}, | ||
); | ||
export function getModel(id?: string) { | ||
return ModelRegistry.get(id); | ||
} | ||
|
||
export { compileModel, compileResponse, getWasmMemory, stderr, stdout } | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if you meant to do this, but I'm happy if you want to run the default server :)