ClearAll[pmax,lmax,lmax2,h1,GVTab,PTTab,DTTab];
SetDirectory[NotebookDirectory[]];
(* choose your favorite CICY *)
<<X5.m
{CY,chi,kappa,c2}

(* Castelnovo bound on the D0-charge for PT invariants *)
MMin[Q_]:=Ceiling[-((Q^2)/(2kappa))-(Q/2)];
(* Castelnovo bound on the genus of GV invariants *)
gMax[Q_]:=Floor[((Q^2)/(2 kappa))+(Q/2)+1];
(* generating function of PT invariants *)
ZPTnew[QMax_]:=Exp[Sum[Sum[(((-1))^(g+1))GVTab[[g+1,Q]]Sum[(1/k)(q^((1-g)k)) (y^(Q k))(((1-(q^k)))^(2g-2)),{k,1,QMax}],{g,0,gMax[Q]}],{Q,1,QMax}]]/.q->-q;

(* maximal genus and degree in the table of GV invariants *)
gmaxGV=Length[GVTab]-1
QmaxGV=Length[Transpose[GVTab]]
(* the maximal degree which gives rise to the genus satisfying the Castelnovo bound *)
QmaxGVg=Floor[Q/. Solve[((Q^2)/(2 kappa))+(Q/2)+1==gmaxGV,Q][[2]]]
(* choose the minimal of the two bound on the degree to avoid appearance of unknow GV invariants *)
Qmax=Min[QmaxGV,QmaxGVg]
(* expand the generating function in y *)
ZPTexpy=Series[ZPTnew[Qmax],{y,0,Qmax}];

(* expand also in q with additional multiplication of each order in y by the power of q corresponding to the Castelnovo bound *)
NMax=30;
ZPTexp=Table[Series[(q^(-MMin[i-1]))ZPTexpy[[3,i]],{q,0,NMax}],{i,1,Length[ZPTexpy[[3]]]}];
(* extract the coefficients of the exppansion *)
PTTabsh=CoefficientList[ZPTexp,q];
(* addes zeros to the first row to complete to a square table *)
PTTabsh[[1]]=Join[PTTabsh[[1]],Array[0 &,NMax]];
(* the shifts corresponding to the Castelnovo bound *)
Shiftn=Join[{0},Array[-MMin[#] &, Qmax]];
(* adds the first column encoding the shifts of the D0-charge *)
PTTabnew=Transpose[Join[{Shiftn},Transpose[PTTabsh]]];
MatrixForm[PTTabnew]