{"action":"created","issue":{"url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/30","labels_url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/30/labels{/name}","comments_url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/30/comments","events_url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/30/events","html_url":"https://github.com/kroneckerbio/kroneckerbio/issues/30","id":71376118,"number":30,"title":"Cross-compartment reactions are handled incorrectly in mass action models","user":{"login":"dflowers7","id":7842102,"avatar_url":"https://avatars.githubusercontent.com/u/7842102?v=3","gravatar_id":"","url":"https://api.github.com/users/dflowers7","html_url":"https://github.com/dflowers7","followers_url":"https://api.github.com/users/dflowers7/followers","following_url":"https://api.github.com/users/dflowers7/following{/other_user}","gists_url":"https://api.github.com/users/dflowers7/gists{/gist_id}","starred_url":"https://api.github.com/users/dflowers7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dflowers7/subscriptions","organizations_url":"https://api.github.com/users/dflowers7/orgs","repos_url":"https://api.github.com/users/dflowers7/repos","events_url":"https://api.github.com/users/dflowers7/events{/privacy}","received_events_url":"https://api.github.com/users/dflowers7/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":19,"created_at":"2015-04-27T19:19:42Z","updated_at":"2015-05-02T02:47:25Z","closed_at":null,"body":"In Kronecker, the states and inputs are internally represented as amounts instead of concentrations. Since reaction rates are defined in terms of concentrations, amounts must be converted to concentrations before they are passed into the rate functions, and the evaluated rate must be converted back into d(amount)/dt and scaled by stoichiometry to determine the time derivative of the states. \r\n\r\nThe time derivative of a state in one compartment may be a function of state concentrations in other compartments. Therefore, ratios of compartment volumes should appear in the equations, but no such ratios ever appear in the evalulation of f, r, and their derivatives in Kronecker's FinalizeModel function. What appears to be wrong is that volume terms are improperly cancelled in the rate equations. The correct equation should look something like\r\n\r\nf = v_x.\\*(A1\\*(x./v_x) + A2\\*(kron(x./v_x,x./v_x)) + A3\\*kron(u./v_u,x./v_x) + A4\\*kron(x./v_x,u./v_u) + A5\\*kron(u./v_u,u./v_u) + A6\\*(u./v_u) + a./v_x)\r\n\r\nwhere x and u are amounts, v_x and v_u are the volumes of the compartments that x and u are in, kron(a,b) is the Kronecker product of a and b, and f is the time derivative of the state amounts. \".*\" refers to element-wise multiplication, and \"./\" refers to element-wise division.\r\n\r\nKronecker improperly simplifies this equation to\r\n\r\nf = A1\\*x + A2\\*(kron(x,x./v_x)) + A3\\*kron(u,x./v_x) + A4\\*kron(x,u./v_u) + A5\\*kron(u,u./v_u) + A6\\*u + a\r\n\r\nThis is incorrect because the outer v_x does not necessarily cancel the terms on the right hand sides of the matrix multiplications, i.e., v_x.\\*A1\\*(x./v_x) ~= A1\\*x. Assuming that this is true causes one to lose the cross-compartmental volume ratios.\r\n\r\nYou can demonstrate the problem rather easily by setting up a simple model X1 -> X2, where the reaction is reversible and governed by forward parameter k1 and reverse parameter k2, and state X1 is in a separate compartment from state X2. Setting k1 and k2 to 1, X1 to be in a large compartment, say V1 = 1000, and X2 to be in a small compartment, with V2 = 1, one would intuitively expect that at equilibrium, X1 = 1000*X2 (where X1 and X2 are amounts), such that [X1] = \\[X2\\] (where [X1] and [X2] are concentrations). Simulating the system in Kronecker results in an equilibrium where X1 = X2 and [X1] = [X2]/1000. \r\n\r\nI'm adding this simple model simulated to steady state as another unit test of Kronecker's compartment functionality. I'm adding it within UT04_Simulation, but it arguably should be its own set of tests. We can move it later if we want. Also, I'm fixing the code in FinalizeModel by rewriting the functions. I'll make this change in a branch called mass-action-compartment-fix before applying it to master."},"comment":{"url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/comments/98294076","html_url":"https://github.com/kroneckerbio/kroneckerbio/issues/30#issuecomment-98294076","issue_url":"https://api.github.com/repos/kroneckerbio/kroneckerbio/issues/30","id":98294076,"user":{"login":"dflowers7","id":7842102,"avatar_url":"https://avatars.githubusercontent.com/u/7842102?v=3","gravatar_id":"","url":"https://api.github.com/users/dflowers7","html_url":"https://github.com/dflowers7","followers_url":"https://api.github.com/users/dflowers7/followers","following_url":"https://api.github.com/users/dflowers7/following{/other_user}","gists_url":"https://api.github.com/users/dflowers7/gists{/gist_id}","starred_url":"https://api.github.com/users/dflowers7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dflowers7/subscriptions","organizations_url":"https://api.github.com/users/dflowers7/orgs","repos_url":"https://api.github.com/users/dflowers7/repos","events_url":"https://api.github.com/users/dflowers7/events{/privacy}","received_events_url":"https://api.github.com/users/dflowers7/received_events","type":"User","site_admin":false},"created_at":"2015-05-02T02:47:25Z","updated_at":"2015-05-02T02:47:25Z","body":"This paper provides an example of a model that uses equations like the ones I wrote:\r\n\r\nhttp://www.ncbi.nlm.nih.gov/pmc/articles/PMC3923578/"}}