int main() { CyGlobalIntEnable; /* Enable global interrupts. */ Clock_Start(); SPIM_Start(); setup_matrix(); for(;;) { for(h=0;h<=256;h++) { for(i=0;i<8;i++) { dato=(((i+1)<<8)+fuente[h][i]); SPIM_WriteTxData(dato); CyDelay(1); } CyDelay(1000); } } }
gsl_matrix* calc_Hs(double s, double tres, double* Qxx_m, double* Qyy_m, double* Qxy_m, double* Qyx_m, int kx, int ky) { /* * * * *H(s) *I=eye(ky); *expMat=mat_exp(-(s*I-Qyy)*tres); *Hxx_s=Qxx+(Qxy*(s*I-Qyy)^-1)*(I-expMat)*Qyx; * */ /* Initialise Qxx,Qxy,Qyx,Qyy */ gsl_matrix *Qxx=setup_matrix(Qxx_m,kx,kx); gsl_matrix *Qxy=setup_matrix(Qxy_m,kx,ky); gsl_matrix *Qyx=setup_matrix(Qyx_m,ky,kx); gsl_matrix *Qyy=setup_matrix(Qyy_m,ky,ky); gsl_matrix *ky_eye; //identitny matrix of dimension ky,ky gsl_matrix *e_eye; //to hold the matrix exponential expMat gsl_matrix *detected_tres; //matrix representing period in states y for tres followed by transition to x gsl_matrix *seye2; gsl_matrix *inv_sI_Qyy; gsl_matrix *res; gsl_matrix *res2; gsl_matrix *res3; gsl_matrix *eye3; //printf("\tAllocate memory for the matrices\n"); //allocate memory for all the matrices ky_eye=gsl_matrix_alloc(ky,ky); e_eye=gsl_matrix_alloc(ky,ky); seye2=gsl_matrix_alloc(ky,ky); detected_tres=gsl_matrix_alloc(ky,ky); inv_sI_Qyy=gsl_matrix_alloc(ky,ky); res=gsl_matrix_alloc(kx,ky); res2=gsl_matrix_alloc(ky,kx); res3 = gsl_matrix_alloc(kx,kx); eye3=gsl_matrix_alloc(ky,ky); //printf("\tAllocated memory for the matrices\n"); gsl_matrix_set_identity(ky_eye); //exp(-(s*I-Qyy)*tres) //build from the identity matrix gsl_matrix_memcpy (detected_tres,ky_eye); gsl_matrix_scale(detected_tres,s); gsl_matrix_sub(detected_tres,Qyy); gsl_matrix_scale(detected_tres,-1); gsl_matrix_scale(detected_tres,tres); gsl_linalg_exponential_ss(detected_tres, e_eye, .01); //printf("\tCalculated exp(-(s*I-Qyy)*tres)\n"); //(s*I-Qyy) gsl_matrix_memcpy (seye2,ky_eye); gsl_matrix_scale(seye2,s); gsl_matrix_sub(seye2,Qyy); //printf("\tCalculated s*I-Qyy\n"); //invert s*I-Qyy int sc; gsl_permutation * p = gsl_permutation_alloc (ky); gsl_linalg_LU_decomp(seye2, p, &sc); gsl_linalg_LU_invert(seye2, p, inv_sI_Qyy); gsl_permutation_free(p); //printf("\tInverted s*I-Qyy\n"); /* for (int i=0; i<ky; i++){ for (int j=0; j<ky; j++){ mexPrintf("\t%f",gsl_matrix_get(inv_sI_Qyy,i,j)); } mexPrintf("\n"); } */ //multiply Qxy * (s*I-Qyy)^-1 gsl_matrix_set_zero(res); // res = (Qxy*(s*I-Qyy)^-1) gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, Qxy, inv_sI_Qyy, 0.0, res); //printf("\tCalculated Qxy * (s*I-Qyy)^-1\n"); //res2 =(I-expMat)*Qyx; gsl_matrix_set_zero(res2); gsl_matrix_memcpy (eye3,ky_eye); //printf("\tMemcpy (I-expMat)\n"); gsl_matrix_sub(eye3,e_eye); //printf("\tSubtract (I-expMat)\n"); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, eye3, Qyx, 0.0, res2); //res3 = (Qxy*(s*I-Qyy)^-1)*(I-expMat)*Qyx; //res3 = res *res2 //res3 is the result we want to return //printf("\t (I-expMat)*Qyx\n"); gsl_matrix_set_zero(res3); gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, res, res2, 0.0, res3); gsl_matrix_add(res3,Qxx); /* for (int i=0; i<kx; i++){ for (int j=0; j<kx; j++){ mexPrintf("\t%f",gsl_matrix_get(res3,i,j)); } mexPrintf("\n"); } */ //printf("\t Calced H(s)\n"); //cleanup gsl_matrix_free(Qxx); gsl_matrix_free(Qxy); gsl_matrix_free(Qyx); gsl_matrix_free(Qyy); gsl_matrix_free(ky_eye); gsl_matrix_free(e_eye); gsl_matrix_free(detected_tres); gsl_matrix_free(seye2); gsl_matrix_free(inv_sI_Qyy); gsl_matrix_free(res); gsl_matrix_free(res2); gsl_matrix_free(eye3); //printf("\t Cleaned up H(s)\n"); return res3; }
ATTR_COLD void matrix_solver_t::setup_base(analog_net_t::list_t &nets) { log().debug("New solver setup\n"); m_nets.clear(); m_terms.clear(); for (auto & net : nets) { m_nets.push_back(net); m_terms.push_back(palloc(terms_t)); m_rails_temp.push_back(palloc(terms_t)); } for (std::size_t k = 0; k < nets.size(); k++) { log().debug("setting up net\n"); analog_net_t *net = nets[k]; net->m_solver = this; for (core_terminal_t *p : net->m_core_terms) { log().debug("{1} {2} {3}\n", p->name(), net->name(), (int) net->isRailNet()); switch (p->type()) { case terminal_t::TERMINAL: switch (p->device().family()) { case device_t::CAPACITOR: if (!m_step_devices.contains(&p->device())) m_step_devices.push_back(&p->device()); break; case device_t::BJT_EB: case device_t::DIODE: case device_t::LVCCS: case device_t::BJT_SWITCH: log().debug("found BJT/Diode/LVCCS\n"); if (!m_dynamic_devices.contains(&p->device())) m_dynamic_devices.push_back(&p->device()); break; default: break; } { terminal_t *pterm = dynamic_cast<terminal_t *>(p); add_term(k, pterm); } log().debug("Added terminal\n"); break; case terminal_t::INPUT: { analog_output_t *net_proxy_output = nullptr; for (auto & input : m_inps) if (input->m_proxied_net == &p->net().as_analog()) { net_proxy_output = input; break; } if (net_proxy_output == nullptr) { //net_proxy_output = palloc(analog_output_t(*this, // this->name() + "." + pfmt("m{1}")(m_inps.size()))); net_proxy_output = palloc(analog_output_t); net_proxy_output->init_object(*this, this->name() + "." + pfmt("m{1}")(m_inps.size())); m_inps.push_back(net_proxy_output); net_proxy_output->m_proxied_net = &p->net().as_analog(); } net_proxy_output->net().register_con(*p); // FIXME: repeated net_proxy_output->net().rebuild_list(); log().debug("Added input\n"); } break; default: log().fatal("unhandled element found\n"); break; } } log().debug("added net with {1} populated connections\n", net->m_core_terms.size()); } /* now setup the matrix */ setup_matrix(); }
// render earth and terrain geometry void miniview::render_geometry(float sbase, BOOLINT anaglyph) { minilayer *nst; if (m_cam==NULL) return; // start timer starttimer(); // get camera lens float fovy = m_cam->get_fovy(); float aspect = (float)get()->winwidth/get()->winheight; double nearp = m_cam->get_nearp(); double farp = m_cam->get_farp(); // update camera lens m_cam->set_lens(fovy, aspect, nearp, farp); // propagate camera lens PARAMS.fovy = fovy; PARAMS.nearp = nearp; PARAMS.farp = farp; miniscene::propagate(); // set reference layer nst = getearth()->getnearest(m_cam->get_eye()); getearth()->setreference(nst); // update scene cache(m_cam->get_eye(), m_cam->get_dir(), m_cam->get_up(), m_cam->get_aspect()); // clear scene clear(); // render scene if (sbase==0.0f) { setup_matrix(); render(); } else { // left stereo channel setup_matrix(-sbase); if (anaglyph) enableRwriting(); else writeleftbuffer(); render(); // right stereo channel setup_matrix(sbase); cleardepthbuffer(); if (anaglyph) enableGBwriting(); else writerightbuffer(); render(); if (anaglyph) enableRGBwriting(); else writebackbuffer(); } // get time spent double delta = gettimer(); // update quality parameters adapt(delta); }