//
//  CHRONO JAVASCRIPT EXAMPLES AND TUTORIALS 
//
//   You can type these examples, line by line, in the
//  scripting window shell: see what happens each time you
//  execute the command with 'enter'.
//
//    Or, to be faster, you can cut & paste portions of 
//  code in the scripting window, then you go with pointer 
//  to the end of each pasted line and press 'enter'.
//


////// Vectors

va = new Vector; 
va.Vset(1,2,3);
va.x=0; 
print(" va is: \n", va);
vb = new Vector;
vb.x= 1; vb.y=4; 
print(" length of vb is: ", vb.Vlength() ); 
vc = new Vector;
0	0	0  
vc.Vnorm(va);
vc.Vadd(va,vb);
print(" sum vc=va+vb is: \n", vc ); 


////// Quaternions

qa = new Quaternion
qa.e1 = 2
qa.Qlenght()
qa
qa.Qnorm(qa)
qa
qa.Qlenght()
qa.QtoCardano()
 
 // From angle-axis of rotation to quaternion, then
 // from quaternion to Cardano angles..
v = new Vector; 
v.y = 1 ; 
v.Vnorm(v);
v
qa.QfromAngAxis(Math.PI, v);
qa
qa.QtoCardano()


////// How to set & get matrix elements

a = new Matrix; // create default 3x3 matrix
a.Reset(5,3);   // resize to 3 rows, 5 columns 
a.SetEl(1,2, 12.3+21);
a.GetEl(1,2);
a.Print();
a.GetEl(5,0);   // ops..out of boundary!


//////  How to perform matrix operations 

ma = new Matrix
mb = new Matrix
mc = new Matrix
ma.FillElem(2) 
ma.Print()
mb.FillDiag(5) 
mb.Print()
mc.Multiply(ma,mb)  // mc = ma*mb
mc.Print()
mc.Add(ma,mb)       // mc = ma+mb
mc.Print()
print("mc determinant is: ", mc.Det() );
mc.Invert();
print("mc^-1 determinant is: ", mc.Det() );


////// How to solve linear systems

 // create a 3x3 matrix, fill with random values
ma = new Matrix
ma.Reset(3,3)
ma.FillRandom(0,1)
ma.Print()		
 // create a 3x1 matrix, fill with random values 
b= new Matrix()
b.Reset(3,1)
b.FillRandom(1,2)
b.Print()		
 // prepare a x matrix, the unknown vector
x = new Matrix()		
 // prepare a pivot vector, will contain pivots
pivots = new Matrix()		
 // solve for x in   [ma]x=b  with this instruction!
ma.SolveLinSys(b,x,pivots)	
x.Print()
 // show that the result is correct...
b2 = new Matrix; 
b2.Reset(3,1)
b2.Multiply(maorig,x)
b2.Print()
b.Print()
print("See, the diff. is null! ", (b.GetEl(0,0)-b2.GetEl(0,0)) );

////// Functions

mf = new ChFunction
 // set parameter
mf.C= 10;
 // gets the value at x=0
mf.y(0)
 // gets the derivative at x=2
mf.y_dx(2)

mf = new ChFunctionRamp
mf.ang=2;
mf.C=1
mf.y(1)
mf.y(2)
mf.y_dx(0)



////// How to make optimizations

op = new ChGeneticOptimizer();
function pollo() {mx = x-1; my = y-2; return mx*mx+my*my;}
op.objective="pollo()";
op.minimize = 1;
op.AddVar("x",-10,+10);
op.AddVar("y",-10,+10);
op.population=30;
op.max_generations=100;
x = 5; y= 5;
op.Optimize();
print("resulting.. x=",x," y=",y, "  optimized funct =", op.opt_fx);

op = new ChHybridOptimizer();
function pollo() {mx = x-1; my = y-2; return mx*mx+my*my;}
op.objective="pollo()";
op.minimize = 1;
op.AddVar("x",-10,+10);
op.AddVar("y",-10,+10);
op.genetic_opt.population=30;
op.genetic_opt.max_generations=100;
x = 5; y= 5;
op.Optimize();
print("resulting.. x=",x," y=",y, "  optimized funct =", op.opt_fx);


////// How to compute jacobian of a function

 // define a function which sets output=f(input),  
function pollo() { y1= x1*x1;  y2= x1*x2}
 // best remember to initialize inputs (maybe nonlinear J!)
x1 = 1; x2 = 4;
 // now compute Jacobian derivatives, the i,j matrix [dYi/dXj],
 // passing the two arrays of input names and output names, and the
 // name of the function.
mJac = ch_jacobian(["x1","x2"] , ["y1","y2"] ,"pollo()");
mJac.Print();
mJac.Det();


////// How to plot 3d functions in 3d world

 // remember: first create a 3d mesh named "mesh1"
me = Geo("mesh1");  // find the "mesh1" in tree of objects 
mat = new Matrix;
mat.Reset(20,25);
for (ir=0; ir <20; ir++)  for (ic=0; ic <25; ic++)  {mat.SetEl(ir,ic, 0.02*sin(12*ir/20.0) + 0.02*sin(20*ic/25.0) );}
PlotMeshOnSurf(mat, me, 2)




/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////


GetSys().tol = 0.000001;
x1=0; x2= 0;
xa =["x1","x2"];
ya =["y1","y2"];
mJac = ch_jacobian(xa,ya,"jacobianize_me()"); mJac.Rcond();

sy = GetSys()
ma = sy.Obj("marker3")
sph = sy.Obj("sph")
sy.AssemblyForces()
x1=0; x2= 0;
xa =["x1","x2"];
ya =["y1","y2"];
mJac = ch_jacobian(xa,ya,"jacobianize_me()"); sy.WireRefresh(); mJac.Cond();
ov1 = -0.1; optimize_me();



ikj_force1=GetSys().Obj("mesh1").Obj("force1");
ikj_force2=GetSys().Obj("mesh2").Obj("force2");
ikj_link= GetSys().Obj("spherical");
mmesh=GetSys().Obj("ground").Geo("rectangle1");
markeffect= GetSys().Obj("marker7");
matr = test_over_mesh(mmesh, 8,8, markeffect, "ik_jacobian().Rcond()");
PlotMeshOnSurf(matr, mmesh, 2);


load ("chrono/javascript/chrono_startup.js")
load ("chrono/javascript/rob03_pre_optimize.js")
me3_u = me3_v = 1; dist1 = 0.4;  v = basic_optimize_me(); GetSys().WireRefresh(); v;


///////////////////////////////////////////////////

