forked from mpusz/mp-units
		
	
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 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 <mp_units/iostream.h>
 | 
						|
#include <mp_units/math.h>  // IWYU pragma: keep
 | 
						|
#include <mp_units/systems/isq/electromagnetism.h>
 | 
						|
#include <mp_units/systems/si/si.h>
 | 
						|
#include <iostream>
 | 
						|
 | 
						|
int main()
 | 
						|
{
 | 
						|
  using namespace mp_units;
 | 
						|
  using namespace mp_units::si::unit_symbols;
 | 
						|
 | 
						|
  std::cout << "mp-units capacitor time curve example...\n";
 | 
						|
  std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
 | 
						|
  std::cout.precision(3);
 | 
						|
 | 
						|
  constexpr auto C = isq::capacitance(0.47 * uF);
 | 
						|
  constexpr auto V0 = isq::voltage(5.0 * V);
 | 
						|
  constexpr auto R = isq::resistance(4.7 * si::kilo<si::ohm>);
 | 
						|
 | 
						|
  for (auto t = 0 * ms; t <= 50 * ms; ++t) {
 | 
						|
    const QuantityOf<isq::voltage> auto Vt = V0 * mp_units::exp(-t / (R * C));
 | 
						|
 | 
						|
    std::cout << "at " << t << " voltage is ";
 | 
						|
 | 
						|
    if (Vt >= 1 * V)
 | 
						|
      std::cout << Vt[V];
 | 
						|
    else if (Vt >= 1 * mV)
 | 
						|
      std::cout << Vt[mV];
 | 
						|
    else if (Vt >= 1 * uV)
 | 
						|
      std::cout << Vt[uV];
 | 
						|
    else if (Vt >= 1 * nV)
 | 
						|
      std::cout << Vt[nV];
 | 
						|
    else
 | 
						|
      std::cout << Vt[pV];
 | 
						|
    std::cout << "\n";
 | 
						|
  }
 | 
						|
}
 |