Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kailaix/b341b96809a21d41a7d7968e4ee9ec02 to your computer and use it in GitHub Desktop.

Select an option

Save kailaix/b341b96809a21d41a7d7968e4ee9ec02 to your computer and use it in GitHub Desktop.
viscous_burgers.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Burger's Equation\n\n$$u_t + uu_{x} = \\frac{0.01}{\\pi}u_{xx}$$\n\n$$u(x,0) = \\sin(\\pi x)$$\n\nReference: \nRaissi, Maziar, Paris Perdikaris, and George Em Karniadakis. \"Physics Informed Deep Learning (Part I): Data-driven solutions of nonlinear partial differential equations.\" arXiv preprint arXiv:1711.10561 (2017)."
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using TensorFlow\nusing PyPlot",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "num_layers = 10\n\nx = placeholder(Float32,shape=[-1,1])\nt = placeholder(Float32,shape=[-1,1])\npde = placeholder(Float32,shape=[-1,1])\nx0 = placeholder(Float32,shape=[-1,1])\nt0 = placeholder(Float32,shape=[-1,1])\nu0 = placeholder(Float32,shape=[-1,1])",
"execution_count": 105,
"outputs": [
{
"data": {
"text/plain": "<Tensor placeholder_80:1 shape=(?, 1) dtype=Float32>"
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function neural(x, reuse=false)\n variable_scope(\"U1\", reuse=reuse) do\n W1 = get_variable(\"W0\", shape=[2,20], dtype=Float32) \n b1 = get_variable(\"b0\", shape=[20], dtype=Float32)\n net = x*W1 + b1\n net = nn.tanh(net)\n for i = 1:num_layers-2\n W1 = get_variable(\"W$i\", shape=[20,20], dtype=Float32) \n b1 = get_variable(\"b$i\", shape=[20], dtype=Float32)\n net = net*W1 + b1\n net = nn.tanh(net)\n end\n W1 = get_variable(\"Wend\", shape=[20,1], dtype=Float32) \n b1 = get_variable(\"bend\", shape=[1], dtype=Float32) \n z = net*W1 + b1\n return z\n end\nend",
"execution_count": 106,
"outputs": [
{
"data": {
"text/plain": "neural (generic function with 3 methods)"
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "u = neural(concat([x0,t0],2), false)\nuf = neural(concat([x,t],2), true)\nf = gradients(uf,t) + uf .* gradients(uf, x) -\n 0.01f0/3.141592654f0*gradients(gradients(uf,x),x)\nloss = reduce_sum((f-pde).^2) + reduce_sum((u0-u).^2)",
"execution_count": 100,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# create a new session\nsess = Session()\ntrain_step = train.minimize(train.AdamOptimizer(), loss)\nrun(sess, global_variables_initializer())\n",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function create_training_data(N1, N2)\n # boundary\n xx1 = rand(Float32,N1)\n tt1 = zeros(Float32,N1)\n uu1 = sin.(2*pi*xx1)\n xx2 = zeros(Float32,N1)\n tt2 = rand(Float32,N1)\n uu2 = zeros(Float32,N1)\n xx3 = ones(Float32,N1)\n tt3 = rand(Float32,N1)\n uu3 = zeros(Float32,N1)\n xx = [xx1;xx2;xx3]\n tt = [tt1;tt2;tt3]\n uu = [uu1;uu2;uu3]\n \n xxx = rand(Float32,N2)\n ttt = rand(Float32,N2)\n fff = zeros(Float32,N2)\n xx, tt, uu, xxx, ttt, fff = map(x->Array{Float32}(reshape(x, length(x),1)), [xx, tt, uu, xxx, ttt, fff])\n return xx, tt, uu, xxx, ttt, fff\nend\n\nfunction testing_data(N)\n a = LinRange{Float32}(0.0,1.0,N)\n X = zeros(Float32,N,N)\n Y = zeros(Float32,N,N)\n for i = 1:N\n for j = 1:N\n X[i,j] = a[i]\n Y[i,j] = a[j]\n end\n end\n return X, Y\nend",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "xx, tt, uu, xxx, ttt, fff = create_training_data(1000,10000)\nX,Y = testing_data(200)\nLos = Float64[]\nfor i in 1:50000\n run(sess, train_step, Dict(x=>xxx, t=>ttt, pde=>fff, x0=>xx, t0=>tt, u0=>uu))\n los = sqrt(run(sess, loss, Dict(x=>xxx, t=>ttt, pde=>fff, x0=>xx, t0=>tt, u0=>uu)))\n if mod(i,1000)==0\n close(\"all\")\n U = run(sess, u, Dict(x0=>reshape(X, length(X),1), t0=>reshape(Y, length(Y),1)));\n pcolor(X,Y,reshape(U,200,200))\n colorbar()\n xlabel(\"x\")\n ylabel(\"t\")\n savefig(\"xt.png\")\n end\n push!(Los, los)\n println(\"$i: $los\")\nend",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![title](xt.png)"
}
],
"metadata": {
"gist": {
"id": "b341b96809a21d41a7d7968e4ee9ec02",
"data": {
"description": "viscous_burgers.ipynb",
"public": true
}
},
"kernelspec": {
"name": "julia-1.0",
"display_name": "Julia 1.0.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.0.2"
},
"_draft": {
"nbviewer_url": "https://gist.github.com/b341b96809a21d41a7d7968e4ee9ec02"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment