int observable_reset_average(observable* self) { observable_average_container* data = (observable_average_container*) self->container; data->n_sweeps=0; int error = observable_calculate(data->reference_observable); for (int i =0; i<self->n; i++) { self->last_value[i] = 0; } return error; }
int observable_update_average(observable* self) { observable_average_container* data = (observable_average_container*) self->container; data->n_sweeps++; int error = observable_calculate(data->reference_observable); if ( error != 0) return 1; double factor = 1 / (double) data->n_sweeps; for (int i =0; i<self->n; i++) { self->last_value[i] = (1-factor)*self->last_value[i] + factor*data->reference_observable->last_value[i]; } return 0; }
int tclcommand_observable_print(Tcl_Interp* interp, int argc, char** argv, int* change, observable* obs) { char buffer[TCL_DOUBLE_SPACE]; if ( observable_calculate(obs) ) { Tcl_AppendResult(interp, "\nFailed to compute observable tclcommand\n", (char *)NULL ); return TCL_ERROR; } if (argc==0) { for (int i = 0; i<obs->n; i++) { Tcl_PrintDouble(interp, obs->last_value[i], buffer); Tcl_AppendResult(interp, buffer, " ", (char *)NULL ); } } else if (argc>0 && ARG0_IS_S("formatted")) { tclcommand_observable_print_formatted(interp, argc-1, argv+1, change, obs, obs->last_value); } else { Tcl_AppendResult(interp, "Unknown argument to observable print: ", argv[0], "\n", (char *)NULL ); return TCL_ERROR; } return TCL_OK; }
int double_correlation_get_data( double_correlation* self ) { // We must now go through the hierarchy and make sure there is space for the new // datapoint. For every hierarchy level we have to decide if it necessary to move // something int i,j; int highest_level_to_compress; unsigned int index_new, index_old, index_res; int error; self->t++; highest_level_to_compress=-1; i=0; j=1; // Lets find out how far we have to go back in the hierarchy to make space for the new value while (1) { if ( ( (self->t - ((self->tau_lin + 1)*((1<<(i+1))-1) + 1) )% (1<<(i+1)) == 0) ) { if ( i < (int(self->hierarchy_depth) - 1) && self->n_vals[i]> self->tau_lin) { highest_level_to_compress+=1; i++; } else break; } else break; } // Now we know we must make space on the levels 0..highest_level_to_compress // Now lets compress the data level by level. for ( i = highest_level_to_compress; i >= 0; i-- ) { // We increase the index indicating the newest on level i+1 by one (plus folding) self->newest[i+1] = (self->newest[i+1] + 1) % (self->tau_lin+1); self->n_vals[i+1]+=1; // printf("t %d compressing level %d no %d and %d to level %d no %d, nv %d\n",self->t, i, (self->newest[i]+1) % (self->tau_lin+1), //(self->newest[i]+2) % (self->tau_lin+1), i+1, self->newest[i+1], self->n_vals[i]); (*self->compressA)(self->A[i][(self->newest[i]+1) % (self->tau_lin+1)], self->A[i][(self->newest[i]+2) % (self->tau_lin+1)], self->A[i+1][self->newest[i+1]],self->dim_A); if (!self->autocorrelation) (*self->compressB)(self->B[i][(self->newest[i]+1) % (self->tau_lin+1)], self->B[i][(self->newest[i]+2) % (self->tau_lin+1)], self->B[i+1][self->newest[i+1]],self->dim_B); } self->newest[0] = ( self->newest[0] + 1 ) % (self->tau_lin +1); self->n_vals[0]++; if ( observable_calculate(self->A_obs) != 0 ) return 1; // copy the result: memcpy(self->A[0][self->newest[0]], self->A_obs->last_value, self->dim_A*sizeof(double)); if (!self->autocorrelation) { if ( observable_calculate(self->B_obs) != 0 ) return 2; memcpy(self->B[0][self->newest[0]], self->B_obs->last_value, self->dim_B*sizeof(double)); } // Now we update the cumulated averages and variances of A and B self->n_data++; for (unsigned k=0; k<self->dim_A; k++) { self->A_accumulated_average[k]+=self->A[0][self->newest[0]][k]; self->A_accumulated_variance[k]+=self->A[0][self->newest[0]][k]*self->A[0][self->newest[0]][k]; } // Here we check if it is an autocorrelation if (!self->autocorrelation) { for (unsigned k=0; k<self->dim_B; k++) { self->B_accumulated_average[k]+=self->B[0][self->newest[0]][k]; self->B_accumulated_variance[k]+=self->B[0][self->newest[0]][k]*self->B[0][self->newest[0]][k]; } } double* temp = (double*)malloc(self->dim_corr*sizeof(double)); if (!temp) return 4; // Now update the lowest level correlation estimates for ( j = 0; j < int(MIN(self->tau_lin+1, self->n_vals[0]) ); j++) { index_new = self->newest[0]; index_old = (self->newest[0] - j + self->tau_lin + 1) % (self->tau_lin + 1); // printf("old %d new %d\n", index_old, index_new); error = (self->corr_operation)(self->A[0][index_old], self->dim_A, self->B[0][index_new], self->dim_B, temp, self->dim_corr, self->args); if ( error != 0) return error; self->n_sweeps[j]++; for (unsigned k = 0; k < self->dim_corr; k++) { self->result[j][k] += temp[k]; } } // Now for the higher ones for ( int i = 1; i < highest_level_to_compress+2; i++) { for ( unsigned j = (self->tau_lin+1)/2+1; j < MIN(self->tau_lin+1, self->n_vals[i]); j++) { index_new = self->newest[i]; index_old = (self->newest[i] - j + self->tau_lin + 1) % (self->tau_lin + 1); index_res = self->tau_lin + (i-1)*self->tau_lin/2 + (j - self->tau_lin/2+1) -1; error=(self->corr_operation)(self->A[i][index_old], self->dim_A, self->B[i][index_new], self->dim_B, temp, self->dim_corr, self->args); if ( error != 0) return error; self->n_sweeps[index_res]++; for (unsigned k = 0; k < self->dim_corr; k++) { self->result[index_res][k] += temp[k]; } } } free(temp); return 0; }