WARNING: Can't resolve .param in LTspice
All the .param definitions are evaluated prior to simulation start, because they need to be available in the schematic for the duration of the simulation. For this reason you can't evaluate .params that involve time-dependent quantities: gr, gi1, gi2, Dprim and D0 (or DO). There are ready-made examples, and not only in LTspice. Best take those instead of making your own, at least until you learn more about the tool you are using.