SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / fflibutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2012,2013,2014,2015 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 #include "gmxpre.h"
37
38 #include "fflibutil.h"
39
40 #include <cstring>
41
42 #include <string>
43 #include <vector>
44
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/utility/datafilefinder.h"
47 #include "gromacs/utility/dir_separator.h"
48 #include "gromacs/utility/directoryenumerator.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/path.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/stringutil.h"
55
56 const char* fflib_forcefield_dir_ext()
57 {
58     return ".ff";
59 }
60
61 const char* fflib_forcefield_itp()
62 {
63     return "forcefield.itp";
64 }
65
66 const char* fflib_forcefield_doc()
67 {
68     return "forcefield.doc";
69 }
70
71 void fflib_filename_base(const char* filename, char* filebase, int maxlen)
72 {
73     const char* cptr;
74     char*       ptr;
75
76     cptr = strrchr(filename, DIR_SEPARATOR);
77     if (cptr != nullptr)
78     {
79         /* Skip the separator */
80         cptr += 1;
81     }
82     else
83     {
84         cptr = filename;
85     }
86     if (strlen(filename) >= static_cast<size_t>(maxlen))
87     {
88         gmx_fatal(FARGS, "filename is longer (%zu) than maxlen (%d)", strlen(filename), maxlen);
89     }
90     strcpy(filebase, cptr);
91     /* Remove the extension */
92     ptr = strrchr(filebase, '.');
93     if (ptr != nullptr)
94     {
95         ptr[0] = '\0';
96     }
97 }
98
99 std::vector<std::string> fflib_search_file_end(const char* ffdir, const char* file_end, bool bFatalError)
100 {
101     try
102     {
103         std::string              ffdirFull(gmx::getLibraryFileFinder().findFile(ffdir));
104         std::vector<std::string> result = gmx::DirectoryEnumerator::enumerateFilesWithExtension(
105                 ffdirFull.c_str(), file_end, true);
106         if (result.empty() && bFatalError)
107         {
108             std::string message = gmx::formatString(
109                     "Could not find any files ending on '%s' "
110                     "in the force field directory '%s'",
111                     file_end,
112                     ffdir);
113             GMX_THROW(gmx::InvalidInputError(message));
114         }
115         for (std::string& filename : result)
116         {
117             filename = gmx::Path::join(ffdir, filename);
118         }
119         return result;
120     }
121     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
122 }
123
124 std::vector<gmx::DataFileInfo> fflib_enumerate_forcefields()
125 {
126     const char* const              dirend     = fflib_forcefield_dir_ext();
127     const char* const              filename   = fflib_forcefield_itp();
128     std::vector<gmx::DataFileInfo> candidates = gmx::getLibraryFileFinder().enumerateFiles(
129             gmx::DataFileOptions(dirend).throwIfNotFound(false));
130
131     std::vector<gmx::DataFileInfo> result;
132     for (size_t i = 0; i < candidates.size(); ++i)
133     {
134         std::string testPath(gmx::Path::join(candidates[i].dir, candidates[i].name, filename));
135         // TODO: Consider also checking that the directory can be listed.
136         if (gmx::File::exists(testPath, gmx::File::returnFalseOnError))
137         {
138             result.push_back(candidates[i]);
139         }
140     }
141
142     // TODO: Consider merging this into enumerateFiles(), such that the error
143     // could also list the directories searched.
144     if (result.empty())
145     {
146         std::string message = gmx::formatString(
147                 "No force fields found (files with name '%s' "
148                 "in subdirectories ending on '%s')",
149                 filename,
150                 dirend);
151         GMX_THROW(gmx::InvalidInputError(message));
152     }
153
154     return result;
155 }
156
157 bool fflib_fexist(const std::string& file)
158 {
159     return !gmx::findLibraryFile(file, true, false).empty();
160 }
161
162
163 FILE* fflib_open(const std::string& file)
164 {
165     std::string fileFullPath = gmx::findLibraryFile(file);
166     fprintf(stderr, "Opening force field file %s\n", fileFullPath.c_str());
167     return gmx_ffopen(fileFullPath, "r");
168 }