double K_getPhasedRVLine(ok_kernel* k, int planet, int row, int column) { static gsl_matrix* phasedRVLine = NULL; if (planet >= 1) { if (k->ndata == 0) return -1; int np = K_getNplanets(k); double masses[np + 1]; double periods[np + 1]; for (int i = 1; i <= np; i++) { masses[i] = K_getElement(k, i, MASS); periods[i] = K_getElement(k, i, PER); if (i != planet) { K_setElement(k, i, MASS, 0.); K_setElement(k, i, PER, 10000.); } }; double period = K_getElement(k, planet, PER); int samples = -row; if (phasedRVLine != NULL) { gsl_matrix_free(phasedRVLine); phasedRVLine = NULL; } double** comp = K_getCompiled(k); phasedRVLine = K_integrateStellarVelocity(k, comp[0][0], comp[k->ndata - 1][0], samples, NULL, NULL); double mint = MGET(phasedRVLine, 0, T_TIME); for (int i = 0; i < MROWS(phasedRVLine); i++) { double t = fmod((MGET(phasedRVLine, i, 0) - mint), period); MSET(phasedRVLine, i, 0, t); } ok_sort_matrix(phasedRVLine, 0); for (int i = 1; i <= np; i++) { K_setElement(k, i, MASS, masses[i]); K_setElement(k, i, PER, periods[i]); } return 1; } else { return MGET(phasedRVLine, row, column); } }
double K_getPhasedDataForPlanet(ok_kernel* k, int planet, int row, int column) { static gsl_matrix* phased_data = NULL; if (planet >= 1) { if (phased_data != NULL) { gsl_matrix_free(phased_data); phased_data = NULL; } double chi2 = k->chi2; double rms = k->rms; double jitter = k->jitter; double chi2_rvs = k->chi2_rvs; planet = MIN(planet, K_getNplanets(k)); double mass = K_getElement(k, planet, MASS); double period = K_getElement(k, planet, PER); K_setElement(k, planet, MASS, 0); K_calculate(k); phased_data = K_getCompiledDataMatrix(k); double mint = MGET(phased_data, 0, T_TIME); for (int i = 0; i < MROWS(phased_data); i++) { double t = fmod((MGET(phased_data, i, T_TIME) - mint), period); double v = MGET(phased_data, i, T_SVAL) - MGET(phased_data, i, T_PRED); MSET(phased_data, i, T_TIME, t); MSET(phased_data, i, T_VAL, v); } ok_sort_matrix(phased_data, T_TIME); K_setElement(k, planet, MASS, mass); K_calculate(k); k->chi2 = chi2; k->rms = rms; k->jitter = jitter; k->chi2_rvs = chi2_rvs; return 1; } else { return MGET(phased_data, row, column); } }
gsl_matrix* ok_periodogram_full(ok_kernel* k, int type, int algo, bool circular, unsigned int sample, const unsigned int samples, const double Pmin, const double Pmax) { k = K_clone(k); K_calculate(k); // Input data for LS periodogram gsl_matrix* data = ok_buf_to_matrix(K_compileData(k), K_getNdata(k), DATA_SIZE); if (type == PS_TYPE_RESIDUALS) { // If residuals periodogram, subtract signal from data for (int i = 0; i < data->size1; i++) MSET(data, i, T_SVAL, MGET(data, i, T_SVAL) - MGET(data, i, T_PRED)); } else if (type == PS_TYPE_DATA) { // If full periodogram, then start with no planets K_removePlanet(k, -1); } // Calculate LS periodogram gsl_matrix* ret = ok_periodogram_ls(data, samples, Pmin, Pmax, 0, T_TIME, T_SVAL, T_ERR, NULL); int np = K_getNplanets(k) + 1; // Number of minimizable offsets int no = 0; for (int i = 0; i < DATA_SETS_SIZE; i++) if (VIGET(k->parFlags, i) & MINIMIZE) { no++; } // Calculate baseline chi^2 (Chi^2_H) double Chi2_H = _kminimize(k, algo); // Normalizing factor for power double nd = 0.5 * (K_getNdata(k) - no); #pragma omp parallel for for (int r = 0; r < samples; r++) { double P = MGET(ret, r, PS_TIME); double K = sqrt(MGET(ret, r, PS_Z)); ok_kernel* k2 = K_clone(k); K_calculate(k2); double args[] = {PER, P, DONE}; K_addPlanet(k2, args); K_setElement(k2, np, SEMIAMP, K); K_setElementFlag(k2, np, PER, ACTIVE); if (circular) { K_setElementFlag(k2, np, ECC, ACTIVE); K_setElementFlag(k2, np, LOP, ACTIVE); } double Chi2_K = _kminimize(k2, algo); double z = nd * (Chi2_H - Chi2_K) / Chi2_H; MSET(ret, r, PS_Z, z); fflush(stdout); } return ret; }
double K_integrateForward(ok_kernel* k, const int mode, const double nyears, const int row, const int col) { static ok_system* sys = NULL; static double time; static double yearspast; static double dt; static gsl_vector* times = NULL; static gsl_matrix* els = NULL; if (K_getNplanets(k) == 0) return 0; if (mode == JS_I_START) { if (times == NULL) times = gsl_vector_alloc(2); dt = MIN(nyears, 10); time = K_getEpoch(k); yearspast = 0; sys = ok_copy_system(k->system); ok_setup(sys); } else if (mode == JS_I_STEP) { if (yearspast >= nyears) return JS_I_ENDREACHED; if (els != NULL) { gsl_matrix_free(els); els = NULL; } VSET(times, 0, time); VSET(times, 1, time + dt * 365.25); yearspast += dt; int error; ok_system** bag = ok_integrate(sys, times, k->intOptions, RK89, NULL, &error); els = ok_get_els(bag, 2, false); ok_free_system(sys); sys = ok_copy_system(bag[1]); ok_free_systems(bag, 2); time += dt * 365.25; if (error != INTEGRATION_SUCCESS) return -error; return yearspast; } else if (mode == JS_I_END) { if (els != NULL) { gsl_matrix_free(els); els = NULL; } if (sys != NULL) { ok_free_system(sys); sys = NULL; } } else if (mode == JS_I_GET) { if (col == SMA) { int idx = row * ELEMENTS_SIZE + PER + 1; double mstar = K_getMstar(k) * K2; double mp = K_getElement(k, row, MASS) * MJUP / MSUN * K2; double P = MGET(els, 1, idx); return ok_acalc(P, mstar, mp); } else { int idx = row * ELEMENTS_SIZE + col + 1; return MGET(els, 1, idx); } }; return 0; }