PetscErrorCode SetInitialGuess(Vec X,AppCtx *user) { PetscErrorCode ierr; PetscInt n,i; PetscScalar *xx,*cv_p,*ci_p,*wv_p,*wi_p; PetscViewer view; PetscScalar initv = .00069; PetscFunctionBeginUser; ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"file_initial",FILE_MODE_WRITE,&view);CHKERRQ(ierr); ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); ierr = VecSet(user->cv,initv);CHKERRQ(ierr); ierr = VecSet(user->ci,initv);CHKERRQ(ierr); ierr = VecSet(user->eta,0.0);CHKERRQ(ierr); ierr = DPsi(user);CHKERRQ(ierr); ierr = VecCopy(user->DPsiv,user->wv);CHKERRQ(ierr); ierr = VecCopy(user->DPsii,user->wi);CHKERRQ(ierr); ierr = VecGetArray(X,&xx);CHKERRQ(ierr); ierr = VecGetArray(user->cv,&cv_p);CHKERRQ(ierr); ierr = VecGetArray(user->ci,&ci_p);CHKERRQ(ierr); ierr = VecGetArray(user->wv,&wv_p);CHKERRQ(ierr); ierr = VecGetArray(user->wi,&wi_p);CHKERRQ(ierr); for (i=0; i<n/5; i++) { xx[5*i] =wv_p[i]; xx[5*i+1]=cv_p[i]; xx[5*i+2]=wi_p[i]; xx[5*i+3]=ci_p[i]; xx[5*i+4]=0.0; } ierr = VecView(user->wv,view);CHKERRQ(ierr); ierr = VecView(user->cv,view);CHKERRQ(ierr); ierr = VecView(user->wi,view);CHKERRQ(ierr); ierr = VecView(user->ci,view);CHKERRQ(ierr); ierr = PetscViewerDestroy(&view);CHKERRQ(ierr); ierr = VecRestoreArray(X,&xx);CHKERRQ(ierr); ierr = VecRestoreArray(user->cv,&cv_p);CHKERRQ(ierr); ierr = VecRestoreArray(user->ci,&ci_p);CHKERRQ(ierr); ierr = VecRestoreArray(user->wv,&wv_p);CHKERRQ(ierr); ierr = VecRestoreArray(user->wi,&wi_p);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode SetInitialGuess(Vec X,AppCtx *user) { PetscErrorCode ierr; PetscInt n,i,Mda; PetscScalar *xx,*cv_p,*wv_p,*eta_p; PetscViewer view_out; /* needed for the void growth case */ PetscScalar xmid,cv_v=1.0,cv_m=user->Sv*user->cv0,eta_v=1.0,eta_m=0.0,h,lambda; PetscInt nele,nen,idx[2]; const PetscInt *ele; PetscScalar x[2]; Vec coords; const PetscScalar *_coords; PetscScalar xwidth = user->xmax - user->xmin; PetscFunctionBeginUser; ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr); ierr = DMDAGetInfo(user->da2,NULL,&Mda,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr); ierr = DMGetCoordinatesLocal(user->da2,&coords);CHKERRQ(ierr); ierr = VecGetArrayRead(coords,&_coords);CHKERRQ(ierr); if (user->periodic) h = (user->xmax-user->xmin)/Mda; else h = (user->xmax-user->xmin)/(Mda-1.0); xmid = (user->xmax + user->xmin)/2.0; lambda = 4.0*h; ierr = DMDAGetElements(user->da2,&nele,&nen,&ele);CHKERRQ(ierr); for (i=0; i < nele; i++) { idx[0] = ele[2*i]; idx[1] = ele[2*i+1]; x[0] = _coords[idx[0]]; x[1] = _coords[idx[1]]; PetscInt k; PetscScalar vals_DDcv[2],vals_cv[2],vals_eta[2],s,hhr,r; for (k=0; k < 2; k++) { s = PetscAbs(x[k] - xmid); if (s <= xwidth*(5.0/64.0)) { vals_cv[k] = cv_v; vals_eta[k] = eta_v; vals_DDcv[k] = 0.0; } else if (s> xwidth*(5.0/64.0) && s<= xwidth*(7.0/64.0)) { /*r = (s - xwidth*(6.0/64.0))/(0.5*lambda);*/ r = (s - xwidth*(6.0/64.0))/(xwidth/64.0); hhr = 0.25*(-r*r*r + 3*r + 2); vals_cv[k] = cv_m + (1.0 - hhr)*(cv_v - cv_m); vals_eta[k] = eta_m + (1.0 - hhr)*(eta_v - eta_m); vals_DDcv[k] = (cv_v - cv_m)*r*6.0/(lambda*lambda); } else { vals_cv[k] = cv_m; vals_eta[k] = eta_m; vals_DDcv[k] = 0.0; } } ierr = VecSetValuesLocal(user->cv,2,idx,vals_cv,INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValuesLocal(user->eta,2,idx,vals_eta,INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValuesLocal(user->work2,2,idx,vals_DDcv,INSERT_VALUES);CHKERRQ(ierr); } ierr = DMDARestoreElements(user->da2,&nele,&nen,&ele);CHKERRQ(ierr); ierr = VecRestoreArrayRead(coords,&_coords);CHKERRQ(ierr); ierr = VecAssemblyBegin(user->cv);CHKERRQ(ierr); ierr = VecAssemblyEnd(user->cv);CHKERRQ(ierr); ierr = VecAssemblyBegin(user->eta);CHKERRQ(ierr); ierr = VecAssemblyEnd(user->eta);CHKERRQ(ierr); ierr = VecAssemblyBegin(user->work2);CHKERRQ(ierr); ierr = VecAssemblyEnd(user->work2);CHKERRQ(ierr); ierr = DPsi(user);CHKERRQ(ierr); ierr = VecCopy(user->DPsiv,user->wv);CHKERRQ(ierr); ierr = VecAXPY(user->wv,-2.0*user->kav,user->work2);CHKERRQ(ierr); ierr = VecGetArray(X,&xx);CHKERRQ(ierr); ierr = VecGetArray(user->wv,&wv_p);CHKERRQ(ierr); ierr = VecGetArray(user->cv,&cv_p);CHKERRQ(ierr); ierr = VecGetArray(user->eta,&eta_p);CHKERRQ(ierr); for (i=0; i<n/3; i++) { xx[3*i] =wv_p[i]; xx[3*i+1]=cv_p[i]; xx[3*i+2]=eta_p[i]; } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"file_initial",FILE_MODE_WRITE,&view_out);CHKERRQ(ierr); ierr = VecView(user->wv,view_out);CHKERRQ(ierr); ierr = VecView(user->cv,view_out);CHKERRQ(ierr); ierr = VecView(user->eta,view_out);CHKERRQ(ierr); ierr = PetscViewerDestroy(&view_out);CHKERRQ(ierr); ierr = VecRestoreArray(X,&xx);CHKERRQ(ierr); ierr = VecRestoreArray(user->wv,&wv_p);CHKERRQ(ierr); ierr = VecRestoreArray(user->cv,&cv_p);CHKERRQ(ierr); ierr = VecRestoreArray(user->eta,&eta_p);CHKERRQ(ierr); PetscFunctionReturn(0); }