/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "physics/bufferstop.h" #include #include #include #include #include namespace bufferstop { namespace { // the step is integrated in slices of this length at most, and in no more than so many of them, as collision::hit() does [s] constexpr double slice{0.0005}; constexpr double slices_max{200.0}; // the transition between the loading and the unloading curve of the buffers, to their secant stiffness, as the // buffers of a pair of vehicles have it (couplerphysics.cpp) constexpr double transition_factor{3.0}; // a brake element of a sliding stop holds less the longer it slides: Rawie 10 EB, 40 kJ/m over 0-5 m, 36 over 5-8 m, // 32 over 8-12 m, 28 past that constexpr std::array, 4> element_force{{{5.0, 40.0e3}, {8.0, 36.0e3}, {12.0, 32.0e3}, {std::numeric_limits::infinity(), 28.0e3}}}; // the frames past the stages: a rigid stop of rails or concrete gives at 1.5 MN, then at 1.0 MN as it bends (Kowalczyk: // 225 kJ of the FEM case deform it by about 0.2 m), wrecked past 0.6 m (Kirkby); the concrete base of a hydraulic or a // sliding stop is stronger than the end of a coach or a wagon (3 MN at its peak, 2 MN past it, collision::default_crush()), // the end crushes first collision::crush_curve rigid_frame() { return {1.5e6, 1.0e6, 0.05, 1.0, 0.0, std::numeric_limits::infinity(), 1.5}; } collision::crush_curve base_frame() { return {3.5e6, 2.5e6, 0.05, 1.0, 0.0, std::numeric_limits::infinity(), 1.5}; } std::string_view trimmed(std::string_view Text) { while (false == Text.empty() && std::isspace(static_cast(Text.front()))) { Text.remove_prefix(1); } while (false == Text.empty() && std::isspace(static_cast(Text.back()))) { Text.remove_suffix(1); } return Text; } // a field of the attribute: empty, a number, or not one (nullopt within the optional) std::optional> number(std::string_view Text) { Text = trimmed(Text); if (Text.empty()) { return std::optional{}; } auto value{0.0}; auto const [end, error]{std::from_chars(Text.data(), Text.data() + Text.size(), value)}; if (error != std::errc{} || end != Text.data() + Text.size() || false == std::isfinite(value)) { return std::nullopt; } return std::optional{value}; } std::vector fields(std::string_view Text, char const Separator) { std::vector result; while (true) { auto const split{Text.find(Separator)}; result.emplace_back(Text.substr(0, split)); if (split == std::string_view::npos) { return result; } Text.remove_prefix(split + 1); } } std::string lowercase(std::string_view Text) { std::string result{Text}; std::transform(result.begin(), result.end(), result.begin(), [](unsigned char const Character) { return static_cast(std::tolower(Character)); }); return result; } // the force of the stage between the buffers and the frame: the absorber over its stroke, the brake elements over the // run-out; none past them double stage_force(definition const &Stop, state const &State) { switch (Stop.type) { case kind::hydraulic: return State.absorber < Stop.absorber_stroke ? Stop.absorber_force : std::numeric_limits::infinity(); case kind::friction: return State.slid < Stop.runout ? friction_force(Stop, State.slid) : std::numeric_limits::infinity(); default: return std::numeric_limits::infinity(); } } double series_stiffness(double const First, double const Second) { if (false == std::isfinite(Second)) { return First; } if (false == std::isfinite(First)) { return Second; } return First * Second / (First + Second); } } // namespace std::string_view kind_name(kind const Kind) { switch (Kind) { case kind::none: return "none"; case kind::buffered: return "buffered"; case kind::hydraulic: return "hydraulic"; case kind::friction: return "friction"; case kind::rigid: default: return "rigid"; } } std::optional kind_of(std::string_view const Name) { for (auto const candidate : {kind::none, kind::rigid, kind::buffered, kind::hydraulic, kind::friction}) { if (Name == kind_name(candidate)) { return candidate; } } return std::nullopt; } definition preset(kind const Kind) { definition stop; stop.type = Kind; switch (Kind) { case kind::none: break; case kind::rigid: // the beam of a stop of BN-79 is 960 mm above the rail (Kowalczyk). its structure gives elastically: with 17.5 MN/m // in series with the steel of the vehicle some 15 % of the energy of the FEM case springs back, as there stop.frame = rigid_frame(); stop.stiffness = 17.5e6; break; case kind::buffered: // two buffers of EN 15551 category A, 2 x 30 kJ over 105 mm stop.height = 1.04; stop.buffer_energy = 60.0e3; stop.buffer_stroke = 0.105; stop.frame = rigid_frame(); stop.stiffness = 17.5e6; break; case kind::hydraulic: // Oleo: 336 kJ over 0.6 m, so a steady 560 kN stop.height = 1.04; stop.absorber_force = 336.0e3 / 0.6; stop.absorber_stroke = 0.6; stop.frame = base_frame(); stop.demolish = 0.4; break; case kind::friction: // Rawie 10 EB: 6 elements, 8 m of braking distance; the body some 3 t. the track ends where the run-out does, its // face stands that far before the rail end stop.height = 1.04; stop.elements = 6; stop.runout = 8.0; stop.setback = stop.runout; stop.body_mass = 3000.0; stop.frame = base_frame(); stop.demolish = 0.4; break; } return stop; } std::optional read_attribute(std::string_view const Value) { auto const parts{fields(trimmed(Value), ';')}; if (parts.size() > 4) { return std::nullopt; } auto const type{kind_of(lowercase(trimmed(parts[0])))}; if (false == type.has_value()) { return std::nullopt; } auto stop{preset(*type)}; if (*type == kind::none) { return stop; } std::array, 3> values; for (std::size_t index = 1; index < parts.size(); ++index) { auto const field{number(parts[index])}; if (false == field.has_value()) { return std::nullopt; } values[index - 1] = *field; } auto const &[first, second, setback]{values}; // the energy, the stroke, the elements and the run-out are more than nothing if ((first && *first <= 0.0) || (second && *second <= 0.0)) { return std::nullopt; } switch (*type) { case kind::buffered: if (first) { stop.buffer_energy = *first * 1000.0; } break; case kind::hydraulic: { auto const energy{first.value_or(stop.absorber_force * stop.absorber_stroke / 1000.0) * 1000.0}; stop.absorber_stroke = second.value_or(stop.absorber_stroke); stop.absorber_force = energy / stop.absorber_stroke; break; } case kind::friction: if (first) { stop.elements = std::max(1, static_cast(std::lround(*first))); } stop.runout = second.value_or(stop.runout); stop.setback = stop.runout; break; default: break; } if (setback) { stop.setback = *setback; if (*type == kind::friction) { // the track ends where the run-out does: a face nearer the end leaves less of it stop.runout = std::min(stop.runout, std::max(*setback, 0.0)); } } return stop; } coupler_physics::spring_curve buffers_of(definition const &Stop) { if (Stop.buffer_energy <= 0.0 || Stop.buffer_stroke <= 0.0) { return {1.0, 0.0, 0.0}; } // linear, as collision::buffer_curve() of no progression: the energy is half the end force over the stroke auto const endforce{2.0 * Stop.buffer_energy / Stop.buffer_stroke}; return {endforce / Stop.buffer_stroke, Stop.buffer_stroke, 0.0}; } double friction_force(definition const &Stop, double const Slid) { auto const slid{std::max(Slid, 0.0)}; for (auto const &[upto, force] : element_force) { if (slid < upto) { return Stop.elements * force; } } return Stop.elements * element_force.back().second; } std::string_view outcome_name(outcome const Outcome) { switch (Outcome) { case outcome::held: return "held"; case outcome::damaged: return "damaged"; case outcome::rode_up: return "rode_up"; case outcome::demolished: return "demolished"; case outcome::overrun: return "overrun"; case outcome::none: default: return "none"; } } outcome worse(outcome const First, outcome const Second) { return static_cast(First) >= static_cast(Second) ? First : Second; } collision::hit_result frame_hit(definition const &Stop, state const &Stop_state, contact_input const &Input, double const Velocity, double const Springs, double const Dt, double const Budget) { collision::hit_input input; input.velocity = Velocity; input.mass1 = Input.mass; input.mass2 = immovable; input.restitution = Stop.restitution; input.stiffness = series_stiffness(Input.stiffness, Stop.stiffness); input.springs = Springs; input.dt = Dt; input.start_velocity = Velocity; input.budget = Budget; return collision::hit(Input.vehicle_end, {Stop.frame, Stop_state.crush}, input); } contact_result after_override(definition const &Stop, state &Stop_state, double const Mass, double const Closing) { contact_result result; result.climbed = true; result.touched = true; Stop_state.climbed = true; auto const velocity{std::max(Closing, 0.0)}; auto const brings{0.5 * Mass * velocity * velocity}; auto const frame{Stop.frame.energy(Stop_state.crush, Stop.demolish)}; auto const holds{frame + 0.5 * Mass * collision::gravity * ride_up_lift}; if (brings <= holds) { // it comes to rest on the stop, which stands, damaged result.residual = 0.0; result.result = outcome::rode_up; } else { // it wrecks the stop and goes on with the rest result.residual = std::sqrt(std::max(0.0, velocity * velocity - 2.0 * holds / std::max(Mass, 1.0))); result.work = frame; Stop_state.crush = std::max(Stop_state.crush, Stop.demolish); Stop_state.demolished = true; result.result = outcome::demolished; } result.velocity = result.residual; result.impulse = Mass * (velocity - result.residual); return result; } contact_result contact(definition const &Stop, state &Stop_state, contact_input const &Input) { contact_result result; result.velocity = Input.closing; if (Stop.type == kind::none || Stop_state.spent() || Input.mass <= 0.0 || Input.dt <= 0.0) { result.travel = Input.closing * std::max(Input.dt, 0.0); return result; } auto &now{Stop_state}; auto const stopbuffers{buffers_of(Stop)}; auto const travel{std::max(Input.buffers.travel, 0.0) + stopbuffers.travel}; auto const top{std::max(Input.buffers.travel > 0.0 ? Input.buffers.end_force() : 0.0, stopbuffers.end_force())}; auto const transition{transition_factor * std::max(top, 1.0) / std::max(travel, 1e-3)}; // springing back the buffers give the square of the restitution of the force they took loading; with the transition // between the curves a vehicle bounces off them at about 0.64 of its speed, 41 % of the energy back (EN 15551 has // the buffers take in at least half of it) auto const unloading{Stop.restitution * Stop.restitution}; auto const slices{std::clamp(std::ceil(Input.dt / slice), 1.0, slices_max)}; auto const length{Input.dt / slices}; // the override of the end onto the stop: the stop never interlocks with an anticlimber, nor does the anticlimber of // the end hold a beam off its own height by more than the vertical offset of the design collision of EN 15227 (the // Class 710 of Enfield, built to it, rode up the stop) auto const engaged{Input.anticlimber && std::abs(Input.vehicle_height - Stop.height) < collision::override_height - 1e-9}; auto const climbs = [&](double const VehicleCrush) { collision::override_input climbing; climbing.crush1 = VehicleCrush; climbing.crush2 = now.crush; climbing.mass1 = Input.mass; climbing.mass2 = immovable; climbing.height1 = Input.vehicle_height; climbing.height2 = Stop.height; climbing.anticlimber1 = engaged; climbing.zone1 = Input.zone; climbing.zone2 = Stop.demolish; return collision::override_of(climbing).first; }; auto velocity{Input.closing}; auto penetration{Input.penetration}; auto vehiclecrush{Input.vehicle_end.crush}; auto time{0.0}; auto final{outcome::none}; while (Input.dt - time > 1e-12) { auto const step{std::min(length, Input.dt - time)}; auto deflection{penetration - (now.absorber + now.slid + now.crush + vehiclecrush)}; if (deflection <= 0.0 && now.absorber > 0.0) { // the absorber springs back after the vehicle, pressing on nothing now.absorber = std::max(0.0, now.absorber + deflection); deflection = 0.0; } if (deflection <= 0.0) { // apart: the buffers unloaded, the body of a sliding stop at rest now.elastic.reset(); now.absorbed = 0.0; now.sliding = false; penetration += velocity * step; result.travel += velocity * step; time += step; continue; } result.touched = true; result.duration += step; auto const loading{velocity > 0.0}; auto const split{coupler_physics::series(Input.buffers, stopbuffers, deflection)}; auto force{now.elastic.update(deflection, split.force, unloading * split.force, transition)}; auto const stage{stage_force(Stop, now)}; auto const stopforce{Stop.frame.force(now.crush)}; auto const endforce{Input.vehicle_end.curve.force(vehiclecrush)}; auto const frameforce{std::min(stopforce, endforce)}; auto const yield{std::min(stage, frameforce)}; auto mass{Input.mass}; auto crushed{false}; auto yielded{false}; if (loading && force > yield) { // the weaker stage gives at its force, the buffers stay pressed as far as that force has them yielded = true; auto const held{std::min(Input.buffers.deflection(yield) + stopbuffers.deflection(yield), travel)}; auto const excess{std::max(deflection - held, 0.0)}; if (stage <= frameforce) { if (Stop.type == kind::hydraulic) { now.absorber = std::min(now.absorber + excess, Stop.absorber_stroke); } else { if (false == now.sliding) { // the body of the stop takes its share of the momentum as it starts off velocity *= Input.mass / (Input.mass + Stop.body_mass); now.sliding = true; } now.slid = std::min(now.slid + excess, Stop.runout); mass += Stop.body_mass; if (now.slid >= Stop.runout - 1e-9) { // slid off the end of its run-out: it goes on with the vehicle, holding nothing now.overrun = true; final = outcome::overrun; } } } else { // the frame of the stop or the end of the vehicle crushes, the weaker one, both alike when they're alike if (std::abs(stopforce - endforce) <= 1e-9 * yield) { now.crush += 0.5 * excess; vehiclecrush += 0.5 * excess; } else if (stopforce < endforce) { now.crush += excess; } else { vehiclecrush += excess; } result.work += yield * excess; crushed = true; } force = yield; now.elastic.force = yield; now.elastic.deflection = held; } else if (loading && deflection >= travel - 1e-9) { // the buffers solid, the hit goes onto the steel and the frames: the crush takes only what the hit brings past // what the buffers of EN 15551 of the end take in auto const budget{std::min(Input.budget, 0.5 * Input.mass * velocity * velocity - std::max(0.0, Input.vehicle_capacity - now.absorbed))}; auto input{Input}; input.vehicle_end.crush = vehiclecrush; auto const hit{frame_hit(Stop, now, input, velocity, force, Input.dt - time, budget)}; if (hit.crushing) { vehiclecrush += hit.crush1; now.crush += hit.crush2; result.work += hit.work; result.force = std::max(result.force, hit.force); result.travel += hit.crush1 + hit.crush2; result.duration = std::min(Input.dt, result.duration + hit.duration); velocity = hit.velocity; time = Input.dt; if (climbs(vehiclecrush)) { auto const climbed{after_override(Stop, now, Input.mass, velocity)}; result.climbed = true; result.work += climbed.work; velocity = climbed.velocity; final = climbed.result; } else if (now.crush >= Stop.demolish) { now.demolished = true; final = outcome::demolished; } break; } // the steel takes it, the vehicle bounces off velocity = -Stop.restitution * velocity; } result.force = std::max(result.force, force); if (false == yielded) { if (loading && velocity > 0.0) { now.absorbed += force * velocity * step; } else { now.absorbed = std::min(now.absorbed, 0.5 * force * deflection); } } velocity -= force * step / mass; penetration += velocity * step; result.travel += velocity * step; time += step; if (crushed) { if (climbs(vehiclecrush)) { auto const climbed{after_override(Stop, now, Input.mass, velocity)}; result.climbed = true; result.work += climbed.work; velocity = climbed.velocity; final = climbed.result; break; } if (now.crush >= Stop.demolish) { now.demolished = true; final = outcome::demolished; break; } } if (now.overrun) { break; } } result.velocity = velocity; result.residual = now.spent() || result.climbed ? std::max(velocity, 0.0) : 0.0; result.impulse = Input.mass * (Input.closing - velocity); result.vehicle_crush = vehiclecrush - Input.vehicle_end.crush; now.peak_force = std::max(now.peak_force, result.force); if (final != outcome::none) { result.result = final; } else if (result.touched) { result.result = now.crush >= collision::crush_mark ? outcome::damaged : outcome::held; } return result; } std::string cell_text(notice const &Notice) { return Notice.vehicle + " " + std::string{outcome_name(Notice.result)} + " " + (Notice.track.empty() ? std::string{"none"} : Notice.track); } std::string cell_name(std::string_view const Track) { return std::string{Track} + ":bufferstop"; } std::vector default_rules() { return read_rules("!*kozlak* // wiatrak_kozlak: a windmill\n" "koziol_lublin buffered 0.0 // the buffers of a vehicle on its beam\n" "koziol_chylonia buffered 0.0\n" "koziol* rigid 0.0\n" "*oporow* rigid 0.0\n" "prellbock* rigid 0.0\n" "bufferstop* rigid 0.0\n"); } std::vector read_rules(std::string_view Text) { std::vector rules; for (auto line : fields(Text, '\n')) { if (auto const comment{line.find("//")}; comment != std::string_view::npos) { line = line.substr(0, comment); } std::vector words; for (auto const word : fields(trimmed(line), ' ')) { if (false == trimmed(word).empty()) { words.emplace_back(trimmed(word)); } } if (words.empty()) { continue; } model_rule rule; if (words[0].front() == '!') { rule.excluded = true; rule.pattern = lowercase(words[0].substr(1)); rules.emplace_back(rule); continue; } auto const type{words.size() > 1 ? kind_of(lowercase(words[1])) : std::optional{kind::rigid}}; auto const face{words.size() > 2 ? number(words[2]) : std::optional>{std::optional{0.0}}}; if (false == type.has_value() || *type == kind::none || false == face.has_value()) { continue; } rule.pattern = lowercase(words[0]); rule.type = *type; rule.face = face->value_or(0.0); rules.emplace_back(rule); } return rules; } bool wildcard(std::string_view const Pattern, std::string_view const Text) { // the usual greedy match with backtracking to the last star std::size_t pattern{0}; std::size_t text{0}; auto star{std::string_view::npos}; std::size_t resume{0}; while (text < Text.size()) { if (pattern < Pattern.size() && Pattern[pattern] == '*') { star = pattern++; resume = text; } else if (pattern < Pattern.size() && Pattern[pattern] == Text[text]) { ++pattern; ++text; } else if (star != std::string_view::npos) { pattern = star + 1; text = ++resume; } else { return false; } } while (pattern < Pattern.size() && Pattern[pattern] == '*') { ++pattern; } return pattern == Pattern.size(); } std::optional match(std::string_view const ModelFile, std::vector const &Rules) { // the name of the file alone, without its folders and its extension auto name{ModelFile}; if (auto const folder{name.find_last_of("/\\")}; folder != std::string_view::npos) { name.remove_prefix(folder + 1); } if (auto const extension{name.rfind('.')}; extension != std::string_view::npos) { name = name.substr(0, extension); } auto const file{lowercase(name)}; if (file.empty()) { return std::nullopt; } for (auto const &rule : Rules) { if (rule.excluded && wildcard(rule.pattern, file)) { return std::nullopt; } } for (auto const &rule : Rules) { if (false == rule.excluded && wildcard(rule.pattern, file)) { return rule; } } return std::nullopt; } placement place(double const EndX, double const EndZ, double const OutwardX, double const OutwardZ, double const ModelX, double const ModelZ) { auto const dx{ModelX - EndX}; auto const dz{ModelZ - EndZ}; return {std::hypot(dx, dz), dx * OutwardX + dz * OutwardZ}; } bool at_end(placement const &Placement) { return Placement.distance <= model_reach && std::abs(Placement.along) <= model_reach; } double model_setback(placement const &Placement, model_rule const &Rule) { // the face lies the offset of the rule from the origin towards the track return std::clamp(Rule.face - Placement.along, -model_setback_limit, model_setback_limit); } } // namespace bufferstop