numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
string_wave_1d_problem.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cmath>
23
24#include <Eigen/Core>
25
30
31namespace num_prob_collect::ode {
32
37public:
39 double speed{1.0};
40
43
45 double length{1.0};
46};
47
56public:
58 using variable_type = Eigen::VectorXd;
59
61 using scalar_type = double;
62
64 static constexpr auto allowed_evaluations =
65 num_collect::ode::evaluation_type{.diff_coeff = true};
66
73 : speed_(params.speed), num_points_(params.num_points) {
74 NUM_COLLECT_ASSERT(params.num_points >= 3);
75 points_ = variable_type::LinSpaced(num_points_, 0.0, params.length);
76 diff_coeff_ = variable_type::Zero(num_points_ * 2);
77 }
78
84 void evaluate_on(scalar_type /*time*/, const variable_type& variable,
85 num_collect::ode::evaluation_type /*evaluations*/) {
86 const double dx = points_(1) - points_(0);
87 // acceleration.
88 diff_coeff_.segment(1, num_points_ - 2) = speed_ * speed_ / (dx * dx) *
89 (variable.segment(num_points_ + 2, num_points_ - 2) -
90 2.0 // NOLINT
91 * variable.segment(num_points_ + 1, num_points_ - 2) +
92 variable.segment(num_points_, num_points_ - 2));
93
94 // velocity.
96 variable.segment(0, num_points_);
97 }
98
104 [[nodiscard]] auto diff_coeff() const noexcept -> const variable_type& {
105 return diff_coeff_;
106 }
107
113 [[nodiscard]] auto points() const noexcept -> const variable_type& {
114 return points_;
115 }
116
117private:
119 double speed_;
120
123
126
129};
130
135public:
142 : speed_(params.speed),
143 length_(params.length),
144 points_(Eigen::VectorXd::LinSpaced(
145 params.num_points, 0.0, params.length)),
146 solution_(Eigen::VectorXd::Zero(params.num_points * 2)) {}
147
153 void evaluate_on(double time) {
154 const num_collect::index_type num_points = points_.size();
155
156 // velocity.
157 solution_.segment(0, num_points) =
159 .array()
160 .sin() *
162 std::sin(
164
165 // displacement.
166 solution_.segment(num_points, num_points) =
168 .array()
169 .sin() *
170 std::cos(
172 }
173
179 [[nodiscard]] auto solution() -> const Eigen::VectorXd& {
180 return solution_;
181 }
182
183private:
185 double speed_;
186
188 double length_;
189
191 Eigen::VectorXd points_;
192
194 Eigen::VectorXd solution_;
195};
196
197} // namespace num_prob_collect::ode
Definition of assertion macros.
#define NUM_COLLECT_ASSERT(CONDITION)
Macro to check whether a condition is satisfied.
Definition assert.h:66
Class of ODE problem to solve 1D wave equation of strings discretized using finite difference.
void evaluate_on(scalar_type, const variable_type &variable, num_collect::ode::evaluation_type)
Evaluate on a (time, variable) pair.
static constexpr auto allowed_evaluations
Allowed evaluations.
num_collect::index_type num_points_
Number of points.
auto points() const noexcept -> const variable_type &
Get the spacial points.
auto diff_coeff() const noexcept -> const variable_type &
Get the differential coefficient.
variable_type diff_coeff_
Differential coefficient.
Eigen::VectorXd variable_type
Type of variables.
string_wave_1d_problem(const string_wave_1d_parameters &params)
Constructor.
Class to calculate exact solution of string_wave_1d_problem.
void evaluate_on(double time)
Evaluate solution.
auto solution() -> const Eigen::VectorXd &
Get the solution.
string_wave_1d_solution(const string_wave_1d_parameters &params)
Constructor.
Definition of evaluation_type enumeration.
Definition of index_type type.
Namespace of Eigen library.
Definition variable.h:416
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
constexpr T pi
Value of pi, .
Definition pi.h:40
Namespace of ordinary differential equation problems.
Definition namespaces.h:28
Definition of pi.
Struct to specify types of evaluations.
Struct of parameters in string_wave_1d_problem.
num_collect::index_type num_points
Number of spatial points.