ResPkModel.cpp
Go to the documentation of this file.00001 #include "ResPkModel.h"
00002 #include "FatalException.h"
00003 #include <fstream>
00004 #include <iomanip>
00005 #include <iostream>
00006 #include <algorithm>
00007 #include <boost/cast.hpp>
00008 using namespace std;
00009
00010 namespace gplib
00011 {
00012 ResPkModel::ResPkModel()
00013 {
00014 }
00015 ResPkModel::~ResPkModel()
00016 {
00017 }
00018
00019 ResPkModel::ResPkModel(const int nlayers) :
00020 SeismicModel(nlayers)
00021 {
00022
00023 }
00024
00025 ResPkModel::ResPkModel(const SeismicModel& source)
00026 {
00027 this->SeismicModel::operator=(source);
00028 }
00029
00030 ResPkModel::ResPkModel(const ResPkModel& source)
00031 {
00032 this->SeismicModel::operator=(source);
00033 this->slowness = source.slowness;
00034 copy(source.Strike.begin(), source.Strike.end(), back_inserter(
00035 this->Strike));
00036 copy(source.Dip.begin(), source.Dip.end(), back_inserter(this->Dip));
00037 }
00038
00039 ResPkModel& ResPkModel::operator=(const ResPkModel& source)
00040 {
00041 if (this == &source)
00042 return *this;
00043 this->SeismicModel::operator=(source);
00044 this->slowness = source.slowness;
00045 Strike.assign(source.Strike.size(), 0);
00046 Dip.assign(source.Dip.size(), 0);
00047 copy(source.Strike.begin(), source.Strike.end(), Strike.begin());
00048 copy(source.Dip.begin(), source.Dip.end(), Dip.begin());
00049 return *this;
00050 }
00051
00052 void ResPkModel::WriteModel(const std::string filename)
00053 {
00054 ofstream neu;
00055
00056 neu.open(filename.c_str());
00057 const unsigned int nlayers = GetPVelocity().size();
00058 if (Dip.size() != nlayers)
00059 {
00060 Dip.assign(nlayers, 0.0);
00061 }
00062 if (Strike.size() != nlayers)
00063 {
00064 Strike.assign(nlayers, 0.0);
00065 }
00066 neu << setw(3) << nlayers << " " << filename << endl;
00067
00068 for (unsigned int i = 0; i < nlayers; ++i)
00069 {
00070 neu.setf(ios::fixed);
00071 neu << setw(3);
00072
00073
00074
00075
00076
00077 neu << i + 1 << setw(8) << setprecision(4) << GetPVelocity().at(i);
00078 neu << setw(8) << setprecision(4) << GetSVelocity().at(i)
00079 << setw(8) << setprecision(4) << GetDensity().at(i);
00080 neu << setw(8) << setprecision(2) << GetThickness().at(i)
00081 << setw(8) << setprecision(4) << GetQp().at(i);
00082 neu << setw(8) << setprecision(4) << GetQs().at(i);
00083 neu << setw(8) << setprecision(4) << GetStrike().at(i) << setw(8)
00084 << setprecision(4) << GetDip().at(i) << " 0.2500" << endl;
00085 }
00086 neu.close();
00087 }
00088
00089 void ResPkModel::ReadModel(const std::string filename)
00090 {
00091 ifstream infile(filename.c_str());
00092 char dummy[255];
00093 double number;
00094 int nlayers = -1;
00095 infile >> nlayers;
00096 infile.getline(dummy, 255);
00097 if (nlayers < 1)
00098 throw FatalException("Problem reading file: " + filename);
00099 Init(nlayers);
00100 int i = 0;
00101 while (infile.good())
00102 {
00103
00104 infile >> number;
00105
00106 if (infile.good())
00107 {
00108 infile >> SetPVelocity().at(i) >> SetSVelocity().at(i)
00109 >> SetDensity().at(i) >> SetThickness().at(i)
00110 >> SetQp().at(i) >> SetQs().at(i);
00111 infile >> SetStrike().at(i) >> SetDip().at(i) >> number;
00112 ++i;
00113 }
00114 }
00115 if (i != nlayers)
00116 throw FatalException("Problem reading file: " + filename);
00117 }
00118
00119 void ResPkModel::WriteRunFile(const std::string &filename)
00120 {
00121 const unsigned int mintime = 200;
00122 ofstream runfile;
00123 runfile.open(filename.c_str());
00124 runfile << "#!/bin/bash" << endl;
00125 runfile << "respknt 2>&1> /dev/null << eof" << endl;
00126 runfile << filename + ".mod" << endl;
00127 runfile << "n" << endl;
00128 runfile << "1" << endl;
00129 runfile << GetDt() << endl;
00130 runfile << max(boost::numeric_cast<unsigned int>((GetNpts() - 1)
00131 * GetDt()), mintime) << endl;
00132 runfile << GetSlowness() << endl;
00133 runfile << "f" << endl;
00134 runfile << "y" << endl;
00135 runfile << "eof" << endl;
00136 runfile.close();
00137 size_t status;
00138 status = system(("chmod u+x " + filename).c_str());
00139 }
00140 }