AnisoSurfaceWaveModel.cpp
Go to the documentation of this file.00001 #include "AnisoSurfaceWaveModel.h"
00002 #include <fstream>
00003 #include <cassert>
00004 #include <algorithm>
00005 #include <numeric>
00006 #include <boost/bind.hpp>
00007 #include <string>
00008 #include <boost/filesystem.hpp>
00009 #include "FatalException.h"
00010 #include <iostream>
00011
00012 namespace gplib
00013 {
00014 AnisoSurfaceWaveModel::AnisoSurfaceWaveModel()
00015 {
00016 }
00017
00018 AnisoSurfaceWaveModel::~AnisoSurfaceWaveModel()
00019 {
00020 }
00021
00022 void AnisoSurfaceWaveModel::ReadModel(const std::string &filename)
00023 {
00024 std::ifstream infile(filename.c_str());
00025 size_t nlayers = 0;
00026 const size_t titlelength = 512;
00027 char title[titlelength];
00028
00029 infile.getline(title, titlelength);
00030
00031 infile >> nlayers;
00032
00033 nlayers += 1;
00034
00035
00036 infile.getline(title, titlelength);
00037
00038 thicknesses.assign(nlayers, 0.0);
00039 vp.assign(nlayers, 0.0);
00040 vs.assign(nlayers, 0.0);
00041 B.assign(nlayers, 0.0);
00042 C.assign(nlayers, 0.0);
00043 E.assign(nlayers, 0.0);
00044 theta.assign(nlayers, 0.0);
00045 phi.assign(nlayers, 0.0);
00046 densities.assign(nlayers, 0.0);
00047 for (size_t i = 0; i < nlayers; ++i)
00048 {
00049
00050 infile >> theta.at(i) >> phi.at(i);
00051
00052 infile.getline(title, titlelength);
00053
00054 infile >> thicknesses.at(i) >> vp.at(i) >> B.at(i) >> C.at(i)
00055 >> vs.at(i) >> E.at(i) >> densities.at(i);
00056
00057 thicknesses.at(i) /= 1000.0;
00058 vp.at(i) /= 1000.0;
00059 vs.at(i) /= 1000.0;
00060 densities.at(i) /= 1000.0;
00061
00062 infile.getline(title, titlelength);
00063 }
00064
00065 std::adjacent_difference(thicknesses.begin(), thicknesses.end(),
00066 thicknesses.begin());
00067 }
00068
00069 void AnisoSurfaceWaveModel::WriteModel(const std::string &filename) const
00070 {
00071 const size_t nlayers = thicknesses.size();
00072
00073 assert(nlayers> 0);
00074 assert(nlayers == vp.size());
00075 assert(nlayers == vs.size());
00076 assert(nlayers == B.size());
00077 assert(nlayers == C.size());
00078 assert(nlayers == E.size());
00079 assert(nlayers == theta.size());
00080 assert(nlayers == phi.size());
00081 assert(nlayers == densities.size());
00082
00083 std::ofstream outfile(filename.c_str());
00084
00085 outfile << "Surface Wave model generated by gplib" << std::endl;
00086
00087 outfile << nlayers - 1 << std::endl;
00088
00089 double currdepth = 0.0;
00090 for (size_t i = 0; i < nlayers; ++i)
00091 {
00092
00093 outfile << theta.at(i) << " " << phi.at(i) << "\n";
00094
00095 currdepth += thicknesses.at(i) * 1000.0;
00096
00097
00098 outfile << currdepth << " " << vp.at(i) * 1000.0 << " " << fabs(
00099 B.at(i)) << " " << C.at(i) << " " << vs.at(i) * 1000.0 << " "
00100 << fabs(E.at(i)) << " " << densities.at(i) * 1000.0 << "\n";
00101 }
00102 if (!outfile.good())
00103 throw FatalException("Problem writing to file: " + filename);
00104 }
00105
00106 void AnisoSurfaceWaveModel::WriteRunFile(const std::string &filename) const
00107 {
00108 std::ofstream runfile(filename.c_str());
00109 boost::filesystem::path Path(filename);
00110 std::string dirname(filename + "_dir");
00111 runfile << "#!/bin/bash" << std::endl;
00112 runfile << "mkdir " << dirname << std::endl;
00113 runfile << "mv " << filename << ".* " << dirname << std::endl;
00114 runfile << "cd " << dirname << std::endl;
00115 runfile << "cp " << Path.leaf() << ".mod animodel" << std::endl;
00116 runfile << "aniprop 2>&1> /dev/null" << std::endl;
00117 runfile << "cp out_cvel_R ../" << filename + ".cvel" << std::endl;
00118 if (!runfile.good())
00119 throw FatalException("Problem writing to file: " + filename);
00120 }
00121
00122 void AnisoSurfaceWaveModel::WritePlot(const std::string &filename) const
00123 {
00124 std::ofstream outfile(filename.c_str());
00125 double cumthick = 0;
00126 const size_t nlayers = thicknesses.size();
00127 for (unsigned int i = 0; i < nlayers; ++i)
00128 {
00129
00130
00131 outfile << cumthick << " " << vs.at(i) << " " << B.at(i) << " "
00132 << phi.at(i) << std::endl;
00133 cumthick += thicknesses.at(i);
00134
00135 outfile << cumthick << " " << vs.at(i) << " " << B.at(i) << " "
00136 << phi.at(i) << std::endl;
00137 }
00138
00139 outfile << cumthick * 2.0 << " " << vs.back() << " " << B.back()
00140 << " " << phi.back() << std::endl;
00141 }
00142 }