{"action":"created","comment":{"url":"https://api.github.com/repos/opesci/propagator/pulls/comments/36742444","id":36742444,"diff_hunk":"@@ -0,0 +1,1040 @@\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+    \"\"\"\n+    MyCPrinter extends sympy.printing.ccode.CCodePrinter\n+    \"\"\"\n+    def __init__(self, settings={}):\n+        CCodePrinter.__init__(self, settings)\n+\n+    def _print_Indexed(self, expr):\n+        \"\"\"\n+        override method in CCodePrinter\n+        Print field as C style multidimensional array\n+        e.g. U[t,x,y,z] -> U[t][x][y][z]\n+        \"\"\"\n+        output = self._print(expr.base.label) + ''.join(['[' + self._print(x) + ']' for x in expr.indices])\n+        return output\n+\n+    def _print_Rational(self, expr):\n+        \"\"\"\n+        override method in CCodePrinter\n+        print fractional number as float/float\n+        (default was long double/long double)\n+        \"\"\"\n+        p, q = int(expr.p), int(expr.q)\n+        return '%d.0F/%d.0F' % (p, q) # float precision by default\n+\n+    def _print_Mod(self, expr):\n+        \"\"\"\n+        override method in CCodePrinter\n+        print mod using % operator in C++\n+        \"\"\"\n+        args = map(ccode, expr.args)\n+        args = ['('+x+')' for x in args]\n+        result = '%'.join(args)\n+        return result\n+\n+def ccode(expr, **settings):\n+    \"\"\"\n+    generate C++ code from expression expr\n+    calling MyCPrinter class\n+    \"\"\"\n+    return MyCPrinter(settings).doprint(expr, None)\n+\n+def ccode_eq(eq, **settings):\n+    \"\"\"\n+    genereate C++ assignment from equation eq\n+    assigning RHS to LHS\n+    \"\"\"\n+    return MyCPrinter(settings).doprint(eq.lhs, None) + ' = ' + MyCPrinter(settings).doprint(eq.rhs, None)\n+\n+def render(tmpl, dict1):\n+    \"\"\"\n+    render Mako template using dictionary dict1\n+    returns the result as string\n+    \"\"\"\n+    buf = StringIO()\n+    ctx = Context(buf, **dict1)\n+    tmpl.render_context(ctx)\n+    return buf.getvalue()\n+\n+def IndexedBases(s):\n+    \"\"\"\n+    declare multiple IndexedBase objects\n+    param s: string of names seperated by white space\n+    returns IndxedBase objects as tuple\n+    \"\"\"\n+    l = s.split();\n+    bases = [IndexedBase(x) for x in l]\n+    return tuple(bases)\n+\n+def tc(dx, n):\n+    \"\"\"\n+    return coefficient of power n term in Taylor series expansion\n+    param n: power\n+    param dx: distance from expansion reference\n+    \"\"\"\n+    return (dx**n)/factorial(n)\n+    \n+def Taylor(dx, n):\n+    \"\"\"\n+    create 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 list of values at neighbour grid point [.. f(x-dx), f(x), f(x+dx) ..]\n+    param dx: spacing between grid points\n+    param n: length of D and R (i.e. number of derivatives to calculate)\n+    returns Maxtrix object M\n+    \"\"\"\n+    l = []\n+    for i in range(-n+1,n):\n+        ll = [tc((i*dx),j) for j in range(2*n-1)]\n+        l.append(ll)\n+    return Matrix(l)\n+\n+def Taylor_half(dx, n):\n+    \"\"\"\n+    similar function as Taylor() for staggered grids\n+    create 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 list of values at neighbour grid point [.. f(x-3/2*dx), f(x-dx/2), f(x), f(x+dx/2), f(x+3/2*dx) ..]\n+    param dx: spacing between grid points\n+    param n: length of D and R (i.e. number of derivatives to calculate)\n+    returns Maxtrix object M\n+    \"\"\"\n+    l = []\n+    for i in range(-n*2+1,n*2,2):\n+        ll = [tc((i*dx/2),j) for j in range(2*n)]\n+        l.append(ll)\n+    return Matrix(l)\n+\n+def Deriv(U, i, k, d, n):\n+    \"\"\"\n+    calculate the FD approximation for nth derivative\n+    the function works by using M * D = R, as specified in Taylor(), then inverting M, so that D = M_inverse * R\n+    param U: the field\n+    param i: list of indices (Symbol objects) of field U, e.g. possibley [t,x,y,z] for 3D\n+    param k: determine the derivative is with respect to which dimension, e.g. dU/dt if k=0, dU/dx if k=0, assuming i=[t,x,y,z]\n+    param d: spacing of the grid, e.g. dx, dt\n+    param n: accuracy of approximation, e.g. n=2 for 2nd-order FD approximation\n+    returns D=list of expressions for FD approxiation. D[1] for first derivative, D[2] for 2nd derivative etc\n+    raises NotImplementedError exception if dimension is more than 4 (1 time and 3 space dimensions)\n+    \"\"\"\n+    M = Taylor(d, n)\n+    s = [0]*len(i)\n+    s[k] = 1 # switch on kth dimension\n+    # generate matrix of RHS, i.e. [ ... U[x-1], U[x], U[x+1] ... ]\n+    if len(i)==1:\n+        RX = Matrix([U[i[0]+s[0]*x] for x in range(-n+1,n)])\n+    elif len(i)==2:\n+        RX = Matrix([U[i[0]+s[0]*x,i[1]+s[1]*x] for x in range(-n+1,n)])\n+    elif len(i)==3:\n+        RX = 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+    elif len(i)==4:\n+        RX = 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+    else:\n+        raise NotImplementedError(\">4 dimensions, need to fix\")\n+\n+    return M.inv() * RX\n+\n+def Deriv_half(U, i, k, d, n):\n+    \"\"\"\n+    similar function as Deriv() for staggered grids\n+    calculate the FD approximation for nth derivative\n+    the function works by using M * D = R, as specified in Taylor(), then inverting M, so that D = M_inverse * R\n+    param U: the field\n+    param i: list of indices (Symbol objects) of field U, e.g. possibley [t,x,y,z] for 3D\n+    param k: determine the derivative is with respect to which dimension, e.g. dU/dt if k=0, dU/dx if k=0, assuming i=[t,x,y,z]\n+    param d: spacing of the grid, e.g. dx, dt\n+    param n: accuracy of approximation, e.g. n=1 for 2nd-order FD approximation, n=2 for 4th-order FD approximation\n+    returns D=list of expressions for FD approxiation. D[1] for first derivative, D[2] for 2nd derivative etc\n+    raises NotImplementedError exception if dimension is more than 4 (1 time and 3 space dimensions)\n+    \"\"\"\n+    M = Taylor_half(d, n)\n+    s = [0]*len(i)\n+    s[k] = 1 # switch on kth dimension\n+    # generate matrix of RHS, i.e. [ ... U[x-1], U[x], U[x+1] ... ]\n+    if len(i)==1:\n+        RX = Matrix([U[i[0]+s[0]*x*hf] for x in range(-n*2+1,n*2,2)])\n+    elif len(i)==2:\n+        RX = 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+    elif len(i)==3:\n+        RX = 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+    elif len(i)==4:\n+        RX = 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+    else:\n+        raise NotImplementedError(\">4 dimensions, need to fix\")\n+\n+    result = M.inv() * RX\n+    return result\n+\n+def is_half(expr):\n+    \"\"\"\n+    test if constants in an expression values to 1/2, 3/2 ...","path":"codegen/grid.py","position":182,"original_position":182,"commit_id":"e472361cdbe0c1994a765500eecb620b583ad72e","original_commit_id":"e472361cdbe0c1994a765500eecb620b583ad72e","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":"that's right.. amended to:\r\ntest if constants (non symbols) in an expression is integer+1/2 or integer (indices on staggered grid are either integer or integer+1/2)","created_at":"2015-08-11T13:13:44Z","updated_at":"2015-08-11T13:13:44Z","html_url":"https://github.com/opesci/propagator/pull/17#discussion_r36742444","pull_request_url":"https://api.github.com/repos/opesci/propagator/pulls/17","_links":{"self":{"href":"https://api.github.com/repos/opesci/propagator/pulls/comments/36742444"},"html":{"href":"https://github.com/opesci/propagator/pull/17#discussion_r36742444"},"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-11T13:13:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6bc7b4246842b9cbc93dcc7b634c3b766ad4ce69","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/e472361cdbe0c1994a765500eecb620b583ad72e","head":{"label":"opesci:codegen","ref":"codegen","sha":"e472361cdbe0c1994a765500eecb620b583ad72e","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-10T17:12:13Z","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":1620,"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-10T17:12:13Z","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":1620,"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/e472361cdbe0c1994a765500eecb620b583ad72e"}}}}