# Path: dolfin_navier_scipy/residual_checks.py
# def get_imex_res(V=None, outflowds=None, gradvsymmtrc=True, nu=None,
#                  implscheme='crni', explscheme='abtw'):
#     """ define the residual for an IMEX/AB2 time discretization
# 
#     """
#     if not implscheme == 'crni':
#         raise NotImplementedError()
# 
#     if explscheme == 'abtw':
#         def convform(cvo=None, cvt=None, phi=None):
#             return (1.5*inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx -
#                     .5*inner(dolfin.dot(cvt, nabla_grad(cvt)), phi)*dx)
# 
#     elif explscheme == 'heun':
#         def convform(cvo=None, cvt=None, phi=None):
#             return (.5*inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx +
#                     .5*inner(dolfin.dot(cvt, nabla_grad(cvt)), phi)*dx)
# 
#     elif explscheme == 'eule':
#         def convform(cvo=None, cvt=None, phi=None):
#             return inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx
# 
#     def imex_res(vel, pres, dt, lastvel=None, othervel=None, phi=None):
#         if phi is None:
#             phi = dolfin.TestFunction(V)
# 
#         diffvel = .5*(vel+lastvel)  # Crank-Nicolson
#         diffrm = nu*inner(grad(diffvel)+grad(diffvel).T, grad(phi))*dx
#         if gradvsymmtrc:
#             nvec = dolfin.FacetNormal(V.mesh())
#             diffrm = diffrm - (nu*inner(grad(diffvel).T*nvec, phi))*outflowds
#         cnvfrm = convform(cvo=lastvel, cvt=othervel, phi=phi)
# 
#         pfrm = -inner(pres, div(phi))*dx
#         dtprt = 1./dt*dolfin.assemble(inner(vel, phi)*dx) \
#             - 1./dt*dolfin.assemble(inner(lastvel, phi)*dx)
#         res = dolfin.assemble(diffrm+cnvfrm+pfrm) + dtprt
# 
#         # import numpy as np
#         # nfc_c = dolfin.assemble(cnvfrm).get_local()
#         # print(np.linalg.norm(nfc_c), nfc_c[0], nfc_c.size)
#         # print('debggng')
#         return res
# 
#     return imex_res


import numpy as np
import matplotlib.pyplot as plt
import json
import dolfin
import dolfin_navier_scipy.stokes_navier_utils as snu
import dolfin_navier_scipy.dolfin_to_sparrays as dts
import dolfin_navier_scipy.problem_setups as dnsps
from dolfin_navier_scipy.residual_checks import get_imex_res

            try:
                ovfn = dts.expand_vp_dolfunc(vc=memory['lastlastv'], **femp)[0]
                modres = abtwres
            except KeyError:  # no lastlastv yet -- we can check the Heun res
                ovfn = dts.expand_vp_dolfunc(vc=memory['heunpred'], **femp)[0]
                modres = heunres

            trqe = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=phitwo)
            lift = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=poy)
            drag = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=pox)

            memory.update(dict(lastlastv=np.copy(memory['lastv'])))
            memory.update(dict(lastv=vel))

            memory['trqs'].append(trqe)
            memory['lfts'].append(lift)
            memory['drgs'].append(drag)
            memory['tims'].append(t)
            memory.update(dict(lastt=t, lastdt=curdt))

        deltap = pfun(a_1) - pfun(a_2)
        memory['dtps'].append(deltap)
        return rotval, memory

    rotcondict = dict(lastt=None,
                      trqs=[], omegs=[], lfts=[], drgs=[], dtps=[], tims=[],

# Path: dolfin_navier_scipy/residual_checks.py
# def get_imex_res(V=None, outflowds=None, gradvsymmtrc=True, nu=None,
#                  implscheme='crni', explscheme='abtw'):
#     """ define the residual for an IMEX/AB2 time discretization
# 
#     """
#     if not implscheme == 'crni':
#         raise NotImplementedError()
# 
#     if explscheme == 'abtw':
#         def convform(cvo=None, cvt=None, phi=None):
#             return (1.5*inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx -
#                     .5*inner(dolfin.dot(cvt, nabla_grad(cvt)), phi)*dx)
# 
#     elif explscheme == 'heun':
#         def convform(cvo=None, cvt=None, phi=None):
#             return (.5*inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx +
#                     .5*inner(dolfin.dot(cvt, nabla_grad(cvt)), phi)*dx)
# 
#     elif explscheme == 'eule':
#         def convform(cvo=None, cvt=None, phi=None):
#             return inner(dolfin.dot(cvo, nabla_grad(cvo)), phi)*dx
# 
#     def imex_res(vel, pres, dt, lastvel=None, othervel=None, phi=None):
#         if phi is None:
#             phi = dolfin.TestFunction(V)
# 
#         diffvel = .5*(vel+lastvel)  # Crank-Nicolson
#         diffrm = nu*inner(grad(diffvel)+grad(diffvel).T, grad(phi))*dx
#         if gradvsymmtrc:
#             nvec = dolfin.FacetNormal(V.mesh())
#             diffrm = diffrm - (nu*inner(grad(diffvel).T*nvec, phi))*outflowds
#         cnvfrm = convform(cvo=lastvel, cvt=othervel, phi=phi)
# 
#         pfrm = -inner(pres, div(phi))*dx
#         dtprt = 1./dt*dolfin.assemble(inner(vel, phi)*dx) \
#             - 1./dt*dolfin.assemble(inner(lastvel, phi)*dx)
#         res = dolfin.assemble(diffrm+cnvfrm+pfrm) + dtprt
# 
#         # import numpy as np
#         # nfc_c = dolfin.assemble(cnvfrm).get_local()
#         # print(np.linalg.norm(nfc_c), nfc_c[0], nfc_c.size)
#         # print('debggng')
#         return res
# 
#     return imex_res


# Path: tests/tdp_2D_simu.py
import numpy as np
import matplotlib.pyplot as plt
import json
import dolfin
import dolfin_navier_scipy.stokes_navier_utils as snu
import dolfin_navier_scipy.dolfin_to_sparrays as dts
import dolfin_navier_scipy.problem_setups as dnsps
from dolfin_navier_scipy.residual_checks import get_imex_res


            try:
                ovfn = dts.expand_vp_dolfunc(vc=memory['lastlastv'], **femp)[0]
                modres = abtwres
            except KeyError:  # no lastlastv yet -- we can check the Heun res
                ovfn = dts.expand_vp_dolfunc(vc=memory['heunpred'], **femp)[0]
                modres = heunres

            trqe = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=phitwo)
            lift = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=poy)
            drag = modres(vfun, pfun, curdt, lastvel=lvfun, othervel=ovfn,
                          phi=pox)

            memory.update(dict(lastlastv=np.copy(memory['lastv'])))
            memory.update(dict(lastv=vel))

            memory['trqs'].append(trqe)
            memory['lfts'].append(lift)
            memory['drgs'].append(drag)
            memory['tims'].append(t)
            memory.update(dict(lastt=t, lastdt=curdt))

        deltap = pfun(a_1) - pfun(a_2)
        memory['dtps'].append(deltap)
        return rotval, memory

    rotcondict = dict(lastt=None,
                      trqs=[], omegs=[], lfts=[], drgs=[], dtps=[], tims=[],

                      lastdt=None)