/////////////////////////////////////////////////////////////////////////
// mmp1
// $Revision: 31078 $
// $Date: 2010-11-30 23:38:30 +1100 (Tue, 30 Nov 2010) $
// $LastChangedBy: kasprzyk $
/////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
// Minimal Model Programme
/////////////////////////////////////////////////////////////////////////
"------\nMinimal Model Programme\n";

//////////////////////////////////////
// Example 1
//////////////////////////////////////
"------\nExample 1\n";

// Minimal Model Program for a 3-fold
// start with a Weighted Projective Space:
F6:=FanOfWPS([1,1,2,3]);
assert IsTerminal(F6);

// Resolve the half singularity:
v:=&+[Rays(F6)[i] : i in [1,2,4]];
F7:=Blowup(F6,v);
assert IsTerminal(F7);
X7:=ToricVariety(Rationals(),F7);
X7;

// Look for its extremal contractions:
MoriCone(X7);
ExtremalRays(X7);
// Call MoriCone again to demonstrate how the new knowledge is
// reflected in the printing
MoriCone(X7);
TypesOfContractions(X7);

// View the canonical class
CanonicalClass(X7);

// Do the flip:
i:=Index(TypesOfContractions(X7), "flip");
X8:=Flip(X7, i);
WeightsOfFlip(X7,i);

bool,ample:=IsProjective(X8);
assert bool;
ample;
assert IsTerminal(X8);
assert IsNonsingular(X8);
Fan(X8);
CoxRing(X8);

//////////////////////////////////////
// Example 2: Takes approx 19s
//////////////////////////////////////
"------\nExample 2\n";

rays := [ [-1, 2, 1, 1, 1],
          [ 1, 0, 0, 0, 0],
          [ 0,-2,-1,-1,-1],
          [ 0, 1, 0, 0, 0],
          [ 0, 0, 1, 0, 0],
          [ 0, 0, 0, 1, 0],
          [ 0, 0, 0, 0, 1],
          [-1, 1, 1, 1, 1] ];
old_cones := [ [1,3,4,5,6],
               [1,3,4,5,7],
               [1,3,4,6,7],
               [1,3,5,6,7],
               [1,4,5,6,7],
               [2,3,4,5,6],
               [2,3,4,5,7],
               [2,3,4,6,7],
               [2,3,5,6,7],
               [2,4,5,6,7] ];
new_cones := [ [1,3,5,6,8],
               [1,3,5,7,8],
               [1,3,6,7,8],
               [1,5,6,7,8],
               [3,5,6,7,8] ];
cones := Remove(old_cones,4) cat new_cones;
time F := Fan(rays,cones);                  // approx 3.7s
X := ToricVariety(Rationals(),F);
assert Dimension(X) eq 5;
assert not IsNonsingular(X);
assert IsTerminal(X);
time vars,maps := MMP(X : type:="all");     // approx 15s
assert #vars eq 13;
assert #maps eq 22;

/////////////////////////////////////////////////////////////////////////
// Finally, clear the caches
/////////////////////////////////////////////////////////////////////////
"------\nClearing caches\n";

CacheClearToricVariety();
CacheClearToricLattice();
