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

67 lines
2.1 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
*/
#include <units/generic/dimensionless.h>
#include <units/isq/dimensions/electric_current.h>
#include <units/isq/si/capacitance.h>
#include <units/isq/si/resistance.h>
#include <units/isq/si/time.h>
#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>
2020-01-14 12:50:30 +01:00
#include <iostream>
int main()
{
using namespace units::isq;
using namespace units::isq::si::capacitance_references;
using namespace units::isq::si::voltage_references;
using namespace units::isq::si::resistance_references;
using namespace units::isq::si::time_references;
std::cout << "mp-units capacitor time curve example...\n";
2020-01-14 12:50:30 +01:00
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(3);
constexpr auto C = 0.47 * uF;
constexpr auto V0 = 5.0 * V;
constexpr auto RR = 4.7 * kR; // cannot use just 'R' here
2020-01-14 12:50:30 +01:00
for (auto t = 0 * ms; t <= 50 * ms; ++t) {
const Voltage auto Vt = V0 * units::exp(-t / (RR * C));
2020-01-14 12:50:30 +01:00
std::cout << "at " << t << " voltage is ";
2020-01-14 12:50:30 +01:00
if (Vt >= 1 * V)
std::cout << Vt;
else if (Vt >= 1 * mV)
std::cout << quantity_cast<si::millivolt>(Vt);
else if (Vt >= 1 * uV)
std::cout << quantity_cast<si::microvolt>(Vt);
else if (Vt >= 1 * nV)
std::cout << quantity_cast<si::nanovolt>(Vt);
2020-01-14 12:50:30 +01:00
else
std::cout << quantity_cast<si::picovolt>(Vt);
2020-01-14 12:50:30 +01:00
std::cout << "\n";
}
}