Skip to content

Commit 76be97f

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

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

packages/catlog-wasm/src/theories.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use catlog::one::Path;
1212
use catlog::stdlib::{analyses, models, theories, theory_morphisms};
1313
use catlog::zero::name;
1414

15-
use super::model_morphism::{motifs, MotifOccurrence, MotifsOptions};
15+
use super::model_morphism::{MotifOccurrence, MotifsOptions, motifs};
1616
use super::result::JsResult;
1717
use super::{analyses::*, model::DblModel, theory::DblTheory};
1818

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::dbl::{
2222
};
2323
use crate::one::FgCategory;
2424
use crate::simulate::ode::{NumericalPolynomialSystem, ODEProblem, PolynomialSystem};
25-
use crate::zero::{alg::Polynomial, name, rig::Monomial, QualifiedName};
25+
use crate::zero::{QualifiedName, alg::Polynomial, name, rig::Monomial};
2626

2727
/// Data defining a mass-action ODE problem for a model.
2828
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

packages/catlog/src/stdlib/theories.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Standard library of double theories.
22
33
use crate::dbl::theory::*;
4-
use crate::one::{fp_category::FpCategory, Path};
4+
use crate::one::{Path, fp_category::FpCategory};
55
use crate::zero::name;
66

77
/// The empty theory, which has a single model, the empty model.

packages/catlog/src/zero/rig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! the same data structure, but with different notation!
1515
1616
use num_traits::{One, Pow, Zero};
17-
use std::collections::{btree_map, BTreeMap};
17+
use std::collections::{BTreeMap, btree_map};
1818
use std::fmt::Display;
1919
use std::iter::{Product, Sum};
2020
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg};

0 commit comments

Comments
 (0)