Skip to content
Open
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
58 changes: 58 additions & 0 deletions util/cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import re

def load_array(lines, tuple_length=3, skip=0):
regex = r"^([\-eE0-9\.]+)"
for i in range(tuple_length+skip-1):
regex += r"[ \t]+([\-eE0-9\.]+)"
regex += r".*"
compiled_regex = re.compile(regex)
result = []
for line in lines:
m = compiled_regex.match(line)
if m is not None:
r = [float(m.group(i+skip+1)) for i in range(tuple_length)]
result.append(tuple(r))
return result

def load_cells(cells_filename, coords_filename = None):
if coords_filename is None:
coords_filename = 'cells-coords.cfg'
cells_lines = []
coords_lines = []
with open(cells_filename) as f:
cells_lines += f.readlines()
with open(coords_filename) as f:
coords_lines += f.readlines()
cells_mags = load_array(cells_lines)
cells_coords = load_array(coords_lines)
assert(len(cells_mags) == len(cells_coords))
result = []
for i in range(len(cells_mags)):
(x, y, z) = cells_coords[i]
(u, v, w) = cells_mags[i]
result.append((x,y,z,u,v,w))
return result

def load_atoms(atoms_filename, coords_filename = None):
if coords_filename is None:
coords_filename = 'atoms-coords.cfg'
atoms_lines = []
coords_lines = []
with open(atoms_filename) as f:
atoms_lines += f.readlines()
with open(coords_filename) as f:
coords_lines += f.readlines()
atoms_mags = load_array(atoms_lines)
atoms_coords = load_array(coords_lines, skip=2)
assert(len(atoms_mags) == len(atoms_coords))
result = []
for i in range(len(atoms_mags)):
(x, y, z) = atoms_coords[i]
(u, v, w) = atoms_mags[i]
result.append((x,y,z,u,v,w))
return result





125 changes: 125 additions & 0 deletions util/cfg2x3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import cfg
import sys
import math

x3d_atom_template = \
r"""<Transform translation='{position[0]:f} {position[1]:f} {position[2]:f}'>
<Transform rotation='0 0 1 {rotation[phi]:f}'> <!--phi-->
<Transform rotation='0 1 0 {rotation[theta]:f}'> <!--theta-->
<Transform scale='{scale:f} {scale:f} {scale:f}'>
<Transform rotation='1 0 0 1.570796325'>
<Transform scale='0.2 0.2 0.2'>
<Shape>
<Appearance>
<Material diffuseColor='0.7 0.7 0.7' ></Material>
</Appearance>
<Sphere></Sphere>
</Shape>
</Transform>
<Transform translation='0 -0.05 0'> <Transform scale='0.075 0.45 0.075'>
<Shape>
<Appearance>
<Material diffuseColor='{color[0]} {color[1]} {color[2]}' ></Material>
</Appearance>
<Cylinder></Cylinder>
</Shape>
</Transform> </Transform>
<Transform translation='0 0.4 0'> <Transform scale='0.15 0.1 0.15'>
<Shape>
<Appearance>
<Material diffuseColor='{color[0]} {color[1]} {color[2]}' ></Material>
</Appearance>
<Cone></Cone>
</Shape>
</Transform> </Transform>
</Transform>
</Transform>
</Transform>
</Transform>
</Transform>
"""

x3d_start_template = \
r"""<X3D version='3.2' profile='Immersive' xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation='http://www.web3d.org/specifications/x3d-3.2.xsd'>
<Scene>
<Viewpoint bind='true' isActive='true' position='{position[0]:f} {position[1]:f} {position[2]:f}' centerOfRotation='{center[0]:f} {center[1]:f} {center[2]:f}'></Viewpoint>
"""

x3d_end_template = \
r"""</Scene>
</X3D>
"""

def norm(v):
return (sum([x*x for x in v]))**0.5

def diff(arr):
return [arr[i+1] - arr[i] for i in range(len(arr)-1)]

def main():
# get parsers
print("EXPERIMENTAL! USE WITH CAUTION! Email yifangu" + "@andrew.cmu.edu for questions.")
parsers = {"atoms": cfg.load_atoms, "cells": cfg.load_cells}
if len(sys.argv) != 4:
print("Usage {:} [{:}] CELLS_OR_ATOMS_DATA_CFG CELLS_OR_ATOMS_COORDS_CFG".format(sys.argv[0], "|".join(parsers)))
return -1
mode = sys.argv[1]
if mode not in parsers:
print("{:} is not a valid mode")
print("valid modes are {:}".format("|".join(parsers)))
return -1
filename = sys.argv[2]
coords_filename = sys.argv[3]
out_filename = filename + ".x3d"
print("Processing {:}".format(filename))

# load data
try:
data = []
for d in parsers[mode](filename, coords_filename):
if sum([abs(x) for x in d]) != 0:
data.append(d)
except Exception as e:
print("Error while parsing file. Check selected mode and source files.")
return -1

# preprocess data
data_t = list(zip(*data))
xyz_sorted = [sorted(arr) for arr in data_t[:3]]
mins = [K[0] for K in xyz_sorted]
maxs = [K[-1] for K in xyz_sorted]
centers = [(a+b)/2.0 for (a,b) in zip(maxs, mins)]
dims = [a-b for (a,b) in zip(maxs, mins)]
grid_sizes = [max(diff(arr)) for arr in xyz_sorted]

x3d_start_data = {
"center": centers,
"position": [centers[0], centers[1], centers[2] + max(dims) * 2.0]
}

# output plt file
with open(out_filename, "w") as f:
f.write(x3d_start_template.format(**x3d_start_data))
for d in data:
(x,y,z,u,v,w) = d
n = norm(d[3:])
if n != 0:
color = [(((k/n+1.0)/2.0)) for k in d[3:]]
x3d_data = {
"position": (x, y, z),
"rotation": {
"theta":math.atan2((u**2.0+v**2.0)**0.5,w),
"phi":math.atan2(v,u)
},
"scale":0.8*min(grid_sizes),
"color": color
}
f.write(x3d_atom_template.format(**x3d_data))
else:
print("Invalid Magnetization! Skipping")
f.write(x3d_end_template)
print("Done, output {:}".format(out_filename))
return 0

if __name__ == '__main__':
main()
43 changes: 43 additions & 0 deletions util/x3dviewer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Dual licensed under the MIT and GPL licenses.

==[MIT]====================================================================
Copyright (c) 2009 X3DOM

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


==[GPL]====================================================================

X3DOM - Declarative 3D for HTML

Copyright (C) 2009 X3DOM

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Loading