A sequence $\{a_n\}$ is defined recursively by $a_1=\frac{1}{2}, $ and for $n\ge 2,$ $0<a_n\leq a_{n-1}$ and
\[a_n^2(a_{n-1}+1)+a_{n-1}^2(a_n+1)-2a_na_{n-1}(a_na_{n-1}+a_n+1)=0.\]
$(1)$ Determine the general formula of the sequence $\{a_n\};$
$(2)$ Let $S_n=a_1+\cdots+a_n.$ Prove that for $n\ge 1,$ $\ln\left(\frac{n}{2}+1\right)<S_n<\ln(n+1).$
1. **Determine the general formula of the sequence $\{a_n\}$:**

   Given the recursive relation:
   \[
   a_n^2(a_{n-1}+1) + a_{n-1}^2(a_n+1) - 2a_na_{n-1}(a_na_{n-1} + a_n + 1) = 0
   \]
   Let's simplify this equation step-by-step.

   First, expand the terms:
   \[
   a_n^2 a_{n-1} + a_n^2 + a_{n-1}^2 a_n + a_{n-1}^2 - 2a_n^2 a_{n-1}^2 - 2a_n a_{n-1}^2 - 2a_n^2 a_{n-1} = 0
   \]

   Combine like terms:
   \[
   a_n^2 a_{n-1} + a_n^2 + a_{n-1}^2 a_n + a_{n-1}^2 - 2a_n^2 a_{n-1}^2 - 2a_n a_{n-1}^2 - 2a_n^2 a_{n-1} = 0
   \]

   Group the terms involving $a_n^2$ and $a_{n-1}^2$:
   \[
   a_n^2 (a_{n-1} + 1 - 2a_{n-1}^2 - 2a_{n-1}) + a_{n-1}^2 (a_n + 1 - 2a_n) = 0
   \]

   Simplify the coefficients:
   \[
   a_n^2 (1 - a_{n-1} - 2a_{n-1}^2) + a_{n-1}^2 (1 - a_n) = 0
   \]

   Factor out $a_n$ and $a_{n-1}$:
   \[
   a_n^2 (1 - a_{n-1} - 2a_{n-1}^2) = a_{n-1}^2 (a_n - 1)
   \]

   Rearrange to isolate $a_n$:
   \[
   a_n^2 (1 - a_{n-1} - 2a_{n-1}^2) = a_{n-1}^2 (a_n - 1)
   \]

   This is a quadratic equation in $a_n$. Solving for $a_n$, we get two possible solutions:
   \[
   a_n = a_{n-1} \frac{1 + a_{n-1}}{1 - a_{n-1} - 2a_{n-1}^2} \quad \text{or} \quad a_n = \frac{a_{n-1}}{a_{n-1} + 1}
   \]

   Since $0 < a_n \leq a_{n-1}$, we choose the solution that satisfies this condition:
   \[
   a_n = \frac{a_{n-1}}{a_{n-1} + 1}
   \]

   We can rewrite this as:
   \[
   \frac{1}{a_n} = 1 + \frac{1}{a_{n-1}}
   \]

   Given $a_1 = \frac{1}{2}$, we can find the general formula:
   \[
   a_n = \frac{1}{n+1}
   \]

2. **Prove that for $n \ge 1$, $\ln\left(\frac{n}{2} + 1\right) < S_n < \ln(n + 1)$:**

   Let $S_n = a_1 + a_2 + \cdots + a_n$. Using the general formula $a_n = \frac{1}{n+1}$, we have:
   \[
   S_n = \sum_{k=1}^n \frac{1}{k+1}
   \]

   This sum can be rewritten as:
   \[
   S_n = \sum_{k=2}^{n+1} \frac{1}{k}
   \]

   Using the properties of the harmonic series, we know:
   \[
   \sum_{k=1}^n \frac{1}{k} \approx \ln(n) + \gamma
   \]
   where $\gamma$ is the Euler-Mascheroni constant. Therefore:
   \[
   S_n = \sum_{k=2}^{n+1} \frac{1}{k} = \left( \sum_{k=1}^{n+1} \frac{1}{k} \right) - 1
   \]

   Using the approximation:
   \[
   \sum_{k=1}^{n+1} \frac{1}{k} \approx \ln(n+1) + \gamma
   \]

   Thus:
   \[
   S_n \approx \ln(n+1) + \gamma - 1
   \]

   To prove the inequalities, we use the integral test for the harmonic series:
   \[
   \int_1^{n+1} \frac{1}{x} \, dx < \sum_{k=1}^{n} \frac{1}{k} < 1 + \int_1^n \frac{1}{x} \, dx
   \]

   Evaluating the integrals:
   \[
   \ln(n+1) < \sum_{k=1}^{n} \frac{1}{k} < 1 + \ln(n)
   \]

   Therefore:
   \[
   \ln(n+1) - 1 < S_n < \ln(n+1)
   \]

   Since $\ln\left(\frac{n}{2} + 1\right) < \ln(n+1) - 1$, we have:
   \[
   \ln\left(\frac{n}{2} + 1\right) < S_n < \ln(n+1)
   \]

The final answer is $\boxed{a_n = \frac{1}{n+1}}$