Commit d25f7f70 authored by Roger Kaeppeli's avatar Roger Kaeppeli

Add week 13

parent 74e5577f
cmake_minimum_required(VERSION 2.8)
project(week13)
# C++11
set(CMAKE_CXX_STANDARD 11)
foreach(I 1 2 3 4)
add_executable(formatted${I} formatted${I}.cpp)
endforeach(I)
add_executable(write2strings write2strings.cpp)
#include <iostream>
int main() {
// integers
std::cout << "Integers:\n";
std::cout << "|" << 1 << "|\n"; // |1|
std::cout << "|" << -1 << "|\n"; // |-1|
std::cout << "|" << 123456789 << "|\n"; // |123456789|
std::cout << "|" << -123456789 << "|\n"; // |-123456789|
// floating-point
std::cout << "Floating-point numbers:\n";
// trailing zeros not displayed
std::cout << "|" << 1.20000 << "|\n"; // |1.2|
// default precision is 6 digits
std::cout << "|" << 1.23456 << "|\n"; // |1.23456|
std::cout << "|" << -1.23456 << "|\n"; // |-1.23456|
std::cout << "|" << 1.234567 << "|\n"; // |1.23457|
std::cout << "|" << 123456.7 << "|\n"; // |123457|
// scientific-notation for exponent >=6
std::cout << "|" << 1234567.89 << "|\n"; // |1.23457e+006|
// leading zeros not counted for precision
std::cout << "|" << 0.0001234567 << "|\n"; // |0.000123457|
// scientific-notation for exponent <=-5
std::cout << "|" << 0.00001234567 << "|\n"; // |1.23457e-005|
}
#include <iostream>
#include <iomanip>
int main() {
// std::setw() is non-sticky!
std::cout << "|" << std::setw(5) << 123 << "|" << 123 << '\n';
// minus sign is included in field width!
std::cout << "|" << std::setw(5) << -123 << "|\n";
// no truncation of data
std::cout << "|" << std::setw(5) << 1234567 << "|\n";
// std::setfill() is sticky!
std::cout << std::setfill('_');
std::cout << std::setw(6) << 123 << std::setw(4) << 12 << '\n';
std::cout << std::setw(12) << 12356 << '\n';
std::cout << std::setfill(' '); // because it's sticky, we reset it!
// alignment
std::cout << std::showpos; // show positive sign
std::cout << '|' << std::setw(6) << 123 << "|\n"; // | +123|
std::cout << std::left
<< '|' << std::setw(6) << -123 << "|\n"; // |+123 |
std::cout << std::right
<< '|' << std::setw(6) << 123 << "|\n"; // | +123|
std::cout << std::internal
<< '|' << std::setw(6) << -123 << "|\n"; // |+ 123|
}
#include <iostream>
#include <iomanip>
int main() {
// precision
std::cout << std::setprecision(2); // sticky!
std::cout << "|" << 123.456789 << "|" << std::endl;
std::cout << std::setprecision(2); // sticky!
std::cout << "|" << 123. << "|" << std::endl;
std::cout << std::setprecision(5); // sticky!
std::cout << "|" << 123.456789 << "|" << std::endl;
std::cout << std::setprecision(15); // sticky!
std::cout << "|" << 123.456789 << "|" << std::endl;
std::cout << std::setprecision(6); // reset to default
// default formatting (defaultfloat c++11!)
std::cout << std::defaultfloat; // sticky!
std::cout << "|" << 1234567.89 << "|" << std::endl;
// fixed-point formatting
std::cout << std::fixed; // sticky!
std::cout << "|" << 1234567.89 << "|" << std::endl;
// scientific formatting
std::cout << std::scientific;
std::cout << "|" << 1234567.89 << "|" << std::endl;
}
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
std::ostream& print(std::ostream& out,int beg,int end) {
// table description
out << "i" << " "
<< "pow(M_PI, i)" << " "
<< "pow(M_PI,2*i)" << " "
<< "pow(M_PI,3*i)" << std::endl;
// write table
for (int i = beg; i <= end; ++i) {
out << i << " "
<< std::pow(M_PI, i) << " "
<< std::pow(M_PI,2*i) << " "
<< std::pow(M_PI,3*i) << std::endl;
}
return out;
}
std::ostream& print_formatted(std::ostream& out,int beg,int end) {
// table description
out << std::setw( 5) << "i" << " "
<< std::setw(15) << "pow(M_PI, i)" << " "
<< std::setw(15) << "pow(M_PI,2*i)" << " "
<< std::setw(15) << "pow(M_PI,3*i)" << std::endl;
// write table
for (int i = beg; i <= end; ++i) {
out << std::setw( 5) << i << " "
<< std::setw(15) << std::pow(M_PI, i) << " "
<< std::setw(15) << std::pow(M_PI,2*i) << " "
<< std::setw(15) << std::pow(M_PI,3*i) << std::endl;
}
return out;
}
std::ostream& print_more_formatted(std::ostream& out,int beg,int end) {
// table description
out << std::setw( 5) << "i" << " "
<< std::setw(15) << "pow(M_PI, i)" << " "
<< std::setw(15) << "pow(M_PI,2*i)" << " "
<< std::setw(15) << "pow(M_PI,3*i)" << std::endl;
// write table
for (int i = beg; i <= end; ++i) {
out << std::fixed;
out << std::setw( 5) << i << " "
<< std::setw(15) << std::pow(M_PI, i) << " "
<< std::setw(15) << std::pow(M_PI,2*i) << " "
<< std::setw(15) << std::pow(M_PI,3*i) << std::endl;
}
return out;
}
std::ostream& print_more2_formatted(std::ostream& out,int beg,int end) {
// table description
out << std::setw( 5) << "i" << " "
<< std::setw(15) << "pow(M_PI, i)" << " "
<< std::setw(15) << "pow(M_PI,2*i)" << " "
<< std::setw(15) << "pow(M_PI,3*i)" << std::endl;
// write table
for (int i = beg; i <= end; ++i) {
out << std::scientific;
out << std::setw( 5) << i << " "
<< std::setw(15) << std::pow(M_PI, i) << " "
<< std::setw(15) << std::pow(M_PI,2*i) << " "
<< std::setw(15) << std::pow(M_PI,3*i) << std::endl;
}
return out;
}
int main() {
std::cout << "Table with default format:" << std::endl;
print(std::cout,-5,+5);
std::cout << std::string(80,'#') << std::endl;
std::cout << "Table with some formatting:" << std::endl;
print_formatted(std::cout,-5,+5);
std::cout << std::string(80,'#') << std::endl;
std::cout << "Table with more formatting:" << std::endl;
print_more_formatted(std::cout,-5,+5);
std::cout << std::string(80,'#') << std::endl;
std::cout << "Table with even more formatting:" << std::endl;
print_more2_formatted(std::cout,-5,+5);
std::cout << std::string(80,'#') << std::endl;
}
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
std::ostringstream ss;
ss << std::setw(5) << std::setfill('0') << 42;
std::string s2(ss.str());
std::cout << s2 << '\n';
}
cmake_minimum_required(VERSION 2.8)
project(week13)
# C++11
set(CMAKE_CXX_STANDARD 11)
add_executable(file_io file_io.cpp)
add_executable(file_io_bin file_io_bin.cpp)
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string fname("test.txt"); // file name
// write to file //
// construct & connect to file
std::ofstream fout(fname); // default mode: fstream::out
// | fstream::trunc
// check if it succeeded
if (!fout) {
std::cerr << "Error: open file for output failed!" << std::endl;
return -1;
}
// write some stuff
fout << "Kawasaki\n";
fout << "Yamaha\n";
fout << "Suzuki\n";
fout << "Honda\n";
// close the file
fout.close();
// read from file //
// construct & connect to file
std::ifstream fin(fname); // default mode: fstream::in
// check if it succeeded
if (!fin) {
std::cerr << "Error: open file for input failed!\n";
return -1;
}
std::string word;
while (fin >> word) { // read until end-of-file (a.k.a. EoF)
std::cout << word << '\n';
}
fin.close();
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main() {
int const N = 7;
std::vector<double> data(N);
for(int i = 0; i < N; ++i) {
data[i] = i;
}
std::string fname("test.bin"); // file name
// write to file //
// construct & connect to file
std::ofstream fout(fname, std::fstream::binary);
// check if it succeeded
if (!fout) {
std::cerr << "Error: open file for output failed!" << std::endl;
return -1;
}
// write some stuff (ugly reinterpret_cast!!!)
fout.write(reinterpret_cast<char*>(&data[0]),data.size()*sizeof(double));
// close the file
fout.close();
// read from file //
// construct & connect to file
std::ifstream fin(fname, std::fstream::binary);
// check if it succeeded
if (!fin) {
std::cerr << "Error: open file for input failed!" << std::endl;
return -1;
}
// read some stuff (ugly reinterpret_cast!!!)
std::vector<double> data_in(N);
fin.read(reinterpret_cast<char*>(&data_in[0])
,data_in.size()*sizeof(double));
fin.close();
// compare!
int errs = 0;
for(int i = 0; i < N; ++i) {
if ( data_in[i] != data[i]) ++errs;
}
if ( errs > 0) std::cout << "Found " << errs << " errors!" << std::endl;
}
cmake_minimum_required(VERSION 2.8)
project(week13)
find_package(HDF5 REQUIRED)
include_directories(${HDF5_INCLUDE_DIRS})
add_executable(h5_file h5_file.cpp)
target_link_libraries(h5_file ${HDF5_LIBRARIES})
add_executable(h5_create_dataset h5_create_dataset.cpp)
target_link_libraries(h5_create_dataset ${HDF5_LIBRARIES})
add_executable(h5_write_dataset h5_write_dataset.cpp)
target_link_libraries(h5_write_dataset ${HDF5_LIBRARIES})
add_executable(h5_read_dataset h5_read_dataset.cpp)
target_link_libraries(h5_read_dataset ${HDF5_LIBRARIES})
#include <hdf5.h> // hdf5 header
int main() {
int const rank = 2; // rank of data, i.e. number of dimensions of data!
hid_t file_id; // file identifier
hid_t dataset_id; // dataset identifier
hid_t dataspace_id; // data identifier
hsize_t dims[rank]; // data dimensions
herr_t status; // error code
// create & open a new file using default (creation & access) properties
file_id = H5Fcreate("dataset.h5", // file name
H5F_ACC_TRUNC, // file access flags
H5P_DEFAULT, // file creation property list
H5P_DEFAULT); // file access property list
// create dataspace for the dataset
dims[0] = 4;
dims[1] = 6;
dataspace_id = H5Screate_simple(rank,dims,NULL);
// create the dataset using default properties
dataset_id = H5Dcreate2(file_id, // location identifier
"/dset", // dataset name
H5T_NATIVE_INT, // datatype identifier
dataspace_id, // dataspace identifier
H5P_DEFAULT, // link creation property list
H5P_DEFAULT, // dataset creation property list
H5P_DEFAULT); // dataset access property list
// close the dataset
status = H5Dclose(dataset_id);
// close the dataspace
status = H5Sclose(dataspace_id);
// close the file
status = H5Fclose(file_id);
}
#include <hdf5.h> // hdf5 header
int main() {
hid_t file_id; // file identifier
herr_t status; // error code
// create & open a new file using default (creation & access) properties
file_id = H5Fcreate("file.h5", // file name
H5F_ACC_TRUNC, // file access flags
H5P_DEFAULT, // file creation property list
H5P_DEFAULT); // file access property list
// close the file
status = H5Fclose(file_id);
}
#include <hdf5.h> // hdf5 header
#include <iostream>
int main() {
hid_t file_id; // file identifier
hid_t dataset_id; // dataset identifier
herr_t status; // error code
// open existing file using default (creation & access) properties
file_id = H5Fopen("dataset.h5", // file name
H5F_ACC_RDONLY, // file access flags
// (read only here)
H5P_DEFAULT); // file access properties list
// check if open succeeded (file_id negative means failure!)
if ( file_id < 0 ) {
std::cout << "Error reading dataset.h5!\n";
return -1;
}
// open existing dataset
dataset_id = H5Dopen2(file_id, // location identifier
"/dset", // dataset name
H5P_DEFAULT); // dataset access property list
// write the dataset
int dset_data[4][6];
status = H5Dread(dataset_id, // dataset identifier
H5T_NATIVE_INT, // memory datatype
H5S_ALL, // memory dataspace (H5S_ALL means all)
H5S_ALL, // dataset's dataspace in the file
H5P_DEFAULT, // transfer property list
dset_data); // buffer with data to be written
// close the dataset
status = H5Dclose(dataset_id);
// close the file
status = H5Fclose(file_id);
// check read data
int errs = 0;
for(int i = 0; i < 4; ++i) {
for (int j = 0; j < 6; ++j) {
if ( dset_data[i][j] != 6*i + j ) ++errs;
}
}
if ( errs > 0) std::cout << "Found " << errs << " errors!\n";
}
#include <hdf5.h> // hdf5 header
#include <iostream>
int main() {
hid_t file_id; // file identifier
hid_t dataset_id; // dataset identifier
herr_t status; // error code
// initialize data with some values
int dset_data[4][6];
for(int i = 0; i < 4; ++i) {
for (int j = 0; j < 6; ++j) {
dset_data[i][j] = 6*i + j;
}
}
// open existing file using default (creation & access) properties
file_id = H5Fopen("dataset.h5", // file name
H5F_ACC_RDWR, // file access flags
// (read & write here)
H5P_DEFAULT); // file access properties list
// check if open succeeded (file_id negative means failure!)
if ( file_id < 0 ) {
std::cout << "Error reading dataset.h5!\n";
return -1;
}
// open existing dataset
dataset_id = H5Dopen2(file_id, // location identifier
"/dset", // dataset name
H5P_DEFAULT); // dataset access property list
// write the dataset
status = H5Dwrite(dataset_id, // dataset identifier
H5T_NATIVE_INT, // memory datatype
H5S_ALL, // memory dataspace (H5S_ALL means all)
H5S_ALL, // dataset's dataspace in the file
H5P_DEFAULT, // transfer property list
dset_data); // buffer with data to be written
// close the dataset
status = H5Dclose(dataset_id);
// close the file
status = H5Fclose(file_id);
}
cmake_minimum_required(VERSION 2.8)
project(week13)
# C++11
set(CMAKE_CXX_STANDARD 11)
foreach(I 1 2 3)
add_executable(stdstream${I} stdstream${I}.cpp)
endforeach(I)
#include <iostream>
int main() {
int ival;
std::cout << "Enter integer ival = ";
if (std::cin >> ival) { // check if input valid
std::cout << "You entered ival = " << ival << '\n';
} else { // invalid input!
std::cerr << "Bad input!" << '\n';
}
}
#include <iostream>
#include <string>
int main() {
std::string word;
while ( std::cin >> word ) {
std::cout << word << '\n';
}
}
#include <iostream>
int main() {
std::cout << "Writing to standard output!" << '\n';
std::cerr << "Writing to standard err!" << '\n';
}
This file contains just
some words
import numpy as np
import h5py as h5
import xml.etree.ElementTree as ET
from xml.dom import minidom
def generate_data(dim=2):
"""
Generate a grid and some data on it.
The generated grids are based on:
Calhoun, Helzel, LeVeque, "Logically Rectangular Grids and Finite Volume
Methods for PDEs ,in Circular and Spherical Domains", SIAM Review, 2008
"""
# set grid dimensions
nx = 43
ny = 21
nz = 44
# generate grid, some point & cell data
if ( dim == 2 ):
# generate computational domain grid
xc = np.linspace(-1., +1., nx+1)
yc = np.linspace(-1., +1., ny+1)
zc = np.linspace( 0., 0., 1)
[XC, YC, ZC] = np.meshgrid(xc, yc, zc)
# generate physical domain grid
r1 = 1.
D = np.maximum(np.abs(XC),np.abs(YC))
r = np.sqrt(XC**2 + YC**2)
r = np.maximum(r, 1e-10)
XP = r1*D*XC/r
YP = r1*D*YC/r
W = D**2
XP = W*XP + (1. - W)*XC/np.sqrt(2)
YP = W*YP + (1. - W)*YC/np.