void setRegionWidth (int width) { if (isFlipX()) { setU(u2 + width / (float) texture.getWidth()); } else { setU2(u + width / (float) texture.getWidth()); } }
float BlockIntegralTrap::update(float input) { // trapezoidal integration setY(_limit.update(getY() + (getU() + input) / 2.0f * getDt())); setU(input); return getY(); }
float BlockHighPass::update(float input) { float b = 2 * float(M_PI) * getFCut() * getDt(); float a = 1 / (1 + b); setY(a * (getY() + input - getU())); setU(input); return getY(); }
int main(int *argc, char argv[]) { char sendBuf[100]; char receiveBuf[100]; struct timespec waitTime; clock_gettime(CLOCK_REALTIME, &waitTime); udp_init_client(&conn, 9999, "192.168.0.1"); //VARIABLES FOR REGULATING double error, integral, u, y; double reference = 1; double Kp = 10; double Ki = 800; double period = PERIOD; strncpy(sendBuf,"START", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); //strlen(sendBuf)+1 //strncpy(sendBuf,"SET:12.3456",sizeof(sendBuf)); //udp_send(&conn,sendBuf,sizeof(sendBuf)); int numRep = RUNTIME/PERIOD; int i; for ( i = 0 ; i < numRep ; i++ ) { timespec_add_us(&waitTime,PERIOD*1000000); clock_nanosleep(&waitTime); strncpy(sendBuf,"GET", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); udp_receive(&conn,receiveBuf,sizeof(receiveBuf)); char * numVal = receiveBuf + 8; y = atof(numVal); //printf("Y-value from server is %f\n",y); //WE HAVE A Y-VALUE, DO PI REGULATING error = reference - y; integral = integral + (error * period); u = Kp * error + Ki * integral; setU(u); } strncpy(sendBuf,"STOP", sizeof(sendBuf)); udp_send(&conn,sendBuf,sizeof(sendBuf)); return 0; }
void PIreg ( void ){ double error, integral, u, y; const double reference = 1; const double Kp = 10; const double Ki = 800; const double period = PERIOD; int numRep = RUNTIME/PERIOD; struct timespec waitTime; clock_gettime(CLOCK_REALTIME, &waitTime); sendMsg("START",6); int i; for ( i = 0 ; i < numRep ; i++ ){ timespec_add_us(&waitTime,PERIOD*1000000); clock_nanosleep(&waitTime); // request new value sendMsg("GET",4); // wait for value from listener while(!newPIval){ /* wait */ } // reset flag pthread_mutex_lock(&newPIval_mut); newPIval = false; pthread_mutex_unlock(&newPIval_mut); // extract value char * numVal = globalMsgArray + 8; y = atof(numVal); // run PI-regulator error = reference - y; integral = integral + (error * period); u = Kp * error + Ki * integral; setU(u); } sendMsg("STOP",5); }
float BlockDerivative::update(float input) { float output; if (_initialized) { output = _lowPass.update((input - getU()) / getDt()); } else { // if this is the first call to update // we have no valid derivative // and so we use the assumption the // input value is not changing much, // which is the best we can do here. output = 0.0f; _initialized = true; } setU(input); return output; }
void setRegionX (int x) { setU(x / (float) texture.getWidth()); }
float BlockDerivative::update(float input) { float output = _lowPass.update((input - getU()) / getDt()); setU(input); return output; }
/** * This methods calculates the eigenvalues for a range of U values. The interval is [Ubegin, Uend] * with a stepsize of step. The resulting data is written to a file in the HDF5 file format. * @param Ubegin the startpoint for U * @param Uend the endpoint for U. We demand Ubegin < Uend * @param step the stepsize to use for U * @param filename the name of the file write to written the eigenvalues to */ void MomHamiltonian::GenerateData(double Ubegin, double Uend, double step, std::string filename) { double Ucur = Ubegin; if( !baseUp.size() || !baseDown.size() ) BuildBase(); std::vector<double> eigenvalues(dim); hid_t file_id, group_id, dataset_id, dataspace_id, attribute_id; herr_t status; file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); HDF5_STATUS_CHECK(file_id); group_id = H5Gcreate(file_id, "run", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); HDF5_STATUS_CHECK(group_id); dataspace_id = H5Screate(H5S_SCALAR); attribute_id = H5Acreate (group_id, "L", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_INT, &L ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "Nu", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_INT, &Nu ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "Nd", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_INT, &Nd ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "J", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_DOUBLE, &J ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "Ubegin", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_DOUBLE, &Ubegin ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "Uend", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_DOUBLE, &Uend ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); attribute_id = H5Acreate (group_id, "Ustep", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attribute_id, H5T_NATIVE_DOUBLE, &step ); HDF5_STATUS_CHECK(status); status = H5Aclose(attribute_id); HDF5_STATUS_CHECK(status); status = H5Sclose(dataspace_id); HDF5_STATUS_CHECK(status); status = H5Gclose(group_id); HDF5_STATUS_CHECK(status); status = H5Fclose(file_id); HDF5_STATUS_CHECK(status); std::vector<double> diagonalelements(dim); std::vector< std::unique_ptr<double []> > offdiag; offdiag.resize(L); // make sure that we don't rebuild the whole hamiltonian every time. // store the hopping and interaction part seperate so we can just // add them in every step #pragma omp parallel for for(int B=0; B<L; B++) { int cur_dim = mombase[B].size(); int offset = 0; for(int tmp=0; tmp<B; tmp++) offset += mombase[tmp].size(); offdiag[B].reset(new double [cur_dim*cur_dim]); for(int i=0; i<cur_dim; i++) { int a = mombase[B][i].first; int b = mombase[B][i].second; diagonalelements[offset+i] = hopping(baseUp[a]) + hopping(baseDown[b]); for(int j=i; j<cur_dim; j++) { int c = mombase[B][j].first; int d = mombase[B][j].second; offdiag[B][j+cur_dim*i] = 1.0/L*interaction(a,b,c,d); offdiag[B][i+cur_dim*j] = offdiag[B][j+cur_dim*i]; } } } while(Ucur <= Uend) { std::cout << "U = " << Ucur << std::endl; setU(Ucur); // make hamiltonian #pragma omp parallel for for(int B=0; B<L; B++) { int cur_dim = mombase[B].size(); int offset = 0; for(int tmp=0; tmp<B; tmp++) offset += mombase[tmp].size(); std::memcpy(blockmat[B].get(),offdiag[B].get(),cur_dim*cur_dim*sizeof(double)); int tmp = cur_dim*cur_dim; int inc = 1; dscal_(&tmp,&Ucur,blockmat[B].get(),&inc); for(int i=0; i<cur_dim; i++) blockmat[B][i+cur_dim*i] += diagonalelements[offset+i]; } #pragma omp parallel for for(int B=0; B<L; B++) { int dim = mombase[B].size(); int offset = 0; for(int tmp=0; tmp<B; tmp++) offset += mombase[tmp].size(); Diagonalize(dim, blockmat[B].get(), &eigenvalues[offset], false); } hid_t U_id; std::stringstream name; name << std::setprecision(5) << std::fixed << "/run/" << Ucur; file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); HDF5_STATUS_CHECK(file_id); U_id = H5Gcreate(file_id, name.str().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); HDF5_STATUS_CHECK(U_id); for(int B=0; B<L; B++) { int dim = mombase[B].size(); int offset = 0; for(int tmp=0; tmp<B; tmp++) offset += mombase[tmp].size(); hsize_t dimarr = dim; dataspace_id = H5Screate_simple(1, &dimarr, NULL); std::stringstream cur_block; cur_block << B; dataset_id = H5Dcreate(U_id, cur_block.str().c_str(), H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &eigenvalues[offset] ); HDF5_STATUS_CHECK(status); status = H5Sclose(dataspace_id); HDF5_STATUS_CHECK(status); status = H5Dclose(dataset_id); HDF5_STATUS_CHECK(status); } status = H5Gclose(U_id); HDF5_STATUS_CHECK(status); status = H5Fclose(file_id); HDF5_STATUS_CHECK(status); Ucur += step; } }
static __attribute__((constructor)) void myInit() { setU(); //fprintf(stderr, "init u\n"); }