PISM, A Parallel Ice Sheet Model stable 0.4.1779

src/base/iMviewers.cc

Go to the documentation of this file.
00001 // Copyright (C) 2004-2011 Jed Brown, Ed Bueler and Constantine Khroulev
00002 //
00003 // This file is part of PISM.
00004 //
00005 // PISM is free software; you can redistribute it and/or modify it under the
00006 // terms of the GNU General Public License as published by the Free Software
00007 // Foundation; either version 2 of the License, or (at your option) any later
00008 // version.
00009 //
00010 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
00011 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00013 // details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with PISM; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #include <sstream>
00020 #include <cstring>
00021 #include <cmath>
00022 #include <petscda.h>
00023 #include <petscksp.h>
00024 #include "iceModel.hh"
00025 
00027 
00030 PetscErrorCode IceModel::update_viewers() {
00031   PetscErrorCode ierr;
00032   set<string>::iterator i;
00033 
00034   PetscInt viewer_size = (PetscInt)config.get("viewer_size");
00035 
00036   // map-plane viewers
00037   for (i = map_viewers.begin(); i != map_viewers.end(); ++i) {
00038     IceModelVec *v = variables.get(*i);
00039     bool de_allocate = false;
00040 
00041     // if not found, try to compute:
00042     if (v == NULL) {
00043       de_allocate = true;
00044       PISMDiagnostic *diag = diagnostics[*i];
00045 
00046       if (diag) {
00047         ierr = diag->compute(v); CHKERRQ(ierr);
00048       } else {
00049         v = NULL;
00050       }
00051     }
00052 
00053     // if still not found, ignore
00054     if (v == NULL)
00055       continue;
00056 
00057     int dims = v->get_ndims();
00058 
00059     if (dims != 2) {
00060       ierr = PetscPrintf(grid.com,
00061                          "PISM ERROR: map-plane views of 3D quantities are not supported.\n");
00062       CHKERRQ(ierr);
00063       PISMEnd();
00064     }
00065 
00066     if (v->get_dof() == 1) {    // scalar fields
00067       string name = v->string_attr("short_name");
00068       PetscViewer viewer = viewers[name];
00069 
00070       if (viewer == PETSC_NULL) {
00071         ierr = grid.create_viewer(viewer_size, name, viewer); CHKERRQ(ierr);
00072         viewers[name] = viewer;
00073       }
00074 
00075       IceModelVec2S *v2d = dynamic_cast<IceModelVec2S*>(v);
00076       if (v2d == NULL) SETERRQ(1,"get_ndims() returns GRID_2D but dynamic_cast gives a NULL");
00077 
00078       ierr = v2d->view(viewer, PETSC_NULL); CHKERRQ(ierr);
00079 
00080     } else if (v->get_dof() == 2) { // vector fields
00081       string name_1 = v->string_attr("short_name", 0),
00082         name_2 = v->string_attr("short_name", 1);
00083       PetscViewer v1 = viewers[name_1],
00084         v2 = viewers[name_2];
00085 
00086       if (v1 == PETSC_NULL) {
00087         ierr = grid.create_viewer(viewer_size, name_1, v1); CHKERRQ(ierr);
00088         viewers[name_1] = v1;
00089       }
00090 
00091       if (v2 == PETSC_NULL) {
00092         ierr = grid.create_viewer(viewer_size, name_2, v2); CHKERRQ(ierr);
00093         viewers[name_2] = v2;
00094       }
00095 
00096       IceModelVec2V *v2d = dynamic_cast<IceModelVec2V*>(v);
00097       if (v2d == NULL) SETERRQ(1,"get_ndims() returns GRID_2D but dynamic_cast gives a NULL");
00098 
00099       ierr = v2d->view(v1, v2); CHKERRQ(ierr);
00100     }
00101     
00102     if (de_allocate) delete v;
00103   }
00104 
00105   // sounding viewers:
00106   for (i = sounding_viewers.begin(); i != sounding_viewers.end(); ++i) {
00107     IceModelVec *v = variables.get(*i);
00108     bool de_allocate = false;
00109 
00110     // if not found, try to compute:
00111     if (v == NULL) {
00112       de_allocate = true;
00113       PISMDiagnostic *diag = diagnostics[*i];
00114 
00115       if (diag) {
00116         ierr = diag->compute(v); CHKERRQ(ierr);
00117       } else {
00118         v = NULL;
00119       }
00120     }
00121 
00122     // if still not found, ignore
00123     if (v == NULL)
00124       continue;
00125 
00126     int dims = v->get_ndims();
00127 
00128     // if it's a 2D variable, stop
00129     if (dims == 2) {
00130       ierr = PetscPrintf(grid.com, "PISM ERROR: soundings of 2D quantities are not supported.\n");
00131       PISMEnd();
00132     }
00133 
00134     string name = v->string_attr("short_name");
00135     PetscViewer viewer = viewers[name];
00136 
00137     if (viewer == PETSC_NULL) {
00138       ierr = grid.create_viewer(viewer_size, name, viewers[name]); CHKERRQ(ierr);
00139       viewer = viewers[name];
00140     }
00141 
00142     if (dims == 3) {
00143         IceModelVec3D *v3d = dynamic_cast<IceModelVec3D*>(v);
00144         if (v3d == NULL) SETERRQ(1,"get_ndims() returns GRID_3D but dynamic_cast gives a NULL");
00145         ierr = v3d->view_sounding(id, jd, viewer); CHKERRQ(ierr);
00146     }
00147 
00148     if (de_allocate) delete v;
00149   } // sounding viewers
00150   return 0;
00151 }
00152 
00154 PetscErrorCode IceModel::init_viewers() {
00155   PetscErrorCode ierr;
00156   PetscTruth flag;
00157   char tmp[TEMPORARY_STRING_LENGTH];
00158 
00159   ierr = PetscOptionsBegin(grid.com, PETSC_NULL,
00160                            "Options controlling run-time diagnostic viewers",
00161                            PETSC_NULL); CHKERRQ(ierr);
00162 
00163   PetscInt viewer_size = (PetscInt)config.get("viewer_size");
00164   ierr = PetscOptionsInt("-view_size", "specifies desired viewer size",
00165                          "", viewer_size, &viewer_size, &flag); CHKERRQ(ierr);
00166 
00167   if (flag)
00168     config.set("viewer_size", viewer_size); 
00169 
00170   // map-plane (and surface) viewers:
00171   ierr = PetscOptionsString("-view_map", "specifies the comma-separated list of map-plane viewers", "", "empty",
00172                             tmp, TEMPORARY_STRING_LENGTH, &flag); CHKERRQ(ierr);
00173   string var_name;
00174   if (flag) {
00175     istringstream arg(tmp);
00176 
00177     while (getline(arg, var_name, ',')) {
00178       map_viewers.insert(var_name);
00179     }
00180   }
00181 
00182   // sounding viewers:
00183   ierr = PetscOptionsString("-view_sounding", "specifies the comma-separated list of sounding viewers", "", "empty",
00184                             tmp, TEMPORARY_STRING_LENGTH, &flag); CHKERRQ(ierr);
00185   if (flag) {
00186     istringstream arg(tmp);
00187 
00188     while (getline(arg, var_name, ','))
00189       sounding_viewers.insert(var_name);
00190   }
00191 
00192   // Done with the options.
00193   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
00194 
00195   return 0;
00196 }
00197 
00198 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines