<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def calc_h_v1(self):
 """Approximate the water stage resulting in a certain reference discarge with the Pegasus iteration method. Required control parameters: |QTol| |HTol| Required flux sequence: |QRef| Modified aide sequences: |HMin| |HMax| |QMin| |QMax| Calculated flux sequence: |H| Besides the parameters and sequences given above, those of the actual method for calculating the discharge of the total cross section are required. Examples: Essentially, the Pegasus method is a root finding algorithm which sequentially decreases its search radius (like the simple bisection algorithm) and shows superlinear convergence properties (like the Newton-Raphson algorithm). Ideally, its convergence should be proved for each application model to be derived from HydPy-L-Stream. The following examples focus on the methods |calc_hmin_qmin_hmax_qmax_v1| and |calc_qg_v1| (including their submethods) only: Define the geometry and roughness values for the first test channel: Set the error tolerances of the iteration small enough to not compromise the shown first six decimal places of the following results: Derive the required secondary parameters: Define a test function, accepting a reference discharge and printing both the approximated water stage and the related discharge value: Zero discharge and the following discharge values are related to the only discontinuities of the given root finding problem: qm(8.399238) qv(left=154.463234, right=23.073584) The related water stages are the ones (directly or indirectly) defined above: h(0.0) qg(0.0) h(1.0) qg(8.399238) h(2.0) qg(154.463234) h(1.25) qg(23.073584) Test some intermediate water stages, inundating the only the main channel, the main channel along with the right foreland, and the main channel along with both forelands respectively: h(0.859452) qg(6.0) h(1.047546) qg(10.0) h(1.77455) qg(100.0) Finally, test two extreme water stages, inundating both outer foreland embankments: h(2.152893) qg(200.0) h(4.240063) qg(2000.0) There is a potential risk of the implemented iteration method to fail for special channel geometries. To test such cases in a more condensed manner, the following test methods evaluates different water stages automatically in accordance with the example above. An error message is printed only, the estimated discharge does not approximate the reference discharge with six decimal places: Check for a triangle main channel: Check for a completely flat main channel: Repeat the last example but with a decreased value of |QTol| allowing to trigger another stopping mechanisms if the iteration algorithm: Check for a nonexistend main channel: Check for a nonexistend forelands: Check for nonexistend outer foreland embankments: To take the last test as an illustrative example, one can see that the given reference discharge is met by the estimated total discharge, which consists of components related to the main channel and the forelands only: qref(3932.452785) qg(3932.452785) qm(530.074621) qv(113.780226, 3288.597937) qvr(0.0, 0.0) """

    con = self.parameters.control.fastaccess
    flu = self.sequences.fluxes.fastaccess
    aid = self.sequences.aides.fastaccess
    aid.qmin -= flu.qref
    aid.qmax -= flu.qref
    if modelutils.fabs(aid.qmin) < con.qtol:
        flu.h = aid.hmin
        self.calc_qg()
    elif modelutils.fabs(aid.qmax) < con.qtol:
        flu.h = aid.hmax
        self.calc_qg()
    elif modelutils.fabs(aid.hmax-aid.hmin) < con.htol:
        flu.h = (aid.hmin+aid.hmax)/2.
        self.calc_qg()
    else:
        while True:
            flu.h = aid.hmin-aid.qmin*(aid.hmax-aid.hmin)/(aid.qmax-aid.qmin)
            self.calc_qg()
            aid.qtest = flu.qg-flu.qref
            if modelutils.fabs(aid.qtest) < con.qtol:
                return
            if (((aid.qmax < 0.) and (aid.qtest < 0.)) or
                    ((aid.qmax > 0.) and (aid.qtest > 0.))):
                aid.qmin *= aid.qmax/(aid.qmax+aid.qtest)
            else:
                aid.hmin = aid.hmax
                aid.qmin = aid.qmax
            aid.hmax = flu.h
            aid.qmax = aid.qtest
            if modelutils.fabs(aid.hmax-aid.hmin) < con.htol:
                return