Skip to content

Commit b2c42d0

Browse files
committed
fixing tests; walking singed links
1 parent 0b838b2 commit b2c42d0

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

packages/catlog/src/stdlib/analyses/ode/mass_action.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,21 @@ impl StockFlowMassActionAnalysis {
250250
})
251251
.collect();
252252

253-
for link in model
254-
.mor_generators_with_type(&self.pos_link_mor_type)
255-
.chain(model.mor_generators_with_type(&self.neg_link_mor_type))
256-
{
253+
for link in model.mor_generators_with_type(&self.pos_link_mor_type) {
254+
let dom = model.mor_generator_dom(&link).unwrap_basic();
255+
let path = model.mor_generator_cod(&link).unwrap_tabulated();
256+
let Some(TabEdge::Basic(cod)) = path.only() else {
257+
panic!("Codomain of link should be basic morphism");
258+
};
259+
if let Some(term) = terms.get_mut(&cod) {
260+
let mon: Monomial<_, i8> = [(dom, 1)].into_iter().collect();
261+
*term = std::mem::take(term) * mon;
262+
} else {
263+
panic!("Codomain of link does not belong to model");
264+
};
265+
}
266+
267+
for link in model.mor_generators_with_type(&self.neg_link_mor_type) {
257268
let dom = model.mor_generator_dom(&link).unwrap_basic();
258269
let path = model.mor_generator_cod(&link).unwrap_tabulated();
259270
let Some(TabEdge::Basic(cod)) = path.only() else {
@@ -331,9 +342,9 @@ mod tests {
331342
use crate::stdlib::{models::*, theories::*};
332343

333344
#[test]
334-
fn backward_link_dynamics() {
345+
fn positive_backward_link_dynamics() {
335346
let th = Rc::new(th_category_links());
336-
let model = backward_link(th);
347+
let model = positive_backward_link(th);
337348
let sys = StockFlowMassActionAnalysis::default().build_system(&model);
338349
let expected = expect!([r#"
339350
dx = ((-1) f) x y
@@ -342,6 +353,18 @@ mod tests {
342353
expected.assert_eq(&sys.to_string());
343354
}
344355

356+
#[test]
357+
fn negative_backward_link_dynamics() {
358+
let th = Rc::new(th_category_links());
359+
let model = positive_backward_link(th);
360+
let sys = StockFlowMassActionAnalysis::default().build_system(&model);
361+
let expected = expect!([r#"
362+
dx = ((-1) f) x y^{-1}
363+
dy = f x y^{-1}
364+
"#]);
365+
expected.assert_eq(&sys.to_string());
366+
}
367+
345368
#[test]
346369
fn catalysis_dynamics() {
347370
let th = Rc::new(th_sym_monoidal_category());

packages/catlog/src/stdlib/models.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::rc::Rc;
44

55
use crate::dbl::{model::*, theory::*};
66
use crate::one::{Path, QualifiedPath};
7-
use crate::zero::{QualifiedName, name};
7+
use crate::zero::{name, QualifiedName};
88

99
/// The positive self-loop.
1010
///
@@ -108,6 +108,44 @@ pub fn backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
108108
model
109109
}
110110

111+
/// The "walking" backward positive link.
112+
///
113+
/// This is the free category with signed links that has a positive link from
114+
/// the codomain of a morphism back to the morphism itself.
115+
pub fn positive_backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
116+
let ob_type = TabObType::Basic(name("Object"));
117+
let mut model = DiscreteTabModel::new(th.clone());
118+
model.add_ob(name("x"), ob_type.clone());
119+
model.add_ob(name("y"), ob_type.clone());
120+
model.add_mor(name("f"), name("x").into(), name("y").into(), th.hom_type(ob_type));
121+
model.add_mor(
122+
name("link"),
123+
name("y").into(),
124+
model.tabulated_gen(name("f")),
125+
TabMorType::Basic(name("PositiveLink")),
126+
);
127+
model
128+
}
129+
130+
/// The "walking" backward negative link.
131+
///
132+
/// This is the free category with signed links that has a negative link from
133+
/// the codomain of a morphism back to the morphism itself.
134+
pub fn negative_backward_link(th: Rc<DiscreteTabTheory>) -> DiscreteTabModel {
135+
let ob_type = TabObType::Basic(name("Object"));
136+
let mut model = DiscreteTabModel::new(th.clone());
137+
model.add_ob(name("x"), ob_type.clone());
138+
model.add_ob(name("y"), ob_type.clone());
139+
model.add_mor(name("f"), name("x").into(), name("y").into(), th.hom_type(ob_type));
140+
model.add_mor(
141+
name("link"),
142+
name("y").into(),
143+
model.tabulated_gen(name("f")),
144+
TabMorType::Basic(name("NegativeLink")),
145+
);
146+
model
147+
}
148+
111149
/// A reaction involving three species, one playing the role of a catalyst.
112150
///
113151
/// A free symmetric monoidal category, viewed as a reaction network.

0 commit comments

Comments
 (0)