{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3d mortar assembly\n",
    "\n",
    "Author: Jukka Aho\n",
    "\n",
    "**Abstract**: Mortar assembly for 3d problems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "using JuliaFEM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1-element Array{JuliaFEM.Core.Element{E<:JuliaFEM.Core.AbstractElement},1}:\n",
       " JuliaFEM.Core.Element{JuliaFEM.Core.Tri3}([4,5,6],Dict{ASCIIString,JuliaFEM.Core.Field{A<:Union{JuliaFEM.Core.Continuous,JuliaFEM.Core.Discrete},B<:Union{JuliaFEM.Core.Constant,JuliaFEM.Core.Variable},C<:Union{JuliaFEM.Core.TimeInvariant,JuliaFEM.Core.TimeVariant}}}(\"geometry\"=>JuliaFEM.Core.Field{JuliaFEM.Core.Discrete,JuliaFEM.Core.Variable,JuliaFEM.Core.TimeInvariant}([[-1.0,1.0,0.1],[2.0,-0.5,0.1],[2.0,2.0,0.1]])))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nodes = Vector{Float64}[\n",
    "    [0.0, 0.0, 0.0],\n",
    "    [3.0, 0.0, 0.0],\n",
    "    [0.0, 3.0, 0.0],\n",
    "    [-1.0,  1.0, 0.1],\n",
    "    [ 2.0, -0.5, 0.1],\n",
    "    [ 2.0,  2.0, 0.1]]\n",
    "\n",
    "mel = JuliaFEM.Core.Tri3([4, 5, 6])\n",
    "mel[\"geometry\"] = Vector{Float64}[nodes[4], nodes[5], nodes[6]]\n",
    "\n",
    "sel = JuliaFEM.Core.Tri3([1, 2, 3])\n",
    "sel[\"geometry\"] = Vector{Float64}[nodes[1], nodes[2], nodes[3]]\n",
    "R = [\n",
    "    0.0 1.0 0.0\n",
    "    0.0 0.0 1.0\n",
    "    1.0 0.0 0.0]\n",
    "sel[\"nodal ntsys\"] = Matrix{Float64}[R, R, R]\n",
    "sel[\"master elements\"] = JuliaFEM.Core.Element[mel]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "2x3 Array{Float64,2}:\n",
       " -1.0   2.0  -1.0\n",
       " -1.0  -1.0   2.0,\n",
       "\n",
       "2x3 Array{Float64,2}:\n",
       " -2.0   1.0  1.0\n",
       "  0.0  -1.5  1.0,\n",
       "\n",
       "2x6 Array{Float64,2}:\n",
       " -1.0   0.0   1.0  1.0  0.25  -1.0     \n",
       " -0.5  -1.0  -1.0  0.0  0.75   0.333333,\n",
       "\n",
       "3x3 Array{Int64,2}:\n",
       " 1  0  1\n",
       " 1  1  0\n",
       " 0  1  1,\n",
       "\n",
       "[0.039743589743589755,-0.1952991452991453])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = 0.0\n",
    "x0, Q = JuliaFEM.Core.create_auxiliary_plane(sel, time)\n",
    "S = Vector{Float64}[]\n",
    "for p in sel(\"geometry\", time)\n",
    "    push!(S, JuliaFEM.Core.project_point_to_auxiliary_plane(p, x0, Q))\n",
    "end\n",
    "\n",
    "M = Vector{Float64}[]\n",
    "for p in mel(\"geometry\", time)\n",
    "    push!(M, JuliaFEM.Core.project_point_to_auxiliary_plane(p, x0, Q))\n",
    "end\n",
    "\n",
    "#S = Matrix[x' for x in S]\n",
    "#S, M\n",
    "S = reshape([S...;], 2, 3)\n",
    "M = reshape([M...;], 2, 3)\n",
    "P, neighbours = JuliaFEM.Core.clip_polygon(S, M)\n",
    "C = JuliaFEM.Core.calculate_polygon_centerpoint(P)\n",
    "S, M, P, neighbours, C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: npts = 6\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33308 characters]",
      "text/plain": [
       "PyPlot.Figure(PyObject <matplotlib.figure.Figure object at 0x7f170b2a8490>)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(-2.1,2.1,-1.6,2.1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using PyPlot\n",
    "fig = figure(figsize=(5.0, 5.0))\n",
    "plot(S[1, [1, 2, 3, 1]]', S[2, [1, 2, 3, 1]]', \"-yo\", lw=2.0)\n",
    "plot(M[1, [1, 2, 3, 1]]', M[2, [1, 2, 3, 1]]', \"-ro\", lw=2.0)\n",
    "#plot(P[1, [1, 2, 3, 4, 5, 6, 1]]', P[2, [1, 2, 3, 4, 5, 6, 1]]', \"-k\", lw=2.5)\n",
    "npts = size(P, 2)\n",
    "info(\"npts = $npts\")\n",
    "# loop over cells\n",
    "ips = JuliaFEM.Core.get_integration_points(JuliaFEM.Core.Tri3, Val{5})\n",
    "#info(\"ips = $ips\")\n",
    "SM = zeros(3, 3)\n",
    "SS = zeros(3, 3)\n",
    "for i=1:npts\n",
    "    xvec = [C[1], P[1, i], P[1, mod(i, npts)+1]]\n",
    "    yvec = [C[2], P[2, i], P[2, mod(i, npts)+1]]\n",
    "    plot(xvec, yvec, \"--ko\", lw=1.0)\n",
    "    X = hcat(xvec, yvec)'\n",
    "    geom = JuliaFEM.Core.Field(Vector{Float64}[X[:,i] for i=1:size(X,2)])\n",
    "    #info(\"geom = $geom\")\n",
    "    for ip in ips\n",
    "        dN = JuliaFEM.Core.get_dbasis(JuliaFEM.Core.Tri3, ip.xi)\n",
    "        J = sum([kron(dN[:,j], geom[j]') for j=1:length(geom)])\n",
    "        w = ip.weight*det(J)\n",
    "        x = vec(JuliaFEM.Core.get_basis(JuliaFEM.Core.Tri3, ip.xi)*geom)\n",
    "        plot(x[1], x[2], \".g\")\n",
    "        theta1 = JuliaFEM.Core.project_point_from_plane_to_surface(x, x0, Q, sel, time)\n",
    "        N1 = sel(theta1[2:3], time)\n",
    "        theta2 = JuliaFEM.Core.project_point_from_plane_to_surface(x, x0, Q, mel, time)\n",
    "        N2 = mel(theta2[2:3], time)\n",
    "        SS += w*N1'*N1\n",
    "        SM += w*N1'*N2\n",
    "    end\n",
    "end\n",
    "plot(C[1], C[2], \"ko\")\n",
    "xlim(-2.1, 2.1)\n",
    "ylim(-1.6, 2.1)\n",
    "axis(\"off\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: SS = \n",
      "[0.5123885459533705 0.29124871399177565 0.23957261659808454\n",
      " 0.29124871399177565 0.40967399691358797 0.23773469650206241\n",
      " 0.23957261659808454 0.23773469650206241 0.2491587362825836]\n",
      "INFO: SM = \n",
      "[0.4042245370370448 0.38539094650206535 0.2535943930041208\n",
      " 0.21609760802469608 0.37920524691358753 0.3433545524691425\n",
      " 0.24657600308642466 0.1835519547325143 0.2963380915637917]\n"
     ]
    }
   ],
   "source": [
    "info(\"SS = \\n$SS\")\n",
    "info(\"SM = \\n$SM\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.4.1",
   "language": "julia",
   "name": "julia-0.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
