00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "iceModelVec.hh"
00020 #include "pism_const.hh"
00021
00022 IceModelVec2V::IceModelVec2V() : IceModelVec2() {
00023 dof = 2;
00024 vars.resize(dof);
00025
00026 reset_attrs();
00027 component_da = PETSC_NULL;
00028 }
00029
00030 IceModelVec2V::~IceModelVec2V() {
00031 if (!shallow_copy) {
00032 destroy();
00033 }
00034 }
00035
00036 PetscErrorCode IceModelVec2V::destroy() {
00037 PetscErrorCode ierr;
00038
00039 ierr = IceModelVec::destroy(); CHKERRQ(ierr);
00040
00041 if (component_da != PETSC_NULL) {
00042 ierr = DADestroy(component_da); CHKERRQ(ierr);
00043 }
00044 return 0;
00045 }
00046
00047 PetscErrorCode IceModelVec2V::create(IceGrid &my_grid, const char my_short_name[], bool local,
00048 int stencil_width) {
00049
00050 PetscErrorCode ierr = IceModelVec2::create(my_grid, my_short_name, local, DA_STENCIL_BOX,
00051 stencil_width, dof); CHKERRQ(ierr);
00052
00053 #if PETSC_VERSION_MAJOR >= 3
00054 const PetscInt *lx = NULL, *ly = NULL;
00055 ierr = DAGetOwnershipRanges(my_grid.da2, &ly, &lx, PETSC_NULL); CHKERRQ(ierr);
00056 #else
00057 PetscInt *lx = NULL, *ly = NULL;
00058 #endif
00059
00060
00061 ierr = DACreate2d(grid->com, DA_XYPERIODIC, DA_STENCIL_BOX,
00062 grid->My, grid->Mx,
00063 grid->Ny, grid->Nx,
00064 1, stencil_width,
00065 ly, lx, &component_da); CHKERRQ(ierr);
00066
00067 string s_name = name;
00068 vars[0].init("u" + s_name, my_grid, GRID_2D);
00069 vars[1].init("v" + s_name, my_grid, GRID_2D);
00070
00071 return 0;
00072 }
00073
00074 PetscErrorCode IceModelVec2V::get_array(PISMVector2** &a) {
00075 PetscErrorCode ierr;
00076 ierr = begin_access(); CHKERRQ(ierr);
00077 a = static_cast<PISMVector2**>(array);
00078 return 0;
00079 }
00080
00081 inline
00082 PISMVector2& IceModelVec2V::operator()(int i, int j) {
00083 return static_cast<PISMVector2**>(array)[i][j];
00084 }
00085
00086
00087 PetscErrorCode IceModelVec2V::read(const char filename[], const unsigned int time) {
00088 PetscErrorCode ierr;
00089
00090 ierr = checkAllocated(); CHKERRQ(ierr);
00091 if (grid->da2 == PETSC_NULL)
00092 SETERRQ(1, "IceModelVec::read_from_netcdf: grid.da2 is NULL.");
00093
00094 Vec tmp;
00095
00096 ierr = DACreateGlobalVector(component_da, &tmp); CHKERRQ(ierr);
00097
00098 for (int j = 0; j < dof; ++j) {
00099
00100 ierr = vars[j].read(filename, time, tmp); CHKERRQ(ierr);
00101 ierr = IceModelVec2::set_component(j, tmp); CHKERRQ(ierr);
00102 }
00103
00104
00105
00106 if (localp) {
00107 ierr = beginGhostComm(); CHKERRQ(ierr);
00108 ierr = endGhostComm(); CHKERRQ(ierr);
00109 }
00110
00111
00112 ierr = VecDestroy(tmp); CHKERRQ(ierr);
00113 return 0;
00114 }
00115
00116 PetscErrorCode IceModelVec2V::write(const char filename[], nc_type nctype) {
00117 PetscErrorCode ierr;
00118
00119 ierr = checkAllocated(); CHKERRQ(ierr);
00120 if (grid->da2 == PETSC_NULL)
00121 SETERRQ(1, "IceModelVec::read_from_netcdf: grid.da2 is NULL.");
00122
00123 Vec tmp;
00124
00125 ierr = DACreateGlobalVector(component_da, &tmp); CHKERRQ(ierr);
00126
00127 for (int j = 0; j < dof; ++j) {
00128 vars[j].time_independent = time_independent;
00129 ierr = IceModelVec2::get_component(j, tmp); CHKERRQ(ierr);
00130 ierr = vars[j].write(filename, nctype,
00131 write_in_glaciological_units, tmp);
00132 }
00133
00134
00135 ierr = VecDestroy(tmp);
00136 return 0;
00137 }
00138
00139 PetscErrorCode IceModelVec2V::magnitude(IceModelVec2S &result) {
00140 PetscErrorCode ierr;
00141 PISMVector2** a;
00142 PetscScalar **mag;
00143
00144 ierr = result.get_array(mag); CHKERRQ(ierr);
00145 ierr = get_array(a);
00146
00147 for (PetscInt i=grid->xs; i<grid->xs+grid->xm; ++i) {
00148 for (PetscInt j=grid->ys; j<grid->ys+grid->ym; ++j) {
00149 mag[i][j] = sqrt(PetscSqr(a[i][j].u) + PetscSqr(a[i][j].v));
00150 }
00151 }
00152
00153 ierr = result.end_access(); CHKERRQ(ierr);
00154 ierr = end_access();
00155 return 0;
00156 }
00157
00158 PetscErrorCode IceModelVec2V::regrid(const char filename[], LocalInterpCtx &lic, bool critical) {
00159 PetscErrorCode ierr;
00160 MaskInterp *m = NULL;
00161
00162 Vec tmp;
00163
00164 ierr = DACreateGlobalVector(component_da, &tmp); CHKERRQ(ierr);
00165
00166 if (use_interpolation_mask) m = &interpolation_mask;
00167
00168 for (int j = 0; j < dof; ++j) {
00169 ierr = vars[j].regrid(filename, lic, critical, false, 0.0, m, tmp); CHKERRQ(ierr);
00170 ierr = IceModelVec2::set_component(j, tmp); CHKERRQ(ierr);
00171 }
00172
00173
00174
00175 if (localp) {
00176 ierr = beginGhostComm(); CHKERRQ(ierr);
00177 ierr = endGhostComm(); CHKERRQ(ierr);
00178 }
00179
00180
00181 ierr = VecDestroy(tmp);
00182 return 0;
00183 }
00184
00185 PetscErrorCode IceModelVec2V::regrid(const char filename[], LocalInterpCtx &lic, PetscScalar default_value) {
00186 PetscErrorCode ierr;
00187 MaskInterp *m = NULL;
00188
00189 Vec tmp;
00190
00191 ierr = DACreateGlobalVector(component_da, &tmp); CHKERRQ(ierr);
00192
00193 if (use_interpolation_mask) m = &interpolation_mask;
00194
00195 for (int j = 0; j < dof; ++j) {
00196 ierr = vars[j].regrid(filename, lic, false, true, default_value, m, tmp); CHKERRQ(ierr);
00197 ierr = IceModelVec2::set_component(j, tmp); CHKERRQ(ierr);
00198 }
00199
00200
00201
00202 if (localp) {
00203 ierr = beginGhostComm(); CHKERRQ(ierr);
00204 ierr = endGhostComm(); CHKERRQ(ierr);
00205 }
00206
00207
00208 ierr = VecDestroy(tmp);
00209 return 0;
00210 }
00211
00212 bool IceModelVec2V::is_valid(PetscScalar u, PetscScalar v) {
00213 return vars[0].is_valid(u) && vars[1].is_valid(v);
00214 }
00215
00216
00217 PetscErrorCode IceModelVec2V::get_component(int n, IceModelVec2S &result) {
00218 PetscErrorCode ierr;
00219
00220 ierr = IceModelVec2::get_component(n, result.v); CHKERRQ(ierr);
00221
00222 return 0;
00223 }
00224
00225 PetscErrorCode IceModelVec2V::set_component(int n, IceModelVec2S &source) {
00226 PetscErrorCode ierr;
00227
00228 ierr = IceModelVec2::set_component(n, source.v); CHKERRQ(ierr);
00229
00230 return 0;
00231 }
00232
00235 PetscErrorCode IceModelVec2V::view(PetscInt viewer_size) {
00236 PetscErrorCode ierr;
00237 Vec g2;
00238
00239 ierr = DACreateGlobalVector(grid->da2, &g2); CHKERRQ(ierr);
00240
00241 string prefixes[2];
00242 prefixes[0] = "u";
00243 prefixes[1] = "v";
00244
00245 for (int j = 0; j < dof; ++j) {
00246 string c_name = prefixes[j] + name;
00247 if ((*map_viewers)[c_name] == PETSC_NULL) {
00248 string title = string_attr("long_name", j) + " (" + string_attr("glaciological_units", j) + ")";
00249
00250 ierr = create_viewer(viewer_size, title, (*map_viewers)[c_name]); CHKERRQ(ierr);
00251 }
00252
00253 ierr = IceModelVec2::get_component(j, g2); CHKERRQ(ierr);
00254
00255 ierr = vars[j].to_glaciological_units(g2); CHKERRQ(ierr);
00256
00257 ierr = VecView(g2, (*map_viewers)[c_name]); CHKERRQ(ierr);
00258 }
00259
00260
00261 ierr = VecDestroy(g2); CHKERRQ(ierr);
00262
00263 return 0;
00264 }