@molcrafts/molrs 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +40 -143
- package/molrs.d.ts +2239 -345
- package/molrs.js +7 -1401
- package/molrs_bg.js +4017 -0
- package/molrs_bg.wasm +0 -0
- package/package.json +4 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, MolCrafts
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,175 +1,72 @@
|
|
|
1
|
-
# molrs-wasm
|
|
1
|
+
# molrs-wasm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
WASM bindings for `molrs`, aimed at browser and JavaScript/TypeScript consumers.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Local build
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
await init();
|
|
11
|
-
|
|
12
|
-
// Create a new frame
|
|
13
|
-
const frame = new Frame();
|
|
14
|
-
const atoms = frame.createBlock("atoms");
|
|
15
|
-
|
|
16
|
-
// Set column data
|
|
17
|
-
atoms.setColumnF32("x", new Float32Array([1.0, 2.0, 3.0]));
|
|
18
|
-
const data = atoms.getColumnF32("x");
|
|
19
|
-
|
|
20
|
-
// Read from file
|
|
21
|
-
const reader = new XyzReader(fileContent);
|
|
22
|
-
const loadedFrame = reader.read(0);
|
|
7
|
+
```bash
|
|
8
|
+
wasm-pack build --target web
|
|
9
|
+
bash scripts/postprocess-pkg.sh
|
|
23
10
|
```
|
|
24
11
|
|
|
25
|
-
|
|
12
|
+
This produces `pkg/molwasm.js` plus `pkg/typed-views.js` for local use.
|
|
26
13
|
|
|
27
|
-
|
|
14
|
+
## npm package
|
|
28
15
|
|
|
29
|
-
The
|
|
16
|
+
The published package is `@molcrafts/molrs`.
|
|
30
17
|
|
|
31
18
|
```javascript
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// Create a block
|
|
35
|
-
const atoms = frame.createBlock("atoms");
|
|
36
|
-
|
|
37
|
-
// Get an existing block
|
|
38
|
-
const atomsAgain = frame.getBlock("atoms");
|
|
39
|
-
|
|
40
|
-
// Remove a block
|
|
41
|
-
frame.removeBlock("atoms");
|
|
42
|
-
|
|
43
|
-
// Clear all blocks
|
|
44
|
-
frame.clear();
|
|
45
|
-
|
|
46
|
-
// Drop the frame (invalidates all blocks)
|
|
47
|
-
frame.drop();
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Block
|
|
51
|
-
|
|
52
|
-
A block contains columnar data with consistent row counts.
|
|
53
|
-
|
|
54
|
-
```javascript
|
|
55
|
-
const block = frame.createBlock("atoms");
|
|
56
|
-
|
|
57
|
-
// Set column data
|
|
58
|
-
const positions = new Float32Array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
|
|
59
|
-
const shape = new Uint32Array([2, 3]); // 2 atoms, 3 coordinates each
|
|
60
|
-
block.setColumnF32("positions", positions, shape);
|
|
19
|
+
import init, { Frame, WasmArray, XyzReader, writeFrame } from "@molcrafts/molrs";
|
|
20
|
+
import { typedArrayView } from "@molcrafts/molrs/typed-views";
|
|
61
21
|
|
|
62
|
-
|
|
63
|
-
const ids = new BigInt64Array([1n, 2n, 3n]);
|
|
64
|
-
block.setColumnI64("id", ids);
|
|
65
|
-
|
|
66
|
-
// Get column data (returns copy)
|
|
67
|
-
const data = block.getColumnF32("positions"); // Float32Array
|
|
68
|
-
const idData = block.getColumnI64("id"); // BigInt64Array
|
|
69
|
-
|
|
70
|
-
// Get column with shape info
|
|
71
|
-
const { data, shape } = block.getColumnF32WithShape("positions");
|
|
72
|
-
|
|
73
|
-
// Query block info
|
|
74
|
-
const keys = block.keys(); // ["positions"]
|
|
75
|
-
const nrows = block.nrows(); // 2
|
|
76
|
-
```
|
|
22
|
+
await init();
|
|
77
23
|
|
|
78
|
-
|
|
24
|
+
const frame = new Frame();
|
|
25
|
+
const atoms = frame.createBlock("atoms");
|
|
26
|
+
atoms.set("x", new Float32Array([0.0, 1.0, 2.0]));
|
|
79
27
|
|
|
80
|
-
|
|
28
|
+
const xView = atoms.view("x");
|
|
29
|
+
const xTyped = typedArrayView(xView); // Float32Array
|
|
81
30
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const xyzReader = new XyzReader(fileContent);
|
|
85
|
-
const frameCount = xyzReader.len();
|
|
86
|
-
const frame = xyzReader.read(0); // Read frame 0
|
|
87
|
-
|
|
88
|
-
// PDB files
|
|
89
|
-
const pdbReader = new PdbReader(pdbContent);
|
|
90
|
-
const frame2 = pdbReader.read(0);
|
|
91
|
-
|
|
92
|
-
// LAMMPS data files
|
|
93
|
-
const lammpsReader = new LammpsReader(lammpsContent);
|
|
94
|
-
const frame3 = lammpsReader.read(0);
|
|
31
|
+
const xyz = writeFrame(frame, "xyz");
|
|
32
|
+
console.log(xyz);
|
|
95
33
|
```
|
|
96
34
|
|
|
97
|
-
|
|
35
|
+
## Local file usage
|
|
98
36
|
|
|
99
37
|
```javascript
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
## TypeScript Example
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
import init, { Frame, Block, XyzReader } from './pkg/molrs.js';
|
|
38
|
+
import init, { Frame, WasmArray, XyzReader, writeFrame } from "./pkg/molwasm.js";
|
|
39
|
+
import { typedArrayView, columnViewMeta } from "./pkg/typed-views.js";
|
|
108
40
|
|
|
109
41
|
await init();
|
|
110
42
|
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
// Set positions
|
|
116
|
-
const positions = new Float32Array([
|
|
117
|
-
0.0, 0.0, 0.0, // atom 1
|
|
118
|
-
1.0, 0.0, 0.0, // atom 2
|
|
119
|
-
0.0, 1.0, 0.0, // atom 3
|
|
120
|
-
]);
|
|
121
|
-
atoms.setColumnF32("x", positions);
|
|
43
|
+
const frame = new Frame();
|
|
44
|
+
const atoms = frame.createBlock("atoms");
|
|
45
|
+
atoms.set("x", new Float32Array([0.0, 1.0, 2.0]));
|
|
122
46
|
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
|
|
47
|
+
const xCopy = atoms.view("x").toCopy();
|
|
48
|
+
const xView = atoms.view("x");
|
|
49
|
+
const xTypedArray = typedArrayView(xView); // Float32Array | Uint32Array | Uint8Array
|
|
50
|
+
const xMeta = columnViewMeta(xView);
|
|
127
51
|
|
|
128
|
-
|
|
52
|
+
const arr = WasmArray.from(new Float32Array([0, 0, 0, 1, 0, 0]), new Uint32Array([2, 3]));
|
|
129
53
|
const reader = new XyzReader(fileContent);
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
const loadedAtoms = loadedFrame.getBlock("atoms");
|
|
133
|
-
if (loadedAtoms) {
|
|
134
|
-
console.log(`Loaded ${loadedAtoms.nrows()} atoms`);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
54
|
+
const loaded = reader.read(0);
|
|
55
|
+
const xyz = writeFrame(frame, "xyz");
|
|
137
56
|
```
|
|
138
57
|
|
|
139
|
-
##
|
|
140
|
-
|
|
141
|
-
Handles follow strict invalidation rules:
|
|
142
|
-
|
|
143
|
-
- **Block handles** become invalid when:
|
|
144
|
-
- The parent frame is dropped
|
|
145
|
-
- The block is removed from the frame
|
|
146
|
-
- The frame is cleared
|
|
147
|
-
|
|
148
|
-
- **Frame handles** become invalid when:
|
|
149
|
-
- `frame.drop()` is called
|
|
150
|
-
|
|
151
|
-
Attempting to use an invalid handle will throw an error.
|
|
58
|
+
## API surface
|
|
152
59
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
-
|
|
157
|
-
-
|
|
158
|
-
-
|
|
159
|
-
|
|
160
|
-
The API is designed to be:
|
|
161
|
-
- **Object-oriented**: Natural class hierarchy (Frame → Block)
|
|
162
|
-
- **Type-safe**: Clear ownership and lifetime semantics
|
|
163
|
-
- **Explicit**: No hidden copies or implicit conversions
|
|
164
|
-
- **Simple**: Store is hidden - Frame is the main entry point
|
|
60
|
+
- `Frame`, `Block`, `ColumnView`, `WasmArray`
|
|
61
|
+
- `typed-views.js` helpers for `ptr + len + shape + dtype`
|
|
62
|
+
- `Box`
|
|
63
|
+
- `XyzReader`, `PdbReader`, `LammpsReader`
|
|
64
|
+
- `SimulationReader`
|
|
65
|
+
- `writeFrame(frame, "xyz" | "pdb")`
|
|
165
66
|
|
|
166
67
|
## Testing
|
|
167
68
|
|
|
168
69
|
```bash
|
|
169
70
|
cargo test -p molrs-wasm --lib
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
For WASM-specific tests:
|
|
173
|
-
```bash
|
|
174
|
-
wasm-pack test --node wasm
|
|
71
|
+
wasm-pack test --node
|
|
175
72
|
```
|