{"action":"created","comment":{"url":"https://api.github.com/repos/opesci/propagator/pulls/comments/36571522","id":36571522,"diff_hunk":"@@ -0,0 +1,665 @@\n+from sympy import Symbol, symbols, factorial, Matrix, Rational, Indexed, simplify, IndexedBase, solve, Eq\n+from mako.template import Template\n+from mako.lookup import TemplateLookup\n+from mako.runtime import Context\n+from StringIO import StringIO\n+\n+from sympy.printing.ccode import CCodePrinter\n+\n+hf = Rational(1,2) # 1/2\n+\n+\n+class MyCPrinter(CCodePrinter):\n+\tdef __init__(self, settings={}):\n+\t\tCCodePrinter.__init__(self, settings)\n+\n+\tdef _print_Indexed(self, expr):\n+\t\t# Array base and append indices\n+\t\toutput = self._print(expr.base.label) + ''.join(['[' + self._print(x) + ']' for x in expr.indices])\n+\t\treturn output\n+\n+\tdef _print_Rational(self, expr):\n+\t\tp, q = int(expr.p), int(expr.q)\n+\t\treturn '%d.0F/%d.0F' % (p, q) # float precision by default\n+\n+\tdef _print_Mod(self, expr):\n+\t\t# print mod as % in C++\n+\t\targs = map(ccode, expr.args)\n+\t\targs = ['('+x+')' for x in args]\n+\t\tresult = '%'.join(args)\n+\t\treturn result\n+\n+def ccode(expr, **settings):\n+\treturn MyCPrinter(settings).doprint(expr, None)\n+\n+def ccode_eq(eq, **settings):\n+\t# print assignment code from equation\n+\treturn MyCPrinter(settings).doprint(eq.lhs, None) + ' = ' + MyCPrinter(settings).doprint(eq.rhs, None)\n+\n+def render(tmpl, dict1):\n+\tbuf = StringIO()\n+\tctx = Context(buf, **dict1)\n+\ttmpl.render_context(ctx)\n+\treturn buf.getvalue()\n+\n+def IndexedBases(s):\n+\tl = s.split();\n+\tbases = [IndexedBase(x) for x in l]\n+\treturn tuple(bases)\n+\n+def tc(dx, n):\n+    # return coefficient of power n Taylor series term\n+    return (dx**n)/factorial(n)\n+\n+def Taylor(dx, n):\n+    # return Matrix of Taylor Coefficients M, such that M * D = R\n+    # where D is list of derivatives at x [f, f', f'' ..]\n+    # R is value at neighbour grid point [.. f(x-dx), f(x), f(x+dx) ..]\n+    l = []\n+    for i in range(-n+1,n):\n+    \tll = [tc((i*dx),j) for j in range(2*n-1)]\n+    \tl.append(ll)\n+    return Matrix(l)\n+\n+def Taylor_half(dx, n):\n+    # return Matrix of Taylor Coefficients M, such that M * D = R\n+    # where D is list of derivatives at x [f, f', f'' ..]\n+    # R is value at neighbour grid point [.. f(x-dx/2), f(x), f(x+dx/2), f(x+3/2*dx) ..]\n+    l = []\n+    for i in range(-n*2+1,n*2,2):\n+    \tll = [tc((i*dx/2),j) for j in range(2*n)]\n+    \tl.append(ll)\n+    return Matrix(l)\n+\n+def Deriv(U, i, k, d, n):\n+\t# get the FD approximation for nth derivative in terms of grid U\n+\t# i is list of indices of U, e.g. [x,y,z,t] for 3D\n+\t# k = which dimension to expand, k=0 for x, k=1 for t etc\n+\tM = Taylor(d, n)\n+\ts = [0]*len(i)\n+\ts[k] = 1 # switch on kth dimension\n+\t# generate matrix of RHS, i.e. [ ... U[x-1], U[x], U[x+1] ... ]\n+\tif len(i)==1:\n+\t\tRX = Matrix([U[i[0]+s[0]*x] for x in range(-n+1,n)])\n+\telif len(i)==2:\n+\t\tRX = Matrix([U[i[0]+s[0]*x,i[1]+s[1]*x] for x in range(-n+1,n)])\n+\telif len(i)==3:\n+\t\tRX = Matrix([U[i[0]+s[0]*x,i[1]+s[1]*x,i[2]+s[2]*x] for x in range(-n+1,n)])\n+\telif len(i)==4:\n+\t\tRX = Matrix([U[i[0]+s[0]*x,i[1]+s[1]*x,i[2]+s[2]*x,i[3]+s[3]*x] for x in range(-n+1,n)])\n+\telse:\n+\t\traise NotImplementedError(\">4 dimensions, need to fix\")\n+\n+\treturn M.inv() * RX\n+\n+def Deriv_half(U, i, k, d, n):\n+\t# get the FD approximation for nth derivative in terms of grid U\n+\t# i is list of indices of U, e.g. [x,y,z,t] for 3D\n+\t# k = which dimension to expand, k=0 for x, k=1 for t etc\n+\tM = Taylor_half(d, n)\n+\ts = [0]*len(i)\n+\ts[k] = 1 # switch on kth dimension\n+\t# generate matrix of RHS, i.e. [ ... U[x-1], U[x], U[x+1] ... ]\n+\tif len(i)==1:\n+\t\tRX = Matrix([U[i[0]+s[0]*x*hf] for x in range(-n*2+1,n*2,2)])\n+\telif len(i)==2:\n+\t\tRX = Matrix([U[i[0]+s[0]*x*hf,i[1]+s[1]*x*hf] for x in range(-n*2+1,n*2,2)])\n+\telif len(i)==3:\n+\t\tRX = Matrix([U[i[0]+s[0]*x*hf,i[1]+s[1]*x*hf,i[2]+s[2]*x*hf] for x in range(-n*2+1,n*2,2)])\n+\telif len(i)==4:\n+\t\tRX = Matrix([U[i[0]+s[0]*x*hf,i[1]+s[1]*x*hf,i[2]+s[2]*x*hf,i[3]+s[3]*x*hf] for x in range(-n*2+1,n*2,2)])\n+\telse:\n+\t\traise NotImplementedError(\">4 dimensions, need to fix\")\n+\n+\tresult = M.inv() * RX\n+\treturn result\n+\n+def is_half(expr):\n+\td = {x:0 for x in expr.free_symbols}\n+\treturn not expr.subs(d).is_Integer\n+\n+def shift_grid(expr):\n+\tif expr.is_Symbol:","path":"codegen/grid.py","position":122,"original_position":122,"commit_id":"10a30f0eea90e404e32b09691b466bc035db8592","original_commit_id":"10a30f0eea90e404e32b09691b466bc035db8592","user":{"login":"tj-sun","id":9328006,"avatar_url":"https://avatars.githubusercontent.com/u/9328006?v=3","gravatar_id":"","url":"https://api.github.com/users/tj-sun","html_url":"https://github.com/tj-sun","followers_url":"https://api.github.com/users/tj-sun/followers","following_url":"https://api.github.com/users/tj-sun/following{/other_user}","gists_url":"https://api.github.com/users/tj-sun/gists{/gist_id}","starred_url":"https://api.github.com/users/tj-sun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tj-sun/subscriptions","organizations_url":"https://api.github.com/users/tj-sun/orgs","repos_url":"https://api.github.com/users/tj-sun/repos","events_url":"https://api.github.com/users/tj-sun/events{/privacy}","received_events_url":"https://api.github.com/users/tj-sun/received_events","type":"User","site_admin":false},"body":"done","created_at":"2015-08-08T00:07:55Z","updated_at":"2015-08-08T00:07:55Z","html_url":"https://github.com/opesci/propagator/pull/17#discussion_r36571522","pull_request_url":"https://api.github.com/repos/opesci/propagator/pulls/17","_links":{"self":{"href":"https://api.github.com/repos/opesci/propagator/pulls/comments/36571522"},"html":{"href":"https://github.com/opesci/propagator/pull/17#discussion_r36571522"},"pull_request":{"href":"https://api.github.com/repos/opesci/propagator/pulls/17"}}},"pull_request":{"url":"https://api.github.com/repos/opesci/propagator/pulls/17","id":41682663,"html_url":"https://github.com/opesci/propagator/pull/17","diff_url":"https://github.com/opesci/propagator/pull/17.diff","patch_url":"https://github.com/opesci/propagator/pull/17.patch","issue_url":"https://api.github.com/repos/opesci/propagator/issues/17","number":17,"state":"open","locked":false,"title":"codegen branch","user":{"login":"tj-sun","id":9328006,"avatar_url":"https://avatars.githubusercontent.com/u/9328006?v=3","gravatar_id":"","url":"https://api.github.com/users/tj-sun","html_url":"https://github.com/tj-sun","followers_url":"https://api.github.com/users/tj-sun/followers","following_url":"https://api.github.com/users/tj-sun/following{/other_user}","gists_url":"https://api.github.com/users/tj-sun/gists{/gist_id}","starred_url":"https://api.github.com/users/tj-sun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tj-sun/subscriptions","organizations_url":"https://api.github.com/users/tj-sun/orgs","repos_url":"https://api.github.com/users/tj-sun/repos","events_url":"https://api.github.com/users/tj-sun/events{/privacy}","received_events_url":"https://api.github.com/users/tj-sun/received_events","type":"User","site_admin":false},"body":"Please refer to README in codegen","created_at":"2015-08-05T14:09:12Z","updated_at":"2015-08-08T00:07:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1321162c4563b4f9e579bfe2d74dcfcd246ceefa","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/opesci/propagator/pulls/17/commits","review_comments_url":"https://api.github.com/repos/opesci/propagator/pulls/17/comments","review_comment_url":"https://api.github.com/repos/opesci/propagator/pulls/comments{/number}","comments_url":"https://api.github.com/repos/opesci/propagator/issues/17/comments","statuses_url":"https://api.github.com/repos/opesci/propagator/statuses/10a30f0eea90e404e32b09691b466bc035db8592","head":{"label":"opesci:codegen","ref":"codegen","sha":"10a30f0eea90e404e32b09691b466bc035db8592","user":{"login":"opesci","id":10018551,"avatar_url":"https://avatars.githubusercontent.com/u/10018551?v=3","gravatar_id":"","url":"https://api.github.com/users/opesci","html_url":"https://github.com/opesci","followers_url":"https://api.github.com/users/opesci/followers","following_url":"https://api.github.com/users/opesci/following{/other_user}","gists_url":"https://api.github.com/users/opesci/gists{/gist_id}","starred_url":"https://api.github.com/users/opesci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opesci/subscriptions","organizations_url":"https://api.github.com/users/opesci/orgs","repos_url":"https://api.github.com/users/opesci/repos","events_url":"https://api.github.com/users/opesci/events{/privacy}","received_events_url":"https://api.github.com/users/opesci/received_events","type":"Organization","site_admin":false},"repo":{"id":32512811,"name":"propagator","full_name":"opesci/propagator","owner":{"login":"opesci","id":10018551,"avatar_url":"https://avatars.githubusercontent.com/u/10018551?v=3","gravatar_id":"","url":"https://api.github.com/users/opesci","html_url":"https://github.com/opesci","followers_url":"https://api.github.com/users/opesci/followers","following_url":"https://api.github.com/users/opesci/following{/other_user}","gists_url":"https://api.github.com/users/opesci/gists{/gist_id}","starred_url":"https://api.github.com/users/opesci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opesci/subscriptions","organizations_url":"https://api.github.com/users/opesci/orgs","repos_url":"https://api.github.com/users/opesci/repos","events_url":"https://api.github.com/users/opesci/events{/privacy}","received_events_url":"https://api.github.com/users/opesci/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/opesci/propagator","description":"Solvers for 3D wave equation","fork":false,"url":"https://api.github.com/repos/opesci/propagator","forks_url":"https://api.github.com/repos/opesci/propagator/forks","keys_url":"https://api.github.com/repos/opesci/propagator/keys{/key_id}","collaborators_url":"https://api.github.com/repos/opesci/propagator/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/opesci/propagator/teams","hooks_url":"https://api.github.com/repos/opesci/propagator/hooks","issue_events_url":"https://api.github.com/repos/opesci/propagator/issues/events{/number}","events_url":"https://api.github.com/repos/opesci/propagator/events","assignees_url":"https://api.github.com/repos/opesci/propagator/assignees{/user}","branches_url":"https://api.github.com/repos/opesci/propagator/branches{/branch}","tags_url":"https://api.github.com/repos/opesci/propagator/tags","blobs_url":"https://api.github.com/repos/opesci/propagator/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/opesci/propagator/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/opesci/propagator/git/refs{/sha}","trees_url":"https://api.github.com/repos/opesci/propagator/git/trees{/sha}","statuses_url":"https://api.github.com/repos/opesci/propagator/statuses/{sha}","languages_url":"https://api.github.com/repos/opesci/propagator/languages","stargazers_url":"https://api.github.com/repos/opesci/propagator/stargazers","contributors_url":"https://api.github.com/repos/opesci/propagator/contributors","subscribers_url":"https://api.github.com/repos/opesci/propagator/subscribers","subscription_url":"https://api.github.com/repos/opesci/propagator/subscription","commits_url":"https://api.github.com/repos/opesci/propagator/commits{/sha}","git_commits_url":"https://api.github.com/repos/opesci/propagator/git/commits{/sha}","comments_url":"https://api.github.com/repos/opesci/propagator/comments{/number}","issue_comment_url":"https://api.github.com/repos/opesci/propagator/issues/comments{/number}","contents_url":"https://api.github.com/repos/opesci/propagator/contents/{+path}","compare_url":"https://api.github.com/repos/opesci/propagator/compare/{base}...{head}","merges_url":"https://api.github.com/repos/opesci/propagator/merges","archive_url":"https://api.github.com/repos/opesci/propagator/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/opesci/propagator/downloads","issues_url":"https://api.github.com/repos/opesci/propagator/issues{/number}","pulls_url":"https://api.github.com/repos/opesci/propagator/pulls{/number}","milestones_url":"https://api.github.com/repos/opesci/propagator/milestones{/number}","notifications_url":"https://api.github.com/repos/opesci/propagator/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/opesci/propagator/labels{/name}","releases_url":"https://api.github.com/repos/opesci/propagator/releases{/id}","created_at":"2015-03-19T09:39:45Z","updated_at":"2015-05-05T12:52:38Z","pushed_at":"2015-08-07T17:46:49Z","git_url":"git://github.com/opesci/propagator.git","ssh_url":"git@github.com:opesci/propagator.git","clone_url":"https://github.com/opesci/propagator.git","svn_url":"https://github.com/opesci/propagator","homepage":null,"size":1446,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":9,"forks":3,"open_issues":9,"watchers":0,"default_branch":"master"}},"base":{"label":"opesci:master","ref":"master","sha":"589afd4dc85d28af6172eb7e2af5b6cc80babf35","user":{"login":"opesci","id":10018551,"avatar_url":"https://avatars.githubusercontent.com/u/10018551?v=3","gravatar_id":"","url":"https://api.github.com/users/opesci","html_url":"https://github.com/opesci","followers_url":"https://api.github.com/users/opesci/followers","following_url":"https://api.github.com/users/opesci/following{/other_user}","gists_url":"https://api.github.com/users/opesci/gists{/gist_id}","starred_url":"https://api.github.com/users/opesci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opesci/subscriptions","organizations_url":"https://api.github.com/users/opesci/orgs","repos_url":"https://api.github.com/users/opesci/repos","events_url":"https://api.github.com/users/opesci/events{/privacy}","received_events_url":"https://api.github.com/users/opesci/received_events","type":"Organization","site_admin":false},"repo":{"id":32512811,"name":"propagator","full_name":"opesci/propagator","owner":{"login":"opesci","id":10018551,"avatar_url":"https://avatars.githubusercontent.com/u/10018551?v=3","gravatar_id":"","url":"https://api.github.com/users/opesci","html_url":"https://github.com/opesci","followers_url":"https://api.github.com/users/opesci/followers","following_url":"https://api.github.com/users/opesci/following{/other_user}","gists_url":"https://api.github.com/users/opesci/gists{/gist_id}","starred_url":"https://api.github.com/users/opesci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opesci/subscriptions","organizations_url":"https://api.github.com/users/opesci/orgs","repos_url":"https://api.github.com/users/opesci/repos","events_url":"https://api.github.com/users/opesci/events{/privacy}","received_events_url":"https://api.github.com/users/opesci/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/opesci/propagator","description":"Solvers for 3D wave equation","fork":false,"url":"https://api.github.com/repos/opesci/propagator","forks_url":"https://api.github.com/repos/opesci/propagator/forks","keys_url":"https://api.github.com/repos/opesci/propagator/keys{/key_id}","collaborators_url":"https://api.github.com/repos/opesci/propagator/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/opesci/propagator/teams","hooks_url":"https://api.github.com/repos/opesci/propagator/hooks","issue_events_url":"https://api.github.com/repos/opesci/propagator/issues/events{/number}","events_url":"https://api.github.com/repos/opesci/propagator/events","assignees_url":"https://api.github.com/repos/opesci/propagator/assignees{/user}","branches_url":"https://api.github.com/repos/opesci/propagator/branches{/branch}","tags_url":"https://api.github.com/repos/opesci/propagator/tags","blobs_url":"https://api.github.com/repos/opesci/propagator/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/opesci/propagator/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/opesci/propagator/git/refs{/sha}","trees_url":"https://api.github.com/repos/opesci/propagator/git/trees{/sha}","statuses_url":"https://api.github.com/repos/opesci/propagator/statuses/{sha}","languages_url":"https://api.github.com/repos/opesci/propagator/languages","stargazers_url":"https://api.github.com/repos/opesci/propagator/stargazers","contributors_url":"https://api.github.com/repos/opesci/propagator/contributors","subscribers_url":"https://api.github.com/repos/opesci/propagator/subscribers","subscription_url":"https://api.github.com/repos/opesci/propagator/subscription","commits_url":"https://api.github.com/repos/opesci/propagator/commits{/sha}","git_commits_url":"https://api.github.com/repos/opesci/propagator/git/commits{/sha}","comments_url":"https://api.github.com/repos/opesci/propagator/comments{/number}","issue_comment_url":"https://api.github.com/repos/opesci/propagator/issues/comments{/number}","contents_url":"https://api.github.com/repos/opesci/propagator/contents/{+path}","compare_url":"https://api.github.com/repos/opesci/propagator/compare/{base}...{head}","merges_url":"https://api.github.com/repos/opesci/propagator/merges","archive_url":"https://api.github.com/repos/opesci/propagator/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/opesci/propagator/downloads","issues_url":"https://api.github.com/repos/opesci/propagator/issues{/number}","pulls_url":"https://api.github.com/repos/opesci/propagator/pulls{/number}","milestones_url":"https://api.github.com/repos/opesci/propagator/milestones{/number}","notifications_url":"https://api.github.com/repos/opesci/propagator/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/opesci/propagator/labels{/name}","releases_url":"https://api.github.com/repos/opesci/propagator/releases{/id}","created_at":"2015-03-19T09:39:45Z","updated_at":"2015-05-05T12:52:38Z","pushed_at":"2015-08-07T17:46:49Z","git_url":"git://github.com/opesci/propagator.git","ssh_url":"git@github.com:opesci/propagator.git","clone_url":"https://github.com/opesci/propagator.git","svn_url":"https://github.com/opesci/propagator","homepage":null,"size":1446,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":9,"forks":3,"open_issues":9,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/opesci/propagator/pulls/17"},"html":{"href":"https://github.com/opesci/propagator/pull/17"},"issue":{"href":"https://api.github.com/repos/opesci/propagator/issues/17"},"comments":{"href":"https://api.github.com/repos/opesci/propagator/issues/17/comments"},"review_comments":{"href":"https://api.github.com/repos/opesci/propagator/pulls/17/comments"},"review_comment":{"href":"https://api.github.com/repos/opesci/propagator/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/opesci/propagator/pulls/17/commits"},"statuses":{"href":"https://api.github.com/repos/opesci/propagator/statuses/10a30f0eea90e404e32b09691b466bc035db8592"}}}}