URI:
       tViewer.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
  HTML git clone git://src.adamsgaard.dk/pism
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
       tViewer.cc (2965B)
       ---
            1 /* Copyright (C) 2014, 2015, 2017 PISM Authors
            2  *
            3  * This file is part of PISM.
            4  *
            5  * PISM is free software; you can redistribute it and/or modify it under the
            6  * terms of the GNU General Public License as published by the Free Software
            7  * Foundation; either version 3 of the License, or (at your option) any later
            8  * version.
            9  *
           10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
           11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
           12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
           13  * details.
           14  *
           15  * You should have received a copy of the GNU General Public License
           16  * along with PISM; if not, write to the Free Software
           17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
           18  */
           19 
           20 #include "Viewer.hh"
           21 
           22 #include <petscdraw.h>
           23 #include <cassert>
           24 #include "pism/util/error_handling.hh"
           25 
           26 namespace pism {
           27 namespace petsc {
           28 
           29 Viewer::Viewer(MPI_Comm com,  const std::string &title, unsigned int target_size,
           30                double Lx, double Ly) {
           31   PetscErrorCode ierr;
           32   unsigned int X, Y;
           33 
           34   compute_size(target_size, Lx, Ly, X, Y);
           35 
           36   ierr = PetscViewerDrawOpen(com, NULL, title.c_str(),
           37                              PETSC_DECIDE, PETSC_DECIDE, X, Y, &m_value);
           38   PISM_CHK(ierr, "PetscViewerDrawOpen");
           39 
           40   // following should be redundant, but may put up a title even under 2.3.3-p1:3 where
           41   // there is a no titles bug
           42   PetscDraw draw;
           43   ierr = PetscViewerDrawGetDraw(m_value, 0, &draw);
           44   PISM_CHK(ierr, "PetscViewerDrawGetDraw");
           45 
           46   ierr = PetscDrawSetTitle(draw, title.c_str());
           47   PISM_CHK(ierr, "PetscDrawSetTitle");
           48 }
           49 
           50 Viewer::Viewer(MPI_Comm com) {
           51   PetscErrorCode ierr = PetscViewerCreate(com, &m_value);
           52   PISM_CHK(ierr, "PetscViewerCreate");
           53 }
           54 
           55 Viewer::Viewer(PetscViewer v) {
           56   m_value = v;
           57 }
           58 
           59 Viewer::Viewer() {
           60   m_value = NULL;
           61 }
           62 
           63 Viewer::~Viewer() {
           64   if (m_value != NULL) {
           65     PetscErrorCode ierr = PetscViewerDestroy(&m_value); CHKERRCONTINUE(ierr);
           66   }
           67 }
           68 
           69 void Viewer::compute_size(unsigned int target_size, double Lx, double Ly, unsigned int &X, unsigned int &Y) {
           70 
           71   assert(Lx > 0 && Ly > 0);
           72 
           73   // aim for smaller dimension equal to target, larger dimension larger by Ly/Lx or Lx/Ly proportion
           74   const double yTOx = Ly / Lx;
           75   if (Ly > Lx) {
           76     X = target_size;
           77     Y = (unsigned int) ((double)target_size * yTOx);
           78   } else {
           79     Y = target_size;
           80     X = (unsigned int) ((double)target_size / yTOx);
           81   }
           82 
           83   // if either dimension is larger than twice the target, shrink appropriately
           84   if (X > 2 * target_size) {
           85     Y = (unsigned int) ((double)(Y) * (2.0 * (double)target_size / (double)(X)));
           86     X = 2 * target_size;
           87   } else if (Y > 2 * target_size) {
           88     X = (unsigned int) ((double)(X) * (2.0 * (double)target_size / (double)(Y)));
           89     Y = 2 * target_size;
           90   }
           91 
           92   // make sure minimum dimension is sufficient to see
           93   if (X < 20) {
           94     X = 20;
           95   }
           96   if (Y < 20) {
           97     Y = 20;
           98   }
           99 }
          100 
          101 } // end of namespace petsc
          102 } // end of namespace pism