The code works fine for me, no “Invalid cast” error. I disabled the solver before opening the file and then disabled parts of it that are known to be slow (primarily SDiff) before re-enabling it to examine the model. The very last component (SDiff) is extremely slow (5.2 minutes) and should be disabled in order to examine and understand the rest of the file. Ideally, the model would be wired such that SDiff is executed only when you want to, not every time you make any change to a parameter.