kat_spa.c 138 KB
Newer Older
1 2
// $Id$

3
/*!
4
 * \file kat_spa.c
5
 * \brief Sparse matrix library interface routines.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * These routines also set up the interferometer matrix.
 * 
 * @section Copyright notice
 *
 *  This file is part of the interferometer simulation Finesse
 *  http://www.gwoptics.org/finesse
 *
 *  Copyright (C) 1999 onwards Andreas Freise
 *  with parts of the code written by Daniel Brown, Paul Cochrane
 *  and Gerhard Heinzel.
 *
 *  This program is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24
 *  See the GNU General Public License for more details.
25
 *
26
 *  You should have received a copy of the GNU General Public License along with
27 28 29
 *  this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 *  Suite 330, Boston, MA 02111-1307 USA
 */
30

31 32
#include "kat.h"
#include "kat_inline.c"
33
#include "kat_spa.h"
34
#include "kat_calc.h"
35
#include "klu.h"
36
#include "kat_check.h"
37
#include "kat_io.h"
38
#include "kat_aux.h"
39
#include "kat_matrix_ccs.h"
Daniel Brown's avatar
Daniel Brown committed
40 41
#include "kat_optics.h"
#include "kat_knm_mirror.h"
42
#include "kat_knm_bs.h"
Daniel Brown's avatar
Daniel Brown committed
43
#include "kat_aa.h"
44
#include "kat_quant.h"
45

46
#include <gsl/gsl_cblas.h>
47

Daniel Brown's avatar
Daniel Brown committed
48
const complex_t complex_i = {0.0, 1.0}; //!<  sqrt(-1) or 0 + i
Daniel Brown's avatar
Daniel Brown committed
49
const complex_t complex_mi = {0.0, -1.0}; //!<  sqrt(-1) or 0 - i
Daniel Brown's avatar
Daniel Brown committed
50 51 52
const complex_t complex_1 = {1.0, 0.0}; //!<  1 in complex space: 1 + 0i
const complex_t complex_m1 = {-1.0, 0.0}; //!< -1 in complex space: -1 + 0i
const complex_t complex_0 = {0.0, 0.0}; //!<  0 in complex space: 0 + 0i
53

54
//extern init_variables_t in;
55
extern interferometer_t inter;
56
extern options_t options;
57
extern memory_t mem;
58
extern klu_sparse_var_t klu;
59

60 61
extern ifo_matrix_vars_t M_ifo_car;
extern ifo_matrix_vars_t M_ifo_sig;
62

63 64
extern complex_t *quant_s;

65 66 67 68 69
extern int *c_s; //!< Frequency value list [frequency]
extern double *f_s; //!< Frequency value list [frequency]
extern complex_t ***a_s; //!< Amplitude list [field] [output] [frequency]
extern int *t_s; //!< Type_of_Signal list [frequency]

Daniel Brown's avatar
Daniel Brown committed
70 71
int nijlist1; //!< One list of nij values
int nijlist2; //!< Another list of nij values
72

73 74
extern complex_t *car_ws; // Workspace for carrier computations for rad pressure effects

75

76
//! Solve a sparse matrix
77 78 79 80

/*!
 * \param matrix_type the type of matrix to solve (standard or quantum)
 */
81
void solve_matrix(int matrix_type) {
82
    int tid = startTimer("SOLVE_MATRIX");
83
    // sanity checks on input
84
    assert(matrix_type == STANDARD || matrix_type == SIGNAL_QCORR);
85

86 87 88 89 90 91
    if(matrix_type == STANDARD)
        solve_ccs_matrix(&M_ifo_car, M_ifo_car.rhs_values, false, false);
    else if(matrix_type == SIGNAL_QCORR)
        solve_ccs_matrix(&M_ifo_sig, M_ifo_sig.rhs_values, false, false);
    else
        bug_error("matrix_type not handled");
92
    endTimer(tid);
93 94
}

95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/**
 * Fills in the reflected q values for a beamsplitter for knm computations.
 * This does not fill in transmitted values as it is currently only used for
 * reflections of surface motions.
 * 
 * @param bs beamsplitter in question
 * @param knm_q struct to fill
 */
void get_refl_q_in_out_bs(beamsplitter_t *bs, bs_knm_q_t *knm_q){
    int component = get_overall_component_index(BEAMSPLITTER, bs->comp_index);
    
    node_t *node1 = &inter.node_list[bs->node1_index];
    node_t *node2 = &inter.node_list[bs->node2_index];
    node_t *node3 = &inter.node_list[bs->node3_index];
    node_t *node4 = &inter.node_list[bs->node4_index];
    
112 113 114
    double nr1=1, nr2=1;
    bs_get_nr(bs, &nr1, &nr2);
    
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    complex_t qx1, qx2, qy1, qy2;
    complex_t qx3, qx4, qy3, qy4;
    
    if (NOT node1->gnd_node) {
        if (node1->component_index == component) {
            // reverse q so that beam comes _to_ the bs from node1
            qx1 = cminus(cconj(node1->qx));
            qy1 = cminus(cconj(node1->qy));
        } else {
            qx1 = node1->qx;
            qy1 = node1->qy;
        }
    }else{
        qx1 = complex_0;
        qy1 = complex_0;
    }

    if (NOT node2->gnd_node) {
        if (node2->component_index == component) {
            qx2 = node2->qx;
            qy2 = node2->qy;
        } else {
            // reverse q so that beam goes _from_ the bs to node2
            qx2 = cminus(cconj(node2->qx));
            qy2 = cminus(cconj(node2->qy));
        }
    }else{
        qx2 = complex_0;
        qy2 = complex_0;
    }

    if (NOT node3->gnd_node) {
        if (node3->component_index == component) {
            // reverse q so that beam comes _to_ the bs from node3
            qx3 = cminus(cconj(node3->qx));
            qy3 = cminus(cconj(node3->qy));
        } else {
            qx3 = node3->qx;
            qy3 = node3->qy;
        }
    }else{
        qx3 = complex_0;
        qy3 = complex_0;
    }

    if (NOT node4->gnd_node) {
        if (node4->component_index == component) {
            qx4 = node4->qx;
            qy4 = node4->qy;
        } else {
            // reverse q so that beam goes _from_ the bs to node4
            qx4 = cminus(cconj(node4->qx));
            qy4 = cminus(cconj(node4->qy));
        }
    }else{
        qx4 = complex_0;
        qy4 = complex_0;
    }
    
    // calculate the individual q values for each KNM and if we should even
    // bother calculating them    
    if (NOT node1->gnd_node && NOT node2->gnd_node) {
        
        calculate_bs_qt_qt2(BS12, knm_q, qx1, qy1, qx2, qy2, qx3, qy3, qx4, qy4, nr1, nr2,
                            bs->node1_index, bs->node2_index, bs->node3_index, bs->node4_index, component);
        calculate_bs_qt_qt2(BS21, knm_q, qx1, qy1, qx2, qy2, qx3, qy3, qx4, qy4, nr1, nr2,
                            bs->node1_index, bs->node2_index, bs->node3_index, bs->node4_index, component);
    } else {
        knm_q->qxt1_12 = complex_0;
        knm_q->qyt1_12 = complex_0;
        knm_q->qxt2_12 = complex_0;
        knm_q->qyt2_12 = complex_0;
        knm_q->qxt1_21 = complex_0;
        knm_q->qyt1_21 = complex_0;
        knm_q->qxt2_21 = complex_0;
        knm_q->qyt2_21 = complex_0;
    }
    
    if (NOT node3->gnd_node && NOT node4->gnd_node) {
        calculate_bs_qt_qt2(BS34, knm_q, qx1, qy1, qx2, qy2, qx3, qy3, qx4, qy4, nr1, nr2,
                            bs->node1_index, bs->node2_index, bs->node3_index, bs->node4_index, component);
        calculate_bs_qt_qt2(BS43, knm_q, qx1, qy1, qx2, qy2, qx3, qy3, qx4, qy4, nr1, nr2,
                            bs->node1_index, bs->node2_index, bs->node3_index, bs->node4_index, component);
    } else {
        knm_q->qxt1_34 = complex_0;
        knm_q->qyt1_34 = complex_0;
        knm_q->qxt2_34 = complex_0;
        knm_q->qyt2_34 = complex_0;
        knm_q->qxt1_43 = complex_0;
        knm_q->qyt1_43 = complex_0;
        knm_q->qxt2_43 = complex_0;
        knm_q->qyt2_43 = complex_0;
    }
}

void get_refl_q_in_out_mirror(mirror_t *mirror, mirror_knm_q_t *knm_q){
Daniel Brown's avatar
Daniel Brown committed
211
    
212
    int component_index = get_overall_component_index(MIRROR, mirror->comp_index);
Daniel Brown's avatar
Daniel Brown committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    
    complex_t qx1, qx2, qy1, qy2;
    node_t *n1 = &inter.node_list[mirror->node1_index];
    node_t *n2 = &inter.node_list[mirror->node2_index];
    
    if (NOT n1->gnd_node) {        
        if (n1->component_index == mirror->comp_index) {
            // reverse q so that beam comes _to_ the mirror from node1
            qx1 = cminus(cconj(n1->qx));
            qy1 = cminus(cconj(n1->qy));
        } else {
            qx1 = n1->qx;
            qy1 = n1->qy;
        }
    }else{
        qx1.re = 0;
        qx1.im = 0;
        qy1.re = 0;
        qy1.im = 0;
    }

    if (NOT n2->gnd_node) {        
        if (n2->component_index == component_index) {
            qx2 = n2->qx;
            qy2 = n2->qy;
        } else {
            // reverse q so that beam goes _from_ the mirror to node2,
            // this requires to use -beta for the reflection node2 -> node2 
            qx2 = cminus(cconj(n2->qx));
            qy2 = cminus(cconj(n2->qy));
        }
    }else{
        qx2.re = 0;
        qx2.im = 0;
        qy2.re = 0;
        qy2.im = 0;
    }
    
251 252 253
    double nr1 = (n1->n) ? *n1->n : 0;
    double nr2 = (n2->n) ? *n2->n : 0;
    
Daniel Brown's avatar
Daniel Brown committed
254 255 256
    // calculate the individual q values for each KNM and if we should even
    // bother calculating them    
    if (NOT n1->gnd_node) {
257
        calculate_mirror_qt_qt2(MR11, knm_q, qx1, qy1, qx2, qy2, nr1, nr2, n1->list_index, n2->list_index, component_index);
Daniel Brown's avatar
Daniel Brown committed
258
    } else {
259 260 261 262
        knm_q->qxt1_11 = complex_0;
        knm_q->qxt2_11 = complex_0;
        knm_q->qyt1_11 = complex_0;
        knm_q->qyt2_11 = complex_0;
Daniel Brown's avatar
Daniel Brown committed
263 264 265
    }

    if (NOT n2->gnd_node) {        
266
        calculate_mirror_qt_qt2(MR22, knm_q, qx1, qy1, qx2, qy2, nr1, nr2, n1->list_index, n2->list_index, component_index);
Daniel Brown's avatar
Daniel Brown committed
267
    } else {
268 269 270 271
        knm_q->qxt1_22 = complex_0;
        knm_q->qxt2_22 = complex_0;
        knm_q->qyt1_22 = complex_0;
        knm_q->qyt2_22 = complex_0;
Daniel Brown's avatar
Daniel Brown committed
272
    }
273 274
}

275
void fill_mirror_rot_motion_to_field(frequency_t *fcar, mirror_t *mirror, complex_t factor_x_a,
276 277
                              complex_t ***x_a1, complex_t ***x_a2, complex_t ***y_a1,
                              complex_t ***y_a2, complex_t *ac_1i, complex_t *ac_2i,
278
                              complex_t tuning1u, complex_t tuning1l, complex_t tuning2u, complex_t tuning2l){
279 280 281 282 283 284 285 286 287 288
    int lidx, uidx;
    
    // get lower and upper sideband index
    int slidx = fcar->sig_lower->index; 
    int suidx = fcar->sig_upper->index;
    
    node_t *n1 = &inter.node_list[mirror->node1_index];
    node_t *n2 = &inter.node_list[mirror->node2_index];
        
    mirror_knm_q_t knm_q = {{0}};
Daniel Brown's avatar
Daniel Brown committed
289
    
290
    get_refl_q_in_out_mirror(mirror, &knm_q);
291
                
292 293 294
    double nr1 = (n1->n) ? *n1->n : 1;
    double nr2 = (n2->n) ? *n2->n : 1;
    
Daniel Brown's avatar
Daniel Brown committed
295 296 297
    // we now compute the knmx/y coupling coefficient, note the q value we use here is q1' as this
    // is the expansion parameter used when separating all the coupling matrices so that later we multiply
    // Knmx from the left not the right.
298 299 300
    complex_t knmx1 = (!n1->gnd_node && mirror->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_11, nr1), gouy(knm_q.qxt1_11)) : complex_0;
    complex_t knmx2 = (!n2->gnd_node && mirror->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_22, nr1), gouy(knm_q.qxt1_22)) : complex_0;
    complex_t knmy1 = (!n1->gnd_node && mirror->Iy > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qyt1_11, nr2), gouy(knm_q.qyt1_11)) : complex_0;
301 302
    // extra minus sign here makes the result agree with mirror tilting from before.
    // not sure of the exact origin though...
303
    complex_t knmy2 = (!n2->gnd_node && mirror->Iy > 0) ? z_by_xphr(complex_1,  -0.5 * w_size(knm_q.qyt1_22, nr2), gouy(knm_q.qyt1_22)) : complex_0;
Daniel Brown's avatar
Daniel Brown committed
304
    
Daniel Brown's avatar
Daniel Brown committed
305
    int i, j, u, v, u_, v_;
Daniel Brown's avatar
Daniel Brown committed
306
        
Daniel Brown's avatar
Daniel Brown committed
307 308 309
    for(i=0; i<inter.num_fields; i++){
        get_tem_modes_from_field_index(&u, &v, i);
        
Daniel Brown's avatar
Daniel Brown committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        if(mirror->Ix > 0){
            if(!n1->gnd_node){
                *x_a1[suidx][i] = complex_0;
                *x_a1[slidx][i] = complex_0;
            }
            if(!n2->gnd_node){
                *x_a2[suidx][i] = complex_0;
                *x_a2[slidx][i] = complex_0;
            }
        }
        
        if(mirror->Iy > 0){
            if(!n1->gnd_node){
                *y_a1[suidx][i] = complex_0;
                *y_a1[slidx][i] = complex_0;
            }
            if(!n2->gnd_node){
                *y_a2[suidx][i] = complex_0;
                *y_a2[slidx][i] = complex_0;
            }
        }
        
Daniel Brown's avatar
Daniel Brown committed
332 333 334 335 336 337 338 339 340 341
        for(j=0; j<inter.num_fields; j++){
            get_tem_modes_from_field_index(&u_, &v_, j);
            
            complex_t knmx_total_1 = {0,0};
            complex_t knmx_total_2 = {0,0};
            complex_t knmy_total_1 = {0,0};
            complex_t knmy_total_2 = {0,0};
            
            // Here the two elements of the 
            if(mirror->Ix > 0){
342
                if(v == v_){
343
                    
344 345
                    if(u > 0) { 
                        lidx = get_field_index_from_tem(u-1, v);
346 347 348 349
                        // conjugate times knm_no_rgouy is to match up with previous results when no moment of inertia is present.
                        // Only tested against with a tilt applied to the mirror and a signal also applied
                        if(!n1->gnd_node) z_inc_z(&knmx_total_1, z_by_zc(z_by_x(knmx1, sqrt(u)), mirror->knm_no_rgouy.k11[lidx][j]));
                        if(!n2->gnd_node) z_inc_z(&knmx_total_2, z_by_zc(z_by_x(knmx2, sqrt(u)), mirror->knm_no_rgouy.k22[lidx][j]));
350
                    }
Daniel Brown's avatar
Daniel Brown committed
351

352 353
                    if(get_field_index_from_tem(u+1, v) < inter.num_fields && u+v+1 <= inter.tem) {
                        uidx = get_field_index_from_tem(u+1, v);
354 355 356 357
                        // conjugate times knm_no_rgouy is to match up with previous results when no moment of inertia is present.
                        // Only tested against with a tilt applied to the mirror and a signal also applied
                        if(!n1->gnd_node) z_inc_z(&knmx_total_1, zc_by_zc(z_by_x(knmx1, sqrt(u+1)), mirror->knm_no_rgouy.k11[uidx][j]));
                        if(!n2->gnd_node) z_inc_z(&knmx_total_2, zc_by_zc(z_by_x(knmx2, sqrt(u+1)), mirror->knm_no_rgouy.k22[uidx][j]));
358 359
                    }
                    
360 361 362 363 364
                    if(!n1->gnd_node){
                        complex_t tmp1 = z_by_z(ac_1i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_1, u, v, u_, v_, knm_q.qxt1_11, knm_q.qxt2_11, knm_q.qyt1_11, knm_q.qyt2_11)));
                        z_inc_z(x_a1[suidx][i], tmp1);
                        z_inc_z(x_a1[slidx][i], cconj(tmp1));
                    }
365
                    
366 367 368 369 370
                    if(!n2->gnd_node){
                        complex_t tmp2 = z_by_z(ac_2i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_2, u, v, u_, v_, knm_q.qxt1_22, knm_q.qxt2_22, knm_q.qyt1_22, knm_q.qyt2_22)));
                        z_inc_z(x_a2[suidx][i], tmp2);
                        z_inc_z(x_a2[slidx][i], cconj(tmp2));
                    }
Daniel Brown's avatar
Daniel Brown committed
371 372 373 374 375 376
                }
            }
            
            if(mirror->Iy > 0){
                if(v > 0) { 
                    lidx = get_field_index_from_tem(u, v-1);
377 378
                    if(!n1->gnd_node) z_inc_z(&knmy_total_1, z_by_z(z_by_x(knmy1, sqrt(v)), mirror->knm_no_rgouy.k11[lidx][j]));
                    if(!n2->gnd_node) z_inc_z(&knmy_total_2, z_by_z(z_by_x(knmy2, sqrt(v)), mirror->knm_no_rgouy.k22[lidx][j]));
Daniel Brown's avatar
Daniel Brown committed
379 380 381
                }

                if(get_field_index_from_tem(u, v+1) < inter.num_fields && u+v+1 <= inter.tem) {
382
                    uidx = get_field_index_from_tem(u, v+1);
383 384
                    if(!n1->gnd_node) z_inc_z(&knmy_total_1, zc_by_z(z_by_x(knmy1, sqrt(v+1)), mirror->knm_no_rgouy.k11[uidx][j]));
                    if(!n2->gnd_node) z_inc_z(&knmy_total_2, zc_by_z(z_by_x(knmy2, sqrt(v+1)), mirror->knm_no_rgouy.k22[uidx][j]));
385
                }
386
                
387 388 389 390 391
                if(!n1->gnd_node){
                    complex_t tmp1 = z_by_z(ac_1i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_1, u, v, u_, v_, knm_q.qxt1_11, knm_q.qxt2_11, knm_q.qyt1_11, knm_q.qyt2_11)));
                    z_inc_z(y_a1[suidx][i], tmp1);
                    z_inc_z(y_a1[slidx][i], cconj(tmp1));
                }
Daniel Brown's avatar
Daniel Brown committed
392
                
393 394 395 396 397
                if(!n1->gnd_node) {
                    complex_t tmp2 = z_by_z(ac_2i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_2, u, v, u_, v_, knm_q.qxt1_22, knm_q.qxt2_22, knm_q.qyt1_22, knm_q.qyt2_22)));
                    z_inc_z(y_a2[suidx][i], tmp2);
                    z_inc_z(y_a2[slidx][i], cconj(tmp2));
                }
Daniel Brown's avatar
Daniel Brown committed
398 399
            }
        }
400 401
        
        // check if there is any tuning that needs to be applied
402
        if(mirror->phi != 0.0) {
403
            if(!n1->gnd_node) {
404 405 406 407 408 409 410 411
                if(mirror->Ix > 0) {
                    *x_a1[suidx][i] = z_by_z( *x_a1[suidx][i], tuning1u);
                    *x_a1[slidx][i] = z_by_zc( *x_a1[slidx][i], tuning1l);
                }
                if(mirror->Iy > 0) {
                    *y_a1[suidx][i] = z_by_z( *y_a1[suidx][i], tuning1u);
                    *y_a1[slidx][i] = z_by_zc( *y_a1[slidx][i], tuning1l);
                }
412 413
            }
            if(!n2->gnd_node) {
414 415 416 417 418 419 420 421
                if(mirror->Ix > 0) {
                    *x_a2[suidx][i] = z_by_z( *x_a2[suidx][i], tuning2u);
                    *x_a2[slidx][i] = z_by_zc( *x_a2[slidx][i], tuning2l);
                }
                if(mirror->Iy > 0) {
                    *y_a2[suidx][i] = z_by_z( *y_a2[suidx][i], tuning2u);
                    *y_a2[slidx][i] = z_by_zc( *y_a2[slidx][i], tuning2l);
                }
422 423
            }
        }
Daniel Brown's avatar
Daniel Brown committed
424 425 426
    }
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
void fill_bs_rot_motion_to_field(frequency_t *fcar, beamsplitter_t *bs, complex_t factor_x_a,
                              complex_t ***x_a1, complex_t ***x_a2, complex_t ***y_a1, complex_t ***y_a2,
                              complex_t ***x_a3, complex_t ***x_a4, complex_t ***y_a3, complex_t ***y_a4,
                              complex_t *ac_1i, complex_t *ac_2i, complex_t *ac_3i, complex_t *ac_4i,
                              complex_t tuning1u, complex_t tuning1l, complex_t tuning2u, complex_t tuning2l,
                              complex_t tuning3u, complex_t tuning3l, complex_t tuning4u, complex_t tuning4l) {
    int lidx, uidx;
    
    // get lower and upper sideband index
    int slidx = fcar->sig_lower->index; 
    int suidx = fcar->sig_upper->index;
    
    node_t *n1 = &inter.node_list[bs->node1_index];
    node_t *n2 = &inter.node_list[bs->node2_index];
    node_t *n3 = &inter.node_list[bs->node3_index];
    node_t *n4 = &inter.node_list[bs->node4_index];
        
    bs_knm_q_t knm_q = {{0}};
    
    get_refl_q_in_out_bs(bs, &knm_q);
                
    double nr1 = (n1->n) ? *n1->n : 1;
    double nr2 = (n3->n) ? *n3->n : 1;
    
    // we now compute the knmx/y coupling coefficient, note the q value we use here is q1' as this
    // is the expansion parameter used when separating all the coupling matrices so that later we multiply
    // Knmx from the left not the right.
    complex_t knmx1 = (!n1->gnd_node && bs->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_21, nr1), gouy(knm_q.qxt1_21)) : complex_0;
    complex_t knmy1 = (!n1->gnd_node && bs->Iy > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qyt1_21, nr1), gouy(knm_q.qyt1_21)) : complex_0;
    complex_t knmx2 = (!n2->gnd_node && bs->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_12, nr1), gouy(knm_q.qxt1_12)) : complex_0;
    complex_t knmy2 = (!n2->gnd_node && bs->Iy > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qyt1_12, nr1), gouy(knm_q.qyt1_12)) : complex_0;
    
    complex_t knmx3 = (!n3->gnd_node && bs->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_43, nr2), gouy(knm_q.qxt1_43)) : complex_0;
    complex_t knmx4 = (!n4->gnd_node && bs->Ix > 0) ? z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_34, nr2), gouy(knm_q.qxt1_34)) : complex_0;
    // extra minus sign here makes the result agree with mirror tilting from before.
    // not sure of the exact origin though...
    complex_t knmy3 = (!n3->gnd_node && bs->Iy > 0) ? z_by_xphr(complex_1,  -0.5 * w_size(knm_q.qyt1_43, nr2), gouy(knm_q.qyt1_43)) : complex_0;
    complex_t knmy4 = (!n4->gnd_node && bs->Iy > 0) ? z_by_xphr(complex_1,  -0.5 * w_size(knm_q.qyt1_34, nr2), gouy(knm_q.qyt1_34)) : complex_0;
    
    int i, j, u, v, u_, v_;
        
    for(i=0; i<inter.num_fields; i++){
        get_tem_modes_from_field_index(&u, &v, i);
        
        if(bs->Ix > 0){
            if(!n1->gnd_node){
                *x_a1[suidx][i] = complex_0;
                *x_a1[slidx][i] = complex_0;
            }
            if(!n2->gnd_node){
                *x_a2[suidx][i] = complex_0;
                *x_a2[slidx][i] = complex_0;
            }
            if(!n3->gnd_node){
                *x_a3[suidx][i] = complex_0;
                *x_a3[slidx][i] = complex_0;
            }
            if(!n4->gnd_node){
                *x_a4[suidx][i] = complex_0;
                *x_a4[slidx][i] = complex_0;
            }
        }
        
        if(bs->Iy > 0){
            if(!n1->gnd_node){
                *y_a1[suidx][i] = complex_0;
                *y_a1[slidx][i] = complex_0;
            }
            if(!n2->gnd_node){
                *y_a2[suidx][i] = complex_0;
                *y_a2[slidx][i] = complex_0;
            }
499
            if(!n3->gnd_node){
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
                *y_a3[suidx][i] = complex_0;
                *y_a3[slidx][i] = complex_0;
            }
            if(!n4->gnd_node){
                *y_a4[suidx][i] = complex_0;
                *y_a4[slidx][i] = complex_0;
            }
        }
        
        for(j=0; j<inter.num_fields; j++){
            get_tem_modes_from_field_index(&u_, &v_, j);
            
            complex_t knmx_total_1 = {0,0};
            complex_t knmx_total_2 = {0,0};
            complex_t knmy_total_1 = {0,0};
            complex_t knmy_total_2 = {0,0};
            complex_t knmx_total_3 = {0,0};
            complex_t knmx_total_4 = {0,0};
            complex_t knmy_total_3 = {0,0};
            complex_t knmy_total_4 = {0,0};
            
            // Here the two elements of the 
            if(bs->Ix > 0){
                if(v == v_){
                    
                    if(u > 0) { 
                        lidx = get_field_index_from_tem(u-1, v);
                        // conjugate times knm_no_rgouy is to match up with previous results when no moment of inertia is present.
                        // Only tested against with a tilt applied to the mirror and a signal also applied
                        if(!n1->gnd_node) z_inc_z(&knmx_total_1, z_by_zc(z_by_x(knmx1, sqrt(u)), bs->knm_no_rgouy.k21[lidx][j]));
                        if(!n2->gnd_node) z_inc_z(&knmx_total_2, z_by_zc(z_by_x(knmx2, sqrt(u)), bs->knm_no_rgouy.k12[lidx][j]));
                        if(!n3->gnd_node) z_inc_z(&knmx_total_3, z_by_zc(z_by_x(knmx3, sqrt(u)), bs->knm_no_rgouy.k43[lidx][j]));
                        if(!n4->gnd_node) z_inc_z(&knmx_total_4, z_by_zc(z_by_x(knmx4, sqrt(u)), bs->knm_no_rgouy.k34[lidx][j]));
                    }

                    if(get_field_index_from_tem(u+1, v) < inter.num_fields && u+v+1 <= inter.tem) {
                        uidx = get_field_index_from_tem(u+1, v);
                        // conjugate times knm_no_rgouy is to match up with previous results when no moment of inertia is present.
                        // Only tested against with a tilt applied to the mirror and a signal also applied
                        if(!n1->gnd_node) z_inc_z(&knmx_total_1, zc_by_zc(z_by_x(knmx1, sqrt(u+1)), bs->knm_no_rgouy.k21[uidx][j]));
                        if(!n2->gnd_node) z_inc_z(&knmx_total_2, zc_by_zc(z_by_x(knmx2, sqrt(u+1)), bs->knm_no_rgouy.k12[uidx][j]));
                        if(!n3->gnd_node) z_inc_z(&knmx_total_3, zc_by_zc(z_by_x(knmx3, sqrt(u+1)), bs->knm_no_rgouy.k43[uidx][j]));
                        if(!n4->gnd_node) z_inc_z(&knmx_total_4, zc_by_zc(z_by_x(knmx4, sqrt(u+1)), bs->knm_no_rgouy.k34[uidx][j]));
                    }
                    
545
                    if(!n1->gnd_node && !n2->gnd_node){
546 547 548
                        complex_t tmp1 = z_by_z(ac_2i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_1, u, v, u_, v_, knm_q.qxt1_21, knm_q.qxt2_21, knm_q.qyt1_21, knm_q.qyt2_21)));
                        z_inc_z(x_a1[suidx][i], tmp1);
                        z_inc_z(x_a1[slidx][i], cconj(tmp1));
549
                        
550 551 552 553 554
                        complex_t tmp2 = z_by_z(ac_1i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_2, u, v, u_, v_, knm_q.qxt1_12, knm_q.qxt2_12, knm_q.qyt1_12, knm_q.qyt2_12)));
                        z_inc_z(x_a2[suidx][i], tmp2);
                        z_inc_z(x_a2[slidx][i], cconj(tmp2));
                    }
                    
555
                    if(!n3->gnd_node && !n4->gnd_node){
556 557
                        complex_t tmp3 = z_by_z(ac_4i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_3, u, v, u_, v_, knm_q.qxt1_43, knm_q.qxt2_43, knm_q.qyt1_43, knm_q.qyt2_43)));
                        z_inc_z(x_a3[suidx][i], tmp3);
558 559
                        z_inc_z(x_a3[slidx][i], cconj(tmp3)); 
                        
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
                        complex_t tmp4 = z_by_z(ac_3i[j], z_by_z(factor_x_a, rev_gouy(knmx_total_4, u, v, u_, v_, knm_q.qxt1_34, knm_q.qxt2_34, knm_q.qyt1_34, knm_q.qyt2_34)));
                        z_inc_z(x_a4[suidx][i], tmp4);
                        z_inc_z(x_a4[slidx][i], cconj(tmp4));
                    }
                }
            }
            
            if(bs->Iy > 0){
                if(v > 0) { 
                    lidx = get_field_index_from_tem(u, v-1);
                    if(!n1->gnd_node) z_inc_z(&knmy_total_1, z_by_z(z_by_x(knmy1, sqrt(v)), bs->knm_no_rgouy.k21[lidx][j]));
                    if(!n2->gnd_node) z_inc_z(&knmy_total_2, z_by_z(z_by_x(knmy2, sqrt(v)), bs->knm_no_rgouy.k12[lidx][j]));
                    if(!n3->gnd_node) z_inc_z(&knmy_total_3, z_by_z(z_by_x(knmy3, sqrt(v)), bs->knm_no_rgouy.k43[lidx][j]));
                    if(!n4->gnd_node) z_inc_z(&knmy_total_4, z_by_z(z_by_x(knmy4, sqrt(v)), bs->knm_no_rgouy.k34[lidx][j]));
                    
                }

                if(get_field_index_from_tem(u, v+1) < inter.num_fields && u+v+1 <= inter.tem) {
                    uidx = get_field_index_from_tem(u, v+1);
                    if(!n1->gnd_node) z_inc_z(&knmy_total_1, zc_by_z(z_by_x(knmy1, sqrt(v+1)), bs->knm_no_rgouy.k21[uidx][j]));
                    if(!n2->gnd_node) z_inc_z(&knmy_total_2, zc_by_z(z_by_x(knmy2, sqrt(v+1)), bs->knm_no_rgouy.k12[uidx][j]));
                    if(!n3->gnd_node) z_inc_z(&knmy_total_3, zc_by_z(z_by_x(knmy3, sqrt(v+1)), bs->knm_no_rgouy.k43[uidx][j]));
                    if(!n4->gnd_node) z_inc_z(&knmy_total_4, zc_by_z(z_by_x(knmy4, sqrt(v+1)), bs->knm_no_rgouy.k34[uidx][j]));
                }
                
585
                if(!n2->gnd_node && !n1->gnd_node){
586 587 588
                    complex_t tmp1 = z_by_z(ac_2i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_1, u, v, u_, v_, knm_q.qxt1_21, knm_q.qxt2_21, knm_q.qyt1_21, knm_q.qyt2_21)));
                    z_inc_z(y_a1[suidx][i], tmp1);
                    z_inc_z(y_a1[slidx][i], cconj(tmp1));
589
                    
590 591 592 593 594
                    complex_t tmp2 = z_by_z(ac_1i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_2, u, v, u_, v_, knm_q.qxt1_12, knm_q.qxt2_12, knm_q.qyt1_12, knm_q.qyt2_12)));
                    z_inc_z(y_a2[suidx][i], tmp2);
                    z_inc_z(y_a2[slidx][i], cconj(tmp2));
                }
                
595
                if(!n4->gnd_node && !n3->gnd_node) {
596 597
                    complex_t tmp3 = z_by_z(ac_4i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_3, u, v, u_, v_, knm_q.qxt1_43, knm_q.qxt2_43, knm_q.qyt1_43, knm_q.qyt2_43)));
                    z_inc_z(y_a3[suidx][i], tmp3);
598
                    z_inc_z(y_a3[slidx][i], cconj(tmp3)); 
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
                    complex_t tmp4 = z_by_z(ac_3i[j], z_by_z(factor_x_a, rev_gouy(knmy_total_4, u, v, u_, v_, knm_q.qxt1_34, knm_q.qxt2_34, knm_q.qyt1_34, knm_q.qyt2_34)));
                    z_inc_z(y_a4[suidx][i], tmp4);
                    z_inc_z(y_a4[slidx][i], cconj(tmp4));
                }
            }
        }
        
        // check if there is any tuning that needs to be applied
        if(bs->phi != 0.0) {
            if(!n1->gnd_node) {
                if(bs->Ix > 0) {
                    *x_a1[suidx][i] = z_by_z( *x_a1[suidx][i], tuning1u);
                    *x_a1[slidx][i] = z_by_zc( *x_a1[slidx][i], tuning1l);
                }
                if(bs->Iy > 0) {
                    *y_a1[suidx][i] = z_by_z( *y_a1[suidx][i], tuning1u);
                    *y_a1[slidx][i] = z_by_zc( *y_a1[slidx][i], tuning1l);
                }
            }
            if(!n2->gnd_node) {
                if(bs->Ix > 0) {
                    *x_a2[suidx][i] = z_by_z( *x_a2[suidx][i], tuning2u);
                    *x_a2[slidx][i] = z_by_zc( *x_a2[slidx][i], tuning2l);
                }
                if(bs->Iy > 0) {
                    *y_a2[suidx][i] = z_by_z( *y_a2[suidx][i], tuning2u);
                    *y_a2[slidx][i] = z_by_zc( *y_a2[slidx][i], tuning2l);
                }
            }
            if(!n3->gnd_node) {
                if(bs->Ix > 0) {
                    *x_a3[suidx][i] = z_by_z( *x_a3[suidx][i], tuning3u);
                    *x_a3[slidx][i] = z_by_zc( *x_a3[slidx][i], tuning3l);
                }
                if(bs->Iy > 0) {
                    *y_a3[suidx][i] = z_by_z( *y_a3[suidx][i], tuning3u);
                    *y_a3[slidx][i] = z_by_zc( *y_a3[slidx][i], tuning3l);
                }
            }
            if(!n4->gnd_node) {
                if(bs->Ix > 0) {
                    *x_a4[suidx][i] = z_by_z( *x_a4[suidx][i], tuning4u);
                    *x_a4[slidx][i] = z_by_zc( *x_a4[slidx][i], tuning4l);
                }
                if(bs->Iy > 0) {
                    *y_a4[suidx][i] = z_by_z( *y_a4[suidx][i], tuning4u);
                    *y_a4[slidx][i] = z_by_zc( *y_a4[slidx][i], tuning4l);
                }
            }
        }
    }
}

Daniel Brown's avatar
Daniel Brown committed
652
/**
653
 * Fills in the matrix elements coupling incoming and outgoing fields on either
Daniel Brown's avatar
Daniel Brown committed
654 655
 * side of an optic to either x or y rotation.
 */
656
void fill_mirror_field_to_rot_motion(motion_type_t type, mirror_t *mirror, complex_t factor_a_x, frequency_t *fcar,
Daniel Brown's avatar
Daniel Brown committed
657
                                complex_t ***a1i_x, complex_t ***a1o_x, complex_t ***a2i_x, complex_t ***a2o_x,
658
                                complex_t *ac_1i, complex_t *ac_1o, complex_t *ac_2i, complex_t *ac_2o){
659
    int i, u, v;
Daniel Brown's avatar
Daniel Brown committed
660 661 662 663 664
    
    // get lower and upper sideband index
    int slidx = fcar->sig_lower->index; 
    int suidx = fcar->sig_upper->index;
    
665 666
    node_t *n1 = &inter.node_list[mirror->node1_index];
    node_t *n2 = &inter.node_list[mirror->node2_index];
Daniel Brown's avatar
Daniel Brown committed
667
        
668
    mirror_knm_q_t knm_q = {{0}};
669
    get_refl_q_in_out_mirror(mirror, &knm_q);
670 671 672

    double nr1 = (n1->n) ? *n1->n : 1;
    double nr2 = (n2->n) ? *n2->n : 1;
Daniel Brown's avatar
Daniel Brown committed
673 674 675
    
    // compute knm values with reverse gouy already applied, i.e don't add it in the
    // first place...
676 677 678 679 680 681 682 683
    complex_t knmx1i = (n1->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_11, nr1), 0);
    complex_t knmx1o = (n1->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_11, nr1), 0);
    complex_t knmx2i = (n2->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_22, nr2), 0);
    complex_t knmx2o = (n2->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_22, nr2), 0);
    
    // this is the usual minus sign you get for all non-diagonal 
    // values in the final matrix
    factor_a_x = cminus(factor_a_x);
Daniel Brown's avatar
Daniel Brown committed
684 685 686 687
    
    for(i=0; i<inter.num_fields; i++){
        get_tem_modes_from_field_index(&u, &v, i);
        
688 689 690 691 692
        complex_t gamma_i_1 = complex_0;
        complex_t gamma_o_1 = complex_0;
        complex_t gamma_i_2 = complex_0;
        complex_t gamma_o_2 = complex_0;
        
693 694
        double sqrtmax;
        
695 696
        if((u > 0 && type == ROTX) || (v > 0 && type == ROTY)) { 
            int lidx = 0;
697 698
            // the minus signs decided here we done mostly by computing the required
            // number of minus signs, and then some trial and error...
699 700
            if(type == ROTX){
                lidx = get_field_index_from_tem(u-1, v);
701
                sqrtmax = sqrt(u);
702 703
                
                if(!n1->gnd_node) {
704 705
                    z_inc_z(&gamma_i_1, z_by_x(z_by_zc(knmx1i, ac_1i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(z_by_zc(knmx1o, ac_1o[lidx]), -sqrtmax));
706 707 708
                }

                if(!n2->gnd_node) {
709 710
                    z_inc_z(&gamma_i_2, z_by_x(z_by_zc(knmx2i, ac_2i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(z_by_zc(knmx2o, ac_2o[lidx]), -sqrtmax));
711 712
                }
                
713 714
            } else {
                lidx = get_field_index_from_tem(u, v-1);
715
                sqrtmax = sqrt(v);
716 717 718 719 720
             
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(z_by_zc(knmx1i, ac_1i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(z_by_zc(knmx1o, ac_1o[lidx]), sqrtmax));
                }
721

722 723 724 725 726
                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(z_by_zc(knmx2i, ac_2i[lidx]), -sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(z_by_zc(knmx2o, ac_2o[lidx]), -sqrtmax));
                }
            }            
727
        }
728

729 730 731 732 733 734 735 736 737
        int uidx = 0;
            
        if(type == ROTX){
            uidx = get_field_index_from_tem(u+1, v);
        } else {
            uidx = get_field_index_from_tem(u, v+1);
        }
        
        if(uidx < inter.num_fields && u+v+1 <= inter.tem) {
738

739
            // the minus signs decided here we done mostly by computing the required
740 741
            // number of minus signs, and then some trial and error so an offset
            // beam amplitude modulated rotates the mirror in the right direction(phase)
742
            if(type == ROTX){
743
                sqrtmax = sqrt(u+1);
744 745
                
                if(!n1->gnd_node) {
746 747
                    z_inc_z(&gamma_i_1, z_by_x(zc_by_zc(knmx1i, ac_1i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(zc_by_zc(knmx1o, ac_1o[uidx]), -sqrtmax));
748 749 750
                }

                if(!n2->gnd_node) {
751 752
                    z_inc_z(&gamma_i_2, z_by_x(zc_by_zc(knmx2i, ac_2i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(zc_by_zc(knmx2o, ac_2o[uidx]), -sqrtmax));
753 754
                }

755
            } else {
756 757 758 759 760 761 762 763
                // there is no msign minus factor for the y motion 
                // but the node 2 side is a negative contribution
                sqrtmax = sqrt(v+1);
                
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(zc_by_zc(knmx1i, ac_1i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(zc_by_zc(knmx1o, ac_1o[uidx]), sqrtmax));
                }
764

765 766 767 768
                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(zc_by_zc(knmx2i, ac_2i[uidx]), -sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(zc_by_zc(knmx2o, ac_2o[uidx]), -sqrtmax));
                }
769 770
            }

771
            
772 773 774
        }
        
        if(!n1->gnd_node) {
775 776 777 778
            *(a1i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_1);
            *(a1o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_1);
            *(a1i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_1);
            *(a1o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_1);
779 780 781
        }

        if(!n2->gnd_node) {
782 783 784 785
            *(a2i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_2);
            *(a2o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_2);
            *(a2i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_2);
            *(a2o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_2);
786
        }
787
    }
788 789
}

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
/**
 * Fills in the matrix elements coupling incoming and outgoing fields on either
 * side of an optic to either x or y rotation.
 */
void fill_bs_field_to_rot_motion(motion_type_t type, beamsplitter_t *bs, complex_t factor_a_x, frequency_t *fcar,
                                complex_t ***a1i_x, complex_t ***a1o_x, complex_t ***a2i_x, complex_t ***a2o_x,
                                complex_t ***a3i_x, complex_t ***a3o_x, complex_t ***a4i_x, complex_t ***a4o_x,
                                complex_t *ac_1i, complex_t *ac_1o, complex_t *ac_2i, complex_t *ac_2o,
                                complex_t *ac_3i, complex_t *ac_3o, complex_t *ac_4i, complex_t *ac_4o){
    int i, u, v;
    
    // get lower and upper sideband index
    int slidx = fcar->sig_lower->index; 
    int suidx = fcar->sig_upper->index;
    
    node_t *n1 = &inter.node_list[bs->node1_index];
    node_t *n2 = &inter.node_list[bs->node2_index];
    node_t *n3 = &inter.node_list[bs->node3_index];
    node_t *n4 = &inter.node_list[bs->node4_index];
    
    bs_knm_q_t knm_q = {{0}};
    get_refl_q_in_out_bs(bs, &knm_q);

813 814
    double nr1=1, nr2=1;
    bs_get_nr(bs, &nr1, &nr2);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    
    // compute knm values with reverse gouy already applied, i.e don't add it in the
    // first place...
    complex_t knmx1i = (n1->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_21, nr1), 0);
    complex_t knmx1o = (n1->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_21, nr1), 0);
    complex_t knmx2i = (n2->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_12, nr1), 0);
    complex_t knmx2o = (n2->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_12, nr1), 0);
    
    complex_t knmx3i = (n3->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_43, nr2), 0);
    complex_t knmx3o = (n3->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_43, nr2), 0);
    complex_t knmx4i = (n4->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt1_34, nr2), 0);
    complex_t knmx4o = (n4->gnd_node) ? complex_0 : z_by_xphr(complex_1,  0.5 * w_size(knm_q.qxt2_34, nr2), 0);
    
    // this is the usual minus sign you get for all non-diagonal 
    // values in the final matrix
    factor_a_x = cminus(factor_a_x);
    
    for(i=0; i<inter.num_fields; i++){
        get_tem_modes_from_field_index(&u, &v, i);
        
        complex_t gamma_i_1 = complex_0;
        complex_t gamma_o_1 = complex_0;
        complex_t gamma_i_2 = complex_0;
        complex_t gamma_o_2 = complex_0;
        complex_t gamma_i_3 = complex_0;
        complex_t gamma_o_3 = complex_0;
        complex_t gamma_i_4 = complex_0;
        complex_t gamma_o_4 = complex_0;
        
        double sqrtmax;
        
        if((u > 0 && type == ROTX) || (v > 0 && type == ROTY)) { 
            int lidx = 0;
            // the minus signs decided here we done mostly by computing the required
            // number of minus signs, and then some trial and error...
            if(type == ROTX){
                lidx = get_field_index_from_tem(u-1, v);
                sqrtmax = sqrt(u);
                
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(z_by_zc(knmx1i, ac_1i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(z_by_zc(knmx1o, ac_1o[lidx]), -sqrtmax));
                }

                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(z_by_zc(knmx2i, ac_2i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(z_by_zc(knmx2o, ac_2o[lidx]), -sqrtmax));
                }
                
                if(!n3->gnd_node) {
                    z_inc_z(&gamma_i_3, z_by_x(z_by_zc(knmx3i, ac_3i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_3, z_by_x(z_by_zc(knmx3o, ac_3o[lidx]), -sqrtmax));
                }

                if(!n4->gnd_node) {
                    z_inc_z(&gamma_i_4, z_by_x(z_by_zc(knmx4i, ac_4i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_4, z_by_x(z_by_zc(knmx4o, ac_4o[lidx]), -sqrtmax));
                }
                
            } else {
                lidx = get_field_index_from_tem(u, v-1);
                sqrtmax = sqrt(v);
             
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(z_by_zc(knmx1i, ac_1i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(z_by_zc(knmx1o, ac_1o[lidx]), sqrtmax));
                }

                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(z_by_zc(knmx2i, ac_2i[lidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(z_by_zc(knmx2o, ac_2o[lidx]), sqrtmax));
                }

                if(!n3->gnd_node) {
                    z_inc_z(&gamma_i_3, z_by_x(z_by_zc(knmx3i, ac_3i[lidx]), -sqrtmax));
                    z_inc_z(&gamma_o_3, z_by_x(z_by_zc(knmx3o, ac_3o[lidx]), -sqrtmax));
                }
                
                if(!n4->gnd_node) {
                    z_inc_z(&gamma_i_4, z_by_x(z_by_zc(knmx4i, ac_4i[lidx]), -sqrtmax));
                    z_inc_z(&gamma_o_4, z_by_x(z_by_zc(knmx4o, ac_4o[lidx]), -sqrtmax));
                }
            }            
        }

        int uidx = 0;
            
        if(type == ROTX){
            uidx = get_field_index_from_tem(u+1, v);
        } else {
            uidx = get_field_index_from_tem(u, v+1);
        }
        
        if(uidx < inter.num_fields && u+v+1 <= inter.tem) {

            // the minus signs decided here we done mostly by computing the required
            // number of minus signs, and then some trial and error so an offset
            // beam amplitude modulated rotates the mirror in the right direction(phase)
            if(type == ROTX){
                sqrtmax = sqrt(u+1);
                
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(zc_by_zc(knmx1i, ac_1i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(zc_by_zc(knmx1o, ac_1o[uidx]), -sqrtmax));
                }

                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(zc_by_zc(knmx2i, ac_2i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(zc_by_zc(knmx2o, ac_2o[uidx]), -sqrtmax));
                }

                if(!n3->gnd_node) {
                    z_inc_z(&gamma_i_3, z_by_x(zc_by_zc(knmx3i, ac_3i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_3, z_by_x(zc_by_zc(knmx3o, ac_3o[uidx]), -sqrtmax));
                }

                if(!n4->gnd_node) {
                    z_inc_z(&gamma_i_4, z_by_x(zc_by_zc(knmx4i, ac_4i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_4, z_by_x(zc_by_zc(knmx4o, ac_4o[uidx]), -sqrtmax));
                }
                
            } else {
                // there is no msign minus factor for the y motion 
                // but the node 2 side is a negative contribution
                sqrtmax = sqrt(v+1);
                
                if(!n1->gnd_node) {
                    z_inc_z(&gamma_i_1, z_by_x(zc_by_zc(knmx1i, ac_1i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_1, z_by_x(zc_by_zc(knmx1o, ac_1o[uidx]), sqrtmax));
                }

                if(!n2->gnd_node) {
                    z_inc_z(&gamma_i_2, z_by_x(zc_by_zc(knmx2i, ac_2i[uidx]), sqrtmax));
                    z_inc_z(&gamma_o_2, z_by_x(zc_by_zc(knmx2o, ac_2o[uidx]), sqrtmax));
                }

                if(!n3->gnd_node) {
                    z_inc_z(&gamma_i_3, z_by_x(zc_by_zc(knmx2i, ac_3i[uidx]), -sqrtmax));
                    z_inc_z(&gamma_o_3, z_by_x(zc_by_zc(knmx2o, ac_3o[uidx]), -sqrtmax));
                }
                
                if(!n4->gnd_node) {
                    z_inc_z(&gamma_i_4, z_by_x(zc_by_zc(knmx4i, ac_4i[uidx]), -sqrtmax));
                    z_inc_z(&gamma_o_4, z_by_x(zc_by_zc(knmx4o, ac_4o[uidx]), -sqrtmax));
                }
            }
        }
        
        if(!n1->gnd_node) {
            *(a1i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_1);
            *(a1o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_1);
            *(a1i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_1);
            *(a1o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_1);
        }

        if(!n2->gnd_node) {
            *(a2i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_2);
            *(a2o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_2);
            *(a2i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_2);
            *(a2o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_2);
        }
        
        if(!n3->gnd_node) {
            *(a3i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_3);
            *(a3o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_3);
            *(a3i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_3);
            *(a3o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_3);
        }

        if(!n4->gnd_node) {
            *(a4i_x[suidx][i]) = z_by_z(factor_a_x, gamma_i_4);
            *(a4o_x[suidx][i]) = z_by_z(factor_a_x, gamma_o_4);
            *(a4i_x[slidx][i]) = z_by_zc(factor_a_x, gamma_i_4);
            *(a4o_x[slidx][i]) = z_by_zc(factor_a_x, gamma_o_4);
        }
    }
}

993
void fill_surf_motion_coupling(int motion_idx, int surface_idx, mirror_t *m, complex_t factor_a_x, complex_t factor_x_a, frequency_t *fcar,
994 995
        complex_t *__restrict__ ac_1i, complex_t *__restrict__ ac_1o,
        complex_t *__restrict__ ac_2i, complex_t *__restrict__ ac_2o,
996
        complex_t tuning1u, complex_t tuning1l, complex_t tuning2u, complex_t tuning2l){
997 998 999 1000
    
    node_t *n1 = &inter.node_list[m->node1_index];
    node_t *n2 = &inter.node_list[m->node2_index];
    
1001
    int i;
1002
    
1003 1004
    int slidx = fcar->sig_lower->index;
    int suidx = fcar->sig_upper->index;
1005
        
1006 1007 1008
    complex_t *__restrict__ tmp = (complex_t*) malloc(inter.num_fields * sizeof(complex_t));
    
    if(!n1->gnd_node){
1009 1010 1011 1012 1013 1014 1015 1016
        complex_t *__restrict__ Knm1o = &(m->knm_surf_motion_1o[surface_idx][0][0]);
        complex_t *__restrict__ Knm1i = &(m->knm_surf_motion_1i[surface_idx][0][0]);
        complex_t **__restrict__ a1il_x = m->a1i_x[motion_idx][slidx];
        complex_t **__restrict__ a1ol_x = m->a1o_x[motion_idx][slidx];
        complex_t **__restrict__ a1iu_x = m->a1i_x[motion_idx][suidx];
        complex_t **__restrict__ a1ou_x = m->a1o_x[motion_idx][suidx];
        complex_t **__restrict__ x_a1l = m->x_a1[motion_idx][slidx];
        complex_t **__restrict__ x_a1u = m->x_a1[motion_idx][suidx];
1017
        
1018 1019 1020 1021 1022 1023 1024 1025 1026
        /*
         * Compute motion to field coupling
         */
        
        complex_t fac = cminus(factor_x_a);
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
1027
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1028 1029
                    &fac, &(m->knm_surf_x_a_1[surface_idx][0][0]), inter.num_fields,
                    ac_1i, 1, &complex_0, x_a1u[0], 1);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        
        // lower sideband creation is simply the conjugate of the upper
        // plus we must also propagate the new sidebands away from the mirror
        for(i=0; i<inter.num_fields; i++) {
            *x_a1l[i] = cconj(*x_a1u[i]);
        }
        
        if(m->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a1u[i] = z_by_z(*x_a1u[i], tuning1u);
                *x_a1l[i] = z_by_zc(*x_a1l[i], tuning1l);
            }
        }
        
        /*
         * Compute Field to motion coupling
         */
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
                    &complex_m1, Knm1o, inter.num_fields, ac_1o, 1, &complex_0, tmp, 1);
1049 1050

        for(i=0; i<inter.num_fields; i++) {
1051 1052
            *a1ou_x[i] = z_by_zc(factor_a_x, tmp[i]);
            *a1ol_x[i] = z_by_z(factor_a_x, tmp[i]);
1053 1054 1055
        }

        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1056
                    &complex_m1, Knm1i, inter.num_fields, ac_1i, 1, &complex_0, tmp, 1);
1057 1058

        for(i=0; i<inter.num_fields; i++) {
1059 1060
            *a1iu_x[i] = z_by_zc(factor_a_x, tmp[i]);
            *a1il_x[i] = z_by_z(factor_a_x, tmp[i]);
1061 1062 1063 1064
        }
    }
    
    if(!n2->gnd_node){
1065 1066 1067 1068 1069 1070 1071 1072
        complex_t *__restrict__ Knm2o = &(m->knm_surf_motion_2o[surface_idx][0][0]);
        complex_t *__restrict__ Knm2i = &(m->knm_surf_motion_2i[surface_idx][0][0]);
        complex_t **__restrict__ a2il_x = m->a2i_x[motion_idx][slidx];
        complex_t **__restrict__ a2ol_x = m->a2o_x[motion_idx][slidx];
        complex_t **__restrict__ a2iu_x = m->a2i_x[motion_idx][suidx];
        complex_t **__restrict__ a2ou_x = m->a2o_x[motion_idx][suidx];
        complex_t **__restrict__ x_a2l = m->x_a2[motion_idx][slidx];
        complex_t **__restrict__ x_a2u = m->x_a2[motion_idx][suidx];
1073
        complex_t fac = factor_x_a;
Daniel Brown's avatar
Daniel Brown committed
1074 1075 1076 1077
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
1078
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1079
                    &fac, &(m->knm_surf_x_a_2[surface_idx][0][0]), inter.num_fields, ac_2i, 1, &complex_0, x_a2u[0], 1);
1080
        
1081 1082
        // lower sideband is simply the conjugate of the upper
        // plus we must also propagate the new sidebands away from the mirror
1083
        for(i=0; i<inter.num_fields; i++) {
1084 1085 1086 1087 1088 1089 1090 1091
            *x_a2l[i] = cconj(*x_a2u[i]);
        }
        
        if(m->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a2u[i] = z_by_z(*x_a2u[i], tuning2u);
                *x_a2l[i] = z_by_zc(*x_a2l[i], tuning2l);
            }
1092
        }
Daniel Brown's avatar
Daniel Brown committed
1093
        
1094
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1095
                    &complex_1, Knm2o, inter.num_fields, ac_2o, 1, &complex_0, tmp, 1);
1096 1097
        
        for(i=0; i<inter.num_fields; i++) {
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
            *a2ou_x[i] = z_by_zc(factor_a_x,  tmp[i]);
            *a2ol_x[i] = z_by_z(factor_a_x, tmp[i]);
        }

        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
                    &complex_1, Knm2i, inter.num_fields, ac_2i, 1, &complex_0, tmp, 1);

        for(i=0; i<inter.num_fields; i++) {
            *a2iu_x[i] = z_by_zc(factor_a_x, tmp[i]);
            *a2il_x[i] = z_by_z(factor_a_x, tmp[i]);
1108 1109 1110
        }
    }
    
1111
    free(tmp);
1112 1113
}

Daniel Brown's avatar
Daniel Brown committed
1114
void fill_mirror_z_motion_coupling(int k, mirror_t *m, complex_t factor_a_x, complex_t factor_x_a, frequency_t *fcar,
1115
        complex_t *__restrict__ ac_1i, complex_t *__restrict__ ac_1o, complex_t *__restrict__ ac_2i, complex_t *__restrict__ ac_2o,
1116
        complex_t tuning1u, complex_t tuning1l, complex_t tuning2u, complex_t tuning2l){
1117 1118 1119 1120 1121 1122 1123 1124
    
    node_t *n1 = &inter.node_list[m->node1_index];
    node_t *n2 = &inter.node_list[m->node2_index];
    
    int i;
    
    int slidx = fcar->sig_lower->index;
    int suidx = fcar->sig_upper->index;
1125

1126 1127 1128 1129 1130 1131 1132
    if(!n1->gnd_node){
        complex_t **__restrict__ a1il_x = m->a1i_x[k][slidx];
        complex_t **__restrict__ a1ol_x = m->a1o_x[k][slidx];
        complex_t **__restrict__ a1iu_x = m->a1i_x[k][suidx];
        complex_t **__restrict__ a1ou_x = m->a1o_x[k][suidx];
        complex_t **__restrict__ x_a1l = m->x_a1[k][slidx];
        complex_t **__restrict__ x_a1u = m->x_a1[k][suidx];
1133 1134 1135
        
        complex_t fac_x_a = cminus(factor_x_a);
        
1136 1137 1138 1139
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1140
                    &fac_x_a, &(m->knm.k11[0][0]), inter.num_fields, ac_1i, 1, &complex_0, x_a1u[0], 1);
1141
        
1142 1143
        complex_t fac_a_x = cminus(factor_a_x);
                        
1144 1145 1146
        // lower sideband is simply the conjugate of the upper
        // plus we must also propagate the new sidebands away from the mirror
        for(i=0; i<inter.num_fields; i++) {
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
            *x_a1l[i] = cconj(*x_a1u[i]);
                     
            *a1ou_x[i] = z_by_zc(fac_a_x, ac_1o[i]);
            *a1ol_x[i] = z_by_z(fac_a_x, ac_1o[i]);
            *a1iu_x[i] = z_by_zc(fac_a_x, ac_1i[i]);
            *a1il_x[i] = z_by_z(fac_a_x, ac_1i[i]);
        }
        
        if(m->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a1u[i] = z_by_z(*x_a1u[i], tuning1u);
                *x_a1l[i] = z_by_zc(*x_a1l[i], tuning1l);
            }
1160 1161 1162 1163 1164 1165
        }
    }
    
    if(!n2->gnd_node){
        complex_t **__restrict__ x_a2l = m->x_a2[k][slidx];
        complex_t **__restrict__ x_a2u = m->x_a2[k][suidx];
1166 1167 1168 1169 1170
        complex_t **__restrict__ a2il_x = m->a2i_x[k][slidx];
        complex_t **__restrict__ a2ol_x = m->a2o_x[k][slidx];
        complex_t **__restrict__ a2iu_x = m->a2i_x[k][suidx];
        complex_t **__restrict__ a2ou_x = m->a2o_x[k][suidx];
        
1171 1172 1173 1174
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
1175 1176 1177
                    &factor_x_a, &(m->knm.k22[0][0]), inter.num_fields, ac_2i, 1, &complex_0, x_a2u[0], 1);
        
        complex_t fac_a_x = factor_a_x;
1178 1179
        
        for(i=0; i<inter.num_fields; i++) {
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
            *x_a2l[i] = cconj(*x_a2u[i]);
            
            *a2ou_x[i] = z_by_zc(fac_a_x,  ac_2o[i]);
            *a2ol_x[i] = z_by_z(fac_a_x, ac_2o[i]);
            *a2iu_x[i] = z_by_zc(fac_a_x,  ac_2i[i]);
            *a2il_x[i] = z_by_z(fac_a_x, ac_2i[i]);
        }
        
        if(m->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a2u[i] = z_by_z(*x_a2u[i], tuning2u);
1191
                *x_a2l[i] = z_by_zc(*x_a2l[i], tuning2l);
1192
            }
1193 1194 1195 1196
        }
    }
}

Daniel Brown's avatar
Daniel Brown committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
void fill_bs_z_motion_coupling(int k, beamsplitter_t *bs, complex_t factor_a_x, complex_t factor_x_a, frequency_t *fcar,
        complex_t *__restrict__ ac_1i, complex_t *__restrict__ ac_1o, complex_t *__restrict__ ac_2i, complex_t *__restrict__ ac_2o,
        complex_t *__restrict__ ac_3i, complex_t *__restrict__ ac_3o, complex_t *__restrict__ ac_4i, complex_t *__restrict__ ac_4o,
        complex_t tuning1u, complex_t tuning1l, complex_t tuning2u, complex_t tuning2l,
        complex_t tuning3u, complex_t tuning3l, complex_t tuning4u, complex_t tuning4l){
    
    node_t *n1 = &inter.node_list[bs->node1_index];
    node_t *n2 = &inter.node_list[bs->node2_index];
    node_t *n3 = &inter.node_list[bs->node3_index];
    node_t *n4 = &inter.node_list[bs->node4_index];
    
    int i;
    
    int slidx = fcar->sig_lower->index;
    int suidx = fcar->sig_upper->index;

1213
    if(!n1->gnd_node && !n2->gnd_node){
Daniel Brown's avatar
Daniel Brown committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
        complex_t **__restrict__ a1il_x = bs->a1i_x[k][slidx];
        complex_t **__restrict__ a1ol_x = bs->a1o_x[k][slidx];
        complex_t **__restrict__ a1iu_x = bs->a1i_x[k][suidx];
        complex_t **__restrict__ a1ou_x = bs->a1o_x[k][suidx];
        complex_t **__restrict__ x_a1l = bs->x_a1[k][slidx];
        complex_t **__restrict__ x_a1u = bs->x_a1[k][suidx];
        
        complex_t fac_x_a = cminus(factor_x_a);
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
Daniel Brown's avatar
Daniel Brown committed
1227
                    &fac_x_a, &(bs->knm.k21[0][0]), inter.num_fields, ac_2i, 1, &complex_0, x_a1u[0], 1);
Daniel Brown's avatar
Daniel Brown committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
        
        complex_t fac_a_x = cminus(factor_a_x);
                        
        // lower sideband is simply the conjugate of the upper
        // plus we must also propagate the new sidebands away from the mirror
        for(i=0; i<inter.num_fields; i++) {
            *x_a1l[i] = cconj(*x_a1u[i]);
                     
            *a1ou_x[i] = z_by_zc(fac_a_x, ac_1o[i]);
            *a1ol_x[i] = z_by_z(fac_a_x, ac_1o[i]);
            *a1iu_x[i] = z_by_zc(fac_a_x, ac_1i[i]);
            *a1il_x[i] = z_by_z(fac_a_x, ac_1i[i]);
        }
        
        if(bs->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a1u[i] = z_by_z(*x_a1u[i], tuning1u);
                *x_a1l[i] = z_by_zc(*x_a1l[i], tuning1l);
            }
        }
1248
        
Daniel Brown's avatar
Daniel Brown committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
        complex_t **__restrict__ a2il_x = bs->a2i_x[k][slidx];
        complex_t **__restrict__ a2ol_x = bs->a2o_x[k][slidx];
        complex_t **__restrict__ a2iu_x = bs->a2i_x[k][suidx];
        complex_t **__restrict__ a2ou_x = bs->a2o_x[k][suidx];
        complex_t **__restrict__ x_a2l = bs->x_a2[k][slidx];
        complex_t **__restrict__ x_a2u = bs->x_a2[k][suidx];
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
Daniel Brown's avatar
Daniel Brown committed
1260
                    &fac_x_a, &(bs->knm.k12[0][0]), inter.num_fields, ac_1i, 1, &complex_0, x_a2u[0], 1);
Daniel Brown's avatar
Daniel Brown committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
        
        // lower sideband is simply the conjugate of the upper
        // plus we must also propagate the new sidebands away from the mirror
        for(i=0; i<inter.num_fields; i++) {
            *x_a2l[i] = cconj(*x_a2u[i]);
                     
            *a2ou_x[i] = z_by_zc(fac_a_x, ac_2o[i]);
            *a2ol_x[i] = z_by_z(fac_a_x, ac_2o[i]);
            *a2iu_x[i] = z_by_zc(fac_a_x, ac_2i[i]);
            *a2il_x[i] = z_by_z(fac_a_x, ac_2i[i]);
        }
        
        if(bs->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a2u[i] = z_by_z(*x_a2u[i], tuning2u);
                *x_a2l[i] = z_by_zc(*x_a2l[i], tuning2l);
            }
        }
    }
    
1281
    if(!n3->gnd_node && !n4->gnd_node){
Daniel Brown's avatar
Daniel Brown committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
        complex_t **__restrict__ x_a3l = bs->x_a3[k][slidx];
        complex_t **__restrict__ x_a3u = bs->x_a3[k][suidx];
        complex_t **__restrict__ a3il_x = bs->a3i_x[k][slidx];
        complex_t **__restrict__ a3ol_x = bs->a3o_x[k][slidx];
        complex_t **__restrict__ a3iu_x = bs->a3i_x[k][suidx];
        complex_t **__restrict__ a3ou_x = bs->a3o_x[k][suidx];
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
Daniel Brown's avatar
Daniel Brown committed
1293
                    &factor_x_a, &(bs->knm.k43[0][0]), inter.num_fields, ac_4i, 1, &complex_0, x_a3u[0], 1);
Daniel Brown's avatar
Daniel Brown committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
        
        complex_t fac_a_x = factor_a_x;
        
        for(i=0; i<inter.num_fields; i++) {
            *x_a3l[i] = cconj(*x_a3u[i]);
            
            *a3ou_x[i] = z_by_zc(fac_a_x,  ac_3o[i]);
            *a3ol_x[i] = z_by_z(fac_a_x, ac_3o[i]);
            *a3iu_x[i] = z_by_zc(fac_a_x,  ac_3i[i]);
            *a3il_x[i] = z_by_z(fac_a_x, ac_3i[i]);
        }
        
        if(bs->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a3u[i] = z_by_z(*x_a3u[i], tuning3u);
1309
                *x_a3l[i] = z_by_zc(*x_a3l[i], tuning3l);
Daniel Brown's avatar
Daniel Brown committed
1310 1311
            }
        }
1312
        
Daniel Brown's avatar
Daniel Brown committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        complex_t **__restrict__ x_a4l = bs->x_a4[k][slidx];
        complex_t **__restrict__ x_a4u = bs->x_a4[k][suidx];
        complex_t **__restrict__ a4il_x = bs->a4i_x[k][slidx];
        complex_t **__restrict__ a4ol_x = bs->a4o_x[k][slidx];
        complex_t **__restrict__ a4iu_x = bs->a4i_x[k][suidx];
        complex_t **__restrict__ a4ou_x = bs->a4o_x[k][suidx];
        
        // as the motion to field elements are all in the same column
        // we don't have to worry about the memory alignment, as we are using CCS
        // matrix. 
        cblas_zgemv(CblasRowMajor, CblasNoTrans, inter.num_fields, inter.num_fields,
Daniel Brown's avatar
Daniel Brown committed
1324
                    &factor_x_a, &(bs->knm.k34[0][0]), inter.num_fields, ac_3i, 1, &complex_0, x_a4u[0], 1);
Daniel Brown's avatar
Daniel Brown committed
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
        
        for(i=0; i<inter.num_fields; i++) {
            *x_a4l[i] = cconj(*x_a4u[i]);
            
            *a4ou_x[i] = z_by_zc(fac_a_x,  ac_4o[i]);
            *a4ol_x[i] = z_by_z(fac_a_x, ac_4o[i]);
            *a4iu_x[i] = z_by_zc(fac_a_x,  ac_4i[i]);
            *a4il_x[i] = z_by_z(fac_a_x, ac_4i[i]);
        }
        
        if(bs->phi != 0.0){
            for(i=0; i<inter.num_fields; i++) {
                *x_a4u[i] = z_by_z(*x_a4u[i], tuning4u);
1338
                *x_a4l[i] = z_by_zc(*x_a4l[i], tuning4l);
Daniel Brown's avatar
Daniel Brown committed
1339 1340 1341 1342 1343
            }
        }
    }
}

1344 1345 1346
inline complex_t compute_rotate_factor(complex_t q, double nr, int n_comp_index, int comp_index){
    if (n_comp_index == comp_index) q = cminus(cconj(q));
    return z_by_x(cconj(q), -1.0 / w0_size(q, nr));
1347 1348
}

1349
void fill_optic_motion_coupling(){
1350
    ifo_matrix_vars_t *Mcar = &M_ifo_car;
1351
    complex_t *crhs  = (complex_t*)Mcar->rhs_values;
1352 1353
    
    assert(Mcar != NULL && Mcar->type == STANDARD);
1354
    assert(inter.num_motion_eqns > 0);
1355
    
1356
    int i, k;
1357
    
1358
    complex_t G_Omega_z = complex_0, G_Omega_rx = complex_0, G_Omega_ry = complex_0;
1359