PetscErrorCode BSSPotR1Vec(BSS self, PF pot, Vec V) { int order = self->order; int nb = self->num_basis; int ne = self->num_ele; int nq = order*ne; PetscErrorCode ierr; ierr = BSSCheck(self); CHKERRQ(ierr); PetscScalar *vs; PetscMalloc1(nq, &vs); ierr = PFApply(pot, nq, self->Rrs, vs); CHKERRQ(ierr); for(int i = 0; i < nb; i++) { int k0, k1; Non0QuadIndex(i, i, order, nq, &k0, &k1); PetscScalar v = 0.0; for(int k = k0; k < k1; k++) { //v += self->vals[k+i*nq] * self->ws[k] * vs[k]; v += self->vals[k+i*nq] * self->ws[k] * self->qrs[k] * vs[k]; } ierr = VecSetValue(V, i, v, INSERT_VALUES); CHKERRQ(ierr); } PetscFree(vs); VecAssemblyBegin(V); VecAssemblyEnd(V); return 0; }
PetscErrorCode BSSPotR1Mat(BSS self, PF pot, Mat M) { int order = self->order; int nb = self->num_basis; int ne = self->num_ele; int nq = order*ne; PetscErrorCode ierr; ierr = BSSCheck(self); CHKERRQ(ierr); InsertMode mode = INSERT_VALUES; PetscScalar *vs; PetscMalloc1(nq, &vs); //ierr = PFApply(pot, nq, self->xs_s, vs); CHKERRQ(ierr); ierr = PFApply(pot, nq, self->Rrs, vs); CHKERRQ(ierr); for(int k = 0; k < nq; k++) { vs[k] *= self->ws[k] * self->qrs[k]; } for(int i = 0; i < nb; i++) { int j0 = i-order+1; j0 = j0 < 0 ? 0: j0; for(int j = j0; j <= i; j++) { int k0, k1; Non0QuadIndex(j, i, order, nq, &k0, &k1); PetscScalar v = 0.0; for(int k = k0; k < k1; k++) v += self->vals[k+i*nq] * self->vals[k+j*nq] * vs[k]; ierr = MatSetValue(M, i, j, v, mode); CHKERRQ(ierr); if(i!=j) ierr = MatSetValue(M, j, i, v, mode); CHKERRQ(ierr); } } PetscFree(vs); MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY); MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY); return 0; }
PetscErrorCode ProductApply(void *ctx, int n, const PetscScalar *x, PetscScalar *y) { PetscErrorCode ierr; Product *self = (Product*)ctx; PetscScalar *y_tmp; PetscMalloc1(n, &y_tmp); for(int i = 0; i < n; i++) y[i] = 1.0; for(int j = 0; j < self->num; j++) { PFApply(self->pfs[j], n, x, y_tmp); for(int i = 0; i < n; i++) y[i] *= y_tmp[i]; } ierr = PetscFree(y_tmp); CHKERRQ(ierr); return 0; }
PetscErrorCode CombinationApply(void *ctx, int n, const PetscScalar *x, PetscScalar *y) { PetscErrorCode ierr; Combination *self = (Combination*)ctx; PetscScalar *y_tmp; PetscMalloc1(n, &y_tmp); for(int i = 0; i < n; i++) y[i] = 0.0; for(int j = 0; j < self->num; j++) { PFApply(self->pfs[j], n, x, y_tmp); for(int i = 0; i < n; i++) y[i] += y_tmp[i]; } ierr = PetscFree(y_tmp); CHKERRQ(ierr); return 0; }
void PETSC_STDCALL pfapply_(PF pf,PetscInt *n, PetscScalar *x,PetscScalar *y, int *__ierr ){ *__ierr = PFApply( (PF)PetscToPointer((pf) ),*n,x,y); }