void PrescribedGradientBCPeriodic :: computeField(FloatArray &sigma, TimeStep *tStep) { DofIDEquationNumbering pnum(true, strain_id); EngngModel *emodel = this->giveDomain()->giveEngngModel(); FloatArray tmp, sig_tmp; int npeq = strain_id.giveSize(); // sigma = residual (since we use the slave dofs) = f_ext - f_int sig_tmp.resize(npeq); sig_tmp.zero(); emodel->assembleVector(sig_tmp, tStep, InternalForceAssembler(), VM_Total, pnum, this->domain); tmp.resize(npeq); tmp.zero(); emodel->assembleVector(tmp, tStep, ExternalForceAssembler(), VM_Total, pnum, this->domain); sig_tmp.subtract(tmp); // Divide by the RVE-volume sig_tmp.times(1.0 / ( this->domainSize(this->giveDomain(), this->set) + this->domainSize(this->giveDomain(), this->masterSet) )); sigma.resize(sig_tmp.giveSize()); if ( sig_tmp.giveSize() == 9 ) { sigma.assemble(sig_tmp, {1, 9, 8, 6, 2, 7, 5, 4, 3}); } else if ( sig_tmp.giveSize() == 4 ) { sigma.assemble(sig_tmp, {1, 4, 3, 2}); } else { sigma = sig_tmp; } }
void PrescribedGradientBCPeriodic :: computeTangent(FloatMatrix &E, TimeStep *tStep) { EModelDefaultEquationNumbering fnum; DofIDEquationNumbering pnum(true, strain_id); EngngModel *rve = this->giveDomain()->giveEngngModel(); ///@todo Get this from engineering model std :: unique_ptr< SparseLinearSystemNM > solver( classFactory.createSparseLinSolver( ST_Petsc, this->domain, this->domain->giveEngngModel() ) ); // = rve->giveLinearSolver(); SparseMtrxType stype = solver->giveRecommendedMatrix(true); std :: unique_ptr< SparseMtrx > Kff( classFactory.createSparseMtrx( stype ) ); std :: unique_ptr< SparseMtrx > Kfp( classFactory.createSparseMtrx( stype ) ); std :: unique_ptr< SparseMtrx > Kpp( classFactory.createSparseMtrx( stype ) ); Kff->buildInternalStructure(rve, this->domain->giveNumber(), fnum); Kfp->buildInternalStructure(rve, this->domain->giveNumber(), fnum, pnum); Kpp->buildInternalStructure(rve, this->domain->giveNumber(), pnum); rve->assemble(*Kff, tStep, TangentAssembler(TangentStiffness), fnum, this->domain); rve->assemble(*Kfp, tStep, TangentAssembler(TangentStiffness), fnum, pnum, this->domain); rve->assemble(*Kpp, tStep, TangentAssembler(TangentStiffness), pnum, this->domain); int neq = Kfp->giveNumberOfRows(); int nsd = this->domain->giveNumberOfSpatialDimensions(); int ncomp = nsd * nsd; FloatMatrix grad_pert(ncomp, ncomp), rhs, sol(neq, ncomp); grad_pert.resize(ncomp, ncomp); // In fact, npeq should most likely equal ndev grad_pert.beUnitMatrix(); // Compute the solution to each of the pertubation of eps Kfp->times(grad_pert, rhs); solver->solve(*Kff, rhs, sol); // Compute the solution to each of the pertubation of eps FloatMatrix E_tmp; Kfp->timesT(sol, E_tmp); // Assuming symmetry of stiffness matrix // This is probably always zero, but for generality FloatMatrix tmpMat; Kpp->times(grad_pert, tmpMat); E_tmp.subtract(tmpMat); E_tmp.times( - 1.0 / ( this->domainSize(this->giveDomain(), this->set) + this->domainSize(this->giveDomain(), this->masterSet) )); E.resize(E_tmp.giveNumberOfRows(), E_tmp.giveNumberOfColumns()); if ( nsd == 3 ) { if ( E_tmp.giveNumberOfRows() == 6 ) { E.assemble(E_tmp, {1, 6, 5, 6, 2, 4, 5, 4, 3}); } else { E.assemble(E_tmp, {1, 9, 8, 6, 2, 7, 5, 4, 3}); } } else if ( nsd == 2 ) { E.assemble(E_tmp, {1, 4, 3, 2}); } else { E = E_tmp; } }
void PatternFunc::execute() { ComValue pnum(stack_arg(0)); int pn = pnum.int_val(); reset_stack(); Catalog* catalog = unidraw->GetCatalog(); PSPattern* pattern = catalog->ReadPattern("pattern", pn); PatternCmd* cmd = nil; if (pattern) { cmd = new PatternCmd(_ed, pattern); execute_log(cmd); } }
void TransportGradientPeriodic :: computeField(FloatArray &flux, TimeStep *tStep) { DofIDEquationNumbering pnum(true, grad_ids); EngngModel *emodel = this->giveDomain()->giveEngngModel(); FloatArray tmp; int npeq = grad_ids.giveSize(); // sigma = residual (since we use the slave dofs) = f_ext - f_int flux.resize(npeq); flux.zero(); emodel->assembleVector(flux, tStep, InternalForceAssembler(), VM_Total, pnum, this->domain); tmp.resize(npeq); tmp.zero(); emodel->assembleVector(tmp, tStep, ExternalForceAssembler(), VM_Total, pnum, this->domain); flux.subtract(tmp); // Divide by the RVE-volume flux.times(1.0 / ( this->domainSize(this->giveDomain(), this->set) + this->domainSize(this->giveDomain(), this->masterSet) )); }
int fourier(wordlist *wl, struct plot *current_plot) { struct dvec *time, *vec; struct pnode *pn, *names; double *ff, fundfreq, *data = NULL; int nfreqs, fourgridsize, polydegree; double *freq, *mag, *phase, *nmag, *nphase; /* Outputs from CKTfour */ double thd, *timescale = NULL; char *s; int i, err, fw; char xbuf[20]; int shift; int rv = 1; char newvecname[32]; struct dvec *n; int newveccount = 1; static int callstof = 1; if (!current_plot) return 1; sprintf(xbuf, "%1.1e", 0.0); shift = (int) strlen(xbuf) - 7; if (!current_plot || !current_plot->pl_scale) { fprintf(cp_err, "Error: no vectors loaded.\n"); return 1; } if (!cp_getvar("nfreqs", CP_NUM, &nfreqs) || nfreqs < 1) nfreqs = 10; if (!cp_getvar("polydegree", CP_NUM, &polydegree) || polydegree < 0) polydegree = 1; if (!cp_getvar("fourgridsize", CP_NUM, &fourgridsize) || fourgridsize < 1) fourgridsize = DEF_FOURGRIDSIZE; time = current_plot->pl_scale; if (!isreal(time)) { fprintf(cp_err, "Error: fourier needs real time scale\n"); return 1; } s = wl->wl_word; if ((ff = ft_numparse(&s, FALSE)) == NULL || (*ff <= 0.0)) { fprintf(cp_err, "Error: bad fund freq %s\n", wl->wl_word); return 1; } fundfreq = *ff; freq = TMALLOC(double, nfreqs); mag = TMALLOC(double, nfreqs); phase = TMALLOC(double, nfreqs); nmag = TMALLOC(double, nfreqs); nphase = TMALLOC(double, nfreqs); wl = wl->wl_next; names = ft_getpnames(wl, TRUE); for (pn = names; pn; pn = pn->pn_next) { vec = ft_evaluate(pn); for (; vec; vec = vec->v_link2) { if (vec->v_length != time->v_length) { fprintf(cp_err, "Error: lengths don't match: %d, %d\n", vec->v_length, time->v_length); continue; } if (!isreal(vec)) { fprintf(cp_err, "Error: %s isn't real!\n", vec->v_name); continue; } if (polydegree) { double *dp, d; /* Build the grid... */ timescale = TMALLOC(double, fourgridsize); data = TMALLOC(double, fourgridsize); dp = ft_minmax(time, TRUE); /* Now get the last fund freq... */ d = 1 / fundfreq; /* The wavelength... */ if (dp[1] - dp[0] < d) { fprintf(cp_err, "Error: wavelength longer than time span\n"); goto done; } else if (dp[1] - dp[0] > d) { dp[0] = dp[1] - d; } d = (dp[1] - dp[0]) / fourgridsize; for (i = 0; i < fourgridsize; i++) timescale[i] = dp[0] + i * d; /* Now interpolate the data... */ if (!ft_interpolate(vec->v_realdata, data, time->v_realdata, vec->v_length, timescale, fourgridsize, polydegree)) { fprintf(cp_err, "Error: can't interpolate\n"); goto done; } } else { fourgridsize = vec->v_length; data = vec->v_realdata; timescale = time->v_realdata; } err = CKTfour(fourgridsize, nfreqs, &thd, timescale, data, fundfreq, freq, mag, phase, nmag, nphase); if (err != OK) { ft_sperror(err, "fourier"); goto done; } fprintf(cp_out, "Fourier analysis for %s:\n", vec->v_name); fprintf(cp_out, " No. Harmonics: %d, THD: %g %%, Gridsize: %d, Interpolation Degree: %d\n\n", nfreqs, thd, fourgridsize, polydegree); /* Each field will have width cp_numdgt + 6 (or 7 * with HP-UX) + 1 if there is a - sign. */ fw = ((cp_numdgt > 0) ? cp_numdgt : 6) + 5 + shift; fprintf(cp_out, "Harmonic %-*s %-*s %-*s %-*s %-*s\n", fw, "Frequency", fw, "Magnitude", fw, "Phase", fw, "Norm. Mag", fw, "Norm. Phase"); fprintf(cp_out, "-------- %-*s %-*s %-*s %-*s %-*s\n", fw, "---------", fw, "---------", fw, "-----", fw, "---------", fw, "-----------"); for (i = 0; i < nfreqs; i++) { char *pnumfr, *pnumma, *pnumph, *pnumnm, *pnumnp; pnumfr = pnum(freq[i]); pnumma = pnum(mag[i]); pnumph = pnum(phase[i]); pnumnm = pnum(nmag[i]); pnumnp = pnum(nphase[i]); fprintf(cp_out, " %-4d %-*s %-*s %-*s %-*s %-*s\n", i, fw, pnumfr, fw, pnumma, fw, pnumph, fw, pnumnm, fw, pnumnp); tfree(pnumfr); tfree(pnumma); tfree(pnumph); tfree(pnumnm); tfree(pnumnp); } fputs("\n", cp_out); /* generate name for new vector, using vec->name */ sprintf(newvecname, "fourier%d%d", callstof, newveccount); /* create and assign a new vector n */ /* with size 3 * nfreqs in current plot */ n = alloc(struct dvec); ZERO(n, struct dvec); n->v_name = copy(newvecname); n->v_type = SV_NOTYPE; n->v_flags = (VF_REAL | VF_PERMANENT); n->v_length = 3 * nfreqs; n->v_numdims = 2; n->v_dims[0] = 3; n->v_dims[1] = nfreqs; n->v_realdata = TMALLOC(double, n->v_length); vec_new(n); /* store data in vector: freq, mag, phase */ for (i = 0; i < nfreqs; i++) { n->v_realdata[i] = freq[i]; n->v_realdata[i + nfreqs] = mag[i]; n->v_realdata[i + 2 * nfreqs] = phase[i]; } newveccount++; if (polydegree) { tfree(timescale); tfree(data); } timescale = NULL; data = NULL; } }
int main() { int i,soma,q,r,n,sheet; int left[MAX],right[MAX]; /*int left[]={12,2,10,4,8,6}; int right[]={1,11,3,-1,9,5,-1};*/ while (scanf("%d",&n) == 1) { if (!n) break; r = n % 4; q = (int) ceil( (double)n/4 ); soma = (int) ceil ((double)n/4)*4+1; left[0] = ( r != 0 ? -1 : soma-1); right[0] = 1; right[1] = (r!=0 && r!=3? -1 : soma-2); left[1] = 2; left[2] = (r==1? -1 : soma-3); right[2] = 3; for (i = 3; i < 2*q; ++i) { if ( !(i%2) ) { right[i] = i+1; left[i] = soma-(i+1); } else { left[i] = i+1; right[i] = soma-(i+1); } } /*imprime left and right adequadamente*/ printf("Printing order for %d pages:\n",n); if (n == 1) printf("Sheet 1, front: Blank, 1\n"); else { for (sheet = 1,i = 0; sheet <= q; ++sheet,++i) { printf("Sheet %d, front: ",sheet); pnum(left[i]); printf(", "); pnum(right[i]); puts(""); ++i; printf("Sheet %d, back : ",sheet); pnum(left[i]); printf(", "); pnum(right[i]); puts(""); } } } return 0; }
void dbgset(set<rational>& cset) { for(auto iter = cset.begin(); iter!=cset.end(); ++iter) { printf("%lld, %lld\n", iter->pnum(),iter->pden()); } }