From 4337cafe052c4ad238c969dfa4cb7c7ac52f5e07 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 25 Oct 2018 16:48:36 +0200 Subject: Use IntegerMatrix for all unit cell transformations Get rid of UnitCellTransformation, a thin wrapper which didn't do anything. --- tests/transformation_check.c | 174 ++++++++++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 69 deletions(-) (limited to 'tests/transformation_check.c') diff --git a/tests/transformation_check.c b/tests/transformation_check.c index 2e7e7378..2eb01aa6 100644 --- a/tests/transformation_check.c +++ b/tests/transformation_check.c @@ -37,6 +37,7 @@ #include #include +#include #define MAX_REFLS (10*1024) @@ -134,17 +135,44 @@ static int compare_rvecs(struct rvec *a, int na, struct rvec *b, int nb) } -static int check_transformation(UnitCell *cell, UnitCellTransformation *tfn, +static int check_same_reflections(UnitCell *cell, UnitCell *cnew) +{ + struct rvec *vecs; + struct rvec *tvecs; + int na, nb; + + /* Check that the two cells predict the same reflections */ + vecs = all_refls(cell, 1e9, &na); + tvecs = all_refls(cnew, 1e9, &nb); + if ( compare_rvecs(vecs, na, tvecs, nb) + || compare_rvecs(tvecs, nb, vecs, na) ) + { + ERROR("******* Transformed cell didn't predict the same reflections\n"); + //printf("---\n"); + //for ( i=0; i Before transformation:\n"); cell_print(cell); - tfn_print(tfn); + STATUS("----> The transformation matrix:\n"); + intmat_print(tfn); + STATUS("----> After transformation:\n"); cell_print(cnew); if ( pred_test ) { - /* Check that the two cells predict the same reflections */ - vecs = all_refls(cell, 1e9, &na); - tvecs = all_refls(cnew, 1e9, &nb); - if ( compare_rvecs(vecs, na, tvecs, nb) - || compare_rvecs(tvecs, nb, vecs, na) ) - { - ERROR("Transformed cell didn't predict the same reflections\n"); - //printf("---\n"); - //for ( i=0; i After transformation and transformation back:\n"); cell_print(cback); + } else { + STATUS("The original cell was recovered after inverse transform.\n"); } return fail; @@ -214,22 +224,48 @@ static int check_transformation(UnitCell *cell, UnitCellTransformation *tfn, static int check_uncentering(UnitCell *cell) { UnitCell *ct; - UnitCellTransformation *tr; + IntegerMatrix *tr; + int fail = 0; + + STATUS("-----------------------\n"); + + STATUS("----> Before transformation:\n"); + cell_print(cell); ct = uncenter_cell(cell, &tr); - return check_transformation(cell, tr, 1, ct); + if ( ct == NULL ) return 1; + + STATUS("----> The primitive unit cell:\n"); + cell_print(ct); + + STATUS("----> The matrix to put the centering back:\n"); + intmat_print(tr); + + fail = check_same_reflections(cell, ct); + cell_free(ct); + + return fail; } -static int check_identity(UnitCell *cell, UnitCellTransformation *tfn) +static int check_identity(UnitCell *cell, IntegerMatrix *tfn) { UnitCell *cnew; double a[9], b[9]; int i; int fail = 0; + STATUS("-----------------------\n"); + cnew = cell_transform(cell, tfn); + STATUS("----> Before identity transformation:\n"); + cell_print(cell); + STATUS("----> The identity transformation matrix:\n"); + intmat_print(tfn); + STATUS("----> After identity transformation:\n"); + cell_print(cnew); + cell_get_cartesian(cell, &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8]); @@ -239,14 +275,14 @@ static int check_identity(UnitCell *cell, UnitCellTransformation *tfn) for ( i=0; i<9; i++ ) { if ( !within_tolerance(a[i], b[i], 0.1) ) { fail = 1; - STATUS("%e %e\n", a[i], b[i]); + //STATUS("%e %e\n", a[i], b[i]); } } if ( fail ) { - ERROR("Original cell not recovered after transformation:\n"); + ERROR("Original cell not recovered after identity transformation:\n"); cell_print(cell); - tfn_print(tfn); + intmat_print(tfn); cell_print(cnew); } @@ -258,7 +294,8 @@ int main(int argc, char *argv[]) { int fail = 0; UnitCell *cell, *cref; - UnitCellTransformation *tfn; + IntegerMatrix *tfn; + IntegerMatrix *part1, *part2; gsl_rng *rng; rng = gsl_rng_alloc(gsl_rng_mt19937); @@ -273,53 +310,52 @@ int main(int argc, char *argv[]) if ( cell == NULL ) return 1; cell_free(cref); + tfn = intmat_identity(3); + /* Permutation of axes */ - tfn = tfn_identity(); if ( tfn == NULL ) return 1; - tfn_combine(tfn, tfn_vector(0,1,0), - tfn_vector(0,0,1), - tfn_vector(1,0,0)); + intmat_set_all_3x3(tfn, 0,1,0, + 0,0,1, + 1,0,0); fail += check_transformation(cell, tfn, 1, NULL); - tfn_free(tfn); /* Doubling of cell in one direction */ - tfn = tfn_identity(); if ( tfn == NULL ) return 1; - tfn_combine(tfn, tfn_vector(2,0,0), - tfn_vector(0,1,0), - tfn_vector(0,0,1)); + intmat_set_all_3x3(tfn, 2,0,0, + 0,1,0, + 0,0,1); fail += check_transformation(cell, tfn, 0, NULL); - tfn_free(tfn); - /* Diagonal supercell */ - tfn = tfn_identity(); + /* Shearing */ if ( tfn == NULL ) return 1; - tfn_combine(tfn, tfn_vector(1,1,0), - tfn_vector(0,1,0), - tfn_vector(0,0,1)); + intmat_set_all_3x3(tfn, 1,1,0, + 0,1,0, + 0,0,1); fail += check_transformation(cell, tfn, 1, NULL); - tfn_free(tfn); /* Crazy */ - tfn = tfn_identity(); if ( tfn == NULL ) return 1; - tfn_combine(tfn, tfn_vector(1,1,0), - tfn_vector(0,1,0), - tfn_vector(0,1,1)); + intmat_set_all_3x3(tfn, 1,1,0, + 0,1,0, + 0,1,1); fail += check_transformation(cell, tfn, 0, NULL); - tfn_free(tfn); /* Identity in two parts */ - tfn = tfn_identity(); + part1 = intmat_identity(3); + part2 = intmat_identity(3); if ( tfn == NULL ) return 1; - tfn_combine(tfn, tfn_vector(0,0,1), - tfn_vector(0,1,0), - tfn_vector(-1,0,0)); - tfn_combine(tfn, tfn_vector(0,0,-1), - tfn_vector(0,1,0), - tfn_vector(1,0,0)); + intmat_set_all_3x3(part1, 0,0,1, + 0,1,0, + -1,0,0); + intmat_set_all_3x3(part2, 0,0,-1, + 0,1,0, + 1,0,0); + tfn = intmat_intmat_mult(part1, part2); fail += check_identity(cell, tfn); - tfn_free(tfn); + intmat_free(part1); + intmat_free(part2); + + intmat_free(tfn); /* Check some uncentering transformations */ cref = cell_new_from_parameters(50e-10, 50e-10, 50e-10, -- cgit v1.2.3 From 169e7c5677ffc9c296c0a7eeddb0b77e024a4a55 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 18 Feb 2019 16:04:28 +0100 Subject: Be clear about whether functions need rational or integer transformations --- tests/transformation_check.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/transformation_check.c') diff --git a/tests/transformation_check.c b/tests/transformation_check.c index 2eb01aa6..c5dad16c 100644 --- a/tests/transformation_check.c +++ b/tests/transformation_check.c @@ -176,11 +176,11 @@ static int check_transformation(UnitCell *cell, IntegerMatrix *tfn, STATUS("-----------------------\n"); if ( ct == NULL ) { - cnew = cell_transform(cell, tfn); + cnew = cell_transform_intmat(cell, tfn); } else { cnew = ct; } - cback = cell_transform_inverse(cnew, tfn); + cback = cell_transform_intmat_inverse(cnew, tfn); STATUS("----> Before transformation:\n"); cell_print(cell); @@ -257,7 +257,7 @@ static int check_identity(UnitCell *cell, IntegerMatrix *tfn) STATUS("-----------------------\n"); - cnew = cell_transform(cell, tfn); + cnew = cell_transform_intmat(cell, tfn); STATUS("----> Before identity transformation:\n"); cell_print(cell); -- cgit v1.2.3 From 4f11a9f8530178cfb510f11c7bc4b1829bbd0be4 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 6 Mar 2019 11:40:37 +0100 Subject: Keep track of the "un-centering" matrix, as well as the "centering" This makes it easy to reverse the transformation, if required, which it is when comparing centered cells. --- tests/transformation_check.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'tests/transformation_check.c') diff --git a/tests/transformation_check.c b/tests/transformation_check.c index c5dad16c..06521061 100644 --- a/tests/transformation_check.c +++ b/tests/transformation_check.c @@ -224,25 +224,50 @@ static int check_transformation(UnitCell *cell, IntegerMatrix *tfn, static int check_uncentering(UnitCell *cell) { UnitCell *ct; - IntegerMatrix *tr; + IntegerMatrix *C; + RationalMatrix *Ci; + UnitCell *cback; + double a[9], b[9]; + int i; int fail = 0; STATUS("-----------------------\n"); STATUS("----> Before transformation:\n"); - cell_print(cell); + cell_print_full(cell); - ct = uncenter_cell(cell, &tr); + ct = uncenter_cell(cell, &C, &Ci); if ( ct == NULL ) return 1; STATUS("----> The primitive unit cell:\n"); cell_print(ct); STATUS("----> The matrix to put the centering back:\n"); - intmat_print(tr); + intmat_print(C); + + STATUS("----> The recovered centered cell:\n"); + cback = cell_transform_intmat(ct, C); + cell_print(cback); + + cell_get_cartesian(cell, &a[0], &a[1], &a[2], + &a[3], &a[4], &a[5], + &a[6], &a[7], &a[8]); + cell_get_cartesian(cback, &b[0], &b[1], &b[2], + &b[3], &b[4], &b[5], + &b[6], &b[7], &b[8]); + for ( i=0; i<9; i++ ) { + if ( fabs(a[i] - b[i]) > 1e-12 ) { + fail = 1; + } + } + + if ( fail ) { + ERROR("Original cell not recovered after back transformation\n"); + } - fail = check_same_reflections(cell, ct); + fail += check_same_reflections(cell, ct); cell_free(ct); + cell_free(cback); return fail; } -- cgit v1.2.3 From 7d5453e30c18a59b27b486298c3589046adbc308 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 9 Mar 2019 11:39:37 +0100 Subject: tests/transformation_check: Tidy up and be louder --- tests/transformation_check.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'tests/transformation_check.c') diff --git a/tests/transformation_check.c b/tests/transformation_check.c index 06521061..ba189e4e 100644 --- a/tests/transformation_check.c +++ b/tests/transformation_check.c @@ -147,7 +147,8 @@ static int check_same_reflections(UnitCell *cell, UnitCell *cnew) if ( compare_rvecs(vecs, na, tvecs, nb) || compare_rvecs(tvecs, nb, vecs, na) ) { - ERROR("******* Transformed cell didn't predict the same reflections\n"); + ERROR("********************************************** "); + ERROR("Transformed cell didn't predict the same reflections\n"); //printf("---\n"); //for ( i=0; i Before transformation:\n"); @@ -188,6 +185,8 @@ static int check_transformation(UnitCell *cell, IntegerMatrix *tfn, intmat_print(tfn); STATUS("----> After transformation:\n"); cell_print(cnew); + STATUS("----> After back transformation:\n"); + cell_print(cback); if ( pred_test ) { check_same_reflections(cell, cnew); @@ -210,7 +209,8 @@ static int check_transformation(UnitCell *cell, IntegerMatrix *tfn, } if ( fail ) { - ERROR("******* Original cell not recovered after transformation:\n"); + ERROR("********************************************** "); + ERROR("Original cell not recovered after transformation:\n"); STATUS("----> After transformation and transformation back:\n"); cell_print(cback); } else { @@ -262,6 +262,7 @@ static int check_uncentering(UnitCell *cell) } if ( fail ) { + ERROR("********************************************** "); ERROR("Original cell not recovered after back transformation\n"); } @@ -305,6 +306,7 @@ static int check_identity(UnitCell *cell, IntegerMatrix *tfn) } if ( fail ) { + ERROR("********************************************** "); ERROR("Original cell not recovered after identity transformation:\n"); cell_print(cell); intmat_print(tfn); @@ -342,28 +344,28 @@ int main(int argc, char *argv[]) intmat_set_all_3x3(tfn, 0,1,0, 0,0,1, 1,0,0); - fail += check_transformation(cell, tfn, 1, NULL); + fail += check_transformation(cell, tfn, 1); /* Doubling of cell in one direction */ if ( tfn == NULL ) return 1; intmat_set_all_3x3(tfn, 2,0,0, 0,1,0, 0,0,1); - fail += check_transformation(cell, tfn, 0, NULL); + fail += check_transformation(cell, tfn, 0); /* Shearing */ if ( tfn == NULL ) return 1; intmat_set_all_3x3(tfn, 1,1,0, 0,1,0, 0,0,1); - fail += check_transformation(cell, tfn, 1, NULL); + fail += check_transformation(cell, tfn, 1); /* Crazy */ if ( tfn == NULL ) return 1; intmat_set_all_3x3(tfn, 1,1,0, 0,1,0, 0,1,1); - fail += check_transformation(cell, tfn, 0, NULL); + fail += check_transformation(cell, tfn, 0); /* Identity in two parts */ part1 = intmat_identity(3); -- cgit v1.2.3 From cb51ba0e53425ae663db313d730c1df644e4f76b Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 9 Mar 2019 11:42:55 +0100 Subject: Change matrix notation to match ITA chapter 5.1 --- tests/transformation_check.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/transformation_check.c') diff --git a/tests/transformation_check.c b/tests/transformation_check.c index ba189e4e..2790d6f1 100644 --- a/tests/transformation_check.c +++ b/tests/transformation_check.c @@ -341,9 +341,9 @@ int main(int argc, char *argv[]) /* Permutation of axes */ if ( tfn == NULL ) return 1; - intmat_set_all_3x3(tfn, 0,1,0, - 0,0,1, - 1,0,0); + intmat_set_all_3x3(tfn, 0,0,1, + 1,0,0, + 0,1,0); fail += check_transformation(cell, tfn, 1); /* Doubling of cell in one direction */ @@ -355,28 +355,28 @@ int main(int argc, char *argv[]) /* Shearing */ if ( tfn == NULL ) return 1; - intmat_set_all_3x3(tfn, 1,1,0, - 0,1,0, + intmat_set_all_3x3(tfn, 1,0,0, + 1,1,0, 0,0,1); fail += check_transformation(cell, tfn, 1); /* Crazy */ if ( tfn == NULL ) return 1; - intmat_set_all_3x3(tfn, 1,1,0, - 0,1,0, - 0,1,1); + intmat_set_all_3x3(tfn, 1,0,0, + 1,1,1, + 0,0,1); fail += check_transformation(cell, tfn, 0); /* Identity in two parts */ part1 = intmat_identity(3); part2 = intmat_identity(3); if ( tfn == NULL ) return 1; - intmat_set_all_3x3(part1, 0,0,1, - 0,1,0, - -1,0,0); - intmat_set_all_3x3(part2, 0,0,-1, - 0,1,0, - 1,0,0); + intmat_set_all_3x3(part1, 0,0,-1, + 0,1,0, + 1,0,0); + intmat_set_all_3x3(part2, 0,0,1, + 0,1,0, + -1,0,0); tfn = intmat_intmat_mult(part1, part2); fail += check_identity(cell, tfn); intmat_free(part1); -- cgit v1.2.3