【Velocity】关于#foreach的累加

博主遇到遍历后累计相加的需求,初用vm进行开发,经过无数次尝试,最终成功实现随手记账单小程序,期待大家的反馈。

今天遇到了一个需求 遍历出来后累计相加 vm才接触 就想用这个给它整出来

  $sun = 0
  #foreach($!result in $!map.page.list)
    $sum += $result.num)
  #end

不行……

  $sun = 0
  #foreach($!result in $!map.page.list)
    $sum = $sum + $result.num)
  #end

漂亮……不行

  #foreach($!result in $!map.page.list)
    #set($sum = $sum + $result.num))
  #end

哔了狗了

经过了无数次的尝试 列举了以上几种不行的 终于给它整出来了 初用vm 垃圾小白 大神路过就好 感谢

  #set($sum = 0)
  #foreach($!result in $!map.page.list)
    #set($sum = $sum + $result.num)
  #end
  $sum

随手记账单小程序 期待大家的反馈

微信图片_20200907143529.jpg

Surface force and vorticity We first define a function which computes ∇u⋅n∇u⋅n while taking the boundary conditions on the embedded surface into account. static inline coord embed_gradient (Point point, vector u, coord p, coord n) { coord dudn; foreach_dimension() { bool dirichlet = false; double vb = u.x.boundary[embed] (point, point, u.x, &dirichlet); if (dirichlet) { double val; dudn.x = dirichlet_gradient (point, u.x, cs, n, p, vb, &val); } else // Neumann dudn.x = vb; if (dudn.x == nodata) dudn.x = 0.; } return dudn; } The force exerted by the fluid on the solid can be written FΓ=−∫∂Γ(−pI+2μD)⋅nd∂ΓFΓ​=−∫∂Γ​(−pI+2μD)⋅nd∂Γ with ΓΓ the solid boundary. It can be further decomposed into a pressure (i.e. “form”) drag Fp=∫∂Γpnd∂ΓFp​=∫∂Γ​pnd∂Γ and a viscous drag Fμ=−∫∂Γ2μD⋅nd∂ΓFμ​=−∫∂Γ​2μD⋅nd∂Γ These two vectors are computed by the embed_force() function. trace void embed_force (scalar p, vector u, face vector mu, coord * Fp, coord * Fmu) { coord Fps = {0}, Fmus = {0}; foreach (reduction(+:Fps) reduction(+:Fmus), nowarning) if (cs[] > 0. && cs[] < 1.) { To compute the pressure force, we first get the coordinates of the barycentre of the embedded fragment, its area and normal, and then interpolate the pressure field on the surface. coord n, b; double area = embed_geometry (point, &b, &n); area *= pow (Delta, dimension - 1); double Fn = area*embed_interpolate (point, p, b); foreach_dimension() Fps.x += Fn*n.x; To compute the viscous force, we first need to retrieve the local value of the viscosity (ideally at the barycentre of the embedded fragment). This is not completely trivial since it is defined on the faces of the cell. We use a surface-fraction-weighted average value. if (constant(mu.x) != 0.) { double mua = 0., fa = 0.; foreach_dimension() { mua += mu.x[] + mu.x[1]; fa += fm.x[] + fm.x[1]; } mua /= fa; To compute the viscous force, we need to take into account the (Dirichlet) boundary conditions for the velocity on the surface. We only know how to do this when computing the normal gradient ∇u⋅n∇u⋅n using the embed_gradient() function. We thus need to re-express the viscous force using only normal derivatives of the velocity field. If we assume that uu is constant on the boundary, then ∇u⋅t=0∇u⋅t=0 with tt the unit tangent vector to the boundary. We thus have the relations ∇u=(∇u⋅n)n+(∇u⋅t)t=(∇u⋅n)n∇u=(∇u⋅n)n+(∇u⋅t)t=(∇u⋅n)n D=12(∇u+∇Tu)=12(2(∇u⋅n)nx(∇u⋅n)ny+(∇v⋅n)nx(∇u⋅n)ny+(∇v⋅n)nx2(∇v⋅n)ny)D=21​(∇u+∇Tu)=21​(2(∇u⋅n)nx​(∇u⋅n)ny​+(∇v⋅n)nx​​(∇u⋅n)ny​+(∇v⋅n)nx​2(∇v⋅n)ny​​) Fμ=−∫Γ([2μ(∇u⋅n)nx]nx+μ[(∇u⋅n)ny+(∇v⋅n)nx]ny[2μ(∇v⋅n)ny]ny+μ[(∇u⋅n)ny+(∇v⋅n)nx]nx)Fμ​=−∫Γ​([2μ(∇u⋅n)nx​]nx​+μ[(∇u⋅n)ny​+(∇v⋅n)nx​]ny​[2μ(∇v⋅n)ny​]ny​+μ[(∇u⋅n)ny​+(∇v⋅n)nx​]nx​​) Fμ=−∫Γ(μ[(∇u⋅n)(nx2+1)+(∇v⋅n)nxny]μ[(∇v⋅n)(ny2+1)+(∇u⋅n)nxny])Fμ​=−∫Γ​(μ[(∇u⋅n)(nx2​+1)+(∇v⋅n)nx​ny​]μ[(∇v⋅n)(ny2​+1)+(∇u⋅n)nx​ny​]​) assert (dimension == 2); coord dudn = embed_gradient (point, u, b, n); foreach_dimension() Fmus.x -= area*mua*(dudn.x*(sq(n.x) + 1.) + dudn.y*n.x*n.y); } } *Fp = Fps; *Fmu = Fmus; } In two dimensions, embed_vorticity() returns the vorticity of velocity field u, on the surface of the embedded boundary contained in the cell. p is the relative position of the barycentre of the embedded fragment and n its normal. #if dimension == 2 double embed_vorticity (Point point, vector u, coord p, coord n) { We compute ∇u⋅n∇u⋅n, taking the boundary conditions into account. coord dudn = embed_gradient (point, u, p, n); The vorticity is then obtained using the relations ω=∂xv−∂yu=(∇v⋅n)nx−(∇u⋅n)nyω=∂x​v−∂y​u=(∇v⋅n)nx​−(∇u⋅n)ny​ return dudn.y*n.x - dudn.x*n.y; } #endif // dimension == 2 将以上官网的代码中穿插的解释翻印并总结,并总结其中的所有代码
最新发布
06-05
<think>好的,我现在需要回答用户关于在Java XML模板中使用#foreach和#end指令的问题。首先,我需要回忆一下FreeMarker模板引擎的基本语法,因为用户提到的是Java XML模板,通常这涉及到FreeMarker或Velocity这样的模板引擎。根据引用[2],用户提供的参考内容中有提到freemarker.template.Configuration类,所以可以确定用户是在问FreeMarker的用法。 接下来,我需要明确用户的问题:他们需要一个使用#foreach和#end指令的示例和语法说明。FreeMarker中的#foreach实际上是#list指令,但用户可能混淆了其他模板引擎的术语,比如Velocity中使用的是#foreach。不过,在FreeMarker中正确的指令是#list。不过,用户明确提到了#foreach,可能需要确认是否存在这样的指令,或者是否用户有误。但根据FreeMarker的文档,正确的循环指令是#list,而#foreach并不是原生指令。可能用户将其他模板引擎的语法与FreeMarker混用了,或者可能有自定义的宏。需要进一步确认。 不过,根据引用[3],用户提到了宏展开后的代码,这可能意味着在FreeMarker中可以通过定义宏来实现类似#foreach的功能。或者,用户可能误将Velocity的语法带入到FreeMarker中。这时候需要指出FreeMarker的正确指令是#list,并提供相应的示例。但用户的问题明确提到#foreach和#end,可能需要解释FreeMarker中的对应指令,或者指出差异。 假设用户确实在使用FreeMarker,但错误地使用了#foreach,那么正确的做法是使用#list。例如,遍历一个列表时,应该使用<#list items as item>...</#list>。同时,可能需要提供XML模板中的具体示例,比如生成XML元素时如何循环生成多个节点。 另外,需要确保示例中的语法正确,比如正确的标签闭合,变量插值使用${},以及指令的正确使用。例如,在XML中,可能需要处理标签嵌套,避免语法错误。同时,要注意FreeMarker的指令是<#...>的形式,如<#list>, <#if>等。 可能还需要考虑如何在Java代码中配置FreeMarker的模板,比如通过Configuration类加载模板文件,并传递数据模型。引用[2]中提到的cfg.getTemplate方法,这可能涉及到如何正确获取模板实例,并渲染数据。 最后,生成相关问题,需要考虑用户可能遇到的后续问题,比如处理空列表、循环中的索引、条件判断结合循环等。</think>在Java XML模板中使用FreeMarker的`#list`指令(通常对应其他模板引擎的`#foreach`)时,语法需严格遵循FreeMarker规范。以下是具体示例和语法说明: ### 1. 基础语法结构 ```xml <#list 集合变量 as 元素变量> <!-- 循环内容 --> </#list> ``` ### 2. XML模板示例 假设数据模型包含`users`列表,每个用户有`name`和`id`属性: ```xml <?xml version="1.0" encoding="UTF-8"?> <users> <#list users as user> <user id="${user.id}"> <name>${user.name}</name> </user> </#list> </users> ``` ### 3. 嵌套循环示例 若用户包含多个地址: ```xml <#list users as user> <user> <addresses> <#list user.addresses as addr> <address type="${addr.type}">${addr.value}</address> </#list> </addresses> </user> </#list> ``` ### 4. 空集合处理 FreeMarker 2.3.7+支持`else`指令: ```xml <#list users as user> ... <#else> <error>No users found</error> </#list> ``` ### 5. 循环状态变量 ```xml <#list users as user> Index: ${user?index} <#-- 从0开始 --> Count: ${user?counter} <#-- 从1开始 --> </#list> ``` ### Java侧配置示例 ```java Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setDirectoryForTemplateLoading(new File("/templates")); Template template = cfg.getTemplate("user_template.ftl"); //[^2] Map<String, Object> data = new HashMap<>(); data.put("users", userList); try (Writer out = new FileWriter("output.xml")) { template.process(data, out); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值