Skip to content
Snippets Groups Projects
Commit 265e3f10 authored by Michael Leuschel's avatar Michael Leuschel
Browse files

add vector as pair machine

parent e330c4a6
Branches
No related tags found
No related merge requests found
MACHINE MovingParticles MACHINE MovingParticles
// Encoding of an n-body problem in B using the REAL datatype // Encoding of an n-body problem in B using the REAL datatype
INCLUDES Vectors2 INCLUDES Vectors2 //VectorsAsPairs2 faster; but VisB file needs to be adapted
DEFINITIONS DEFINITIONS
"LibraryReals.def"; "SORT.def"; "LibraryReals.def"; "SORT.def";
MASS == FLOAT; MASS == FLOAT;
......
...@@ -19,8 +19,8 @@ PROPERTIES ...@@ -19,8 +19,8 @@ PROPERTIES
/*@label "mass_values" */ mass = [5.97e24, 1.989e30, 1.989e30] /*@label "mass_values" */ mass = [5.97e24, 1.989e30, 1.989e30]
/*@desc "The mass of the 3 objects" */ /*@desc "The mass of the 3 objects" */
& &
initial_pos = [ [0.0,0.0] , [0.0,4.5e10], [0.0,-4.5e10] ] & initial_pos = [ make_vec([0.0,0.0]) , make_vec([0.0,4.5e10]), make_vec([0.0,-4.5e10]) ] &
initial_speed = [ [0.05e04,0.0], [3.0e04,0.0], [-3.0e04,0.0] ] initial_speed = [ make_vec([0.05e04,0.0]), make_vec([3.0e04,0.0]), make_vec([-3.0e04,0.0]) ]
END END
......
...@@ -18,8 +18,10 @@ PROPERTIES ...@@ -18,8 +18,10 @@ PROPERTIES
nobjs = 4 & nobjs = 4 &
mass = [3.0e28, 3.0e28, 3.0e28, 3.0e28] mass = [3.0e28, 3.0e28, 3.0e28, 3.0e28]
& &
initial_pos = [ [-3.5e10,0.0e00], [-1.0e10,0.0e00], [1.0e10, 0.0e00], [3.5e10, 0.0e00] ] & initial_pos = [ make_vec([-3.5e10,0.0e00]), make_vec([-1.0e10,0.0e00]),
initial_speed = [ [ 0.0e00,1.4e03], [0.0e00,1.4e03], [0.0e00,-1.4e03], [0.0e00,-1.4e03] ] make_vec([1.0e10, 0.0e00]), make_vec([3.5e10, 0.0e00]) ] &
initial_speed = [ make_vec([ 0.0e00,1.4e03]), make_vec([0.0e00,1.4e03]),
make_vec([0.0e00,-1.4e03]), make_vec([0.0e00,-1.4e03]) ]
END END
......
...@@ -14,7 +14,9 @@ ABSTRACT_CONSTANTS ...@@ -14,7 +14,9 @@ ABSTRACT_CONSTANTS
sigma_reals /*@desc "sum of a numbers of a vector" */, sigma_reals /*@desc "sum of a numbers of a vector" */,
add_firstv, add_firstv,
sigma_vecs /*@desc "sum of a sequence of vectors" */ sigma_vecs /*@desc "sum of a sequence of vectors" */,
make_vec /*@desc "construct a vector from a sequence" */
PROPERTIES PROPERTIES
n:NATURAL1 & n:NATURAL1 &
DIM = (1..n) & DIM = (1..n) &
...@@ -41,6 +43,8 @@ PROPERTIES ...@@ -41,6 +43,8 @@ PROPERTIES
times_vec = %(k,v1).(k:REAL & v1:VEC | %i.(i:DIM| k*v1(i))) /*@desc "multiply scalar by a vector" */ & times_vec = %(k,v1).(k:REAL & v1:VEC | %i.(i:DIM| k*v1(i))) /*@desc "multiply scalar by a vector" */ &
distance = %(v1,v2).(v1:VEC & v2:VEC | magnitude(delta_vec(v1,v2))) distance = %(v1,v2).(v1:VEC & v2:VEC | magnitude(delta_vec(v1,v2)))
/*@desc "Eucledian distance between two vectors" */ /*@desc "Eucledian distance between two vectors" */ &
make_vec = %v.(v:seq(REAL) & size(v)=n | v )
END END
\ No newline at end of file
...@@ -12,7 +12,9 @@ ABSTRACT_CONSTANTS ...@@ -12,7 +12,9 @@ ABSTRACT_CONSTANTS
times_vec /*@desc "multiply scalar by a vector" */, times_vec /*@desc "multiply scalar by a vector" */,
distance /*@desc "Eucledian distance between two vectors" */, distance /*@desc "Eucledian distance between two vectors" */,
sigma_vecs /*@desc "sum of a sequence of vectors" */ sigma_vecs /*@desc "sum of a sequence of vectors" */,
make_vec /*@desc "construct a vector from a sequence" */
PROPERTIES PROPERTIES
n:NATURAL1 & n = 2 & n:NATURAL1 & n = 2 &
DIM = (1..n) & DIM = (1..n) &
...@@ -39,6 +41,8 @@ PROPERTIES ...@@ -39,6 +41,8 @@ PROPERTIES
times_vec = %(k,v1).(k:REAL & v1:VEC | [k*v1(1),k*v1(2)]) /*@desc "multiply scalar by a vector" */ & times_vec = %(k,v1).(k:REAL & v1:VEC | [k*v1(1),k*v1(2)]) /*@desc "multiply scalar by a vector" */ &
distance = %(v1,v2).(v1:VEC & v2:VEC | magnitude(delta_vec(v1,v2))) distance = %(v1,v2).(v1:VEC & v2:VEC | magnitude(delta_vec(v1,v2)))
/*@desc "Eucledian distance between two vectors" */ /*@desc "Eucledian distance between two vectors" */ &
make_vec = %v.(v:seq(REAL) & size(v)=2 | v )
END END
\ No newline at end of file
MACHINE VectorsAsPairs2 // an optimized instance of Vectors for n=2 with a pair representation
DEFINITIONS
"LibraryReals.def";
p1(r) == prj1(REAL,REAL)(r);
p2(r) == prj2(REAL,REAL)(r)
ABSTRACT_CONSTANTS
n,
VEC,
add_vec /*@desc "sum of two vectors" */,
delta_vec /*@desc "difference of two vectors" */,
dot_product /*@desc "dot product of two vectors" */,
magnitude /*@desc "Magnitude of a vector" */,
unit_vec /*@desc "unit vector of a vector" */,
times_vec /*@desc "multiply scalar by a vector" */,
distance /*@desc "Eucledian distance between two vectors" */,
sigma_vecs /*@desc "sum of a sequence of vectors" */,
make_vec /*@desc "construct a vector from a sequence" */
PROPERTIES
n:NATURAL1 & n = 2 &
VEC = (REAL*REAL) &
sigma_vecs = %v.(v:seq1(VEC) | ( SIGMA(i).(i:dom(v)|p1(v(i))) , SIGMA(i).(i:dom(v)|p2(v(i))) ) )
/*@desc "sum of a sequence of vectors" */&
add_vec = %(v1,v2).(v1:VEC & v2:VEC | ( p1(v1)+p1(v2) , p2(v1)+p2(v2) ))
/*@desc "sum of two vectors" */ &
delta_vec = %(v1,v2).(v1:VEC & v2:VEC | ( p1(v1)-p1(v2) , p2(v1)-p2(v2) ) )
/*@desc "difference of two vectors" */ &
dot_product = %(v1,v2).(v1:VEC & v2:VEC | p1(v1)*p1(v2) + p2(v1)*p2(v2))
/*@desc "dot product of two vectors" */ &
magnitude = %v1.(v1:VEC | RSQRT(p1(v1)**2 + p2(v1)**2)) /*@desc "Magnitude of a vector" */ &
// alternative: RSQRT(dot_product(v1,v1)))
unit_vec = %v1.(v1:VEC | LET m BE m=magnitude(v1) IN
( p1(v1)/m , p2(v1)/m ) END ) /*@desc "unit vector of a vector" */ &
times_vec = %(k,v1).(k:REAL & v1:VEC | ( k*p1(v1), k*p2(v1) ) ) /*@desc "multiply scalar by a vector" */ &
distance = %(v1,v2).(v1:VEC & v2:VEC | magnitude(delta_vec(v1,v2)))
/*@desc "Eucledian distance between two vectors" */ &
make_vec = %v.(v:seq(REAL) & size(v)=2 | ( v(1) , v(2) ) )
END
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment