clustering  0.12
Clustering suite for molecular dynamics trajectories.
 All Classes Namespaces Files Functions Variables Typedefs
tools.hpp
1 /*
2 Copyright (c) 2015, Florian Sittel (www.lettis.net)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 #pragma once
26 
27 #include "config.hpp"
28 
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <tuple>
33 #include <memory>
34 #include <iostream>
35 
37 #if defined(__INTEL_COMPILER)
38  #include <malloc.h>
39 #else
40  #include <mm_malloc.h>
41 #endif
42 
43 #if defined(__INTEL_COMPILER)
44  #define ASSUME_ALIGNED(c) __assume_aligned( (c), DC_MEM_ALIGNMENT)
45 #else
46  #define ASSUME_ALIGNED(c) (c) = (float*) __builtin_assume_aligned( (c), DC_MEM_ALIGNMENT)
47 #endif
48 
49 namespace Clustering {
51 namespace Tools {
53  using Neighbor = std::pair<std::size_t, float>;
55  using Neighborhood = std::map<std::size_t, Clustering::Tools::Neighbor>;
57  void
58  write_pops(std::string fname, std::vector<std::size_t> pops);
60  void
61  write_fes(std::string fname, std::vector<float> fes);
63  std::vector<std::size_t>
64  read_clustered_trajectory(std::string filename);
66  void
67  write_clustered_trajectory(std::string filename, std::vector<std::size_t> traj);
69  template <typename NUM>
70  std::vector<NUM>
71  read_single_column(std::string filename);
73  template <typename NUM>
74  void
75  write_single_column(std::string filename, std::vector<NUM> dat, bool with_scientific_format=false);
77  template <typename KEY, typename VAL>
78  void
79  write_map(std::string filename, std::map<KEY, VAL> mapping);
81  std::vector<float>
82  read_free_energies(std::string filename);
85  std::pair<Neighborhood, Neighborhood>
86  read_neighborhood(const std::string fname);
89  void
90  write_neighborhood(const std::string fname,
91  const Neighborhood& nh,
92  const Neighborhood& nh_high_dens);
94  std::map<std::size_t, std::size_t>
95  microstate_populations(std::vector<std::size_t> traj);
100  template <typename NUM>
101  std::tuple<NUM*, std::size_t, std::size_t>
102  read_coords(std::string filename,
103  std::vector<std::size_t> usecols = std::vector<std::size_t>());
105  template <typename NUM>
106  void
107  free_coords(NUM* coords);
109  std::string
110  stringprintf(const std::string& str, ...);
112  template <typename NUM>
113  NUM
114  string_to_num(const std::string &s);
115 } // end namespace 'Tools'
116 } // end namespace 'Clustering'
117 
118 // template implementations
119 #include "tools.hxx"
120 
std::tuple< NUM *, std::size_t, std::size_t > read_coords(std::string filename, std::vector< std::size_t > usecols=std::vector< std::size_t >())
Definition: tools.hxx:39
std::map< std::size_t, std::size_t > microstate_populations(std::vector< std::size_t > traj)
compute microstate populations from clustered trajectory
Definition: tools.cpp:163
std::vector< float > read_free_energies(std::string filename)
read free energies from plain text file
Definition: tools.cpp:111
void free_coords(NUM *coords)
free memory pointing to coordinates
Definition: tools.hxx:107
NUM string_to_num(const std::string &s)
convert std::string to number of given template format
Definition: tools.hxx:163
void write_pops(std::string fname, std::vector< std::size_t > pops)
write populations as column into given file
Definition: tools.cpp:48
void write_map(std::string filename, std::map< KEY, VAL > mapping)
write key-value map to plain text file with key as first and value as second column ...
Definition: tools.hxx:113
void write_single_column(std::string filename, std::vector< NUM > dat, bool with_scientific_format=false)
write single column of numbers to given file. number type (int, float, ...) given as template paramet...
Definition: tools.hxx:147
std::pair< std::size_t, float > Neighbor
matches neighbor's frame id to distance
Definition: tools.hpp:53
void write_clustered_trajectory(std::string filename, std::vector< std::size_t > traj)
write state trajectory into plain text file
Definition: tools.cpp:73
std::map< std::size_t, Clustering::Tools::Neighbor > Neighborhood
map frame id to neighbors
Definition: tools.hpp:55
void write_fes(std::string fname, std::vector< float > fes)
write free energies as column into given file
Definition: tools.cpp:34
std::pair< Neighborhood, Neighborhood > read_neighborhood(const std::string fname)
Definition: tools.cpp:116
std::vector< NUM > read_single_column(std::string filename)
read single column of numbers from given file. number type (int, float, ...) given as template parame...
Definition: tools.hxx:126
std::vector< std::size_t > read_clustered_trajectory(std::string filename)
read states from trajectory (given as plain text file)
Definition: tools.cpp:54
std::string stringprintf(const std::string &str,...)
printf-version for std::string
Definition: tools.cpp:95
void write_neighborhood(const std::string fname, const Neighborhood &nh, const Neighborhood &nh_high_dens)
Definition: tools.cpp:145