Skip to content
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

binary data communication #223

Draft
wants to merge 2 commits into
base: main_old
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions zndraw/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def atoms_to_json(atoms: ase.Atoms) -> dict:
except AttributeError:
atoms_dict["connectivity"] = []

atoms_dict["positions"] = np.array(atoms_dict["positions"]).tobytes()

return atoms_dict


Expand All @@ -63,6 +65,7 @@ def atoms_from_json(data: dict) -> ase.Atoms:
numbers=data["numbers"],
cell=data["cell"],
pbc=data["pbc"],
# positions=np.frombuffer(data["positions"], dtype=float).reshape(-1, 3),
positions=data["positions"],
)

Expand Down
11 changes: 11 additions & 0 deletions zndraw/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ function main() {
socket.on("message:log", (msg) => {
console.log(msg);
});

// keypress J
document.addEventListener("keydown", (event) => {
if (event.key === "j") {
socket.emit("numpy", (data) => {
// create FLOAT32ARRAY from data
const array = new Float64Array(data);
console.log(array);
});
}
});
}

main();
21 changes: 19 additions & 2 deletions zndraw/static/pycom/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ class Atom {
}
}

function float64ArrayTo2DArray(arr, shape) {
var result = [];
for (var i = 0; i < shape[0]; i++) {
var row = [];
for (var j = 0; j < shape[1]; j++) {
row.push(arr[i * shape[1] + j]);
}
result.push(row);
}
return result;
}

class Atoms {
constructor({
positions,
Expand All @@ -21,15 +33,20 @@ class Atoms {
calc,
pbc,
}) {
this.positions = positions;
this.cell = cell;
(this.positions = float64ArrayTo2DArray(
new Float64Array(positions),
[300, 3],
)),
(this.cell = cell);
this.numbers = numbers;
this.colors = colors;
this.radii = radii;
this.connectivity = connectivity;
this.calc = calc;
this.pbc = pbc;

console.log(this.positions);

this.length = this.positions.length;
}

Expand Down