Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions polychrom/__polymer_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ long int intersectValue(double *p1, double *v1, double *p2, double *v2) {
double t1 = (v2xp2[2]-v2xp1[2])/v2xv1[2];
double t2 = (v1xp1[2]-v1xp2[2])/v1xv2[2];
if(t1<0 || t1>1 || t2<0 || t2>1) {
free(v2xp2);free(v2xp1);free(v2xv1);free(v1xp1);free(v1xp2);free(v1xv2);
delete[] v2xp2; delete[] v2xp1; delete[] v2xv1; delete[] v1xp1; delete[] v1xp2; delete[] v1xv2;
return 0;
}
else {
Expand All @@ -67,7 +67,8 @@ long int intersectValue(double *p1, double *v1, double *p2, double *v2) {
double z1 = inter1[2];
double z2 = inter2[2];

free(v2xp2);free(v2xp1);free(v2xv1);free(v1xp1);free(v1xp2);free(v1xv2);free(inter1);free(inter2);
delete[] v2xp2; delete[] v2xp1; delete[] v2xv1; delete[] v1xp1; delete[] v1xp2; delete[] v1xv2;
delete[] inter1; delete[] inter2;
if(z1>=z2) return x;
else return -x;
}
Expand Down Expand Up @@ -163,15 +164,18 @@ long int _getLinkingNumberCpp(int M, double *olddata, int N) {
if(j<N-1) v2 = linearCombo(data[j+1],data[j],1,-1);
else v2 = linearCombo(data[M],data[N-1],1,-1);
L+=intersectValue(data[i],v1,data[j],v2);
free(v1);free(v2);
delete[] v1; delete[] v2;
}
}

// L is the full signed crossing sum between the two curves in projection;
// the linking number is half of that, and intersectValue's sign convention
// is opposite to the standard (Gauss integral) one. Verified against an
// independent Gauss-integral implementation: raw L = -2 * lk.
return -L/2;
for(i=0;i<N;i++) delete[] data[i];
delete[] data;

// L is the signed crossing sum of the two curves in the z-projection. Each
// crossing is counted once, with sign (over x under).z, which is the standard
// crossing sign: L = 2 * lk in the Gauss convention
// lk = 1/(4 pi) oint oint (x - y).(dx x dy)/|x - y|^3.
return L/2;
}


Expand Down
15 changes: 13 additions & 2 deletions tests/test_polymer_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,21 @@ def test_hopf_link(self):
ring1, ring2 = create_hopf_link()
L = getLinkingNumber(ring1, ring2, simplify=False)
# A Hopf link has linking number exactly +-1 (sign depends on
# orientation). The old C code returned -2x the true value: it summed
# signed crossings without halving, with an inverted sign convention.
# orientation). The old C code returned 2x the true value: it summed
# signed crossings without halving.
assert abs(L) == 1, f"Hopf link must have |linking number| = 1, got {L}"

def test_hopf_link_sign(self):
"""Test the Hopf link sign against the right-hand rule."""
from polychrom.polymer_analyses import getLinkingNumber

# ring1 runs counter-clockwise seen from +z, so its disk has normal +z.
# ring2 = (0.5 + cos t, 0, sin t) pierces that disk once, at t = pi
# (x = -0.5), moving along -z. The Gauss linking number is therefore -1.
ring1, ring2 = create_hopf_link()
assert getLinkingNumber(ring1, ring2, simplify=False) == -1
assert getLinkingNumber(ring1, ring2[::-1], simplify=False) == 1

def test_simplify_preserves_linking(self):
"""Test that simplification preserves linking number."""
from polychrom.polymer_analyses import getLinkingNumber
Expand Down