Rapydscript <<
Previous Next >> Samples
Atoms
Source Codes:
<div class="glowscript" id="glowscript">
<script type="text/javascript" src="https://s3.amazonaws.com/glowscript/lib/jquery/2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://s3.amazonaws.com/glowscript/lib/jquery/2.1/jquery-ui.custom.min.js"></script>
<script type="text/javascript" src="https://s3.amazonaws.com/glowscript/package/glow.3.0.min.js"></script>
<script type="text/javascript">// <![CDATA[
//--><![CDATA[//><!--
// START JAVASCRIPT
;(function() {;
async function __main__() {
"use strict";
var version = ["3.0", "glowscript"];
Array.prototype.toString = function() { return __parsearray(this) };
var scene = canvas();
var vector = vec;
var axes = [vec(1,0,0), vec(0,1,0), vec(0,0,1)]
var k = 1
var m = 1
var spacing = 1.0
var atom_radius = 0.3["*"](spacing)
var L0 = spacing["-"](1.8["*"](atom_radius))
var V0 = pi["*"](pow(0.5["*"](atom_radius),2))["*"](L0) // initial volume of spring
var N = 3
var crystal = makeCrystal(N, atom_radius, spacing, 0.1["*"](spacing)["*"](sqrt(k["/"](m))))
scene.center = 0.5["*"](N["-"](1))["*"](vec(1,1,1))
scene.autoscale = false
var dt = 0.04["*"](2["*"](pi)["*"](sqrt(m["/"](k))))
// Display text below the 3D graphics:
scene.title = "A model of a solid represented as atoms connected by interatomic bonds"
function makeCrystal( N, atom_radius, spacing, momentumRange ) {
var crystal = { atoms:[], springs:[] }
var atom
var x,y,z
function atomAt(np) {
if (np.x[">="](0) && np.y[">="](0) && np.z[">="](0) && np.x["<"](N) && np.y["<"](N) && np.z["<"](N))
return crystal.atoms[np.x["+"](np.y["*"](N))["+"](np.z["*"](N)["*"](N))]
// Otherwise create an invisible wall and return it
var w = box()
w.visible = false // comment out to see the true model
w.size = atom_radius["*"](vec(1,1,1))
w.pos = np["*"](spacing)
w.momentum = vec(0,0,0)
return w
}
// Create N^3 atoms in a grid
for(z=0; z["<"](N); z++)
for(y=0; y["<"](N); y++)
for(x=0; x["<"](N); x++) {
atom = sphere()
atom.pos = vec(x,y,z)["*"](spacing)
atom.size = 2["*"](atom_radius)["*"](vec(1,1,1))
atom.color = vec(0,0.58,0.69)
atom.momentum = momentumRange["*"](vec.random())
crystal.atoms.push( atom )
}
// Create a grid of springs linking each atom to the adjacent atoms
// in each dimension, or to invisible walls where no atom is adjacent
for(var d=0; d["<"](3); d++)
for(z=1["-u"](); z["<"](N); z++)
for(y=1["-u"](); y["<"](N); y++)
for(x=1["-u"](); x["<"](N); x++) {
atom = atomAt(vec(x,y,z))
var neighbor = atomAt(vec(x,y,z)["+"](axes[d]))
if (atom.visible || neighbor.visible) {
var spring = helix()
spring.visible = atom.visible && neighbor.visible
spring.thickness = 0.05
spring.size = vec(spacing,atom_radius,atom_radius)
spring.atoms = [ atom, neighbor ]
spring.color = vec(1,0.5,0)
crystal.springs.push(spring)
}
}
return crystal
}
while (true) {
await rate(60)
for(var a=0; a["<"](crystal.atoms.length); a++) {
var atom = crystal.atoms[a]
atom.pos = atom.pos["+"](atom.momentum["/"](m)["*"](dt))
}
for(var s=0; s["<"](crystal.springs.length); s++) {
var spring = crystal.springs[s]
spring.axis = spring.atoms[1].pos["-"](spring.atoms[0].pos)
var L = mag(spring.axis)
spring.axis = spring.axis.norm()
spring.pos = spring.atoms[0].pos["+"](0.5["*"](atom_radius)["*"](spring.axis))
var Ls = L["-"](1["*"](atom_radius))
spring.size.x = Ls
var Fdt = spring.axis["*"](k["*"](dt)["*"](1["-"](spacing["/"](L))))
spring.atoms[0].momentum = spring.atoms[0].momentum["+"](Fdt)
spring.atoms[1].momentum = spring.atoms[1].momentum["-"](Fdt)
}
}
}
;$(function(){ window.__context = { glowscript_container: $("#glowscript").removeAttr("id") }; __main__() })})()
// END JAVASCRIPT
//--><!
// ]]></script>
</div>
Rapydscript <<
Previous Next >> Samples