! JAYLAB DPC v0.1: original implementation, small strain, alpha=0, K=1.
! State: 1:6 engineering plastic strain; 7=-trace(ep); 8=pb;
!        9=active branch (0 elastic/1 DP/2 cap); 10=Newton iterations.
! PROPS: E,nu,d,beta(deg),R,pb0,H,kappa_max,alpha(0),Kratio(1).
module jaylab_dpc
  implicit none
contains
  subroutine elastic(pr,c,b,g)
    real(8),intent(in)::pr(10)
    real(8),intent(out)::c(6,6),b,g
    integer i
    b=pr(1)/(3d0*(1d0-2d0*pr(2)))
    g=pr(1)/(2d0*(1d0+pr(2)))
    c=0d0
    c(1:3,1:3)=b-2d0*g/3d0
    do i=1,3
      c(i,i)=c(i,i)+2d0*g
      c(i+3,i+3)=g
    enddo
  end subroutine

  subroutine harden(pr,k,pb,pa,hc)
    real(8),intent(in)::pr(10),k
    real(8),intent(out)::pb,pa,hc
    real(8) t
    t=tan(pr(4)*acos(-1d0)/180d0)
    pb=pr(6)+pr(7)*min(max(k,0d0),pr(8))
    hc=0d0
    if(k>0d0.and.k<pr(8)) hc=pr(7)/(1d0+pr(5)*t)
    pa=(pb-pr(5)*pr(3))/(1d0+pr(5)*t)
  end subroutine

  subroutine residual(pr,ko,pt,qt,b,g,branch,x,f,j,rt,pa,pb)
    real(8),intent(in)::pr(10),ko,pt,qt,b,g,x(3)
    integer,intent(in)::branch
    real(8),intent(out)::f(3),j(3,3),rt(3),pa,pb
    real(8) k,hc,t,a,aa,cc,r,gp,gq,gpa,gpq,gqa,gqq,ap,l
    t=tan(pr(4)*acos(-1d0)/180d0)
    k=ko+(pt-x(1))/b
    call harden(pr,k,pb,pa,hc)
    a=x(1)-pa; l=x(3)
    if(branch==2)then
      aa=1d0;cc=pr(5)**2
    else
      aa=t*t;cc=1d0
    endif
    r=sqrt(aa*a*a+cc*x(2)**2)
    r=max(r,1d-30)
    gp=aa*a/r;gq=cc*x(2)/r
    gpa=aa*cc*x(2)**2/r**3
    gpq=-aa*cc*a*x(2)/r**3
    gqa=gpq;gqq=aa*cc*a*a/r**3
    ap=1d0+hc/b
    f(1)=x(1)-pt+b*l*gp
    f(2)=x(2)-qt+3d0*g*l*gq
    j(1,:)=(/1d0+b*l*gpa*ap,b*l*gpq,b*gp/)
    j(2,:)=(/3d0*g*l*gqa*ap,1d0+3d0*g*l*gqq,3d0*g*gq/)
    rt(1)=-1d0-l*hc*gpa
    rt(2)=-3d0*g*l*gqa*hc/b
    if(branch==2)then
      f(3)=r-pr(5)*(pr(3)+pa*t)
      j(3,:)=(/gp*ap+pr(5)*t*hc/b,gq,0d0/)
      rt(3)=-(gp+pr(5)*t)*hc/b
    else
      f(3)=x(2)-x(1)*t-pr(3)
      j(3,:)=(/-t,1d0,0d0/)
      rt(3)=0d0
    endif
  end subroutine

  subroutine solve3(a,r,x,ok)
    real(8),intent(in)::a(3,3),r(3)
    real(8),intent(out)::x(3)
    logical,intent(out)::ok
    real(8) m(3,4),v(4),fac
    integer i,k,p
    m(:,1:3)=a;m(:,4)=r;ok=.false.;x=0d0
    do k=1,3
      p=k-1+maxloc(abs(m(k:3,k)),dim=1)
      if(abs(m(p,k))<1d-20)return
      v=m(k,:);m(k,:)=m(p,:);m(p,:)=v
      do i=k+1,3
        fac=m(i,k)/m(k,k)
        m(i,k:4)=m(i,k:4)-fac*m(k,k:4)
      enddo
    enddo
    do i=3,1,-1
      x(i)=(m(i,4)-sum(m(i,i+1:3)*x(i+1:3)))/m(i,i)
    enddo
    ok=.true.
  end subroutine

  subroutine dpc_update(pr,so,vo,de,s,v,c,se,dp,ierr)
    real(8),intent(in)::pr(10),so(6),vo(10),de(6)
    real(8),intent(out)::s(6),v(10),c(6,6),se,dp
    integer,intent(out)::ierr
    real(8) ce(6,6),b,g,st(6),dev(6),pt,qt,pa,pb,t,ft,ko
    real(8) x(3),xn(3),f(3),fn(3),j(3,3),jn(3,3),rt(3),rtn(3)
    real(8) dx(3),yp(3),yq(3),ls,norm0,tol,pan,pbn,k,ratio
    real(8) dpt,dqt,dr,dep(6),ee(6),pa0,pb0,h0,rr,cc
    integer branch,attempt,it,bt,col,first
    logical ok,converged
    s=so;v=vo;c=0d0;se=0d0;dp=0d0;ierr=1
    if(any(pr/=pr).or.any(so/=so).or.any(vo/=vo).or.any(de/=de))return
    if(maxval(abs(pr))>1d100.or.maxval(abs(so))>1d100.or.maxval(abs(de))>1d100)return
    if(pr(1)<=0d0.or.pr(2)<=-1d0.or.pr(2)>=.5d0)return
    if(pr(3)<=0d0.or.pr(4)<=0d0.or.pr(4)>=89d0.or.pr(5)<=0d0)return
    if(pr(6)<=pr(5)*pr(3).or.pr(7)<0d0.or.pr(8)<=0d0)return
    if(pr(9)/=0d0.or.pr(10)/=1d0)return
    if(abs(vo(7)+sum(vo(1:3)))>1d-8)return
    call elastic(pr,ce,b,g)
    c=ce;st=so+matmul(ce,de);pt=-sum(st(1:3))/3d0
    dev=st;dev(1:3)=dev(1:3)+pt
    qt=sqrt(1.5d0*(sum(dev(1:3)**2)+2d0*sum(dev(4:6)**2)))
    ko=vo(7);t=tan(pr(4)*acos(-1d0)/180d0)
    call harden(pr,ko,pb0,pa0,h0)
    first=1
    if(pt>=pa0)first=2
    if(first==2)then
      ft=sqrt((pt-pa0)**2+(pr(5)*qt)**2)-pr(5)*(pr(3)+pa0*t)
    else
      ft=qt-pt*t-pr(3)
    endif
    tol=1d-11*max(1d0,pb0,abs(pt),qt)
    if(ft<=tol)then
      s=st;v(8)=pb0;v(9)=0d0;v(10)=0d0
    else
      converged=.false.
      do attempt=1,2
        branch=first
        if(attempt==2)branch=3-first
        x=(/pt,qt,0d0/)
        do it=1,60
          call residual(pr,ko,pt,qt,b,g,branch,x,f,j,rt,pa,pb)
          norm0=maxval(abs(f))
          if(norm0<=tol)then
            if(x(2)>=-tol.and.x(3)>=-1d-12)then
              if((branch==2.and.x(1)>=pa-tol).or.(branch==1.and.x(1)<=pa+tol))converged=.true.
            endif
            exit
          endif
          call solve3(j,-f,dx,ok)
          if(.not.ok)exit
          ls=1d0
          do bt=1,24
            xn=x+ls*dx
            call residual(pr,ko,pt,qt,b,g,branch,xn,fn,jn,rtn,pan,pbn)
            if(xn(2)>=-tol.and.xn(3)>=-1d-12.and.maxval(abs(fn))<norm0)exit
            ls=ls*.5d0
          enddo
          if(bt>24)exit
          x=xn
        enddo
        if(converged)exit
      enddo
      if(.not.converged)then
        ierr=2;return
      endif
      ! Consistent tangent by analytic implicit differentiation of residuals.
      call residual(pr,ko,pt,qt,b,g,branch,x,f,j,rt,pa,pb)
      call solve3(j,-rt,yp,ok)
      if(.not.ok)then
        ierr=3;return
      endif
      call solve3(j,(/0d0,1d0,0d0/),yq,ok)
      if(.not.ok)then
        ierr=3;return
      endif
      k=ko+(pt-x(1))/b
      if(qt>1d-14)then
        ratio=x(2)/qt
      else
        cc=1d0
        rr=sqrt((t*(x(1)-pa))**2+x(2)**2)
        if(branch==2)then
          cc=pr(5)**2;rr=sqrt((x(1)-pa)**2+cc*x(2)**2)
        endif
        ratio=1d0/(1d0+3d0*g*x(3)*cc/max(rr,1d-30))
      endif
      s=ratio*dev;s(1:3)=s(1:3)-x(1)
      do col=1,6
        dpt=0d0
        if(col<=3)dpt=-b
        dqt=0d0
        if(qt>1d-14)dqt=3d0*g*dev(col)/qt
        dr=0d0
        if(qt>1d-14)dr=((yp(2)*dpt+yq(2)*dqt)-ratio*dqt)/qt
        c(:,col)=ratio*ce(:,col)+dev*dr
        c(1:3,col)=c(1:3,col)+ratio*dpt-(yp(1)*dpt+yq(1)*dqt)
      enddo
      ! Engineering plastic strain increment from elastic compliance.
      dep(1:3)=((1d0+pr(2))*(st(1:3)-s(1:3))-pr(2)*sum(st(1:3)-s(1:3)))/pr(1)
      dep(4:6)=(st(4:6)-s(4:6))/g
      v(1:6)=vo(1:6)+dep;v(7)=k;v(8)=pb
      v(9)=dble(branch);v(10)=dble(it)
      ! Discrete trapezoidal plastic work; first-yield quadrature error is O(de^2).
      dp=.5d0*sum((so+s)*dep)
      if(dp < -1d-9)then
        ierr=4;return
      endif
    endif
    ee(1:3)=((1d0+pr(2))*s(1:3)-pr(2)*sum(s(1:3)))/pr(1)
    ee(4:6)=s(4:6)/g
    se=.5d0*sum(s*ee)
    ierr=0
  end subroutine
end module

! Abaqus/Standard 6.14 scalar KSTEP interface. Compile with dpc_core.f90 first.
subroutine umat(stress,statev,ddsdde,sse,spd,scd,rpl,ddsddt,drplde,drpldt, &
  stran,dstran,time,dtime,temp,dtemp,predef,dpred,cmname,ndi,nshr,ntens, &
  nstatv,props,nprops,coords,drot,pnewdt,celent,dfgrd0,dfgrd1,noel,npt, &
  layer,kspt,kstep,kinc)
  use jaylab_dpc
  include 'ABA_PARAM.INC'
  character*80 cmname
  dimension stress(ntens),statev(nstatv),ddsdde(ntens,ntens),ddsddt(ntens), &
    drplde(ntens),stran(ntens),dstran(ntens),time(2),predef(*),dpred(*), &
    props(nprops),coords(3),drot(3,3),dfgrd0(3,3),dfgrd1(3,3)
  real(8) sn(6),vn(10),cn(6,6),en,dw,b,g,ref,expected(6)
  integer ierr,i,j
  if(ndi/=3.or.nshr/=3.or.ntens/=6.or.nstatv/=10.or.nprops/=10)goto 900
  if(dtemp/=0d0)goto 900
  do i=1,3
    do j=1,3
      ref=0d0
      if(i==j)ref=1d0
      if(abs(drot(i,j)-ref)>1d-8)goto 900
    enddo
  enddo
  ! Reject incompatible prestress/state initialization. Zero state, zero strain is supported.
  call elastic(props,cn,b,g)
  expected=matmul(cn,stran-statev(1:6))
  if(maxval(abs(expected-stress))>1d-6+1d-8*maxval(abs(stress)))goto 900
  call dpc_update(props,stress,statev,dstran,sn,vn,cn,en,dw,ierr)
  ddsddt=0d0;drplde=0d0;drpldt=0d0;rpl=0d0
  if(ierr==1)goto 900
  if(ierr/=0)then
    pnewdt=min(pnewdt,.25d0)
    call elastic(props,ddsdde,b,g)
    return
  endif
  ddsdde=cn
  if(dtime>0d0)then
    stress=sn;statev=vn;sse=en;spd=spd+dw;scd=0d0
  endif
  return
900 write(7,*) 'JAYLAB DPC: INVALID INPUT/UNSUPPORTED STATE',noel,npt,kstep,kinc
  call xit
end subroutine
