Files
mp-units/example/aliases/capacitor_time_curve.cpp

63 lines
1.8 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003-2020 Andy Little.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses./
*/
/*
capacitor discharge curve using compile_time
physical_quantities
*/
2021-04-15 19:13:25 +02:00
#include <units/generic/dimensionless.h>
#include <units/isq/si/capacitance.h>
#include <units/isq/si/resistance.h>
#include <units/isq/si/time.h>
2021-04-15 19:13:25 +02:00
#include <units/isq/si/voltage.h>
#include <units/math.h> // IWYU pragma: keep
2020-12-28 15:18:02 +01:00
#include <units/quantity_io.h>
#include <iostream>
int main()
{
2021-04-15 19:13:25 +02:00
using namespace units::isq;
using namespace units::aliases::isq::si;
std::cout << "mp-units capacitor time curve example...\n";
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(3);
2021-04-15 19:13:25 +02:00
constexpr auto C = capacitance::uF<>(0.47);
constexpr auto V0 = voltage::V<>(5.0);
constexpr auto R = resistance::kR<>(4.7);
2021-04-15 19:13:25 +02:00
for (auto t = ms<int>(0); t <= ms<int>(50); ++t) {
const Voltage auto Vt = V0 * units::exp(-t / (R * C));
std::cout << "at " << t << " voltage is ";
2021-04-15 19:13:25 +02:00
if (Vt >= V<>(1))
std::cout << Vt;
2021-04-15 19:13:25 +02:00
else if (Vt >= mV<>(1))
std::cout << mV<>(Vt);
else if (Vt >= uV<>(1))
std::cout << uV<>(Vt);
else if (Vt >= nV<>(1))
std::cout << nV<>(Vt);
else
2021-04-15 19:13:25 +02:00
std::cout << pV<>(Vt);
std::cout << "\n";
}
}