{"action":"created","comment":{"url":"https://api.github.com/repos/mit-crpg/openmc/pulls/comments/50693229","id":50693229,"diff_hunk":"@@ -2982,6 +2983,52 @@ subroutine read_tallies_xml()\n       end if\n \n       ! =======================================================================\n+      ! READ DATA FOR DERIVATIVES\n+\n+      if (check_for_node(node_tal, \"derivative\")) then\n+        call get_node_ptr(node_tal, \"derivative\", node_deriv)\n+        allocate(t % deriv)\n+\n+        temp_str = \"\"\n+        call get_node_value(node_deriv, \"variable\", temp_str)\n+        temp_str = to_lower(temp_str)\n+\n+        select case(temp_str)\n+\n+        case(\"density\")\n+          t % deriv % variable = DIFF_DENSITY\n+          call get_node_value(node_deriv, \"material\", t % deriv % diff_material)\n+\n+        case(\"nuclide_density\")\n+          t % deriv % variable = DIFF_NUCLIDE_DENSITY\n+          call get_node_value(node_deriv, \"material\", t % deriv % diff_material)\n+\n+          call get_node_value(node_deriv, \"nuclide\", word)\n+          word = trim(to_lower(word))\n+          pair_list => nuclide_dict % keys()\n+          do while (associated(pair_list))\n+            if (starts_with(pair_list % key, word)) then\n+              word = pair_list % key(1:150)\n+              exit\n+            end if\n+\n+            ! Advance to next\n+            pair_list => pair_list % next\n+          end do\n+\n+          ! Check if no nuclide was found\n+          if (.not. associated(pair_list)) then\n+            call fatal_error(\"Could not find the nuclide \" &\n+                 &// trim(word) // \" specified in tally \" &\n+                 &// trim(to_str(t % id)) // \" in any material.\")","path":"src/input_xml.F90","position":58,"original_position":58,"commit_id":"5634993bf9447b4fe98e2f634d206da0ee4dedf4","original_commit_id":"5634993bf9447b4fe98e2f634d206da0ee4dedf4","user":{"login":"paulromano","id":743095,"avatar_url":"https://avatars.githubusercontent.com/u/743095?v=3","gravatar_id":"","url":"https://api.github.com/users/paulromano","html_url":"https://github.com/paulromano","followers_url":"https://api.github.com/users/paulromano/followers","following_url":"https://api.github.com/users/paulromano/following{/other_user}","gists_url":"https://api.github.com/users/paulromano/gists{/gist_id}","starred_url":"https://api.github.com/users/paulromano/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulromano/subscriptions","organizations_url":"https://api.github.com/users/paulromano/orgs","repos_url":"https://api.github.com/users/paulromano/repos","events_url":"https://api.github.com/users/paulromano/events{/privacy}","received_events_url":"https://api.github.com/users/paulromano/received_events","type":"User","site_admin":false},"body":"leading ampersands are unnecessary","created_at":"2016-01-25T13:34:25Z","updated_at":"2016-01-25T13:34:25Z","html_url":"https://github.com/mit-crpg/openmc/pull/572#discussion_r50693229","pull_request_url":"https://api.github.com/repos/mit-crpg/openmc/pulls/572","_links":{"self":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/comments/50693229"},"html":{"href":"https://github.com/mit-crpg/openmc/pull/572#discussion_r50693229"},"pull_request":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/572"}}},"pull_request":{"url":"https://api.github.com/repos/mit-crpg/openmc/pulls/572","id":56995081,"html_url":"https://github.com/mit-crpg/openmc/pull/572","diff_url":"https://github.com/mit-crpg/openmc/pull/572.diff","patch_url":"https://github.com/mit-crpg/openmc/pull/572.patch","issue_url":"https://api.github.com/repos/mit-crpg/openmc/issues/572","number":572,"state":"open","locked":false,"title":"Differential tallies","user":{"login":"smharper","id":4622797,"avatar_url":"https://avatars.githubusercontent.com/u/4622797?v=3","gravatar_id":"","url":"https://api.github.com/users/smharper","html_url":"https://github.com/smharper","followers_url":"https://api.github.com/users/smharper/followers","following_url":"https://api.github.com/users/smharper/following{/other_user}","gists_url":"https://api.github.com/users/smharper/gists{/gist_id}","starred_url":"https://api.github.com/users/smharper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smharper/subscriptions","organizations_url":"https://api.github.com/users/smharper/orgs","repos_url":"https://api.github.com/users/smharper/repos","events_url":"https://api.github.com/users/smharper/events{/privacy}","received_events_url":"https://api.github.com/users/smharper/received_events","type":"User","site_admin":false},"body":"Time to present my labor of love for the last year: differential tallies.\r\n\r\nIt turns out that you can propagate a Taylor series through a Monte Carlo calculation and get derivative quantities.  And it seems to work pretty well, too.  This method isn't novel.  MCNP uses it to get the results for the PERT card.\r\n\r\nThis implementation is capable of computing first order derivatives with respect to a material density or with respect to the density of a specific nuclide in a material.  The analog estimator should work with any type of score.  I've implemented collision estimators for a few important scores.  I haven't implemented tracklength tallies at all.\r\n\r\n### Data IO\r\nThe input format looks like this in Python:\r\n```python\r\ntally.diff_variable = 'nuclide_density'\r\ntally.diff_material = 1\r\ntally.diff_nuclide = 'Xe-135'\r\n```\r\nor this in XML\r\n```xml\r\n<tally id=\"1\">\r\n  <scores>flux</scores>\r\n  <derivative variable=\"nuclide_density\" material=\"1\" nuclide=\"Xe-135\" />\r\n</tally>\r\n```\r\n\r\nEntries in the tallies.out file will have a line describing the derivative, like this\r\n```\r\n ==========================>     TALLY 10000     <==========================\r\n Nuclide density derivative  Material 1  Nuclide Xe-135.71c\r\n Total Material\r\n   Flux                                 -3.57784       +/- 7.77174\r\n```\r\n\r\nPandas dataframes get columns with derivative information.\r\n\r\n### Notes on the implementation\r\nDifferential tallies need to keep track of the derivative of the particle's flux (weight) so there are three new subroutine calls in `transport`.  First is `zero_flux_derivs` which initializes the derivatives to zero for new particles.  Second, after every tracking event there is a call to `score_track_derivative`.  And third, there is a call to `score_collision_derivative` after every collision.\r\n\r\nTally.F90 is where most of the action is in this PR, but I've tried to make diff tallies minimally invasive.  All the derivative logic is rolled up in the `apply_derivative_to_score` subroutine which tweaks the score before it's stuck in the `results` array.\r\n\r\nI put a lot of work into the test suite so it covers every new line in tally.F90 except for a `fatal_error` call.\r\n\r\n### Results teaser\r\nI used this branch to predict changes in cross sections in a Xe transient and got pretty good results.  This plot shows a fuel thermal absorption cross section as Xe-135 density increases across 5 orders of magnitude.  The blue shows the right answer, the green shows what you would expect from a constant Xe microscopic cross section, and the red shows the prediction with differential tallies.  (Alas my results are much less impressive for the off-diagonals on the scattering matrix thanks to the tyranny of analog estimators, but such is science.)\r\n![xs3_fuel_absorption](https://cloud.githubusercontent.com/assets/4622797/12534512/1b57e754-c22a-11e5-8953-cb521464aa9a.png)\r\n","created_at":"2016-01-24T04:45:21Z","updated_at":"2016-01-25T13:34:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5e3db58e8e5f9ea2fcb78ad7bd83665eda38bca1","assignee":null,"milestone":{"url":"https://api.github.com/repos/mit-crpg/openmc/milestones/14","html_url":"https://github.com/mit-crpg/openmc/milestones/v0.8","labels_url":"https://api.github.com/repos/mit-crpg/openmc/milestones/14/labels","id":1241750,"number":14,"title":"v0.8","description":"","creator":{"login":"paulromano","id":743095,"avatar_url":"https://avatars.githubusercontent.com/u/743095?v=3","gravatar_id":"","url":"https://api.github.com/users/paulromano","html_url":"https://github.com/paulromano","followers_url":"https://api.github.com/users/paulromano/followers","following_url":"https://api.github.com/users/paulromano/following{/other_user}","gists_url":"https://api.github.com/users/paulromano/gists{/gist_id}","starred_url":"https://api.github.com/users/paulromano/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulromano/subscriptions","organizations_url":"https://api.github.com/users/paulromano/orgs","repos_url":"https://api.github.com/users/paulromano/repos","events_url":"https://api.github.com/users/paulromano/events{/privacy}","received_events_url":"https://api.github.com/users/paulromano/received_events","type":"User","site_admin":false},"open_issues":8,"closed_issues":13,"state":"open","created_at":"2015-08-05T10:39:23Z","updated_at":"2016-01-24T04:45:21Z","due_on":"2016-04-30T05:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/mit-crpg/openmc/pulls/572/commits","review_comments_url":"https://api.github.com/repos/mit-crpg/openmc/pulls/572/comments","review_comment_url":"https://api.github.com/repos/mit-crpg/openmc/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mit-crpg/openmc/issues/572/comments","statuses_url":"https://api.github.com/repos/mit-crpg/openmc/statuses/5634993bf9447b4fe98e2f634d206da0ee4dedf4","head":{"label":"smharper:diff_tally3","ref":"diff_tally3","sha":"5634993bf9447b4fe98e2f634d206da0ee4dedf4","user":{"login":"smharper","id":4622797,"avatar_url":"https://avatars.githubusercontent.com/u/4622797?v=3","gravatar_id":"","url":"https://api.github.com/users/smharper","html_url":"https://github.com/smharper","followers_url":"https://api.github.com/users/smharper/followers","following_url":"https://api.github.com/users/smharper/following{/other_user}","gists_url":"https://api.github.com/users/smharper/gists{/gist_id}","starred_url":"https://api.github.com/users/smharper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smharper/subscriptions","organizations_url":"https://api.github.com/users/smharper/orgs","repos_url":"https://api.github.com/users/smharper/repos","events_url":"https://api.github.com/users/smharper/events{/privacy}","received_events_url":"https://api.github.com/users/smharper/received_events","type":"User","site_admin":false},"repo":{"id":10507820,"name":"openmc","full_name":"smharper/openmc","owner":{"login":"smharper","id":4622797,"avatar_url":"https://avatars.githubusercontent.com/u/4622797?v=3","gravatar_id":"","url":"https://api.github.com/users/smharper","html_url":"https://github.com/smharper","followers_url":"https://api.github.com/users/smharper/followers","following_url":"https://api.github.com/users/smharper/following{/other_user}","gists_url":"https://api.github.com/users/smharper/gists{/gist_id}","starred_url":"https://api.github.com/users/smharper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smharper/subscriptions","organizations_url":"https://api.github.com/users/smharper/orgs","repos_url":"https://api.github.com/users/smharper/repos","events_url":"https://api.github.com/users/smharper/events{/privacy}","received_events_url":"https://api.github.com/users/smharper/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/smharper/openmc","description":"OpenMC Monte Carlo Code","fork":true,"url":"https://api.github.com/repos/smharper/openmc","forks_url":"https://api.github.com/repos/smharper/openmc/forks","keys_url":"https://api.github.com/repos/smharper/openmc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/smharper/openmc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/smharper/openmc/teams","hooks_url":"https://api.github.com/repos/smharper/openmc/hooks","issue_events_url":"https://api.github.com/repos/smharper/openmc/issues/events{/number}","events_url":"https://api.github.com/repos/smharper/openmc/events","assignees_url":"https://api.github.com/repos/smharper/openmc/assignees{/user}","branches_url":"https://api.github.com/repos/smharper/openmc/branches{/branch}","tags_url":"https://api.github.com/repos/smharper/openmc/tags","blobs_url":"https://api.github.com/repos/smharper/openmc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/smharper/openmc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/smharper/openmc/git/refs{/sha}","trees_url":"https://api.github.com/repos/smharper/openmc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/smharper/openmc/statuses/{sha}","languages_url":"https://api.github.com/repos/smharper/openmc/languages","stargazers_url":"https://api.github.com/repos/smharper/openmc/stargazers","contributors_url":"https://api.github.com/repos/smharper/openmc/contributors","subscribers_url":"https://api.github.com/repos/smharper/openmc/subscribers","subscription_url":"https://api.github.com/repos/smharper/openmc/subscription","commits_url":"https://api.github.com/repos/smharper/openmc/commits{/sha}","git_commits_url":"https://api.github.com/repos/smharper/openmc/git/commits{/sha}","comments_url":"https://api.github.com/repos/smharper/openmc/comments{/number}","issue_comment_url":"https://api.github.com/repos/smharper/openmc/issues/comments{/number}","contents_url":"https://api.github.com/repos/smharper/openmc/contents/{+path}","compare_url":"https://api.github.com/repos/smharper/openmc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/smharper/openmc/merges","archive_url":"https://api.github.com/repos/smharper/openmc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/smharper/openmc/downloads","issues_url":"https://api.github.com/repos/smharper/openmc/issues{/number}","pulls_url":"https://api.github.com/repos/smharper/openmc/pulls{/number}","milestones_url":"https://api.github.com/repos/smharper/openmc/milestones{/number}","notifications_url":"https://api.github.com/repos/smharper/openmc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/smharper/openmc/labels{/name}","releases_url":"https://api.github.com/repos/smharper/openmc/releases{/id}","deployments_url":"https://api.github.com/repos/smharper/openmc/deployments","created_at":"2013-06-05T16:55:01Z","updated_at":"2015-08-24T07:46:38Z","pushed_at":"2016-01-25T06:12:41Z","git_url":"git://github.com/smharper/openmc.git","ssh_url":"git@github.com:smharper/openmc.git","clone_url":"https://github.com/smharper/openmc.git","svn_url":"https://github.com/smharper/openmc","homepage":"http://mit-crpg.github.io/openmc/","size":23070,"stargazers_count":1,"watchers_count":1,"language":"FORTRAN","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":1,"default_branch":"develop"}},"base":{"label":"mit-crpg:develop","ref":"develop","sha":"c4e5f4380fbc81a32872546620bc858d996630f1","user":{"login":"mit-crpg","id":1247634,"avatar_url":"https://avatars.githubusercontent.com/u/1247634?v=3","gravatar_id":"","url":"https://api.github.com/users/mit-crpg","html_url":"https://github.com/mit-crpg","followers_url":"https://api.github.com/users/mit-crpg/followers","following_url":"https://api.github.com/users/mit-crpg/following{/other_user}","gists_url":"https://api.github.com/users/mit-crpg/gists{/gist_id}","starred_url":"https://api.github.com/users/mit-crpg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mit-crpg/subscriptions","organizations_url":"https://api.github.com/users/mit-crpg/orgs","repos_url":"https://api.github.com/users/mit-crpg/repos","events_url":"https://api.github.com/users/mit-crpg/events{/privacy}","received_events_url":"https://api.github.com/users/mit-crpg/received_events","type":"Organization","site_admin":false},"repo":{"id":2081118,"name":"openmc","full_name":"mit-crpg/openmc","owner":{"login":"mit-crpg","id":1247634,"avatar_url":"https://avatars.githubusercontent.com/u/1247634?v=3","gravatar_id":"","url":"https://api.github.com/users/mit-crpg","html_url":"https://github.com/mit-crpg","followers_url":"https://api.github.com/users/mit-crpg/followers","following_url":"https://api.github.com/users/mit-crpg/following{/other_user}","gists_url":"https://api.github.com/users/mit-crpg/gists{/gist_id}","starred_url":"https://api.github.com/users/mit-crpg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mit-crpg/subscriptions","organizations_url":"https://api.github.com/users/mit-crpg/orgs","repos_url":"https://api.github.com/users/mit-crpg/repos","events_url":"https://api.github.com/users/mit-crpg/events{/privacy}","received_events_url":"https://api.github.com/users/mit-crpg/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mit-crpg/openmc","description":"OpenMC Monte Carlo Code","fork":false,"url":"https://api.github.com/repos/mit-crpg/openmc","forks_url":"https://api.github.com/repos/mit-crpg/openmc/forks","keys_url":"https://api.github.com/repos/mit-crpg/openmc/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mit-crpg/openmc/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mit-crpg/openmc/teams","hooks_url":"https://api.github.com/repos/mit-crpg/openmc/hooks","issue_events_url":"https://api.github.com/repos/mit-crpg/openmc/issues/events{/number}","events_url":"https://api.github.com/repos/mit-crpg/openmc/events","assignees_url":"https://api.github.com/repos/mit-crpg/openmc/assignees{/user}","branches_url":"https://api.github.com/repos/mit-crpg/openmc/branches{/branch}","tags_url":"https://api.github.com/repos/mit-crpg/openmc/tags","blobs_url":"https://api.github.com/repos/mit-crpg/openmc/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mit-crpg/openmc/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mit-crpg/openmc/git/refs{/sha}","trees_url":"https://api.github.com/repos/mit-crpg/openmc/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mit-crpg/openmc/statuses/{sha}","languages_url":"https://api.github.com/repos/mit-crpg/openmc/languages","stargazers_url":"https://api.github.com/repos/mit-crpg/openmc/stargazers","contributors_url":"https://api.github.com/repos/mit-crpg/openmc/contributors","subscribers_url":"https://api.github.com/repos/mit-crpg/openmc/subscribers","subscription_url":"https://api.github.com/repos/mit-crpg/openmc/subscription","commits_url":"https://api.github.com/repos/mit-crpg/openmc/commits{/sha}","git_commits_url":"https://api.github.com/repos/mit-crpg/openmc/git/commits{/sha}","comments_url":"https://api.github.com/repos/mit-crpg/openmc/comments{/number}","issue_comment_url":"https://api.github.com/repos/mit-crpg/openmc/issues/comments{/number}","contents_url":"https://api.github.com/repos/mit-crpg/openmc/contents/{+path}","compare_url":"https://api.github.com/repos/mit-crpg/openmc/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mit-crpg/openmc/merges","archive_url":"https://api.github.com/repos/mit-crpg/openmc/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mit-crpg/openmc/downloads","issues_url":"https://api.github.com/repos/mit-crpg/openmc/issues{/number}","pulls_url":"https://api.github.com/repos/mit-crpg/openmc/pulls{/number}","milestones_url":"https://api.github.com/repos/mit-crpg/openmc/milestones{/number}","notifications_url":"https://api.github.com/repos/mit-crpg/openmc/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mit-crpg/openmc/labels{/name}","releases_url":"https://api.github.com/repos/mit-crpg/openmc/releases{/id}","deployments_url":"https://api.github.com/repos/mit-crpg/openmc/deployments","created_at":"2011-07-21T00:33:27Z","updated_at":"2016-01-25T01:17:33Z","pushed_at":"2016-01-24T04:45:21Z","git_url":"git://github.com/mit-crpg/openmc.git","ssh_url":"git@github.com:mit-crpg/openmc.git","clone_url":"https://github.com/mit-crpg/openmc.git","svn_url":"https://github.com/mit-crpg/openmc","homepage":"http://mit-crpg.github.io/openmc/","size":41388,"stargazers_count":69,"watchers_count":69,"language":"FORTRAN","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":88,"mirror_url":null,"open_issues_count":51,"forks":88,"open_issues":51,"watchers":69,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/572"},"html":{"href":"https://github.com/mit-crpg/openmc/pull/572"},"issue":{"href":"https://api.github.com/repos/mit-crpg/openmc/issues/572"},"comments":{"href":"https://api.github.com/repos/mit-crpg/openmc/issues/572/comments"},"review_comments":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/572/comments"},"review_comment":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mit-crpg/openmc/pulls/572/commits"},"statuses":{"href":"https://api.github.com/repos/mit-crpg/openmc/statuses/5634993bf9447b4fe98e2f634d206da0ee4dedf4"}}}}