2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020,2021, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
38 #include "simulationinputhandle.h"
42 #include "gromacs/mdrun/legacymdrunoptions.h"
48 * SimulationInput needs much more discussion and refinement before it should be used directly.
49 * Use the interface defined in the headers and refer to #3652 for updates.
51 * Design note: It is probably sufficient for future versions to compose SimulationInput
52 * through a Builder rather than to subclass an Interface or base class. Outside of this
53 * translation unit, we should avoid coupling to the class definition until/unless we
54 * develop a much better understanding of simulation input portability.
60 SimulationInput(const char* tprFilename, const char* cpiFilename) :
61 tprFilename_(tprFilename), cpiFilename_(cpiFilename)
65 std::string tprFilename_;
66 std::string cpiFilename_;
70 class detail::SimulationInputHandleImpl final
73 explicit SimulationInputHandleImpl(std::shared_ptr<SimulationInput> input) :
74 simulationInput_{ std::move(input) }
78 SimulationInputHandleImpl(const SimulationInputHandleImpl&) = default;
79 SimulationInputHandleImpl& operator=(const SimulationInputHandleImpl&) = default;
80 SimulationInputHandleImpl(SimulationInputHandleImpl&&) noexcept = default;
81 SimulationInputHandleImpl& operator=(SimulationInputHandleImpl&&) noexcept = default;
83 [[nodiscard]] SimulationInput* simulationInput() const { return simulationInput_.get(); }
85 /*! \brief Boolean interpretation checks for presence of payload.
87 * \return true if handle has a SimulationInput, else false.
89 operator bool() const { return bool(simulationInput_); }
92 // Note that we should not need to worry about managing the SimulationInput
93 // Deleter as long as we manage the SimulationInputHolderImpl Deleter and
94 // SimulationInputHolderImpl is the only way to own a SimulationInput.
95 // BUT these semantics may change, and we should try to be responsible about
96 // consistent Allocator implementation.
97 std::shared_ptr<SimulationInput> simulationInput_;
101 * TODO: Improve doxygen source scoping.
102 * It seems surprising that doxygen should be looking at this source file, but we
103 * need to exclude the following definitions to avoid warnings.
104 * Possibly related to https://gitlab.com/gromacs/gromacs/-/issues/3402
106 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter() = default;
108 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
109 const SimulationInputHandleImplDeleter&) noexcept = default;
111 detail::SimulationInputHandleImplDeleter::SimulationInputHandleImplDeleter(
112 SimulationInputHandleImplDeleter&&) noexcept = default;
114 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::operator=(
115 const SimulationInputHandleImplDeleter&) noexcept = default;
117 detail::SimulationInputHandleImplDeleter& detail::SimulationInputHandleImplDeleter::operator=(
118 SimulationInputHandleImplDeleter&&) noexcept = default;
120 void detail::SimulationInputHandleImplDeleter::operator()(SimulationInputHandleImpl* impl) const
125 SimulationInputHandle::SimulationInputHandle() = default;
127 SimulationInputHandle::~SimulationInputHandle() = default;
129 SimulationInputHandle::SimulationInputHandle(std::unique_ptr<detail::SimulationInputHandleImpl> impl) :
130 impl_(impl.release())
134 SimulationInputHandle::SimulationInputHandle(const SimulationInputHandle& source)
136 // Dynamically allocate a new holder implementation object using the copy constructor.
137 // Note that make_unique does not provide for custom deleters, and there is no default
138 // overload to move construct/assign from basic unique_ptr to one with custom deleter,
139 // but more elaborate creation functions for SimulationInputHolderImpl seem like
140 // overkill at this point. Still, we use make_unique to confine constructor errors
141 // to the following exception-safe line.
142 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
143 // Move ownership of the heap object to the handle with a custom deleter.
144 impl_.reset(impl.release());
147 SimulationInputHandle& SimulationInputHandle::operator=(const SimulationInputHandle& source)
151 // Dynamically allocate a new holder implementation object using the copy constructor.
152 // Note that make_unique does not provide for custom deleters, and there is no default
153 // overload to move construct/assign from basic unique_ptr to one with custom deleter,
154 // but more elaborate creation functions for SimulationInputHolderImpl seem like
155 // overkill at this point. Still, we use make_unique to confine constructor errors
156 // to the following exception-safe line.
157 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(*source.impl_);
158 // Move ownership of the heap object to the handle with a custom deleter.
159 impl_.reset(impl.release());
165 SimulationInput* SimulationInputHandle::get() const noexcept
171 return impl_->simulationInput();
174 SimulationInputHandle::operator bool() const
183 SimulationInputHandle makeSimulationInput(const LegacyMdrunOptions& options)
185 // Note: it seems clear that we will want a SimulationInput to be linked to
186 // a communications context (whether the SimulationContext or something higher level)
187 // so that it can encapsulate the data locality details. Otherwise, we have
188 // to choose whether to read the files everywhere or just to store the
189 // filenames until a communications context is known.
190 const auto* const tprFilename = ftp2fn(efTPR, options.filenames.size(), options.filenames.data());
191 const auto* const cpiFilename = opt2fn("-cpi", options.filenames.size(), options.filenames.data());
192 auto simulationInput = std::make_unique<SimulationInput>(tprFilename, cpiFilename);
193 auto impl = std::make_unique<detail::SimulationInputHandleImpl>(std::move(simulationInput));
195 return SimulationInputHandle(std::move(impl));
198 } // end namespace gmx